4.8.25 · D2Numerical Methods

Visual walkthrough — Adaptive step-size — RK45, error control

2,065 words9 min readBack to topic

Step 1 — A solution is a curve, and stepping means guessing the next dot

WHAT. We are solving an equation of the form . Read that out loud: "the slope of the curve at any point is given by a rule ." So is a slope machine — feed it a time and a height , it tells you how steep the curve is right there.

WHY start here. Every symbol later (, , ) is about one thing: taking a known dot on the true curve and guessing where the curve goes next. If you don't picture that dot, nothing else lands.

PICTURE. Look at the figure. The white curve is the true solution (which we do not know). The amber dot at is where we currently are, at height . The letter is the horizontal step — how far in we jump. Our job: estimate the height at .

Figure — Adaptive step-size — RK45, error control

Step 2 — One method gives an answer but no honesty check

WHAT. A Runge–Kutta method samples the slope machine at a few clever points inside the step, then takes a weighted average of those slopes to leap forward:

Term by term:

  • — the -th slope sample, a number the slope machine spat out at some point in the step.
  • — a fixed weight telling us how much to trust slope sample .
  • — the total rise: "step width times average slope."

WHY this is not enough. The figure shows the estimated dot next to the true dot. There is a gap — the true curve and our guess disagree. But the method never tells us the gap. We computed one number and have no idea how wrong it is. That silent gap is the whole problem.

PICTURE. The cyan dashed leap lands at ; the amber true dot sits slightly above. The vertical amber bar between them is the local error — real, but invisible to the method.

Figure — Adaptive step-size — RK45, error control

Step 3 — Two answers from the same slopes reveal the gap

WHAT. Here is the "embedded pair" trick. Keep the same slope samples , but average them with two different weight rows:

  • — weights giving a sharper (order-5) estimate.
  • — weights giving a rougher (order-4) estimate.
  • Both use the identical — no new calls to the slope machine.

Their difference is our free error estimate:

WHY it works. "Order" measures how fast the error shrinks as shrinks. The order-5 dot hugs the true curve much tighter than the order-4 dot. So the distance from the rough dot to the sharp dot is basically the distance from the rough dot to the truth — i.e. the order-4 error. We measured the invisible gap using two visible dots.

PICTURE. Two cyan dots land close together; the amber true dot is nearly on top of the order-5 dot. The bracket between the two cyan dots is — small, cheap, and honest.

Figure — Adaptive step-size — RK45, error control
Recall Why "free"?

The costly work is calling to get each . ::: Both answers reuse all the ; the second answer is just another weighted sum — a cheap dot product, no extra calls.


Step 4 — How the gap grows with step size: the law

WHAT. For an order- method, the local error obeys

  • — the order of the method (here 4).
  • — the power on ; local error shrinks one power faster than the order.
  • — an unknown constant baked into the curve's shape at this point. We never learn its value — and we won't need to.

WHY this exact power. Comes from Taylor Series Expansion: the method matches the true curve's Taylor series up to the term, so the first surviving mismatch is the term. Every lower term cancels by design.

PICTURE. A log–log plot of error versus . The relationship becomes a straight line of slope 5. Halving drops the error by . That steepness is the lever we're about to pull.

Figure — Adaptive step-size — RK45, error control

Step 5 — Cancel the unknown constant by taking a ratio

WHAT. We have two facts about the same curve:

The second line is a wish: "find the step whose error would land exactly on the tolerance ." Divide wish by reality:

WHY divide. We do not know and cannot measure it. Dividing the two equations makes cancel — the same unknown appears top and bottom. What survives depends only on quantities we have: , , and .

PICTURE. On the same log–log line from Step 4, mark two points: our current and the target . Both sit on the one straight line — same , same slope. The ratio is just sliding along that line.

Figure — Adaptive step-size — RK45, error control

Step 6 — Take the fifth root to unlock

WHAT. Undo the power by raising both sides to the :

Multiply by :

  • → we had error to spare → grow .
  • → error too big → shrink .
  • The fifth root tames the change: a ratio of only grows by , matching Step 4's slope-5 line.

WHY the root, not the raw ratio. Because error responds to like , a small nudge in is a big nudge in error. To land error on target we must move by only the fifth root of the error ratio — otherwise we massively overshoot.

PICTURE. A dial: the input "error ratio" needle swings wildly from to , but the output " multiplier" needle barely moves ( to ). The fifth root is the gearbox that gentles the response.

Figure — Adaptive step-size — RK45, error control

Step 7 — The safety factor and the clamps (guarding against overconfidence)

WHAT. The raw formula assumes is exact. It isn't — is itself an estimate. So the real controller is padded:

  • safety factor: aim slightly under tol so the retried step usually passes first try.
  • — never shrink by more than at once.
  • — never grow by more than at once.

WHY clamp. The law is only trustworthy near the current . A predicted jump would fling us into terrain the local model never saw. Clamping keeps every jump inside the model's valid zone.

PICTURE. The raw multiplier curve (cyan) vs the clamped one (amber): flat ceiling at , flat floor at , and shifted down slightly everywhere by .

Figure — Adaptive step-size — RK45, error control

Step 8 — The full loop: accept, grow, reject, shrink

WHAT. Assemble all pieces into the decision the algorithm makes every step:

  1. Build once; form , , and .
  2. Compute .
  3. If : accept, advance , then set the next via Step 7 (usually bigger).
  4. If : reject, keep , shrink via Step 7, retry.

WHY this matches the road-driving idea. Two cameras (orders 4 and 5) disagree by ; if they barely differ the road is straight (grow , floor it); if they disagree the turn is sharp (shrink , brake).

PICTURE. A flowchart of the loop with the two branches colour-coded: cyan "accept → advance & grow," amber "reject → stay & shrink."

Figure — Adaptive step-size — RK45, error control

Worked check — the three parent examples on the dial


The one-picture summary

Everything above collapses into one diagram: the log–log error line (slope ) carries you from your measured point to the target point , the fifth-root gearbox gentles the move, and the accept/reject branch decides whether you advance or retry.

Figure — Adaptive step-size — RK45, error control
Recall Feynman: the whole walkthrough in plain words

You're steering a dot along a hidden winding road. At each moment you take two guesses of the next spot — a rough one and a sharp one — using the same peek at the road, so the second guess is basically free. How far apart the two guesses are is your error . You know that if you halve your step the error shrinks by 32×, because error follows a slope-5 line. So to make your error exactly hit your allowed budget , you slide along that line — and because the line is so steep, you only nudge your step by the fifth root of the ratio . Then you play it safe: multiply by 0.9, and never let the step jump more than 5× in either direction. If the two guesses agreed well enough, you accept and speed up; if not, you stay put and slow down. That single rule — "tol over error, to the fifth root, clamped" — is the entire RK45 step controller.


Connections

  • Runge–Kutta Methods (RK4) — the fixed-step method whose stages we reuse.
  • Order of a Numerical Method — defines the that becomes the fifth-root exponent.
  • Local vs Global Truncation Error — why the local error carries the extra power.
  • Taylor Series Expansion — origin of the leading error term.
  • Dormand–Prince (RK45 used by scipy solve_ivp) — the actual weight rows.
  • Stiff ODEs and Stability — where even this clever controller struggles.
  • ← Back to the parent topic