---
title: Adaptive step-size control
heading: Small steps where the solution changes fast, big ones where it doesn't
eyebrow: A13 · Lesson 41 · Adaptive step-size control
xp: 40
---

::: hook
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.
:::

::: play
- 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 $\tau\cdot\text{tol}^{1/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.
:::
:::

```sim plot
view: graph
x: t = 0..4 ms
y: h = 0.0001..1 ms log
slider: tau = 0.01..0.3 step 0.005 = 0.02 ms
slider: tol = 1e-10..0.001 = 1e-6 log
plot: (tau*tol^0.2*exp((t - floor(t))/(5*tau)) + 0.2 - abs(tau*tol^0.2*exp((t - floor(t))/(5*tau)) - 0.2))/2 | samples: 1600 | label: chosen step (capped at 0.2 ms)
readout: steps per half period, adaptive, about = 5/tol^0.2*(1 - exp(-1/(5*tau)))
readout: steps per half period at the edge's step = 1/(tau*tol^0.2)
```

::: predict
## An embedded RK pair estimates a step's error as $2.5\times$ smaller than the tolerance. What should the controller do?

- [ ] Reject the step
- [x] Accept it and make the next step a little larger
- [ ] Accept it and keep the same step
- [ ] Halve the next step

::: reveal
Accept and grow: for a fourth-order error estimate the next step is $h \times 0.9 \times 2.5^{1/5} = 1.08h$. The fifth root keeps changes gentle, and the safety factor 0.9 keeps the next step from landing just over the tolerance.
:::
:::

::: nameit
## 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 $\frac{y_{h/2} - y_h}{2^p - 1}$, as in Richardson extrapolation.

## Controlling the step

With error estimate err and tolerance $\text{tol} = \text{atol} + \text{rtol}\cdot\lvert y\rvert$:

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

$$h_{new} = h\cdot 0.9\left(\frac{\text{tol}}{\text{err}}\right)^{1/(p+1)}$$

clamped to between about $\frac15h$ and $5h$, since the local error scales as $h^{p+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).
:::

::: example
## An RK45 step of 1 ms has error estimate $4 \times 10^{-7}$ against a tolerance of $10^{-6}$. What next? And if the estimate were $10^{-5}$?

::: solution
1. **The first case.** The estimate is within the tolerance, so accept the step, with next step
   $$\begin{aligned} h &= 1 \times 0.9 \times (2.5)^{1/5} \\ &= 1.081\ \text{ms} \end{aligned}$$
2. **The second case.** With $10^{-5}$, reject the step and retry with
   $$\begin{aligned} h &= 0.9 \times (0.1)^{1/5} \\ &= 0.568\ \text{ms} \end{aligned}$$

**Answer:** accept the step and take 1.081 ms next; with an estimate of $10^{-5}$, reject it and retry with 0.568 ms.
:::
:::

::: 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}$.

::: solution
1. **The step at an edge.**
   $$\begin{aligned} h &= \tau\cdot\text{tol}^{1/5} \\ &= 20 \times 0.0631 \\ &= 1.26\ \text{µs} \end{aligned}$$
2. **Fixed step.** A fixed step that small needs 792 steps per 1 ms half period.
3. **Adaptive step.** It grows as $e^{t/5\tau}$ after each edge, giving
   $$\frac{5}{\text{tol}^{1/5}}(1 - e^{-1/(5\tau)}) = 79$$
   steps per half period: 10 times fewer.

**Answer:** about 792 fixed steps per half period against 79 adaptive ones, 10 times fewer.
:::
:::

::: 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?

::: 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 $h^5$, so with 0.9 as a safety factor
   $$\begin{aligned} h_\text{new} &= 0.9h\left(\frac{\text{tol}}{\text{err}}\right)^{1/5} \\ &= 0.9 \times 0.1 \times 0.1^{0.2} \\ &= 0.057 \end{aligned}$$
3. **The expected error.** Retrying with 0.057 should give an error near
   $$0.9^5 \times 10^{-6} = 6 \times 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
given: h = 1 | 10; e = 0.0000002 | 0.000005; t = 0.000001
answer: h*0.9*(t/e)^(1/5)
unit: µs
tolerance: 0.5%

## An order-4 estimate gives err = {e} on a step of {h} µs, with tolerance {t}. What's the next step size?

::: right
Right. $h\cdot 0.9(\text{tol}/\text{err})^{1/5}$ = {answer} µs.
:::

::: misses
- [h*0.9*(t/e)^(1/4)] The local error goes as $h^{p+1}$, so take the fifth root.
- [any] Not yet. $h_{new} = 0.9h(\text{tol}/\text{err})^{1/(p+1)}$.
:::
:::

::: check
given: a = 1.041 | 2.718 | 0.3679; d = -0.001 | 0.002 | 0.004; p = 2 | 4; let b = a + d
answer: (b - a)/(2^p - 1)
tolerance: 0.5%

## Step doubling with an order-{p} method gives {a} from one step and {b} from two half steps. What's the estimated error of the two-step answer?

::: right
Right. $\frac{y_{h/2} - y_h}{2^p - 1}$ = {answer}.
:::

::: misses
- [b - a] Divide by $2^p - 1$: most of the difference is the one-step answer's error.
- [any] Not yet. $\frac{y_{h/2} - y_h}{2^p - 1}$.
:::
:::

::: check
given: at = 0.0001 | 0.001; rt = 0.001 | 0.0001; y = 0.05 | 0.5
answer: at + rt*y
tolerance: 0.5%

## A solver has atol = {at} and rtol = {rt}. What's its tolerance where y = {y}?

::: right
Right. $\text{atol} + \text{rtol}\cdot\lvert y\rvert$ = {answer}.
:::

::: misses
- [rt*y] Add atol, which matters where y is small.
- [any] Not yet. $\text{atol} + \text{rtol}\lvert y\rvert$.
:::
:::

::: check
given: tau = 0.01 | 0.05; tol = 0.000001 | 0.00000001
answer: 1/(tau*tol^0.2)
tolerance: 1%

## An RC with τ = {tau} ms needs a step of $\tau\cdot\text{tol}^{1/5}$ at its edges (RK4, tolerance {tol}). How many steps would a fixed step that small take per 1 ms?

::: right
Right. {answer}.
:::

::: misses
- [1/(tau*tol^0.25)] For RK4 the local error goes as $h^5$, so it's the fifth root.
- [any] Not yet. $\frac{1\text{ ms}}{\tau\cdot\text{tol}^{1/5}}$.
:::
:::

::: check
given: tau = 0.01 | 0.05; tol = 0.000001 | 0.00000001
answer: 5/tol^0.2*(1 - exp(-1/(5*tau)))
tolerance: 1%

## For the same circuit, about how many adaptive steps per 1 ms half period, with the step growing as $e^{t/5\tau}$ after each edge?

::: right
Right. $\frac{5}{\text{tol}^{1/5}}(1 - e^{-1/(5\tau)})$ = {answer}.
:::

::: misses
- [any] Not yet. Integrate $dt/h(t)$ with $h = \tau\,\text{tol}^{1/5}e^{t/(5\tau)}$ over 1 ms.
:::
:::

::: check
kind: choice

## A solver at rtol = $10^{-6}$ and at rtol = $10^{-7}$ gives results that differ in the third digit. What does that tell you?

- [ ] The tighter run is wrong
- [x] Neither is converged to three digits; the problem may be stiff, unstable or have an event the solver is stepping over
- [ ] Both are right to six digits
- [ ] The tolerance has no effect

::: misses
- [1] There's no reason to trust the looser one more.
- [3] If both were right to six digits they'd agree to six.
- [4] It clearly changed the answer.
:::
:::

::: check
kind: parts
given: t = 0.000001 | 0.00001; N = 1000 | 100000

## An adaptive solver keeps each step's local error below {t} for {N} steps.

::: part
answer: N*t
tolerance: 0.5%

### What is the worst case for the accumulated error, if the local errors all add?

::: misses
- [any] N times the local tolerance.
:::
:::

::: part
answer: sqrt(N)*t
tolerance: 0.5%

### If instead the local errors behave like independent random values, about how big is the total?

::: misses
- [any] Random errors add in squares: $\sqrt N$ times the tolerance.
:::
:::
:::

::: check
kind: derivation
vars: h = 0.01..1, e = 0.000001..0.001, t = 0.000001..0.001, p = 2..6

## A method of order p has local error proportional to $h^{p+1}$.

::: step
answer: h*(t/e)^(1/(p + 1))

### A step h gave error e. What step would give exactly the tolerance t?
:::

::: right
Right. Solvers multiply that by a safety factor near 0.9 and limit how fast h may change.
:::
:::

::: explain
## How can a Runge–Kutta solver estimate its own error without knowing the true solution?

::: model
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, $h^{p+1}$, the solver can also predict what step would bring the error to the tolerance, and choose the next step accordingly.
:::
:::

::: practice

::: check
given: h = 2 | 50; e = 0.0000001 | 0.00001; t = 0.000001
answer: h*0.9*(t/e)^(1/5)
unit: µs
tolerance: 0.5%

## Next step from {h} µs, err {e}, tol {t}, order 4?

::: misses
- [any] $0.9h(\text{tol}/\text{err})^{1/5}$.
:::
:::

::: check
given: at = 0.00001 | 0.000000001; rt = 0.0001 | 0.000001; y = 3 | 0.002
answer: at + rt*y
tolerance: 0.5%

## Tolerance with atol {at}, rtol {rt}, y = {y}?

::: misses
- [any] $\text{atol} + \text{rtol}\lvert y\rvert$.
:::
:::

::: check
given: a = 0.3679 | 0.1653; d = 0.0002 | -0.003; p = 1 | 4; let b = a + d
answer: (b - a)/(2^p - 1)
tolerance: 0.5%

## Error estimate from {a} (one step) and {b} (two halves), order {p}?

::: misses
- [any] $\frac{y_{h/2} - y_h}{2^p - 1}$.
:::
:::
:::
