A13 · Lesson 41 · Adaptive step-size control
Small steps where the solution changes fast, big ones where it doesn't
A square wave into an RC filter: right after each edge the output changes fast, then it settles and barely moves. A fixed step small enough for the edges wastes almost all its effort on the flat parts. Every serious ODE solver, from scipy.integrate.solve_ivp to SPICE's transient analysis, instead estimates the error of each step as it goes and chooses the next step to keep that error near a tolerance you set. The estimate comes almost free from an embedded pair of Runge–Kutta formulas that share their slope evaluations.
- The curve is the step an RK4 controller would choose to keep each step's error at the tolerance, for an RC with time constant τ driven by a square wave with edges every 1 ms.
- Right after each edge the step is tiny, about ; as the exponential settles, the step grows by a factor e every 5τ.
- The readouts count steps per half period. With τ = 20 µs and tol = , the adaptive solver takes about 79 steps where a fixed step small enough for the edges would take 790.
Predict
An embedded RK pair estimates a step's error as smaller than the tolerance. What should the controller do?
Accept and grow: for a fourth-order error estimate the next step is . The fifth root keeps changes gentle, and the safety factor 0.9 keeps the next step from landing just over the tolerance.
Name it
Estimating the error
- Embedded pairs: the same slope evaluations give two answers of orders p and p + 1 (Dormand–Prince RK45: orders 4 and 5 from 6 evaluations, 7 with the last reused next step). Their difference estimates the lower-order answer's error, and the solver usually continues with the higher-order one.
- Step doubling: take one step of h and two of h/2; for order p the error of the two-step answer is about , as in Richardson extrapolation.
Controlling the step
With error estimate err and tolerance :
- If err ≤ tol, accept the step; otherwise reject it and redo it with a smaller h.
- Either way, choose the next step as
clamped to between about and , since the local error scales as .
rtol sets the relative accuracy; atol stops the solver from chasing tiny relative accuracy when y passes near zero.
What it controls, and what it doesn't
The tolerance bounds each step's local error. The global error can still grow (stiff or unstable problems) or accumulate over many steps. Check a result by rerunning with a tolerance 10 times tighter: if the answer changes in the digits you care about, it wasn't converged.
Circuit simulators add more: they stop exactly at source edges, and they restart with a tiny step after each one (the C2 integrator does this).
Worked example
An RK45 step of 1 ms has error estimate against a tolerance of . What next? And if the estimate were ?
Show the solution
- The first case. The estimate is within the tolerance, so accept the step, with next step
- The second case. With , reject the step and retry with
Answer accept the step and take 1.081 ms next; with an estimate of , reject it and retry with 0.568 ms.
Worked example
An RC with τ = 20 µs is driven by a 500 Hz square wave. Compare fixed and adaptive RK4 at a tolerance of .
Show the solution
- The step at an edge.
- Fixed step. A fixed step that small needs 792 steps per 1 ms half period.
- Adaptive step. It grows as after each edge, giving steps per half period: 10 times fewer.
Answer about 792 fixed steps per half period against 79 adaptive ones, 10 times fewer.
Worked example
An order-4 error estimate on a step of 0.1 is , against a tolerance of . What should the solver do?
Show the solution
- Accept or reject. Reject the step: the error is ten times too big.
- The new step. The error of an order-4 method scales as , so with 0.9 as a safety factor
- The expected error. Retrying with 0.057 should give an error near inside the tolerance.
- Frequent rejections. A good controller rarely rejects steps; frequent rejections suggest a tolerance too tight for the problem, or a discontinuity the solver keeps bumping into.
Answer reject the step and retry with h = 0.057.
Check
An order-4 estimate gives err = 2 × 10⁻⁷ on a step of 1 µs, with tolerance 1 × 10⁻⁶. What's the next step size?
Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.
Check
Step doubling with an order-4 method gives 2.718 from one step and 2.717 from two half steps. What's the estimated error of the two-step answer?
Check
A solver has atol = 0.0001 and rtol = 0.001. What's its tolerance where y = 0.5?
Check
An RC with τ = 0.01 ms needs a step of at its edges (RK4, tolerance 1 × 10⁻⁶). How many steps would a fixed step that small take per 1 ms?
Check
For the same circuit, about how many adaptive steps per 1 ms half period, with the step growing as after each edge?
Check
A solver at rtol = and at rtol = gives results that differ in the third digit. What does that tell you?
Check
An adaptive solver keeps each step's local error below 1 × 10⁻⁶ for 1000 steps.
(a)
What is the worst case for the accumulated error, if the local errors all add?
(b)
If instead the local errors behave like independent random values, about how big is the total?
Check
A method of order p has local error proportional to .
Step 1
A step h gave error e. What step would give exactly the tolerance t?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Explain why
How can a Runge–Kutta solver estimate its own error without knowing the true solution?
It computes two approximations to the same step that have errors of different sizes. An embedded pair uses the same slope evaluations to form one answer of order p and another of order p + 1. The higher-order answer is much closer to the truth, so the difference between them is almost entirely the lower-order answer's error, and that difference is known. Because the local error scales as a known power of the step, , the solver can also predict what step would bring the error to the tolerance, and choose the next step accordingly.
Practice
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.