I want to use fsolve to numerically find roots of a nonlinear transcendent equation.
The following code does this job.
import numpy as np from scipy.optimize import fsolve import matplotlib.pyplot as pltkappa = 0.1 tau = 90
def equation(x, * parameters): kappa,tau = parameters return -x + kappa * np.sin(-tau*x)
x = np.linspace(-0.5,0.5, 35) roots = fsolve(equation,x, (kappa,tau)) x_2 = np.linspace(-1.5,1.5,1500) plt.plot(x_2 ,x_2 ) plt.plot(x_2 , kappa*np.sin(-x_2 *tau)) plt.scatter(x, roots) plt.show()
I can double check the solutions graphically by plotting the two graphs f1(x)=x and f2(x)=k * sin(-x * tau), which i also included in the code.
fsolve gives me some wrong answers, without throwing any errors or convergence problems.
The Problem is, that I would like to automatize the procedure for varying kappa and tau, without me checking which answers are wrong and which are right. But with wrong answers as output, i can't use this method. Is there any other method or an option I can use, to be on the safe side?
Thanks for the help.