Lite / Textbooks / Engineering Programming With Python / Chapter 8
8. Simulating dynamic systems
Introduction
A cooling mug, a charging capacitor and a falling parachutist all obey a rate equation: how fast something changes depends on where it is now. Euler's method steps forward in small time slices, and the step size controls the error and even whether the answer blows up. Fit the model's parameter to measurements, then validate it on data you did not fit, and state the limits of the model honestly.
Figure 8.1. Halve the step, roughly halve the error (described in words)
Euler's method for dy/dt = −y from y(0) = 1, evaluated at t = 1: step 0.5 gives 0.25 (error 0.1179), step 0.25 gives 0.3164 (error 0.0515), step 0.125 gives 0.3436 (error 0.0243). The exact value is 0.3679; the error roughly halves each time the step halves.
Explanation
Built from 4 School lessons. Each part is the lesson's own explanation.
8.1 Rate equations and exponential decay
A hot drink cools fastest when it is hottest. A capacitor discharges fastest when it is fullest. Write that sentence as an equation and the solution writes itself.
Writing the equation
RC discharge: the current through R is V/R and it drains the capacitor: C·dV/dt = −V/R, so dV/dt = −V/(RC), τ = RC. Newton's cooling: heat flow is proportional to the temperature difference, so dT/dt = −k(T − Tₐ). Both: rate ∝ distance from equilibrium.
The time constant
τ (seconds or minutes) sets the speed. After 1τ, 37% of the initial difference remains; after 3τ, 5%; after 5τ, under 1%. Engineers call a first-order system 'settled' after about 4–5τ. For R = 10 kΩ and C = 100 µF, τ = 1 s.
Why the solution must be exponential
Only one family has a rate proportional to its value: e^(kt), from week 3's chain rule. So once you see 'rate ∝ amount', you can write the exponential solution straight away, then fit k (week 9). The equation also gives the units: k is per second, so kt has no units, as an exponent must.
Derivation: Solving dy/dt = −ky
Model: the rate of decrease is proportional to the amount: dy/dt = −k·y, k > 0, with y(0) = y₀.
Separate variables (for y > 0): (1/y) dy = −k dt.
Integrate both sides: ln y = −kt + C.
Exponentiate: y = e^C·e^(−kt). At t = 0, y = e^C = y₀, so y(t) = y₀·e^(−kt).
Check by differentiating: dy/dt = y₀·(−k)·e^(−kt) = −k·y ✓.
Time constant τ = 1/k: y(τ) = y₀e⁻¹ ≈ 0.368y₀. Half-life: e^(−kt) = ½ gives t½ = ln 2/k = τ·ln 2.
Cooling: let u = T − Tₐ; then du/dt = dT/dt = −k·u, so T(t) = Tₐ + (T₀ − Tₐ)e^(−kt).
Common mix-ups
After one time constant the quantity has halved.
✓ After τ it is at 37%; it halves after τ·ln 2 ≈ 0.69τ.The drink cools towards 0 °C.
✓ It cools towards the room temperature Tₐ: only the difference decays.
Open the Lite lesson: Rate equations and exponential decay
8.2 Euler's method and step-size error
Most differential equations have no formula solution. So computers cheat: take the slope now, step forward a little, repeat. When does that cheating go badly wrong?
The method
Know the state yₙ at time tₙ. Compute the slope f(tₙ, yₙ) from the equation. Step: yₙ₊₁ = yₙ + h·f(tₙ, yₙ), tₙ₊₁ = tₙ + h. Repeat. That is all, and it is exactly how the week 4 forward difference is used in reverse.
Accuracy: first order
Euler's global error ∝ h. To get ten times more accurate, take ten times more steps. Check any simulation by halving h: if the answer changes noticeably, the step was too large. Better methods (such as Runge–Kutta) reach error ∝ h⁴ for a little more work per step.
Stability: a separate danger
Accuracy and stability are different. Too large a step can make a decaying system grow without limit. For dy/dt = −y/τ the limit is h < 2τ, and smooth results need h < τ. Fast processes (small τ) therefore force small steps even when you only care about slow ones.
Derivation: Euler's step factor, its error and its stability limit
For dy/dt = −k·y, one Euler step gives yₙ₊₁ = yₙ + h·(−k·yₙ) = (1 − kh)·yₙ.
The exact solution multiplies by e^(−kh) = 1 − kh + (kh)²/2 − … each step. The difference per step is about (kh)²/2: the local error ∝ h².
To reach a fixed time T takes T/h steps, so the errors add to about (T/h)·h² ∝ h: the global error is first order. Halve h, halve the error.
After n steps yₙ = y₀(1 − kh)ⁿ. This decays only if |1 − kh| < 1, that is 0 < kh < 2, or h < 2τ.
For 1 < kh < 2 the factor is negative: the numerical solution overshoots zero every step while shrinking. For kh > 2 it grows without bound: numerical instability, not physics.
Common mix-ups
If a simulation blows up, the physical system is unstable.
✓ It may be numerical instability from too large a step. Halve h and see if it persists.A stable simulation is an accurate one.
✓ Stable only means it does not explode. Accuracy needs a convergence check.
Open the Lite lesson: Euler's method and step-size error
8.3 Falling with drag: terminal velocity
Drop a paper cone and it stops speeding up within a second. Gravity has not switched off. What is it fighting, and what speed does it settle at?
Same equation, new physics
Falling with linear drag is a first-order system approaching equilibrium v_T with time constant τ = m/b. After τ the object has reached 63% of v_T, after 3τ about 95%. For m = 0.2 kg and b = 0.1 kg/s: v_T = 19.6 m/s and τ = 2 s.
Linear or quadratic drag?
Drag ∝ v (linear) suits small, slow objects in thick fluids: dust, a sensor sinking in oil. Drag ∝ v² (quadratic) suits most everyday objects in air: a paper cone, a parachutist. Quadratic drag gives v = v_T·tanh(gt/v_T): the same start and end, but a sharper approach. Which you choose is an assumption to test against data.
Distance fallen
Integrate the velocity: x(t) = v_T·[t − τ(1 − e^(−t/τ))]. After a few τ the object falls almost exactly v_T metres each second, offset by v_T·τ: the distance 'lost' to the initial acceleration.
Derivation: Solving m·dv/dt = mg − bv
Free-body diagram (downwards positive): weight mg down, drag bv up. Newton's second law: m·dv/dt = mg − bv.
Terminal velocity: when dv/dt = 0, v_T = mg/b.
Rewrite: dv/dt = (mg − bv)/m = −(b/m)(v − v_T). Let τ = m/b and u = v − v_T.
Then du/dt = −u/τ: exactly last lesson's decay equation, with u(0) = −v_T (dropped from rest).
So u = −v_T·e^(−t/τ) and v(t) = v_T·(1 − e^(−t/τ)).
Check: v(0) = 0; v → v_T as t → ∞; dv/dt at t = 0 is v_T/τ = g, free fall at the start, as it must be.
Common mix-ups
At terminal velocity there is no force on the object.
✓ Weight and drag are both there; they are equal and opposite, so the net force is zero.Heavier objects always fall faster.
✓ In a vacuum they fall together. With drag, heavier objects of the same shape reach a higher terminal velocity.
Open the Lite lesson: Falling with drag: terminal velocity
8.4 Validating a model and stating its limits
Your cooling model fits the first 30 minutes beautifully. Does it predict the next 30? The only honest way to find out is to keep some data back.
Verification and validation
Verification: does the code solve the equation correctly? Check against an exact solution and by halving the step (week 8). Validation: does the equation describe the real system? Check against data the model was not tuned on. A perfectly verified model of the wrong physics is still wrong.
Hold-out testing
Split the data before fitting: fit on one part, then predict the other. Report the error on the held-out part, with units: RMS = √(mean of squared residuals), or percentage error |model − data|/data. For time series, hold out the later part: that is what a prediction actually has to do.
Assumptions and their effects
Cooling model: well-mixed liquid (one temperature), constant room temperature, constant k (in fact convection and radiation make k vary with temperature), no evaporation (which speeds early cooling). Drag model: linear drag (often quadratic in air), still air. For each, say which way it pushes the prediction and when it matters.
Common mix-ups
A low error on the fitting data validates a model.
✓ Only error on data not used for fitting tests whether the model generalises.Listing limitations weakens a report.
✓ Stating limits honestly is what makes the remaining claims trustworthy.
Open the Lite lesson: Validating a model and stating its limits
Worked examples
Rate equations and exponential decay: example 1
R = 10 kΩ, C = 100 µF, V(0) = 9 V. Find τ and V at 2 s.
Solution
τ = RC = 10⁴ × 10⁻⁴ = 1 s. V(2) = 9e⁻² = 1.22 V.
Euler's method and step-size error: example 1
dV/dt = −V/2, V(0) = 5 V, h = 0.5 s. Take two Euler steps.
Solution
Factor 1 − 0.5/2 = 0.75. V₁ = 3.75 V, V₂ = 2.8125 V. Exact V(1) = 5e^(−0.5) = 3.033 V. Error 0.22 V; with h = 0.25 it would be about half.
Falling with drag: terminal velocity: example 1
m = 0.2 kg, b = 0.1 kg/s. When does it reach 95% of terminal velocity, and how fast is it then?
Solution
v_T = 0.2 × 9.81/0.1 = 19.62 m/s, τ = 2 s. 1 − e^(−t/2) = 0.95 → t = 2 ln 20 = 5.99 s. Speed 18.6 m/s.
Validating a model and stating its limits: example 1
Held-out residuals are 1, −1, 2 and −2 °C. What is the RMS error?
Solution
Squares: 1, 1, 4, 4; mean 2.5; RMS = √2.5 = 1.58 °C.
Interactive figures, in words
Figure 8.2 from Rate equations and exponential decay
Choose the capacitor data. Turn on the log view: the points should lie on a straight line. Adjust k until the model line matches, then press Best fit. Read τ and the half-life. Why is ln(V) straight when V is curved?
In the decay-fit lab the capacitor readings fall from 9 V towards zero. In the log view, ln V against time is a straight line whose slope is −k; the best fit gives k close to 0.45 per second, so τ ≈ 2.2 s and a half-life of about 1.5 s. The readout states k, τ, half-life and the RMS error.
Figure 8.3 from Euler's method and step-size error
Start with τ = 2 and h = 0.5: read the error table and its ratio. Now try h = 1, 2, 2.5 and watch the regime change from smooth, to overshooting, to oscillating forever, to unstable. Change τ: does the boundary move with h/τ?
The Euler lab plots the exact exponential decay with Euler's points. With τ = 2 and h = 0.5 the points track the curve slightly low and the error halves when h halves. At h = 3 they zigzag about zero while shrinking; at h = 4 they oscillate with constant size; at h = 5 they zigzag and grow. The readout names the regime from h/τ.
Figure 8.4 from Falling with drag: terminal velocity
Set m = 0.2 kg and b = 0.1 kg/s. Read v_T and τ. Slide the time to τ, 2τ and 3τ: what fraction of v_T has it reached each time? Double the mass, then double b. Finally compare quadratic drag: same terminal speed, different approach.
The drag lab plots speed against time: a dotted free-fall line v = gt, the linear-drag curve rising and flattening at the terminal velocity (a dashed horizontal line), and optionally a quadratic-drag curve with the same terminal speed that approaches it more sharply. For m = 0.2 kg and b = 0.1 kg/s, v_T = 19.6 m/s and τ = 2 s.
Laboratory
These run in a browser on the full site (they need a faster connection). Simulations are simplified educational models.
- Equation Lab: First-order systems (Equation Lab exploration)
Step a decay equation with Euler at three step sizes and compare each with the exact solution.
Open on the full site - Simulation OS: RC charging and discharging (Simulation OS experiment)
Read the capacitor voltage after one time constant and check it against your own Euler simulation.
Open on the full site
Practice
Each question links to its Lite lesson, where your answer is checked on our server and a wrong answer gets a hint. Answers are never sent to your device.
- A cooling temperature difference halves every 12 minutes. What is its time constant τ, in minutes?
Answer it in “Rate equations and exponential decay” - What percentage of the initial value remains after 5 time constants?
Answer it in “Rate equations and exponential decay” - A falling object with linear drag obeys dv/dt = g − v/τ with g = 9.81 m/s², τ = 0.5 s and v(0) = 0. Take 5 Euler steps of h = 0.2 s. What is the Euler estimate of v, in m/s?
Answer it in “Euler's method and step-size error” - For dy/dt = −y/τ with τ = 2 s, what is the largest Euler step (in seconds) that does not blow up?
Answer it in “Euler's method and step-size error” - You halve Euler's step size (staying well inside the stable range). Roughly what happens to the error at a fixed time?
Answer it in “Euler's method and step-size error” - An object of mass 0.05 kg is dropped from rest with linear drag b = 0.4 kg/s (g = 9.81 m/s²). Its speed is v(t) = v_T·(1 − e^(−t/τ)) with v_T = mg/b and τ = m/b. What is v at t = 3 s, in m/s?
Answer it in “Falling with drag: terminal velocity” - Held-out residuals are 1, −1, 2 and −2 °C. What is the RMS error, in °C?
Answer it in “Validating a model and stating its limits” - You halve Euler's step and the simulated temperature barely changes. What have you shown?
Answer it in “Validating a model and stating its limits”
Engineering applications
Thermal design
Before building an enclosure, engineers simulate how fast electronics heat up and cool down. A simple rate model, fitted to one measured warm-up curve, predicts whether a fan is needed.
Trust, then verify
A simulation that matches the data it was fitted to proves little. Validating against a separate test, and reporting the error, is what turns a model into evidence.
Watch (optional)
Streaming video uses a lot of data. Everything you need is in the text.
- “Differential equations, a tourist's guide | DE1” by 3Blue1Brown
- “What's so special about Euler's number e? | Chapter 5, Essence of calculus” by 3Blue1Brown
Summary
- A rate equation says how fast a quantity changes from its current value: dy/dt = −y/τ decays exponentially.
- Euler's method steps y(n+1) = y(n) + h·f(y(n)); its error shrinks roughly in proportion to h.
- Too large a step can make Euler unstable even when the real system is stable.
- Verification checks the code solves the equations; validation checks the equations match reality.
Glossary
- Exponential growth and decay
- A quantity decays exponentially when its rate of decrease is proportional to how much is left, giving y = y0 × e^(−t/τ). Field Guide
- First-order system and time constant
- A first-order system responds to a step by approaching its new value exponentially; its time constant τ is the time to cover 63.2 % of the change. Field Guide
- RC time constant
- When a capacitor charges or discharges through a resistor, the voltage changes exponentially with time constant τ = R × C. Field Guide
- Derivative (rate of change)
- The derivative of a quantity is its instantaneous rate of change: the gradient of its graph at a point. Field Guide
Project: A cooling model, fitted and validated
Use a recorded or provided cooling curve for a cup of water. Fit the time constant from part of the data, simulate it with Euler at a step you justify, and validate the model on the rest of the data. Report the RMS error and one honest limitation.
Deliverables
- The fitted time constant with working
- A plot of data and model, with the fitting and validation sections marked
- The RMS error and a stated limitation of the model
Write it up on paper or in your own notes. Lite never stores what you write.
References
- Jaan Kiusalaas (2013). Numerical Methods in Engineering with Python 3. Cambridge University Press.
- Richard L. Burden and J. Douglas Faires (2011). Numerical Analysis, 9th edition. Brooks/Cole.
- John V. Guttag (2016). Introduction to Computation and Programming Using Python, 2nd edition. MIT Press.
Further reading from the lessons
- OpenStax Calculus Volume 1 (free, open textbook)
A complete first-year calculus text with worked examples and exercises, free to read online.