TactivergeLite

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?

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

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.

Predict

Euler's method on τ·dy/dt = −y with τ = 1 s and dt = 1.5 s. What do the samples do?

  1. Decay smoothly
  2. Flip sign each step but shrink
  3. Grow without limit
  4. 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.

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

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

Practice

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

Practice 1. Which update correctly applies explicit Euler to x' = v, v' = −k·x/m?

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?

s

s

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.

← From one second-order equation to two first-order onesThe time constant: 63 % and counting →