Numerical stability and choosing a step size
See how a step that is too large can make a simulation blow up, understand why, fix oscillating systems with semi-implicit Euler, and learn a routine for choosing dt.
Lesson 5 of 6 · 30 min · Self-paced · Free
When a simulation explodes
You simulate a robot arm, run it for ten seconds of virtual time, and the joint angle reads 10³⁰ radians. Nothing physical caused this. The numerical method itself became unstable: tiny errors grew a little every step until they swamped the answer. Instability is one of the most common ways simulations fail, and it can be subtle, starting as a slow drift that looks almost physical. This lesson explains where it comes from and how to avoid it.
Accuracy and stability are different
- Accuracy is how close each answer is to the truth. Inaccurate results are a bit wrong.
- Stability is whether errors stay bounded as the simulation runs. Unstable results grow without limit and become meaningless.
A method can be stable but inaccurate (a coarse step that gets the shape roughly right) or, over a short run, accurate-looking but unstable (errors that are tiny now but doubling every few steps).
The simplest test: exponential decay
Take dy/dt = −k · y with k = 10 per second. The true solution decays smoothly to zero. One Euler step gives
y_next = y + (−k · y) · dt = (1 − k · dt) · ySo each step multiplies y by the same amplification factor, 1 − k · dt. Everything depends on that factor:
K_RATE = 10.0 # decay rate, per second: dy/dt = -10·y
for dt in [0.05, 0.15, 0.25]:
y = 1.0
values = []
for _ in range(6):
y = y + (-K_RATE * y) * dt # one Euler step
values.append(f"{y:8.3f}")
print(f"dt = {dt:4.2f} (k·dt = {K_RATE * dt:.1f}):", " ".join(values))Output:
dt = 0.05 (k·dt = 0.5): 0.500 0.250 0.125 0.062 0.031 0.016
dt = 0.15 (k·dt = 1.5): -0.500 0.250 -0.125 0.062 -0.031 0.016
dt = 0.25 (k·dt = 2.5): -1.500 2.250 -3.375 5.062 -7.594 11.391- With k · dt = 0.5 the factor is 0.5: smooth decay, roughly right.
- With k · dt = 1.5 the factor is −0.5: the value still shrinks, but flips sign every step. The real system never goes negative, so this is badly inaccurate, though still stable.
- With k · dt = 2.5 the factor is −1.5: every step makes the value 1.5 times larger and flips its sign. Unstable. A decaying system has turned into an exploding one.
The rule: Euler's method on this equation is stable only if |1 − k · dt| ≤ 1, which means dt ≤ 2 / k, and it avoids sign flips only if dt ≤ 1 / k. The faster the system's own dynamics (the larger k), the smaller the step must be. Systems that mix very fast and very slow behaviour, called stiff systems, force a tiny step even when you only care about the slow part, which is why special methods exist for them.
Oscillators: Euler always gains energy
Now take the spring–mass system from the previous lesson without damping. Its total energy, ½ m v² + ½ k x², should stay constant forever. With explicit Euler, one can show with a little algebra that every step multiplies the energy by exactly
1 + ω_n² · dt²which is always greater than 1. With m = 0.5 kg, k = 20 N/m (ω_n² = 40) and dt = 0.01 s, the factor is 1.004 per step. That looks harmless, but over 1000 steps (10 s) it compounds to 1.004¹⁰⁰⁰ ≈ 54. No step size makes explicit Euler stable for an undamped oscillator. A smaller step only slows the growth. This is why the previous lesson needed dt = 0.0001 s.
The fix: semi-implicit Euler
A tiny change to the order of updates makes a huge difference. Update the velocity first, then use the new velocity to update the position:
M, K = 0.5, 20.0 # kg, N/m (no damping)
DT = 0.01 # s
STEPS = 1000 # 10 seconds
def energy(x, v):
return 0.5 * M * v * v + 0.5 * K * x * x # joules
def explicit_euler(x, v):
a = -K * x / M
return x + v * DT, v + a * DT # both use OLD values
def semi_implicit_euler(x, v):
a = -K * x / M
v = v + a * DT # update velocity first...
return x + v * DT, v # ...then position uses NEW v
for name, step in [("explicit", explicit_euler), ("semi-implicit", semi_implicit_euler)]:
x, v = 0.10, 0.0
e0 = energy(x, v)
for _ in range(STEPS):
x, v = step(x, v)
print(f"{name:14s} energy after 10 s: {energy(x, v) / e0:7.3f} × start")Output:
explicit energy after 10 s: 54.164 × start
semi-implicit energy after 10 s: 0.977 × startSame equations, same step, same amount of work per step. Explicit Euler has inflated the energy 54-fold; semi-implicit Euler is within a few percent of the true value. If you keep running it, its energy wobbles slightly above and below the starting value (within about ±3.5% for this step) but never drifts away, even after thousands of seconds. This method, also called symplectic Euler, is stable for oscillators as long as ω_n · dt is below 2, and it is widely used in game physics and robotics simulators because it is as cheap as Euler but behaves far better for things that vibrate.
Choosing a step size: a routine
There is no universal correct dt, but there is a reliable procedure:
- Find the fastest timescale in the system: the shortest period of oscillation (2π / ω_n), or the shortest time constant (1 / k). Start with dt well below it, for example a twentieth or less.
- Check stability limits for your method, such as dt ≤ 2 / k for Euler on decay or ω_n · dt below 2 for semi-implicit Euler on an oscillator. Stay well inside them.
- Run a convergence test: halve dt and rerun. If the quantity you care about changes by less than your tolerance, the step is small enough. If not, halve again.
- Watch a conserved quantity such as energy where one exists. Steady growth in a system that should conserve energy is a warning sign.
- Record the dt you used and the result of your convergence test alongside any results you report.
Knowledge check
Check your understanding
Go further
- Semi-implicit Euler method — Wikipedia ↗
The method's update rule, its energy behaviour and why it suits oscillating systems.
- Euler method — Wikipedia ↗
Includes a section on numerical stability using a similar exponential-decay example.
Lesson materials · free to download
Sign in to track your progress. Reading is free without an account.