School/✦ Research Engineer/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?
About 50 min · 3 goals · Checked by automated tests
1.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.
2.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.
Loading the interactive…
Can't use the interactive? Read a description instead
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.
3.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1 · a fresh version every time
For dy/dt = -0.5y + 2 with y(0) = 1, take one explicit Euler step of size h = 0.2. What is y₁?
4.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?
Guess first. Being wrong here is how the surprise works.
5.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
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.
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).
RK4's update matches the Taylor series of the exact solution up to the h⁴ term, so its local error is O(h⁵).
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.
6.Watch
→ 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.
Length: see YouTube · captions on YouTube · source
Nothing loads from YouTube until you press play.
The video is optional: everything you need is also in the lesson text and the interactive.
7.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2 · a fresh version every time
For dy/dt = -2y with y(0) = 3, take one classical RK4 step with h = 0.1. What is y₁? (4 decimal places.)
Practice 3
Explicit Euler on x″ = −x with h = 0.05: by what factor does the energy grow per step?
Practice 4
Why might a long satellite-orbit simulation use a symplectic method instead of RK4?
Practice 5 · a fresh version every time
For dy/dt = 0.5y + 1 with y(0) = 2, take one explicit Euler step of size h = 0.05. What is y₁?
Practice 6
With the same number of function evaluations, which is usually more accurate on smooth problems?
8.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.
9.Reflect
Concepts: Numerical integration of ODEs: Euler, RK4 and stiffness · Simulation and time stepping