---
title: "ODE solvers: Euler, Runge–Kutta and their errors"
heading: Stepping a differential equation forward, and what each step costs in accuracy
eyebrow: "A13 · Lesson 40 · ODE solvers: Euler, Runge–Kutta and their errors"
xp: 40
---

::: hook
A8 used numerical solvers as tools. Here we take them apart. Euler's method follows the slope for one step; Runge–Kutta methods sample the slope at a few points inside the step and combine them so their errors cancel. The payoff is the **order**: halving the step cuts Euler's error in half but RK4's by a factor of 16. On an oscillator, which is the heart of every resonant circuit, the difference is dramatic. Euler's method adds energy every step and spirals outward, while RK4 holds the orbit for thousands of periods.
:::

::: play
- The oscillator $\ddot y = -y$, written as one complex equation $\dot z = jz$ with $z = y + j\dot y$: the exact solution goes round the unit circle (dashed). The step size is h, and the curves run for N steps.
- Euler's method multiplies by $1 + jh$ each step, whose size is $\sqrt{1 + h^2} > 1$: the solution spirals outward, gaining 37% in amplitude per period at h = 0.1.
- RK4 multiplies by $1 + z + \frac{z^2}{2} + \frac{z^3}{6} + \frac{z^4}{24}$ with $z = jh$; its size is $1 - \frac{h^6}{144}$, so at h = 0.1 it loses less than a millionth per period. Raise h toward 2.8 to see RK4 finally go wrong.
:::
:::

```sim plot
view: complex
range: 2.2
slider: h = 0.02..2.9 step 0.01 = 0.1
slider: N = 10..400 step 10 = 120
curve: exp(j*u) | over: u = 0..2*pi | dashed
curve: (1 + j*h)^u | over: u = 0..N | label: Euler
curve: (1 + j*h - h^2/2 - j*h^3/6 + h^4/24)^u | over: u = 0..N | color: ch3 | label: RK4
readout: Euler amplitude after one period = (1 + h^2)^(pi/h)
readout: RK4 amplitude after one period = abs(1 + j*h - h^2/2 - j*h^3/6 + h^4/24)^(2*pi/h)
```

::: predict
## Euler's method on $\dot y = -y$ has error 0.019 at t = 1 with h = 0.1. What's the error with h = 0.05?

- [ ] 0.019
- [x] About 0.0094
- [ ] About 0.0048
- [ ] About 0.0012

::: reveal
0.0094: Euler is first-order, so halving h halves the global error (0.3487 and 0.3585 against $e^{-1} = 0.3679$). RK4 with h = 0.1 is already off by only $3.3 \times 10^{-7}$.
:::
:::

::: nameit
## Local and global error

A method's **local error** is what one step gets wrong starting from the exact value; for a method of order p it's $O(h^{p+1})$. Over a fixed interval there are $1/h$ steps, so the **global error** is $O(h^p)$.

| Method | Slope evaluations per step | Global error |
|---|---|---|
| forward Euler | 1 | $O(h)$ |
| Heun / midpoint (RK2) | 2 | $O(h^2)$ |
| classical RK4 | 4 | $O(h^4)$ |

RK4, for $\dot y = f(t, y)$:

$$k_1 = f(t, y),\; k_2 = f\!\left(t + \tfrac h2, y + \tfrac h2k_1\right),\; k_3 = f\!\left(t + \tfrac h2, y + \tfrac h2k_2\right),\; k_4 = f(t + h, y + hk_3)$$

$$y \leftarrow y + \frac h6(k_1 + 2k_2 + 2k_3 + k_4)$$

## Stability on the test equation

On $\dot y = \lambda y$ a one-step method multiplies by $G(h\lambda)$: Euler $1 + h\lambda$; RK4 the Taylor series of $e^{h\lambda}$ through the fourth power. The method is **stable** where $\lvert G\rvert \le 1$:

- for a decay ($\lambda = -k$), Euler needs $h < 2/k$ and RK4 $h < 2.785/k$;
- for an oscillation ($\lambda = j\omega$), Euler is never stable ($\lvert 1 + jh\omega\rvert > 1$); RK4 is slightly damping for $h\omega < 2.83$.

## Long runs

Over many periods, small per-step energy errors accumulate. **Symplectic** methods such as leapfrog (Verlet), used in FDTD and orbital mechanics, don't drift in energy at all: their errors show up as a tiny phase shift instead.

## Choosing h

For a target error ε with global error $Ch^p$, $h = (\varepsilon/C)^{1/p}$. High order wins when accuracy matters: four times the work per step buys far fewer steps.
:::

::: example
## Solve $\dot y = -y$, y(0) = 1, to t = 1 with Euler (h = 0.1, 0.05) and RK4 (h = 0.1).

::: solution
1. **Euler.**
   $$\begin{aligned} 0.9^{10} &= 0.34868 \\ 0.95^{20} &= 0.35849 \end{aligned}$$
   with errors 0.0192 and 0.0094.
2. **RK4's factor per step.**
   $$\begin{aligned} &1 - 0.1 + 0.005 \\ &\quad - 0.000167 + 0.0000042 \\ &= 0.9048375 \end{aligned}$$
3. **Ten RK4 steps.**
   $$0.9048375^{10} = 0.3678798$$
   an error of $3.3 \times 10^{-7}$, from 40 evaluations against Euler's 20.

**Answer:** Euler gives 0.34868 and 0.35849 (errors 0.0192 and 0.0094); RK4 gives 0.3678798, an error of $3.3 \times 10^{-7}$.
:::
:::

::: example
## An LC tank simulated with Euler at 63 steps per period ($h\omega = 0.1$): how fast does its amplitude grow?

::: solution
1. **Growth per step.** Each step multiplies the amplitude by $\sqrt{1 + 0.01}$.
2. **Steps per period.**
   $$2\pi/0.1 = 62.8$$
3. **Growth per period.**
   $$(1.01)^{31.4} = 1.367$$
   That's 37% more amplitude per cycle, from a method that looks harmless step by step.
4. **Compare with RK4.** At the same step RK4 loses $4.4 \times 10^{-7}$ per cycle.

**Answer:** the amplitude grows by 37% each cycle.
:::
:::

::: example
## Solve $\dot y = -y$, y(0) = 1, to t = 1 with Heun's method (a second-order Runge–Kutta method) and h = 0.1.

::: solution
1. **Heun's factor.** On $\dot y = \lambda y$, Heun's step multiplies y by
   $$\begin{aligned} &1 + h\lambda + \frac{(h\lambda)^2}{2} \\ &= 1 - 0.1 + 0.005 \\ &= 0.905 \end{aligned}$$
   against the exact $e^{-0.1} = 0.904837$.
2. **Ten steps.**
   $$0.905^{10} = 0.368541$$
   The exact value is 0.367879.
3. **Compare the errors.** The error is $6.6 \times 10^{-4}$, between Euler's (about $1.9 \times 10^{-2}$ at this step) and RK4's (about $3 \times 10^{-7}$).
4. **Order 2.** Halving h would cut it by four, as befits order 2.

**Answer:** y(1) ≈ 0.368541, an error of $6.6 \times 10^{-4}$.
:::
:::

::: check
given: k = 3 | 8 | 10; h = 0.01 | 0.05; y0 = 1 | 5
answer: y0*(1 - k*h)
tolerance: 0.01%

## One Euler step on $\dot y = -\given{k}y$ from y = {y0} with h = {h}: what's the new y?

::: right
Right. $y(1 - kh)$ = {answer}.
:::

::: misses
- [y0*exp(-k*h)] That's the exact solution; Euler follows the slope in a straight line.
- [any] Not yet. $y + h\cdot f(y) = y - hky$.
:::
:::

::: check
given: p = 1 | 2 | 4; k = 2 | 10
answer: k^p
tolerance: 0.5%

## A method of order {p}: by what factor does the global error shrink when the step is divided by {k}?

::: right
Right. $k^p$ = {answer}.
:::

::: misses
- [k^(p + 1)] That's the local error's factor per step; there are also k times as many steps.
- [any] Not yet. Global error $\propto h^p$.
:::
:::

::: check
given: k = 50 | 1000 | 100000
answer: 2/k
unit: s
tolerance: 0.5%

## A circuit mode decays as $e^{-kt}$ with k = {k} s⁻¹. Above what step does forward Euler become unstable on it?

::: right
Right. $2/k$ = {answer} s.
:::

::: misses
- [1/k] At $h = 1/k$ Euler jumps straight to zero; it goes unstable at $2/k$.
- [any] Not yet. Need $\lvert 1 - hk\rvert \le 1$.
:::
:::

::: check
given: h = 0.05 | 0.1 | 0.2
answer: (1 + h^2)^(pi/h)
tolerance: 0.5%

## Euler's method on an oscillator with $h\omega$ = {h}: by what factor does the amplitude grow per period?

::: right
Right. $(1 + h^2\omega^2)^{\pi/(h\omega)}$ = {answer}.
:::

::: misses
- [(1 + h^2)^(2*pi/h)] The growth per step is the square root, $\sqrt{1 + h^2\omega^2}$.
- [any] Not yet. $\lvert 1 + jh\omega\rvert$ per step, $2\pi/(h\omega)$ steps per period.
:::
:::

::: check
given: C = 1 | 10; e = 0.000001 | 0.00000001; p = 2 | 4
answer: (e/C)^(1/p)
tolerance: 0.5%

## A method's global error is about $\given{C}h^{\given{p}}$. What step reaches an error of {e}?

::: right
Right. $h = (\varepsilon/C)^{1/p}$ = {answer}.
:::

::: misses
- [e/C] Take the p-th root: $h = (\varepsilon/C)^{1/p}$.
- [any] Not yet. Solve $Ch^p = \varepsilon$.
:::
:::

::: check
kind: choice

## You need to simulate a satellite's orbit, or an undamped LC tank, for a million periods. Which integrator keeps the energy from drifting?

- [ ] Forward Euler with a tiny step
- [ ] RK4
- [x] A symplectic method such as leapfrog (Verlet)
- [ ] Backward Euler

::: misses
- [1] It still adds energy every step; a tiny step only slows the drift.
- [2] Very accurate per period, but its small energy error still accumulates over a million periods.
- [4] It removes energy every step, damping an undamped system.
:::
:::

::: check
kind: parts
given: k = 4 | 10; h = 0.05 | 0.1

## Heun's method solves $\dot y = -\given{k}y$, y(0) = 1, with h = {h}.

::: part
answer: 1 - k*h + (k*h)^2/2
tolerance: 0.5%

### By what factor does each step multiply y?

::: misses
- [1 - k*h] That's Euler's factor; Heun adds the second-order term.
- [any] $1 + z + \frac{z^2}{2}$ with $z = -kh$.
:::
:::

::: part
answer: (1 - k*h + (k*h)^2/2)^10 - exp(-10*k*h)
tolerance: 1%

### What is the error after 10 steps?

::: misses
- [any] The computed $G^{10}$ minus the exact $e^{-10kh}$.
:::
:::
:::

::: check
kind: derivation
vars: z = -2..0.5

## On $\dot y = \lambda y$, each one-step method multiplies y by a polynomial in $z = h\lambda$.

::: step
answer: 1 + z

### What is Euler's factor?
:::

::: step
answer: 1 + z + z^2/2

### Heun's (second-order Runge–Kutta)?
:::

::: step
answer: 1 + z + z^2/2 + z^3/6 + z^4/24

### Classical RK4's?
:::

::: right
Right. Each matches $e^z$ through its order, which is why the stability regions grow with the order.
:::
:::

::: explain
## Why does forward Euler make an undamped oscillator's amplitude grow?

::: model
Euler's method moves along the tangent at the current point for a whole step. For an oscillator the exact motion is a circle in the phase plane, and the tangent to a circle points slightly outward from it: following it for a finite step always lands on a larger circle. Algebraically, each step multiplies the complex state by $1 + jh\omega$, whose size is $\sqrt{1 + h^2\omega^2}$, a little more than 1. The amplitude grows by that factor every step, which compounds to a large gain over many periods. A smaller step reduces the growth per period but never removes it.
:::
:::

::: practice

::: check
given: k = 2 | 20; h = 0.02 | 0.04; y0 = 2 | 3
answer: y0*(1 - k*h)
tolerance: 0.01%

## Euler step on $\dot y = -\given{k}y$, y = {y0}, h = {h}?

::: misses
- [any] $y(1 - kh)$.
:::
:::

::: check
given: p = 2 | 3; k = 3 | 4
answer: k^p
tolerance: 0.5%

## Error reduction, order {p}, step divided by {k}?

::: misses
- [any] $k^p$.
:::
:::

::: check
given: k = 100 | 200 | 500 | 1000 | 5000 | 20000
answer: 2/k
unit: s
tolerance: 0.5%

## Euler's stability limit for decay rate {k} s⁻¹?

::: misses
- [any] $2/k$.
:::
:::
:::
