anthropocene
Computational Physics / ODE Solvers, One by One
◆ core30 min

RK4, and where the magic numbers come from

Four slope samples per step, weighted 1-2-2-1, and suddenly the error falls as the fourth power. The coefficients are not arbitrary — they are what you get when you demand that error terms cancel.

Forward Euler's problem is that it commits to the slope measured at the starting point and uses it for the whole interval. If the slope changes across the step — and it always does — that is wrong from the moment you leave.

The Runge–Kutta idea is to look before you leap: take trial steps inside the interval, sample the slope there, and combine the samples so that the low-order error terms cancel.

Two samples: Heun

Step out with Euler to get a provisional endpoint, sample the slope there, then use the average of the two slopes:

This is the trapezoid rule applied to time-stepping, and it is second order. The reason it works is the same symmetry argument that made the central difference second order: averaging a slope at the start with a slope at the end cancels the term that a one-sided estimate leaves behind.

predictcommit first

Heun costs two f-evaluations per step; forward Euler costs one. At the same total number of f-evaluations, which is more accurate on a smooth problem?

Four samples: the classic

RK4 samples four times — once at the start, twice at the midpoint, once at the end — and weights them :

Those weights look like folklore. They are not. Expand the numerical step as a Taylor series in , expand the true solution the same way, and demand that the two agree through . That gives a system of order conditions on the coefficients, and is a solution. The pattern is Simpson's rule — which is not a coincidence, since for depending on alone, RK4 is Simpson's rule.

If you have met Simpson's rule for integration, you have already met the weights.

convergence — Harmonic oscillator (ω = 1)
roundoff floor10⁻³10⁻²10⁻¹10⁻¹⁴10⁻¹²10⁻¹⁰10⁻⁸10⁻⁶10⁻⁴10⁻²STEP SIZE HENDPOINT ERROR
RK4 (excluded)RK4 — fitted slope 4.038
integrators
RK4 · theory 4measured 4.038
RK4's measured slope on the harmonic oscillator. Note the dimmed points at both ends: the coarse steps are pre-asymptotic (the h⁴ term does not dominate yet) and the finest ones have hit the roundoff floor. Only the middle is fitted — see below.

That plot is worth reading carefully, because it contains the previous chapter's lesson in a new guise. The dimmed points at the left are pre-asymptotic: at large the neglected and higher terms are not yet negligible, so the slope is not 4 there. The dimmed points at the right have hit the roundoff floor from the last chapter — around , where the truncation error is finally smaller than the noise in the arithmetic.

Fitting a line through all the points would give an answer near 4.6, which is wrong. The order is a property of the asymptotic regime, and measuring it means finding that regime first. The platform does this by computing the order between each consecutive pair and fitting only over the longest run where those local orders agree.

recallscheduled for review
Where do RK4's 1/6, 1/3, 1/3, 1/6 weights come from, and why is RK4 fourth order rather than fifth?

Build it

implementgraded on measured order

Implement one RK4 step. You have f(t, y) → array, t, y (array) and h. Compute the four stages and return the weighted combination.

step(f, t, y, h) → State

What to carry forward

  • Order comes from cancellation, engineered by choosing coefficients to kill Taylor terms.
  • Higher order beats smaller steps at equal cost, on smooth problems, by a widening margin.
  • Always measure the order you actually got. The measurement is the test suite for a numerical method, and it is how you catch a subtly wrong stage.
  • Order is measured in the asymptotic regime — not too coarse, not below the roundoff floor.