A442 of 56
0 XP
  1. Hook
  2. Predict
  3. Name it
  4. Check
  5. Explain

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?

Name it

The method

For dydt=f(t,y), one RK4 step from (t,y) to t+h:

k1=f(t,y)
k2=f(t+h2,y+h2k1)
k3=f(t+h2,y+h2k2)
k4=f(t+h,y+hk3)
ynext=y+h6(k1+2k2+2k3+k4)

k1 is Euler's slope; k2 and k3 are slopes at the middle of the step, using trial values; k4 looks at the end. The weighted average matches the true solution's Taylor series up to h4.

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 Vs has two states, the capacitor voltage v and the current i:

dvdt=iCdidt=Vs−Ri−vL

Write y 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 h and h/2. For RK4, the error of the finer run is about |yh−yh/2|/15. 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 dv/dt=−v/τ from 1 V over one time constant with 10 steps, by Euler and by RK4. Compare the errors.

Show the solution
  1. Run both methods side by side. Ten steps of 0.1τ, then compare each with e−1.
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
  1. Compare the errors. Euler is off by 0.0192 and RK4 by 3.3×10−7: 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 3.3×10−7, 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
  1. Write the circuit as two first-order equations. The state is the capacitor voltage v and the current i: dvdt=iC,didt=Vs−Ri−vL
  2. 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
  1. Find the damping ratio. ζ=R2C/L=0.1
  2. Compare with theory. vpk=1+e−πζ/1−ζ2=1.72925V
  3. 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 dv/dt=−v/τ from v=1 over one time constant (tau = 2) in n = 5 steps. Set v to the final value and err to |v−e−1|.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Integrate dv/dt=−v from v=1 to t=2 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 θ¨=−(g/L)sin⁡θ with g = 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 2πL/g.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Integrate dv/dt=−v from v=1 to t=1 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 (y1−y2)/15. 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 f?

Explain why

RK4 needs four evaluations of f per step and Euler needs one. Why is RK4 usually cheaper for a given accuracy?

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.