Numerical methods

Root-finding methods

Bisection, fixed-point iteration, Newton's method and the secant method, each run in doubles the way a textbook runs it, with its table of iterations. Because the root is also known with certainty (from zeros), every row carries the true error, and the order of convergence is measured, not assumed.

const n = newton("x^2 - 2", 1);
n.iterations;   // [{ n, x, fx, dfx, error }, …]   error = |x − √2|, the real one
n.order;        // 1.9998   observed
n.root;         // the certified root the errors are measured against

bisection("x^2 - 2", [1, 2], { steps: 10 }).iterations;   // each with bound (b − a)/2ⁿ⁺¹ and the true error
secant("x^3 - x - 1", 1, 2).order;                         // about 1.6
fixedPoint("cos(x)", 1).multiplier;                        // g′ at the fixed point: −0.6736…

Order of convergence

With en=∣xn−x∗∣e_n = |x_n - x^*|, a method has order pp when en+1≈Cen pe_{n+1} \approx C e_n^{\,p}. Order 1 (with C<1C < 1) gains a fixed number of digits each step; order 2 doubles the number of correct digits each step. From three successive errors,

p≈log⁡(en+1/en)log⁡(en/en−1), p \approx \frac{\log(e_{n+1}/e_n)}{\log(e_n/e_{n-1})},

which is what order reports, using the last errors that are still well above rounding.

methodsteporderneeds
bisectionhalve the bracket, keep the half with the sign change1, with C=12C = \tfrac12a sign change; always converges
fixed pointx←g(x)x \leftarrow g(x)1, with C=∣g′(x∗)∣C = |g'(x^*)|∣g′(x∗)∣<1|g'(x^*)| < 1
Newtonx←x−f(x)/f′(x)x \leftarrow x - f(x)/f'(x)2 at a simple rootthe derivative, a good start
secantx←x−f(x)x−xprevf(x)−f(xprev)x \leftarrow x - f(x)\dfrac{x - x_{\text{prev}}}{f(x) - f(x_{\text{prev}})}(1+5)/2≈1.618(1 + \sqrt5)/2 \approx 1.618two starting points

Why Newton's method squares the error

Expand ff about xnx_n and evaluate at the root: 0=f(xn)+f′(xn)(x∗−xn)+12f′′(ξ)(x∗−xn)20 = f(x_n) + f'(x_n)(x^* - x_n) + \tfrac12 f''(\xi)(x^* - x_n)^2. Dividing by f′(xn)f'(x_n) and rearranging,

xn+1−x∗=f′′(ξ)2f′(xn) (xn−x∗)2. x_{n+1} - x^* = \frac{f''(\xi)}{2 f'(x_n)}\,(x_n - x^*)^2 .

At a multiple root f′(x∗)=0f'(x^*) = 0, the argument fails, and the observed order drops to 1: try newton("(x - 1)^2", 2).

Newton's method with fractions

With { exact: true }, newton works the way it would by hand with fractions: every iterate is an exact Core Rational, so there is no rounding floor at all. For x2−2x^2 - 2 from 1 the iterates are 32,1712,577408,665857470832\tfrac32, \tfrac{17}{12}, \tfrac{577}{408}, \tfrac{665857}{470832}, the convergents of 2\sqrt2 that the Babylonians used. The price shows in denominatorDigits: the denominators double in length each step, just as the correct digits do. floats holds the double-precision iterates from the same start, so the two can be compared row by row. This needs ff built from ++, −-, ×\times, ÷\div and integer powers, so that f(x)f(x) and f′(x)f'(x) are fractions; the default is 6 steps.

Fixed points and the multiplier

For x←g(x)x \leftarrow g(x), xn+1−x∗=g(xn)−g(x∗)≈g′(x∗)(xn−x∗)x_{n+1} - x^* = g(x_n) - g(x^*) \approx g'(x^*)(x_n - x^*). So each step multiplies the error by about g′(x∗)g'(x^*). fixedPoint returns an enclosure of that multiplier at the certified fixed point and the observed ratio of successive errors, which should agree with it. The proof of convergence by the contraction principle is in recursive sequences.

What the true error shows

The root the errors are measured against is the one the method actually converges to. A sign-changing bracket can hold several roots: bisection on (x−1)(x−6)(x−9)(x - 1)(x - 6)(x - 9) over [0,10][0, 10] keeps [0,5][0, 5] after the first step and closes in on 1, not on the root 6 nearest the first midpoint, so bisection takes the certified root inside its last bracket. When that root is known only to a small interval, error is an upper bound for the distance, not the exact distance.

A program cannot know its error, so in practice the step size ∣xn+1−xn∣|x_{n+1} - x_n| or the residual ∣f(xn)∣|f(x_n)| stands in for it. Here the error itself is in the table, measured against a root enclosed to 25 digits, so the stand-ins can be judged. The last rows show the floor that rounding sets: the error stops near 10−1610^{-16} times the size of the root however many more steps are taken.

callreturns
bisection(f, [a, b], { steps = 20 }){ iterations: [{ n, a, b, x, fx, bound, error }], root, order }
newton(f, x0, { steps = 10 }){ iterations: [{ n, x, fx, dfx, error }], root, order, derivative }
newton(f, x0, { exact: true, steps = 6 })the same with x, fx and dfx as Rationals, plus denominatorDigits on each row and floats, the double iterates
secant(f, x0, x1, { steps = 12 }){ iterations: [{ n, x, fx, error }], root, order }
fixedPoint(g, x0, { steps = 25 }){ iterations, root, multiplier, ratio, order }
Try itEdit the code and run it with @ratmath/analytic and Core's R, Rational, RationalInterval in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs