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:
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.
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.
| Method | f-evaluations per step | Order | Halve h → error × |
|---|---|---|---|
| Euler | 1 | 1 | ½ |
| Heun / midpoint (RK2) | 2 | 2 | ¼ |
| RK4 | 4 | 4 | 1/16 |
| Adaptive RK45 (Dormand–Prince: MATLAB ode45, SciPy solve_ivp) | 6 – 7 | 5 with a 4th-order error estimate | step 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:
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
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
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
- Do two Heun steps of h = 0.5 for y′ = y, y(0) = 1, and compare with Euler and RK4 above.
- For y′ = −50(y − cos t), what is the largest stable Euler step? Check it in the lab.
- Write θ″ + 0.2·θ′ + 9.81·sin θ = 0 as a first-order system.
- 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.