TactivergeLite

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?

About 50 min · 3 goals · Checked by automated tests · Full interactive version

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.

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?

  1. About 0.5
  2. About 0.94
  3. 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.

You can also open the interactive on the full site when you have a faster connection.

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):

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
  1. Write x″ = −x as x′ = v, v′ = −x. Energy E = ½(x² + v²) is exactly conserved by the true solution.

  2. Explicit Euler: xₙ₊₁ = xₙ + h vₙ, vₙ₊₁ = vₙ − h xₙ.

  3. Then xₙ₊₁² + vₙ₊₁² = xₙ² + 2h xₙvₙ + h²vₙ² + vₙ² − 2h xₙvₙ + h²xₙ² = (1 + h²)(xₙ² + vₙ²).

  4. 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.

  5. 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

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

Practice 4. Why might a long satellite-orbit simulation use a symplectic method instead of RK4?

Practice 6. With the same number of function evaluations, which is usually more accurate on smooth problems?

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.

← Module check: modal analysis of two massesLocal and global error, and the convergence study →