import numpy as np from scipy import optimize import matplotlib.pyplot as plt x = np.arange(-10, 10, 0.1) def f(x): return x**2 + 10*np.sin(x) grid = (-10, 10, 0.1) xmin_global = optimize.brute(f, (grid,)) xmin_local = optimize.fminbound(f, 0, 10) root = optimize.fsolve(f, 1) # our initial guess is 1 root2 = optimize.fsolve(f, -2.5) xdata = np.linspace(-10, 10, num=20) np.random.seed(1234) ydata = f(xdata) + np.random.randn(xdata.size) def f2(x, a, b): return a*x**2 + b*np.sin(x) guess = [2, 2] params, params_covariance = optimize.curve_fit(f2, xdata, ydata, guess) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(x, f(x), 'b-', label="f(x)") ax.plot(x, f2(x, *params), 'r--', label="Curve fit result") xmins = np.array([xmin_global[0], xmin_local]) ax.plot(xmins, f(xmins), 'go', label="Minima") roots = np.array([root, root2]) ax.plot(roots, f(roots), 'kv', label="Roots") ax.legend() ax.set_xlabel('x') ax.set_ylabel('f(x)')