Modeling with Differential Equations
Most laws of nature do not say what a quantity is; they say how fast it changes. A differential equation turns such a law into mathematics. This lesson sets up the vocabulary (order, solution, initial value problem), builds four standard models from first principles, checks solutions with SymPy, and states when a solution exists and is unique, with a counterexample that every simulation programmer should know.
For a gentler first contact, see What a Differential Equation Is in the survey course.
Vocabulary
A differential equation (DE) relates an unknown function to its derivatives. An ordinary DE has one independent variable (usually time ). Its order is the highest derivative that appears.
| Equation | Order | Linear? |
|---|---|---|
| 1 | yes | |
| 1 | no ( term) | |
| 2 | yes | |
| 1 | no |
A solution on an interval is a function that satisfies the equation at every point of . A first-order equation typically has a one-parameter family of solutions (the general solution); adding an initial condition gives an initial value problem (IVP), which picks out one member of the family.
Four models built from a sentence
Natural growth. "The growth rate is proportional to the size": . Its solutions are (Exponential Growth and Decay).
Logistic growth. "Growth is proportional to the size and slows to zero as the size reaches a carrying capacity ": . For small it behaves like ; for near the rate goes to 0; for it is negative. The constant functions and are equilibrium solutions (the rate is 0), and every other positive solution moves toward . Solved in Models for Population Growth.
Spring. Hooke's law plus Newton's second law: and , so . Second order: a solution needs both the initial position and the initial velocity.
Newton's law of cooling. "An object's temperature changes at a rate proportional to its difference from the surroundings": with .
Checking that a function solves an equation is just differentiation and substitution:
import sympy as sp
t, k, m, C, A, B, P0 = sp.symbols("t k m C A B P0", positive=True)
P = P0 * sp.exp(k * t)
print("growth: P' - kP =", sp.simplify(sp.diff(P, t) - k * P))
w = sp.sqrt(k / m)
x = A * sp.cos(w * t) + B * sp.sin(w * t)
print("spring: m x'' + k x =", sp.simplify(m * sp.diff(x, t, 2) + k * x))
y = sp.Function("y")
print("dsolve y' = y²:", sp.dsolve(sp.Eq(y(t).diff(t), y(t) ** 2)))growth: P' - kP = 0
spring: m x'' + k x = 0
dsolve y' = y²: Eq(y(t), -1/(C1 + t))
already shows a non-linear surprise: the solution blows up at the finite time . The right-hand side is perfectly smooth, yet the solution does not live forever.
Fitting a model to data
A cup of coffee at 90 °C sits in a 20 °C room and is 60 °C after 10 minutes. Solving the cooling equation (it is separable, see Separable Equations) gives . The second reading fixes , and then the model predicts the future:
import sympy as sp
t, k = sp.symbols("t k", positive=True)
T = sp.Function("T")
sol = sp.dsolve(sp.Eq(T(t).diff(t), -k * (T(t) - 20)), T(t), ics={T(0): 90}).rhs
print("T(t) =", sol)
k_val = sp.solve(sp.Eq(sol.subs(t, 10), 60), k)[0]
print("k = ln(7/4)/10?", sp.simplify(k_val - sp.log(sp.Rational(7, 4)) / 10) == 0)
print("k ≈", round(float(k_val), 5), "per minute")
t30 = sp.solve(sp.Eq(sol.subs(k, k_val), 30), t)[0]
print("reaches 30 °C after", round(float(t30), 2), "minutes")T(t) = 20 + 70*exp(-k*t)
k = ln(7/4)/10? True
k ≈ 0.05596 per minute
reaches 30 °C after 34.77 minutes
Existence and uniqueness
Theorem (Picard–Lindelöf). If and are continuous on a rectangle around , the IVP , has exactly one solution on some interval around .
Proof idea. The IVP is equivalent to the integral equation . Start with and iterate . A bound (a Lipschitz condition) makes this map a contraction on a short enough interval, so the iterates converge to a unique fixed point.
Two warnings hide in the statement. The interval may be short ( blows up). And if is not continuous, uniqueness can fail: for , , both and (for ) are solutions, because is infinite at .
import sympy as sp
t = sp.symbols("t", nonnegative=True)
for y in [sp.Integer(0), t**2]:
residual = sp.simplify(sp.diff(y, t) - 2 * sp.sqrt(sp.Abs(y)))
print(f"y = {y}: y' - 2√|y| = {residual}, y(0) = {y.subs(t, 0)}")y = 0: y' - 2√|y| = 0, y(0) = 0
y = t**2: y' - 2√|y| = 0, y(0) = 0
In software
- Every game physics step solves an IVP. with the current position and velocity as the initial condition; the engine's integrator advances it by one frame (Integrators, Springs).
- Uniqueness is what makes replays and lockstep multiplayer possible: same initial state, same equation, same answer. Non-unique or chaotic models break that (Determinism).
- Gradient descent is a discretised DE. The "gradient flow" has the loss as a decreasing quantity; one step is Euler's method on it (Direction Fields and Euler's Method). Neural ODEs make a network's depth a continuous time variable.
- Simulations: epidemics (SIR), chemical kinetics, circuits and pharmacokinetics are all systems of first-order DEs solved with
scipy.integrate.solve_ivp(SciPy ODEs).
Exercises
Exercise 1: Classify
Give the order and say whether each is linear: a) , b) , c) .
Answer
a) order 2, linear; b) order 1, non-linear; c) order 3, linear.
Solution
A linear equation has and its derivatives only to the first power, multiplied by functions of . In b) the term breaks linearity; the coefficients and in a) and c) are fine.
Exercise 2: Find the exponents
For which does solve ?
Answer
and .
Solution
Exercise 3: Equilibria and direction
For , list the equilibria and say whether increases or decreases when , .
Answer
Equilibria and ; increasing at 100 (rate 24), decreasing at 700 (rate −84).
Solution
Exercise 4: Cooling, in code
A metal part leaves an oven at 400 °C into a 25 °C hall and is at 300 °C after 5 minutes. Using , find when it is safe to touch (50 °C).
Answer
, and minutes.
Solution
import math
k = math.log(375 / 275) / 5
print(f"k = {k:.4f}, t = {math.log(15) / k:.2f} min")k = 0.0620, t = 43.66 min
Test yourself
How many initial conditions does the spring equation need to determine a unique motion?
- None
- One: the starting position
- Two: starting position and velocity
- Three
The IVP , has two solutions. Which hypothesis of the uniqueness theorem fails?
- is not continuous
- is not continuous at
- The interval is too short
- The equation is second order
One step of gradient descent is
- Euler's method with step on
- The exact solution of the gradient flow
- Newton's method
- A second-order ODE