Log inStart free
LESSON 15.02 · 6 STAGES · 2 CONTROLS · 3 CHECKS

Numerical integration: Euler vs Runge–Kutta

Watch Euler and Runge–Kutta step a spring, and see when each one fails.


EXPLAINER · 15.02 · SPRING BAY
01
Stage 1 of 6 · The rule
In continuous-time dynamics, a rule gives the rate of change at each instant. The teal curve is the true path.
Explain it simply

The spring starts at position 1 with speed 0. The rule says how fast the position and the speed change right now. The arrow shows that rate. A computer cannot follow the rule smoothly, so it takes steps.

A time plot shows the true position of a spring with damping 0.5, over 10 seconds. A phase plot shows position against speed, with a rate arrow at the start.

true pathEuler dots, RK4 squarestimestep herror
Toy: a damped spring, k = 4, from x = 1, 10 s
HOVER OR TAP A SHAPE TO SEE ITS NUMBERS
0.1 s
The time each step covers.
light · c = 0.5
How fast friction slows the spring.
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
dxdt=v,dvdt=−k x−c v\frac{dx}{dt} = \sy{v}{data}{v}, \quad \frac{dv}{dt} = -\sy{k}{param}{k}\,\sy{x}{data}{x} - \sy{c}{param}{c}\,\sy{v}{data}{v}
dvdt(0)=−4⋅1−0.5⋅0=−4\tfrac{dv}{dt}(0) = -4\cdot 1 - 0.5\cdot 0 = -4

Speed moves the position; the spring and the damping change the speed.

Show the code ▾Hide the code ▴ · Try it yourself · Python 3
import math

k, c = 4.0, 0.5  # stiffness and damping
h, T = 0.1, 10.0  # timestep and run length (s)

def f(x, v):
    return v, -k * x - c * v

def euler(x, v, h):
    dx, dv = f(x, v)
    return x + h * dx, v + h * dv

def rk4(x, v, h):
    k1 = f(x, v)
    k2 = f(x + h / 2 * k1[0], v + h / 2 * k1[1])
    k3 = f(x + h / 2 * k2[0], v + h / 2 * k2[1])
    k4 = f(x + h * k3[0], v + h * k3[1])
    return (x + h / 6 * (k1[0] + 2 * k2[0] + 2 * k3[0] + k4[0]),
            v + h / 6 * (k1[1] + 2 * k2[1] + 2 * k3[1] + k4[1]))

def run(step, h):
    x, v, xs = 1.0, 0.0, [1.0]
    for _ in range(round(T / h)):
        x, v = step(x, v, h)
        xs.append(x)
    return xs

def exact(t):
    a = c / 2
    w = math.sqrt(k - a * a)
    return math.exp(-a * t) * (math.cos(w * t) + a / w * math.sin(w * t))

for name, step in [("euler", euler), ("rk4", rk4)]:
    xs = run(step, h)
    gap = max(abs(x - exact(i * h)) for i, x in enumerate(xs))
    finer = run(step, h / 2)
    change = max(abs(x - finer[2 * i]) for i, x in enumerate(xs))
    print(name, f"gap {gap:.3g}", f"halving change {change:.3g}")
print(f"euler growth per step {math.sqrt(1 - c * h + k * h * h):.3f}")

# euler gap 0.539 halving change 0.384
# rk4 gap 4.03e-05 halving change 3.78e-05
# euler growth per step 0.995

Copy it into a file and run it.

Common mix-ups

MYTHA small enough timestep always keeps Euler stable.

FIXWith no damping, Euler's growth is √(1 + 4h²), above 1 at every h. A small h only slows the blow-up.

MYTHIf RK4 does not blow up, its answer is right.

FIXAt h = 1 s and no damping, RK4 is stable but wrong. Halving h moves its path 0.808: the check fails.

Predict, then reveal
0 / 3 REVEALED
Q01If h falls from 0.1 s to 0.05 s, how much smaller does RK4's largest gap get?
1 · GUESS2 · TRY IT IN THE DIAGRAM
Q02With strong damping, does Euler stay stable at h = 0.25 s?
1 · GUESS2 · TRY IT IN THE DIAGRAM
Q03At h = 2 s, does RK4 stay stable?
1 · GUESS2 · TRY IT IN THE DIAGRAM

Takeaways

  1. Euler uses one slope per step; for small h, halving h halves its error.
  2. RK4 averages four slopes; for small h, halving h cuts its error 16 times.
  3. Check a timestep by halving it; keep each step's growth below 1.