A4 · Lesson 42 · Runge-Kutta and step size
Four looks per step instead of one
Euler's method (lesson 41) takes the slope at the start of each step and walks in a straight line. It's simple and it works, but its error only halves when you halve the step, so accurate answers take a huge number of steps. The classical Runge-Kutta method takes four slope samples per step and combines them; halving its step cuts the error sixteenfold. Most circuit simulators, orbit calculators and physics engines use a method of this family.
Predict
Euler's method gives an error of 1% with some step size. Halving the step, what does RK4's error do, compared with Euler's?
Euler's error is proportional to the step , so it halves. RK4's is proportional to , so halving divides it by . That's why RK4 with a modest step usually beats Euler with a tiny one, even though each RK4 step costs four evaluations.
Name it
The method
For , one RK4 step from to :
is Euler's slope; and are slopes at the middle of the step, using trial values; looks at the end. The weighted average matches the true solution's Taylor series up to .
def rk4_step(f, t, y, h):
k1 = f(t, y)
k2 = f(t + h / 2, y + h / 2 * k1)
k3 = f(t + h / 2, y + h / 2 * k2)
k4 = f(t + h, y + h * k3)
return y + h / 6 * (k1 + 2 * k2 + 2 * k3 + k4)
Systems: several variables at once
Higher-order equations become first-order systems. A series RLC circuit driven by has two states, the capacitor voltage and the current :
Write as a NumPy array [v, i] and f returning np.array([i / C, (Vs - R*i - v) / L]); the same rk4_step works unchanged, because the arithmetic is elementwise.
Choosing the step
- The step must be small compared with the fastest thing happening: a time constant, or a period of ringing. A tenth of it is a reasonable start.
- Check by halving. Run with and . For RK4, the error of the finer run is about . If it's not small enough, halve again.
- Very stiff problems (fast and slow time constants together) want implicit methods, as this course's circuit engine uses; A8 and C2 come back to this.
Worked example
Discharge from 1 V over one time constant with 10 steps, by Euler and by RK4. Compare the errors.
Show the solution
- Run both methods side by side. Ten steps of , then compare each with .
import math
tau, h = 1.0, 0.1
f = lambda t, v: -v / tau
ve = vr = 1.0
for k in range(10):
ve = ve + h * f(0, ve)
vr = rk4_step(f, k * h, vr, h)
print(abs(ve - math.exp(-1)), abs(vr - math.exp(-1))) # 0.0192, 3.3e-7
- Compare the errors. Euler is off by 0.0192 and RK4 by : in the same ten steps, RK4 is about 60,000 times more accurate here.
Answer Euler's error is 0.0192 and RK4's is , about 60,000 times smaller.
Worked example
A series RLC (R = 20 Ω, L = 10 mH, C = 1 µF) gets a 1 V step. Simulate it with RK4 and find the first peak of the capacitor voltage.
Show the solution
- Write the circuit as two first-order equations. The state is the capacitor voltage and the current :
- Step it with RK4 and keep the voltages. A thousand steps of 1 µs.
import numpy as np
R, L, C, Vs = 20, 10e-3, 1e-6, 1.0
f = lambda t, y: np.array([y[1] / C, (Vs - R * y[1] - y[0]) / L])
h, y, vs = 1e-6, np.array([0.0, 0.0]), []
for k in range(1000):
y = rk4_step(f, k * h, y, h)
vs.append(y[0])
print(max(vs)) # 1.72925
- Find the damping ratio.
- Compare with theory.
- Judge the agreement. The simulation agrees to six figures with a step of a six-hundredth of the ringing period.
Answer the capacitor voltage first peaks at 1.72925 V.
Check
Write rk4_step(f, t, y, h) and use it to integrate from over one time constant (tau = 2) in n = 5 steps. Set v to the final value and err to .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Integrate from to with Euler and with RK4, using n = 20 and then 2 * n steps. Set ratio_euler and ratio_rk4 to (error with n steps) / (error with 2n steps) for each method.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Simulate a series RLC (R = 20 Ω, L = 10 mH, C = 1 µF) driven by a 1 V step from rest, with RK4, state [v, i], step 1 µs, for 2000 steps. Set v to the array of capacitor voltages after each step and peak to its largest value.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A pendulum of length Lp = 0.5 m obeys with = 9.81 m/s². Released from rest at th0 = 45°, integrate it with RK4 (state [theta, omega], step 1 ms) until has crossed zero going downward twice. Set T to the time between those two crossings (interpolate linearly within each step), and ratio to T divided by the small-angle period .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Integrate from to with RK4, once with n = 5 steps (y1) and once with 2 * n (y2). For a fourth-order method, the error in y2 (that is, y2 minus the exact answer) is about . Set est to that estimate and true_err to y2 - math.exp(-1).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
How many times does one step of classical RK4 evaluate the slope function ?
Explain why
RK4 needs four evaluations of per step and Euler needs one. Why is RK4 usually cheaper for a given accuracy?
Euler's error shrinks only in proportion to the step, so to get ten times more accurate you need ten times more steps. RK4's error shrinks as the fourth power of the step, so a modest reduction in step size buys a large gain: ten times more accurate needs only about 1.8 times more steps. For any accuracy beyond the crudest, RK4 reaches it with so many fewer steps that its four evaluations per step still cost far less in total.
Practice
Keep practicing
7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.