← All days ◀ Day 3Day 5 ▶
15-Day Differential Equations Course · Week 1 · Day 04

Numerical Methods: Euler, RK4, Error and Stiffness

When no formula exists, the computer walks along the slope field for you. Learn exactly how Euler's method, Heun's method and the classic fourth-order Runge–Kutta work, why halving the step cuts RK4's error sixteen-fold, and why some perfectly smooth problems make simple methods explode.

⏱ about 2–3 hours🧪 Lab: Method race and error order❓ 5-question quiz

1Today's goals

  • Derive Euler's method from the tangent line and know its error order
  • Write out Heun (RK2) and RK4 step by step
  • Read an error-versus-step plot on log–log axes and find the order
  • Turn any higher-order equation into a first-order system
  • Recognise stiffness and know when to use an implicit method

2Euler's method: follow the tangent

Stand at (tn, yn). The equation gives the slope there. Walk a step h along that straight line:

yn+1 = yn + h · f(tn, yn)
tn+1 = tn + h

Each step makes a small error because the true curve bends away from its tangent. Taylor's theorem says that local error is about (h²/2)·y″. Over a fixed interval you take T/h steps, so the global error is proportional to h: Euler is a first-order method. Halve h and the error halves — but the work doubles.

3Better slopes: Heun and Runge–Kutta

The fix is to use a better average slope over the step.

Heun (RK2, improved Euler):
k₁ = f(tn, yn) k₂ = f(tn + h, yn + h·k₁)
yn+1 = yn + (h/2)·(k₁ + k₂) global error ∝ h²

Classic RK4:
k₁ = f(tn, yn)
k₂ = f(tn + h/2, yn + h·k₁/2)
k₃ = f(tn + h/2, yn + h·k₂/2)
k₄ = f(tn + h, yn + h·k₃)
yn+1 = yn + (h/6)·(k₁ + 2k₂ + 2k₃ + k₄) global error ∝ h⁴

RK4 is Simpson's rule in disguise: slopes at the start, twice in the middle, at the end, weighted 1 : 4 : 1. Four evaluations per step buy you a fourth-order method — every lab in this course runs on it.

Methodf-evaluations per stepOrderHalve h → error ×
Euler11½
Heun / midpoint (RK2)22¼
RK4441/16
Adaptive RK45 (Dormand–Prince: MATLAB ode45, SciPy solve_ivp)6 – 75 with a 4th-order error estimatestep chosen automatically

4Systems and higher order

Every method above works unchanged when y is a vector. And any higher-order equation becomes a first-order system by naming the derivatives:

m·x″ + c·x′ + k·x = F(t) → x′ = v, v′ = (F(t) − c·v − k·x)/m

This is how every simulation package — Simulink, Modelica, a rotor-dynamics code, a flight simulator — sees a model: a state vector and a function that returns its rate of change.

5Stiffness: when small steps are forced on you

Apply Euler to the test equation y′ = λ·y with λ < 0. Each step multiplies y by (1 + h·λ). The true solution decays, but if |1 + h·λ| > 1 the numbers grow and oscillate. Euler is stable only for

h < 2 / |λ|

A problem is stiff when it contains a very fast decaying component (large |λ|) alongside the slow behaviour you care about. The fast part is dead after a moment, yet it still forces tiny steps for ever. Chemical kinetics, circuits with small parasitic capacitances, and a thermocouple model coupled to a slow oven are typical.

The cure is an implicit method. Backward Euler, yn+1 = yn + h·f(tn+1, yn+1), multiplies by 1/(1 − h·λ), which is below 1 for any h when λ < 0 — stable at any step. The price: an equation to solve each step. Stiff solvers (ode15s, BDF, Radau, LSODA) do exactly this.

Local error, global error and why they differ by one order

The local truncation error of a method of order p is C·hp+1 per step. Errors made early are carried forward and amplified (or damped) by the equation itself, roughly by eL·(T−t) where L bounds ∂f/∂y. Summing N = T/h such contributions gives a global error ≤ (C/L)·(eLT − 1)·hp. One power of h is "spent" on the number of steps. Note the eLT factor: for unstable or chaotic problems the global error grows exponentially with time no matter how good the method — the theme of Day 11.

6Worked example

y′ = y, y(0) = 1. Estimate y(1) (exact e = 2.71828)Euler, h = 0.5: y₁ = 1 + 0.5·1 = 1.5, y₂ = 1.5 + 0.5·1.5 = 2.25 error 0.468 Euler, h = 0.25: 1.25, 1.5625, 1.9531, 2.4414 error 0.277 (≈ halved) RK4, h = 1 (one step): k₁ = 1, k₂ = 1 + 0.5 = 1.5, k₃ = 1 + 0.75 = 1.75, k₄ = 1 + 1.75 = 2.75 y₁ = 1 + (1 + 3 + 3.5 + 2.75)/6 = 2.7083 error 0.0099 One RK4 step (4 evaluations) beats four Euler steps by a factor of 28.
Where you meet thisEvery simulation you run: rig controller models, rotor run-up simulations, battery state-of-charge estimators, orbit propagation, game physics engines (which often use simple "semi-implicit Euler" for speed), and the training of neural ODEs.

7Lab: method race and error order

Choose a problem and slide the step size. Left: the three methods against the exact solution (dots mark the steps). Right: the error at the end for many step sizes on log–log axes — the slopes are the orders 1, 2 and 4. Try the stiff problem and push h above 0.04: Euler explodes. Try the oscillator: Euler's spiral gains energy every cycle.

8Pro tips and common mistakes

  • At very small h the RK4 curve flattens out at ~10⁻¹⁴: that is round-off, not truncation. Smaller steps stop helping.
  • Always check a simulation by halving the step (or tightening the tolerance). If the answer changes in a digit you care about, it was not converged.
  • Energy-conserving problems (orbits, undamped oscillators) need symplectic methods (leapfrog, Verlet) for long runs; plain RK4 slowly loses energy.
  • If a simulation suddenly needs tiny steps somewhere, suspect stiffness or a discontinuity (a switch, a stop, friction stick-slip) — tell the solver about events.

9Practice

  1. Do two Heun steps of h = 0.5 for y′ = y, y(0) = 1, and compare with Euler and RK4 above.
  2. For y′ = −50(y − cos t), what is the largest stable Euler step? Check it in the lab.
  3. Write θ″ + 0.2·θ′ + 9.81·sin θ = 0 as a first-order system.
  4. From the lab's log–log plot, read the error of Heun at h = 0.1 and at h = 0.01. What order does that imply?

10Quick quiz

5 questions. Click an answer — you get the explanation straight away.

Progress is kept in this browser (and in your account when signed in).