A1341 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 τ⋅tol1/5; 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 = 10−6, 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 2.5× smaller than the tolerance. What should the controller do?

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 yh/2−yh2p−1, as in Richardson extrapolation.

Controlling the step

With error estimate err and tolerance tol=atol+rtol⋅|y|:

  1. If err ≤ tol, accept the step; otherwise reject it and redo it with a smaller h.
  2. Either way, choose the next step as
hnew=h⋅0.9(tolerr)1/(p+1)

clamped to between about 15h and 5h, since the local error scales as hp+1.

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 4×10−7 against a tolerance of 10−6. What next? And if the estimate were 10−5?

Show the solution
  1. The first case. The estimate is within the tolerance, so accept the step, with next step h=1×0.9×(2.5)1/5=1.081ms
  2. The second case. With 10−5, reject the step and retry with h=0.9×(0.1)1/5=0.568ms

Answer accept the step and take 1.081 ms next; with an estimate of 10−5, 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 10−6.

Show the solution
  1. The step at an edge. h=τ⋅tol1/5=20×0.0631=1.26µs
  2. Fixed step. A fixed step that small needs 792 steps per 1 ms half period.
  3. Adaptive step. It grows as et/5τ after each edge, giving 5tol1/5(1−e−1/(5τ))=79 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 10−5, against a tolerance of 10−6. What should the solver do?

Show the solution
  1. Accept or reject. Reject the step: the error is ten times too big.
  2. The new step. The error of an order-4 method scales as h5, so with 0.9 as a safety factor hnew=0.9h(tolerr)1/5=0.9×0.1×0.10.2=0.057
  3. The expected error. Retrying with 0.057 should give an error near 0.95×10−6=6×10−7 inside the tolerance.
  4. 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 τ⋅tol1/5 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 et/5τ after each edge?

Check

A solver at rtol = 10−6 and at rtol = 10−7 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 hp+1.

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?

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.