0

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

RobertPage12
  • 19
  • 1
  • 5
  • It's not clear to me what your question is. What you did, y1=x, is correct mathematically, but for programming x needs to exist (in programming, whatever is on the rhs of the equal sign needs to already exist or be created in that line, like `[]` will create a list so you can say `x=[]`). But it needs to exist for your `exp` function as well, so I think "creating a linear function" isn't really your question. Maybe you want to create a linear array without using `linspace`? If so, please edit your question to reflect this. – tom10 Sep 19 '19 at 18:01

3 Answers3

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.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

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]
SpghttCd
  • 10,510
  • 2
  • 20
  • 25
0

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

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 08 '22 at 16:54