TExcelTAD
Home › Numerical Methods › The Rootfinding Problem

The Rootfinding Problem

Many questions reduce to solving one equation f(x)=0f(x) = 0: the interest rate that gives a loan's payment, the angle that hits a target, the temperature at which two reactions balance, the inverse of a function with no closed-form inverse. This lesson sets up the problem, its conditioning and the language of convergence used by every method in this section.

Formulating a root problem

Any equation g(x)=h(x)g(x) = h(x) becomes f(x)=g(x)−h(x)=0f(x) = g(x) - h(x) = 0. A root (zero) rr satisfies f(r)=0f(r) = 0. Examples:

  • the interest rate ii with monthly payment PP: L i1−(1+i)−n−P=0\frac{L\,i}{1 - (1 + i)^{-n}} - P = 0;
  • the solution of cos⁡x=x\cos x = x: f(x)=cos⁡x−xf(x) = \cos x - x;
  • inverting y=x+tanh⁡xy = x + \tanh x at y=2y = 2: f(x)=x+tanh⁡x−2f(x) = x + \tanh x - 2.
python · try it yourself
import numpy as np
from scipy.optimize import brentq

L, n, P = 200_000, 360, 1200.0                  # loan, months, payment
f = lambda i: L * i / (1 - (1 + i) ** -n) - P
i = brentq(f, 1e-6, 0.05)
print(f"monthly rate {i:.6f}, annual rate {12 * i * 100:.4f} %")
print(f"cos x = x at x = {brentq(lambda x: np.cos(x) - x, 0, 1):.12f}")
monthly rate 0.005006, annual rate 6.0070 %
cos x = x at x = 0.739085133215

Conditioning of a root

If the data change ff to f+ϵgf + \epsilon g, the root moves to about r+δrr + \delta r with

δr≈−ϵ g(r)f′(r).\delta r \approx -\frac{\epsilon\,g(r)}{f'(r)}.

So the absolute condition number of a simple root is 1∣f′(r)∣\frac{1}{\lvert f'(r)\rvert}: a flat crossing (small f′(r)f'(r)) is ill-conditioned. A multiple root (f′(r)=0f'(r) = 0) is worst: for a double root, a perturbation of size ϵ\epsilon moves the root by about ϵ\sqrt{\epsilon}, so only half the digits can be trusted.

python · try it yourself
import numpy as np

for eps in (1e-6, 1e-10, 1e-14):
    simple = np.roots([1, -3, 2 - eps])             # (x-1)(x-2) - eps
    double = np.roots([1, -2, 1 - eps])             # (x-1)^2 - eps
    print(f"eps = {eps:.0e}: simple root moves {abs(min(simple) - 1):.1e}, double root moves {abs(max(double.real) - 1):.1e}")
eps = 1e-06: simple root moves 1.0e-06, double root moves 1.0e-03
eps = 1e-10: simple root moves 1.0e-10, double root moves 1.0e-05
eps = 1e-14: simple root moves 1.0e-14, double root moves 1.0e-07

The residual can mislead

A small residual ∣f(x~)∣\lvert f(\tilde{x})\rvert does not always mean x~\tilde{x} is close to the root: if ff is flat near rr, a large error in xx gives a tiny f(x~)f(\tilde{x}). Error in xx ≈\approx residual ×\times condition number 1∣f′(r)∣\frac{1}{\lvert f'(r)\rvert}.

Rates of convergence

Iterative methods produce x0,x1,x2,…→rx_0, x_1, x_2, \ldots \to r. With errors ϵk=xk−r\epsilon_k = x_k - r:

Name Definition Digits gained per step
linear, rate σ<1\sigma < 1 ∣ϵk+1∣≈σ∣ϵk∣\lvert\epsilon_{k+1}\rvert \approx \sigma\lvert\epsilon_k\rvert constant, −log⁡10σ-\log_{10}\sigma
superlinear, order q>1q > 1 ∣ϵk+1∣≈C∣ϵk∣q\lvert\epsilon_{k+1}\rvert \approx C\lvert\epsilon_k\rvert^q multiply by qq each step
quadratic q=2q = 2 doubles each step

Bisection is linear with σ=12\sigma = \frac{1}{2} (Bisection); Newton's method is quadratic (Newton's Method); the secant method has order 1+52≈1.618\frac{1 + \sqrt{5}}{2} \approx 1.618 (Secant Method).

Stopping criteria

Iterations stop when one of these holds:

  • the step ∣xk+1−xk∣\lvert x_{k+1} - x_k\rvert is below an absolute or relative tolerance (for fast methods, the step estimates the error);
  • the residual ∣f(xk)∣\lvert f(x_k)\rvert is small (careful, see above);
  • a maximum number of iterations is reached, which signals failure.

In software

  • scipy.optimize.root_scalar offers brentq, brenth, ridder, bisect, newton, secant, halley; brentq with a bracket is the safe default (Rootfinding in Practice).
  • Spreadsheet "Goal Seek" and RATE/IRR functions solve exactly these one-variable problems.

Exercises

Exercise 1: Set up the equation

At what x>0x > 0 do y=e−xy = e^{-x} and y=x2y = x^2 intersect? Write it as f(x)=0f(x) = 0 and give an interval containing the root.

Answer

f(x)=e−x−x2f(x) = e^{-x} - x^2, root in [0,1][0, 1].

Solution

f(0)=1>0f(0) = 1 > 0 and f(1)=e−1−1<0f(1) = e^{-1} - 1 < 0, so a root lies in between (it is about 0.70350.7035).

Exercise 2: Condition number

Find the absolute condition number of the root of f(x)=x3−8f(x) = x^3 - 8.

Answer

112\frac{1}{12}.

Solution

r=2r = 2, f′(2)=3⋅4=12f'(2) = 3 \cdot 4 = 12, so κ=1∣f′(r)∣=112\kappa = \frac{1}{\lvert f'(r)\rvert} = \frac{1}{12}.

Exercise 3: Order from data (code)

Errors of a method are 10−1,10−2,10−4,10−810^{-1}, 10^{-2}, 10^{-4}, 10^{-8}. Estimate the order qq from log⁡∣ϵk+1/ϵk∣log⁡∣ϵk/ϵk−1∣\frac{\log\lvert\epsilon_{k+1}/\epsilon_k\rvert}{\log\lvert\epsilon_k/\epsilon_{k-1}\rvert}.

Answer

q=2q = 2.

Solution
python · try it yourself
import numpy as np
e = np.array([1e-1, 1e-2, 1e-4, 1e-8])
L = np.log(e)
print(np.diff(L)[1:] / np.diff(L)[:-1])
[2. 2.]

Test yourself

The condition number of a simple root rr of ff is…

  1. ∣f(r)∣\lvert f(r)\rvert
  2. 1/∣f′(r)∣1/\lvert f'(r)\rvert
  3. ∣f′′(r)∣\lvert f''(r)\rvert
  4. always 1

Quadratic convergence means the number of correct digits…

  1. increases by 2 each step
  2. roughly doubles each step
  3. is constant
  4. halves each step

For a double root, a perturbation of size 10−1610^{-16} can move the root by about…

  1. 10−1610^{-16}
  2. 10−810^{-8}
  3. 10−3210^{-32}
  4. 11