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 , a method has order when . Order 1 (with ) gains a fixed number of digits each step; order 2 doubles the number of correct digits each step. From three successive errors,
which is what order reports, using the last errors that are still well above rounding.
| method | step | order | needs |
|---|---|---|---|
| bisection | halve the bracket, keep the half with the sign change | 1, with | a sign change; always converges |
| fixed point | 1, with | ||
| Newton | 2 at a simple root | the derivative, a good start | |
| secant | two starting points |
Why Newton's method squares the error
Expand about and evaluate at the root: . Dividing by and rearranging,
At a multiple root , 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 from 1 the iterates are , the convergents of 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 built from , , , and integer powers, so that and are fractions; the default is 6 steps.
Fixed points and the multiplier
For , . So each step multiplies the error by about . 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 over keeps 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 or the residual 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 times the size of the root however many more steps are taken.
| call | returns |
|---|---|
| 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 } |
@ratmath/analytic and Core's R, Rational, RationalInterval in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.