Lite / Textbooks / The Mathematics of Machines / Chapter 8
8. Differential equations
Introduction
Many machines are described not by a formula for their output but by a rule for how it changes: the rate of cooling depends on the temperature difference, a spring's acceleration depends on its stretch. This chapter writes those rules as differential equations, solves the simplest ones by hand, and steps the rest forward numerically with Euler's method and RK4, watching for error and instability.
Figure 8.1. Euler's method against the exact decay (described in words)
The exact solution y = e^(−t/τ) of dy/dt = −y/τ, starting at 1, plotted from t = 0 to 2τ. Euler steps of size h = 0.5τ give 1, 0.5, 0.25, 0.125, 0.0625. At t = τ the exact value is about 0.368 and Euler gives 0.25.
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 Linear ODEs you can solve by hand
Tap a wine glass, pluck a guitar string, release a car's suspension: one second-order equation describes them all. What does it predict?
First order
dx/dt = −kx has solution x(t) = x₀ e^{−kt}. The time constant τ = 1/k is the time to fall to about 37%. After 5τ it is under 1%. RC circuits, cooling and many sensor lags are first-order.
Second order
m x″ + c x′ + k x = 0 needs two initial conditions (position and velocity). Its behaviour is set by ω_n (how fast it wants to oscillate) and ζ (how strongly it is damped). Engineers often design for ζ around 0.6–0.7: quick, with little overshoot.
Benchmarks for numerics
With c = 0, m = k = 1 and x(0) = 1, x′(0) = 0, the exact solution is x = cos t and the energy ½x′² + ½x² stays at 0.5 for ever. That exact answer is what week 7 uses to measure integrator errors.
Derivation: Solving m x″ + c x′ + k x = 0
Try x = e^{rt}. Then x′ = r e^{rt}, x″ = r² e^{rt}, and the equation becomes (m r² + c r + k) e^{rt} = 0.
So r solves the characteristic equation m r² + c r + k = 0: r = (−c ± √(c² − 4mk)) / (2m).
Write ω_n = √(k/m) and ζ = c/(2√(km)). Then r = −ζω_n ± ω_n√(ζ² − 1).
ζ > 1 (overdamped): two negative real roots, x = A e^{r₁t} + B e^{r₂t}. ζ = 1 (critical): x = (A + Bt) e^{−ω_n t}.
ζ < 1 (underdamped): r = −ζω_n ± iω_d with ω_d = ω_n√(1 − ζ²), so x = e^{−ζω_n t}(A cos ω_d t + B sin ω_d t).
A and B come from the initial position and velocity. By linearity, any combination of solutions is a solution (week 1).
Common mix-ups
More damping always makes a system settle faster.
✓ Beyond critical damping, it creeps more slowly. Fastest settling without overshoot is at ζ = 1.The damped frequency equals the natural frequency.
✓ ω_d = ω_n√(1 − ζ²) is a little lower, noticeably so for large ζ.
Open the Lite lesson: Linear ODEs you can solve by hand
8.4 Euler, semi-implicit Euler and RK4
Simulate a frictionless spring with Euler's method and it gains energy from nowhere. Is the physics wrong, or the arithmetic?
The three methods
For y′ = f(t, y):
- Explicit Euler: yₙ₊₁ = yₙ + h f(tₙ, yₙ). One slope, at the start.
- Semi-implicit (symplectic) Euler for x′ = v, v′ = a(x): vₙ₊₁ = vₙ + h a(xₙ), then xₙ₊₁ = xₙ + h vₙ₊₁.
- RK4: k₁ = f(tₙ, yₙ), k₂ = f(tₙ + h/2, yₙ + h k₁/2), k₃ = f(tₙ + h/2, yₙ + h k₂/2), k₄ = f(tₙ + h, yₙ + h k₃); yₙ₊₁ = yₙ + (h/6)(k₁ + 2k₂ + 2k₃ + k₄).
Accuracy versus structure
RK4 is far more accurate per step: with h = 0.1 over t = 10 its position error is about 4×10⁻⁶, against about 0.6 for explicit Euler. But RK4 still slowly loses energy (with h = 0.2 over 10 periods, about 0.4998 instead of 0.5). Semi-implicit Euler is only first-order accurate, yet its energy never drifts away. For long orbital or molecular simulations, that structure can matter more than raw accuracy.
Cost
Count function evaluations, the expensive part for real models. RK4 costs four per step, Euler one. RK4 with step 4h therefore costs the same as Euler with step h, and is still usually far more accurate.
Derivation: Why explicit Euler gains energy on the oscillator
Write x″ = −x as x′ = v, v′ = −x. Energy E = ½(x² + v²) is exactly conserved by the true solution.
Explicit Euler: xₙ₊₁ = xₙ + h vₙ, vₙ₊₁ = vₙ − h xₙ.
Then xₙ₊₁² + vₙ₊₁² = xₙ² + 2h xₙvₙ + h²vₙ² + vₙ² − 2h xₙvₙ + h²xₙ² = (1 + h²)(xₙ² + vₙ²).
So Eₙ = (1 + h²)ⁿ E₀: exponential growth for any h > 0. Over a fixed time T = nh, Eₙ ≈ E₀ e^{hT}, which tends to E₀ only as h → 0.
Semi-implicit Euler updates v first and uses the new v for x. It conserves a slightly modified energy exactly, so its energy error stays bounded for all time (it is symplectic).
Common mix-ups
If the simulation looks smooth, it is accurate.
✓ Euler's spiral looks smooth and is completely wrong about energy. Compare with an exact or converged answer.Higher order always wins.
✓ It wins on accuracy for smooth problems; structure (energy, stability) can matter more over long times.
Open the Lite lesson: Euler, semi-implicit Euler and RK4
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.
Linear ODEs you can solve by hand: example 1
m = 2 kg, k = 50 N/m, c = 4 N·s/m. Find ω_n, ζ and ω_d.
Solution
ω_n = √(50/2) = 5 rad/s. ζ = 4/(2√100) = 0.2. ω_d = 5√(1 − 0.04) ≈ 4.90 rad/s. Underdamped; the envelope decays like e^{−1·t}.
Euler, semi-implicit Euler and RK4: example 1
One explicit Euler step for x′ = v, v′ = −x from (1, 0) with h = 0.1.
Solution
x₁ = 1 + 0.1·0 = 1, v₁ = 0 − 0.1·1 = −0.1. Energy ½(1 + 0.01) = 0.505 = 0.5 × 1.01, as the derivation predicts.
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 Linear ODEs you can solve by hand
In 'Mass–spring–damper' mode, set k = 4 and sweep c from 0 to 6. Watch the trajectories change from circles (c = 0) to spirals to straight-in curves. Note the value of c where the eigenvalues become real, and compare with 2√k.
The phase-portrait diagram plots velocity against position with direction arrows and six trajectories. Sliders set stiffness and damping; text gives the eigenvalues, the critical damping value and the type of equilibrium (centre, spiral or node).
Figure 8.5 from Euler, semi-implicit Euler and RK4
In 'Oscillator energy', set h = 0.2 and run 10 periods. Which curve drifts up, which stays bounded, which slowly sinks? Halve h and see which drift shrinks fastest.
The integrator diagram plots energy against time for explicit Euler, semi-implicit Euler (dashed) and RK4, with the exact energy 0.5 as a dashed line. Text lists each method's final energy and the Euler growth factor per step.
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)
Change the time constant and check that the response reaches about 63 % of its final value after one τ.
Open on the full site - Equation Lab: Oscillator (Equation Lab exploration)
Vary the damping ratio and classify each response as under-, critically or over-damped.
Open on the full site - Simulation OS: RC charge and discharge (Simulation OS experiment)
Compare the simulated capacitor voltage with the exact exponential at three times.
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” - dV/dt = −V/τ with τ = 2 s and V(0) = 5 V. One Euler step of h = 0.5 s gives what V, in volts?
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” - A mass–spring–damper has m = 2 kg, k = 8 N/m, c = 4 N·s/m. What is its damping ratio ζ?
Answer it in “Linear ODEs you can solve by hand” - For m = 1 kg and k = 9 N/m, what damping gives critical damping?
Answer it in “Linear ODEs you can solve by hand” - Explicit Euler on x″ = −x with h = 0.05: by what factor does the energy grow per step?
Answer it in “Euler, semi-implicit Euler and RK4” - With the same number of function evaluations, which is usually more accurate on smooth problems?
Answer it in “Euler, semi-implicit Euler and RK4”
Engineering applications
Simulators
Every physics engine and circuit simulator steps differential equations forward. Choosing the method and step size is a trade between accuracy, stability and computing time.
Suspension and control tuning
The damping ratio of a mass–spring–damper decides whether a vehicle's suspension bounces or settles smoothly. The same second-order model describes many feedback loops.
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
- “Coding a Fourth-Order Runge-Kutta Integrator in Python and Matlab” by Steve Brunton
Summary
- dy/dt = −y/τ has the solution y = y0·e^(−t/τ); after one τ about 37 % remains, after 5τ less than 1 %.
- Euler's method: y(n+1) = y(n) + h·f(t(n), y(n)). Its error per unit time shrinks in proportion to h (first order).
- For decay, Euler is stable only if h < 2τ; a large step can make the numbers explode.
- A second-order system's behaviour is set by its natural frequency ωn and damping ratio ζ.
- RK4 uses four slope evaluations per step and is fourth-order accurate; symplectic methods conserve energy better over long runs.
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
- Damping ratio
- The damping ratio ζ describes how quickly oscillations die out in a second-order system: below 1 it oscillates, exactly 1 is critically damped, above 1 it creeps to the target without overshoot. Field Guide
- Hooke's law (springs)
- For a spring within its elastic limit, the force needed to stretch or compress it is proportional to the change in length: F = k × x. Field Guide
Project: How small a step?
Simulate a cooling model dT/dt = −(T − Ta)/τ with Euler's method for three step sizes, compare each with the exact solution, and find the largest step that keeps the error below 1 °C over the run. Repeat with RK4 and compare the cost.
Deliverables
- The model with parameter values and units
- Error tables for each step size and method
- The chosen step size with justification
Write it up on paper or in your own notes. Lite never stores what you write.
References
- William E. Boyce and Richard C. DiPrima (2012). Elementary Differential Equations and Boundary Value Problems, 10th edition. Wiley.
- J. C. Butcher (2016). Numerical Methods for Ordinary Differential Equations, 3rd edition. Wiley.
- Erwin Kreyszig (2011). Advanced Engineering Mathematics, 10th edition. Wiley.
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.