School/□ Systems Engineer/Week 11
Updating every state, one step at a time
Your spring simulation gains energy every swing until it explodes. The physics is right. What went wrong?
About 35 min · 3 goals · Checked by automated tests
1.Why should I care?
Simulation errors can look exactly like physics: oscillations, overshoot, instability. If you can't tell a numerical artefact from real behaviour, you can't trust any controller you tune in simulation.
- ◇ Simulate a multi-state system by computing every state's rate from the same current state, then updating all states.
- ◇ Show that explicit Euler is stable for τ·dy/dt = −y only when dt < 2τ, and rings when dt > τ.
- ◇ Choose a step size from the fastest time constant in the system.
2.Discover
Slide dt from 0.1 s up to 2.6 s. Find the step where the samples start alternating sign, and the step where they start growing. Write both as multiples of τ.
Loading the interactive…
Can't use the interactive? Read a description instead
The Euler stepper plots the exact decay e^(−t) as a smooth curve and Euler's samples as dots. With dt below 1 s the dots follow the curve. Between 1 and 2 s they alternate above and below zero while shrinking. Above 2 s they alternate and grow without limit.
3.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1
Which update correctly applies explicit Euler to x' = v, v' = −k·x/m?
4.Predict
Euler's method on τ·dy/dt = −y with τ = 1 s and dt = 1.5 s. What do the samples do?
Guess first. Being wrong here is how the surprise works.
5.Understand
One step, all states
For dx/dt = f(x, u), each Euler step computes all rates from the current state, then updates all states together: x_new = x + dt·f(x, u). In code, compute the rates into new variables first; don't overwrite position and then use the new position to update velocity unless you mean to use a different method.
Euler's stability limit
For τ·dy/dt = −y, one Euler step is y_new = (1 − dt/τ)·y. If dt < τ the factor is between 0 and 1: smooth decay. If τ < dt < 2τ it is negative: decaying but ringing. If dt > 2τ its size is above 1: the simulation blows up, though the real system is perfectly stable.
The fastest mode sets the step
In a system with several time constants, the smallest one sets the stability limit. Closing a feedback loop makes the system faster (smaller τ), so a step that was fine open-loop can fail closed-loop. Rule of thumb: dt ≤ τ_min/10 for accuracy, never above 2τ_min.
Explain it another way
Euler takes the slope at the start of a step and walks in a straight line. If the step is long compared with how quickly things change, you walk right past the target, and even further back the next time.
For linear dx/dt = A·x, explicit Euler gives x_{n+1} = (I + dt·A)·x_n. It is stable iff every eigenvalue λ of A satisfies |1 + dt·λ| < 1. For λ = −1/τ that is dt < 2τ. For an undamped oscillator (λ = ±jω), |1 + jω·dt| > 1 always, so Euler always gains energy.
y_n = (1 − dt/τ)ⁿ·y₀. Exact: y(n·dt) = e^(−n·dt/τ)·y₀. For small dt, 1 − dt/τ ≈ e^(−dt/τ), so the two agree; the gap grows like dt.
Game physics engines often use semi-implicit (symplectic) Euler for springs precisely because explicit Euler slowly pumps energy into oscillators. Control engineers simulate with RK4 or smaller steps and check convergence.
Worked example 1
A motor model has an electrical time constant of 2 ms and a mechanical one of 200 ms. What Euler step would you choose?
The 2 ms mode limits stability: dt must be under 4 ms, and for accuracy about 0.2 ms. The mechanical mode alone would allow 20 ms, but the fast mode decides. (This is called a stiff system; implicit methods help.)
Common mix-ups
- If the simulation oscillates, the system oscillates.
✓ Euler with dt > τ produces ringing the real first-order system never has. Halve dt and see if it goes away. - A smaller step is always slower but otherwise the same.
✓ Above a limit, step size changes the qualitative answer: stable becomes unstable.
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2
Euler on τ·dy/dt = −y with τ = 0.4 s and dt = 0.6 s. By what factor is y multiplied each step?
Practice 3
Your open-loop simulation was stable with dt = 50 ms. You add a strong P controller and it explodes. Most likely cause?
Practice 4
Explicit Euler on an undamped spring (no friction) for many periods. What happens to the amplitude?
Practice 5 · a fresh version every time
You simulate τ·dy/dt + y = 0 with explicit Euler, τ = 1.5 s. Above what time step dt (in seconds) does the simulation become unstable?
Practice 6 · a fresh version every time
A first-order system has time constant τ = 25 s. How long after a step does it take to get within band = 5 % of its final value?
7.Apply it
Write a 10-line Python loop that simulates m·ẍ + c·ẋ + k·x = 0 with explicit Euler. Run it with c = 0 and three step sizes and plot the energy ½kx² + ½mv² against time. Then switch to RK4 or semi-implicit Euler and compare.
8.Reflect
Concepts: Simulation and time stepping · States and state-space models · Stability, overshoot and delay