Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Root Finding

Open in Colab

One thing we want to be able to do compare two different algorithms that compute the same result.

Many numerical algorithms operate through some sort of iterative process, where we get better and better approximations to the answer at each iteration.

One example of this is Newton’s method for root finding. A root of a continuous function ff is a value xx so that f(x)=0f(x) = 0.

Given a function ff, we would like to find some root xx. Newton’s method computes a sequence of iterations

xk+1=xk−f(xk)f′(xk)x_{k+1} = x_{k} - \frac{f(x_{k})}{f'(x_{k})}

The idea is that the linear approximation of ff at xkx_k is the line

y=f(xk)+f′(xk)(x−xk)y = f(x_k) + f'(x_k) (x - x_k)

The xx-intercept of the line would be at xk+1x_{k+1}. Let’s look at an example

f(x)=x2−2,x0=1.f(x) = x^2 - 2, \quad x_0 = 1.

Another root finding algorithm is the bisection method. This operates by narrowing an interval which is known to contain a root. Assuming ff is continuous, we can use the intermediate value theorem. We know that if f(a)<0f(a) < 0, and f(b)>0f(b) > 0, there must be some c∈(a,b)c\in (a,b) so that f(c)=0f(c) = 0. At each iteration, we narrow the interval by looking at the midpoint c=(a+b)/2c = (a + b)/2 and then looking at the interval (a,c)(a,c) or the interval (c,b)(c,b) depending on which one contains a root based on the intermediate value theorem.

Exercise

Implement the bisection method as a recursive function.

Notebook Cell

Here’s a sequential implementation which keeps track of the iterates.

Let’s test these two methods on the same function

Let’s see how many iterations each method performed

It looks like one algorithm is converging faster than the other - let’s track how close each iteration is to a root:

The below function comes from the section on convergence:

experimentally, we see that the bisection converges linearly, and Newton’s method converges close to its theoretical guarantee of quadratically.

Exercise

Prove that the bisection method converges linearly.

An Application of Decorators

While Newton’s method converges faster, it required us to provide the derivative of the function we’re trying to find a root for. This might not always be easy to implement.

Instead, we might consider numerically approximating the derivative. Recall

f′(x)=lim⁡h→0+f(x)−f(x+h)hf'(x) = \lim_{h\to 0^+} \frac{f(x) - f(x + h)}{h}

We’ll use the symmetric definition

f′(x)=lim⁡h→0+f(x+h)−f(x−h)2hf'(x) = \lim_{h\to 0^+} \frac{f(x+h) - f(x - h)}{2h}

Let’s define a non-limit version

Δhf(x)=f(x+h)−f(x−h)2h\Delta_h f(x) = \frac{f(x + h) - f(x - h)}{2h}

for fixed xx, if hh is sufficiently small, then Δhf≈f′\Delta_h f \approx f' near xx. Δhf\Delta_h f is known as finite difference.

We can explicitly define a numerical derivative of a function ff via

we might also consider defining a decorator that returns f(x),Δh(x)f(x), \Delta_h(x) from a function definition

Exercise

Estimate the rate of convergence for Newton’s method when we use a finite difference to approximate the derivative.

Does this change with the value of hh?

Root Finding in SciPy

scipy.optimize.fsolve finds a root of a function starting from an initial guess x0. It calls the hybrd routine, a Newton-type method with safeguards for when the plain Newton step goes astray. If you do not supply the derivative, fsolve approximates it with finite differences.

fsolve always returns an array, even for a scalar equation. As with Newton’s method, the root you get depends on the initial guess:

You can pass the derivative with fprime. With full_output=True, fsolve also returns a dictionary info (number of function evaluations nfev, final value fvec, ...), an integer flag ier that equals 1 on success, and a message. Here is the cubic from the comparison above:

fsolve evaluated the derivative only once. After that it updates its approximation of the derivative from the values of ff it computes anyway (a Broyden update), and re-evaluates it only when progress stalls. So the finite-difference version costs only one extra evaluation of ff here.

Check the answer

fsolve does not raise an error when it fails. The function x2+1x^2 + 1 has no real root:

All we get is a RuntimeWarning, which is easy to miss, and a value that is not a root. With full_output=True there is no warning at all; the failure only shows up in ier and in the residual:

fsolve stops when two consecutive iterates agree to a relative tolerance xtol (default about 1.5×10−81.5 \times 10^{-8}), so it never tests ∣f(x)∣|f(x)| directly. Its flag can also report a failure when the root has in fact been found to round-off. The reliable check is to look at the residual ∣f(x)∣|f(x)| yourself.