Euler vs. RK4

Most differential equations have no formula for an answer, so a computer walks forward in small time steps and uses the slope to guess where the system goes next. Euler's method uses one slope per step. Fourth-order Runge–Kutta (RK4) samples four. Both rigs below start from the same conditions. The dashed outline is the true motion.

Euler

· true

RK4

· true

RK4 measures the slope 4 times per step, so it costs 4 times as much. This makes the race fair.


Halve the step, check the error

Each row cuts the step in half. Euler's error also drops by about half, which is what "first-order" means. RK4 is fourth-order: half the step gives roughly 1/16 the error.

StepEuler errorGainRK4 errorGain

What one step does

You know the state y now and a rule f(y) that gives its slope. You want the state a time Δt later.

Euler measures the slope once, at the start, and follows that straight line for the whole step. If the true path curves, the line drifts off it.

ynext = y + Δt · f(y)

RK4 takes a trial half-step, measures the slope there, tries again with that better slope, then looks at the far end. The final step uses a weighted average of all four slopes.

k1 = f(y)
k2 = f(y + ½Δt·k1)
k3 = f(y + ½Δt·k2)
k4 = f(y + Δt·k3)
ynext = y + (Δt/6)(k1 + 2k2 + 2k3 + k4)


Try this

  1. Pendulum. What is the largest step that keeps Euler's energy under 110% for the full 20 s? How many steps is that?
  2. Cooling. Set the step to 10 min. Euler's coffee bounces between 90 °C and −50 °C. Work one Euler step by hand with k = 0.2 per minute and explain why.
  3. RLC circuit. Find the step where Euler's ringing neither grows nor dies. The real circuit loses energy in the resistor. Where is Euler's extra energy coming from?
  4. Equal work. Tick the equal-work box on the spring. Does Euler catch up when it gets 4 times as many steps?
  5. Predator–prey. Push the step up until RK4's loop visibly stops closing. Every method fails eventually. What changes is how big a step it tolerates.