A13 · Lesson 40 · ODE solvers: Euler, Runge–Kutta and their errors
Stepping a differential equation forward, and what each step costs in accuracy
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.
- The oscillator , written as one complex equation with : 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 each step, whose size is : the solution spirals outward, gaining 37% in amplitude per period at h = 0.1.
- RK4 multiplies by with ; its size is , so at h = 0.1 it loses less than a millionth per period. Raise h toward 2.8 to see RK4 finally go wrong.
Predict
Euler's method on has error 0.019 at t = 1 with h = 0.1. What's the error with h = 0.05?
0.0094: Euler is first-order, so halving h halves the global error (0.3487 and 0.3585 against ). RK4 with h = 0.1 is already off by only .
Name it
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 . Over a fixed interval there are steps, so the global error is .
| Method | Slope evaluations per step | Global error |
|---|---|---|
| forward Euler | 1 | |
| Heun / midpoint (RK2) | 2 | |
| classical RK4 | 4 |
RK4, for :
Stability on the test equation
On a one-step method multiplies by : Euler ; RK4 the Taylor series of through the fourth power. The method is stable where :
- for a decay (), Euler needs and RK4 ;
- for an oscillation (), Euler is never stable (); RK4 is slightly damping for .
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 , . High order wins when accuracy matters: four times the work per step buys far fewer steps.
Worked example
Solve , y(0) = 1, to t = 1 with Euler (h = 0.1, 0.05) and RK4 (h = 0.1).
Show the solution
- Euler. with errors 0.0192 and 0.0094.
- RK4's factor per step.
- Ten RK4 steps. an error of , 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 .
Worked example
An LC tank simulated with Euler at 63 steps per period (): how fast does its amplitude grow?
Show the solution
- Growth per step. Each step multiplies the amplitude by .
- Steps per period.
- Growth per period. That's 37% more amplitude per cycle, from a method that looks harmless step by step.
- Compare with RK4. At the same step RK4 loses per cycle.
Answer the amplitude grows by 37% each cycle.
Worked example
Solve , y(0) = 1, to t = 1 with Heun's method (a second-order Runge–Kutta method) and h = 0.1.
Show the solution
- Heun's factor. On , Heun's step multiplies y by against the exact .
- Ten steps. The exact value is 0.367879.
- Compare the errors. The error is , between Euler's (about at this step) and RK4's (about ).
- Order 2. Halving h would cut it by four, as befits order 2.
Answer y(1) ≈ 0.368541, an error of .
Check
One Euler step on from y = 5 with h = 0.01: what's the new y?
Check
A method of order 2: by what factor does the global error shrink when the step is divided by 2?
Check
A circuit mode decays as with k = 1000 s⁻¹. Above what step does forward Euler become unstable on it?
Write units the way you normally would: 1.5 s and 1500 ms are the same answer.
Check
Euler's method on an oscillator with = 0.1: by what factor does the amplitude grow per period?
Check
A method's global error is about . What step reaches an error of 1 × 10⁻⁶?
Check
You need to simulate a satellite's orbit, or an undamped LC tank, for a million periods. Which integrator keeps the energy from drifting?
Check
Heun's method solves , y(0) = 1, with h = 0.05.
(a)
By what factor does each step multiply y?
(b)
What is the error after 10 steps?
Check
On , each one-step method multiplies y by a polynomial in .
Step 1
What is Euler's factor?
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°.
Step 2
Heun's (second-order Runge–Kutta)?
Step 3
Classical RK4's?
Explain why
Why does forward Euler make an undamped oscillator's amplitude grow?
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 , whose size is , 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
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.