Lite / Independent Engineering Research / Week 7
Euler, semi-implicit Euler and RK4
Simulate a frictionless spring with Euler's method and it gains energy from nowhere. Is the physics wrong, or the arithmetic?
Why should I care?
Every simulator you will use, from game physics to spacecraft trajectory tools, rests on a time-stepping method. Choosing one badly gives plausible-looking, wrong answers. The undamped oscillator is the classic test because we know the exact answer.
- Implement explicit Euler, semi-implicit Euler and classical RK4 for a first-order system.
- Show that explicit Euler multiplies the oscillator's energy by (1 + h²) every step.
- Compare accuracy and long-time behaviour (energy drift) of the three methods.
Predict
Simulate x″ = −x for one full period with explicit Euler and h = 0.1. The true energy is 0.5. Roughly what does Euler report?
- About 0.5
- About 0.94
- About 0.05
Guess first, then reveal
Each step multiplies the energy by 1 + h² = 1.01. One period is about 63 steps, and 1.01⁶³ ≈ 1.87, so the energy is about 0.94: nearly double.
Discover
In 'Oscillator energy', set h = 0.2 and run 10 periods. Which curve drifts up, which stays bounded, which slowly sinks? Halve h and see which drift shrinks fastest.
What the interactive shows (text version)
The integrator diagram plots energy against time for explicit Euler, semi-implicit Euler (dashed) and RK4, with the exact energy 0.5 as a dashed line. Text lists each method's final energy and the Euler growth factor per step.
Watch (optional)
Watch for: Watch how the four slope evaluations are combined, and how the code is tested.
Writes an RK4 integrator from scratch and tests it on a dynamical system, the same steps as this term's numerical study.
“Coding a Fourth-Order Runge-Kutta Integrator in Python and Matlab” by Steve Brunton (opens YouTube; streaming video uses a lot of data)
The video is optional. Everything you need is in this lesson's text.
Understand
The three methods
For y′ = f(t, y):
- Explicit Euler: yₙ₊₁ = yₙ + h f(tₙ, yₙ). One slope, at the start.
- Semi-implicit (symplectic) Euler for x′ = v, v′ = a(x): vₙ₊₁ = vₙ + h a(xₙ), then xₙ₊₁ = xₙ + h vₙ₊₁.
- RK4: k₁ = f(tₙ, yₙ), k₂ = f(tₙ + h/2, yₙ + h k₁/2), k₃ = f(tₙ + h/2, yₙ + h k₂/2), k₄ = f(tₙ + h, yₙ + h k₃); yₙ₊₁ = yₙ + (h/6)(k₁ + 2k₂ + 2k₃ + k₄).
Accuracy versus structure
RK4 is far more accurate per step: with h = 0.1 over t = 10 its position error is about 4×10⁻⁶, against about 0.6 for explicit Euler. But RK4 still slowly loses energy (with h = 0.2 over 10 periods, about 0.4998 instead of 0.5). Semi-implicit Euler is only first-order accurate, yet its energy never drifts away. For long orbital or molecular simulations, that structure can matter more than raw accuracy.
Cost
Count function evaluations, the expensive part for real models. RK4 costs four per step, Euler one. RK4 with step 4h therefore costs the same as Euler with step h, and is still usually far more accurate.
Explain it another way
Intuitive
Euler is driving by looking only at the road directly under the car. RK4 glances ahead three times and averages. Semi-implicit Euler drives sloppily but never leaves the circuit.
Technical
Explicit Euler on the oscillator has amplification matrix [[1, h], [−h, 1]] with |λ| = √(1 + h²) > 1. Semi-implicit Euler's matrix has determinant exactly 1 (area-preserving).
Mathematical
RK4's update matches the Taylor series of the exact solution up to the h⁴ term, so its local error is O(h⁵).
Applied
Many game and robotics physics engines use semi-implicit Euler because it is cheap and stays bounded for springs and contacts.
Derivation: Why explicit Euler gains energy on the oscillator
Write x″ = −x as x′ = v, v′ = −x. Energy E = ½(x² + v²) is exactly conserved by the true solution.
Explicit Euler: xₙ₊₁ = xₙ + h vₙ, vₙ₊₁ = vₙ − h xₙ.
Then xₙ₊₁² + vₙ₊₁² = xₙ² + 2h xₙvₙ + h²vₙ² + vₙ² − 2h xₙvₙ + h²xₙ² = (1 + h²)(xₙ² + vₙ²).
So Eₙ = (1 + h²)ⁿ E₀: exponential growth for any h > 0. Over a fixed time T = nh, Eₙ ≈ E₀ e^{hT}, which tends to E₀ only as h → 0.
Semi-implicit Euler updates v first and uses the new v for x. It conserves a slightly modified energy exactly, so its energy error stays bounded for all time (it is symplectic).
Worked example 1
One explicit Euler step for x′ = v, v′ = −x from (1, 0) with h = 0.1.
x₁ = 1 + 0.1·0 = 1, v₁ = 0 − 0.1·1 = −0.1. Energy ½(1 + 0.01) = 0.505 = 0.5 × 1.01, as the derivation predicts.
Common mix-ups
If the simulation looks smooth, it is accurate.
✓ Euler's spiral looks smooth and is completely wrong about energy. Compare with an exact or converged answer.Higher order always wins.
✓ It wins on accuracy for smooth problems; structure (energy, stability) can matter more over long times.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
Apply it
Implement all three methods in the Code Lab with a common interface step(f, t, y, h). Reproduce the energy plot from the diagram for h = 0.1 over 20 periods and add the error in x(t) against cos t.
Reflect
In a project you care about, which matters more: accuracy at one moment or behaviour over a long time?
Write your answer on paper or in your own notes. Lite never stores what you write.