Euler to Runge-Kutta
The one-step methods for and a two-step method, run in doubles with their table of steps. The validated solver supplies the true solution at every step, so the global error is in the table, and halving the step shows each method's order.
const r = odeMethod("y - t^2 + 1", { y0: "1/2", until: 2, steps: 10, method: "rk4" });
r.steps; // [{ n, t, y, exact, error }, …]
r.finalError; // the true error at t = 2
stepSizeStudy("y - t^2 + 1", { y0: "1/2", until: 2, method: "euler" }).rows;
// [{ steps, h, error, ratio, observedOrder }, …]
stability("rk4").interval; // [-2.785…, 0]
The methods
| method | step from with size | order |
|---|---|---|
| euler | 1 | |
| heun | , | 2 |
| midpoint | 2 | |
| rk4 | , the classical four stages | 4 |
| ab2 | , Adams-Bashforth, started with one Heun step | 2 |
Local and global error
A method of order makes an error of size in one step (the local error). Over the steps needed to cross a fixed interval these add up, and the global error at the end is of size . So halving divides the final error by about : by 2 for Euler, by 16 for the classical Runge-Kutta method. stepSizeStudy runs the same problem with several step counts and reports the ratio of successive errors and the order it implies.
The errors are true ones: at each step the method's double is compared with the validated enclosure of , and error is the largest possible distance between them.
Stability
On the test equation each of these methods multiplies by a polynomial per step: for Euler, for the second-order methods, and the degree-4 Taylor polynomial of for rk4. The true solution decays when ; the numerical one decays only when . For real negative that is an interval, for Euler and for rk4, with the left end enclosed here as a root of . A step size outside it makes the computed solution grow although the true one dies away: the reason stiff equations force small steps on explicit methods.
| call | returns |
|---|---|
| odeMethod(f, { y0, t0 = 0, until, steps = 10, method }) | { method, order, h, steps: [{ n, t, y, exact, error }], finalError } |
| stepSizeStudy(f, { …, counts }) | { rows: [{ steps, h, error, ratio, observedOrder }] } |
| stability(method) | { polynomial, interval, largestStep(lambda) } |
@ratmath/analytic and Core's R, Rational, RationalInterval in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.