简体   繁体   中英

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 )

这是一张描述我正在尝试做的事情的图片

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.

I'm not a mathematician so perhaps you can explain me sth here, but I don't understand what exactly you mean by "unsolvable analytically".

That's what sympy returns:

from sympy import *

x = symbols('x')
a = 1.5
y1 = x
y2 = a*(1 - exp(-x))
print(solve(y1-y2))

# [0.874217465798717]

“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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM