Nick Charlton Nick Charlton - 6 months ago 77
Python Question

Finding root of two functions with scipy.optimize.brentq

I am trying to find the roots(i.e. where their curves cross) of the functions:

def f1(x):
return np.cos(2 * np.pi * x) * np.exp(-x ** 2)

def f2(x):
return np.log(x + 2.2)

from looking at the graphs of the two functions I know that there are two roots where -2 < x < 2 but I want the root for 0.5 > x > 0 (the other root occurs at x < 0)

I have done some research and it seems that scipy.optimize.brentq is used for finding roots of functions but I don't know how to do it for the root of two functions.

any suggestions?


In my humble opinion, the simplest way of doing this, is to define new function, for example:

def difference(x):
   return f1(x)-f2(x)

and then using scipy.optimize.brentq