This is one of the 100 recipes of the IPython Cookbook, the definitive guide to high-performance scientific computing and data science in Python.
import numpy as np
import scipy as sp
import scipy.optimize as opt
import matplotlib.pyplot as plt
%matplotlib inline
f = lambda x: np.cos(x) - x
x = np.linspace(-5, 5, 1000)
y = f(x)
plt.figure(figsize=(5,3));
plt.plot(x, y);
plt.axhline(0, color='k');
plt.xlim(-5,5);
bisect
function implements the bisection method (also called the dichotomy method). It takes as input the function and the interval to find the root in.opt.bisect(f, -5, 5)
Let's visualize the root on the plot.
plt.figure(figsize=(5,3));
plt.plot(x, y);
plt.axhline(0, color='k');
plt.scatter([_], [0], c='r', s=100);
plt.xlim(-5,5);
brentq
(Brent's method). This algorithm also requires that $f$ is continuous and that $f(a)$ and $f(b)$ have different signs.opt.brentq(f, -5, 5)
The brentq
method is faster than bisect
. If the conditions are satisfied, it is a good idea to try Brent's method first.
%timeit opt.bisect(f, -5, 5)
%timeit opt.brentq(f, -5, 5)
You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).
IPython Cookbook, by Cyrille Rossant, Packt Publishing, 2014 (500 pages).