The Rootfinding Problem
Many questions reduce to solving one equation : 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 becomes . A root (zero) satisfies . Examples:
- the interest rate with monthly payment : ;
- the solution of : ;
- inverting at : .
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 to , the root moves to about with
So the absolute condition number of a simple root is : a flat crossing (small ) is ill-conditioned. A multiple root () is worst: for a double root, a perturbation of size moves the root by about , so only half the digits can be trusted.
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 does not always mean is close to the root: if is flat near , a large error in gives a tiny . Error in residual condition number .
Rates of convergence
Iterative methods produce . With errors :
| Name | Definition | Digits gained per step |
|---|---|---|
| linear, rate | constant, | |
| superlinear, order | multiply by each step | |
| quadratic | doubles each step |
Bisection is linear with (Bisection); Newton's method is quadratic (Newton's Method); the secant method has order (Secant Method).
Stopping criteria
Iterations stop when one of these holds:
- the step is below an absolute or relative tolerance (for fast methods, the step estimates the error);
- the residual is small (careful, see above);
- a maximum number of iterations is reached, which signals failure.
In software
scipy.optimize.root_scalaroffersbrentq,brenth,ridder,bisect,newton,secant,halley;brentqwith a bracket is the safe default (Rootfinding in Practice).- Spreadsheet "Goal Seek" and
RATE/IRRfunctions solve exactly these one-variable problems.
Exercises
Exercise 1: Set up the equation
At what do and intersect? Write it as and give an interval containing the root.
Answer
, root in .
Solution
and , so a root lies in between (it is about ).
Exercise 2: Condition number
Find the absolute condition number of the root of .
Answer
.
Solution
, , so .
Exercise 3: Order from data (code)
Errors of a method are . Estimate the order from .
Answer
.
Solution
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 of is…
- always 1
Quadratic convergence means the number of correct digits…
- increases by 2 each step
- roughly doubles each step
- is constant
- halves each step
For a double root, a perturbation of size can move the root by about…