Lite / Engineering Complex Systems / 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?
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.
Predict
Euler's method on τ·dy/dt = −y with τ = 1 s and dt = 1.5 s. What do the samples do?
- Decay smoothly
- Flip sign each step but shrink
- Grow without limit
- Stay constant
Guess first, then reveal
Each step multiplies y by 1 − dt/τ = −0.5. The sign flips every step and the size halves: ringing that the real system never does. At dt > 2 s the factor's size passes 1 and it explodes.
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 τ.
What the interactive shows (text version)
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.
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
Intuitive
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.
Technical
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.
Mathematical
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.
Applied
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.
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
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.
Reflect
How would you convince a teammate that an oscillation in your simulation is real and not numerical?
Write your answer on paper or in your own notes. Lite never stores what you write.