'How to create linear function in Python without using numpy.linspace()

So, i am trying to create a linear functions in python such has y = x without using numpy.linspace(). In my understanding numpy.linspace() gives you an array which is discontinuous. But to fo

I am trying to find the intersection of y = x and a function unsolvable analytically ( such has the one in the picture ) .

Here is my code I don't know how to define x. Is there a way too express y has a simple continuous function?

import random as rd
import numpy as np

a = int(input('choose a :'))
eps = abs(float(input('choose epsilon :')))

b = 0
c = 10
x = ??????

y1 = x
y2 = a*(1 - np.exp(x))

z = abs(y2 - y1)
while z > eps :
    d = rd.uniform(b,c)
    c = d
    print(c)
print(y1 , y2 )

Here is a picture to describe what I am trying to do



Solution 1:[1]

Since your functions are differentiable, you could use the Newton-Raphson method implemented by scipy.optimize:

>>> scipy.optimize.newton(lambda x: 1.5*(1-math.exp(-x))-x, 10)
0.8742174657987283

Computing the error is very straightforward:

>>> def f(x): return 1.5*(1-math.exp(-x))
...
>>> x = scipy.optimize.newton(lambda x: f(x)-x, 10)
>>> error = f(x) - x
>>> x, error
(0.8742174657987283, -4.218847493575595e-15)

I've somewhat arbitrarily chosen x0=10 as the starting point. Some care needs to be take here to make sure the method doesn't converge to x=0, which in your example is also a root.

Solution 2:[2]

“Not solvable analytically” means there is no closed-form solution. In other words, you cant write down a single answer on paper like a number or equation and circle it and say ”thats my answer.” For some math problems it’s impossible to do so. Instead, for these kinds of problems, we can approximate the solution by running simulations and getting values or a graph of what the solution is.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 NPE
Solution 2 Talal Tahir