Textbooks/Engineering Programming With Python/Chapter 8Lite (plain text) version
8Simulating dynamic systems
Version 1 · Technically checked (automated checks only) · 4 source lessons · 8 practice items
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.
Explanation
This chapter is built from 4 School lessons. Each part below is the lesson's own explanation; open the lesson for its predict-and-discover activities.
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 full 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.
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 full 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 full 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
Interactive figure (online). Description: 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.
Interactive figure (online). Description: 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/τ.
Interactive figure (online). Description: 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
Everything runs in your browser. No kit, nothing mains-powered. Simulations are simplified educational models.
Equation Lab exploration
Equation Lab: First-order systems
Step a decay equation with Euler at three step sizes and compare each with the exact solution.
Open Equation Lab: First-order systems →Simulation OS experiment
Simulation OS: RC charging and discharging
Read the capacitor voltage after one time constant and check it against your own Euler simulation.
Open Simulation OS: RC charging and discharging →
Guided practice
Answers are checked on the server; wrong answers get a hint and another try. Signed in, solved items also count towards the source lesson.
- A cooling temperature difference halves every 12 minutes. What is its time constant τ, in minutes? (min)
- What percentage of the initial value remains after 5 time constants? (%)
- 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? (m/s)
- For dy/dt = −y/τ with τ = 2 s, what is the largest Euler step (in seconds) that does not blow up? (s)
- You halve Euler's step size (staying well inside the stable range). Roughly what happens to the error at a fixed time?
- It halves
- It quarters
- It is unchanged
- 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? (m/s)
- Held-out residuals are 1, −1, 2 and −2 °C. What is the RMS error, in °C? (°C)
- You halve Euler's step and the simulated temperature barely changes. What have you shown?
- The model is validated
- The numerical solution is verified (converged), not that the model matches reality
- The data are correct
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 verified videos. Nothing loads from YouTube until you press play.
- Differential equations, a tourist's guide | DE1 (3Blue1Brown)
- What's so special about Euler's number e? | Chapter 5, Essence of calculus (3Blue1Brown)
Chapter 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/τ).
- 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.
- RC time constant
- When a capacitor charges or discharges through a resistor, the voltage changes exponentially with time constant τ = R × C.
- Derivative (rate of change)
- The derivative of a quantity is its instantaneous rate of change: the gradient of its graph at a point.
Definitions from the 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
Keep your work in your Engineering Journal; it stays private unless you choose to share it.
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 source lessons
- OpenStax Calculus Volume 1 (free, open textbook) ↗A complete first-year calculus text with worked examples and exercises, free to read online.
Publication record
- Version
- 1
- Status
- Technically checked (automated checks only)
- Technical review
- Automated checks only, last run 2026-11-29. No human, educator or mentor review is claimed.src/content/textbooks/textbooks.test.ts: schema, every lesson, diagram, practice item, video, Field Guide entry, lab and reference resolves; figure numbers recomputed; no practice answers in client payloads.
- Written by
- Compiled and written by the Tactiverge team with AI assistance, from Tactiverge School lessons that were themselves written with AI assistance.
- Licence
- All rights reserved by the founders unless a licence is approved.
- Corrections
- None recorded yet.Spotted a mistake? Use “Report a problem” on any chapter, or see the corrections page.