TExcelTAD
Home › Calculus in Depth › Modeling with Differential Equations

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 tt). Its order is the highest derivative that appears.

Equation Order Linear?
y′=kyy' = ky 1 yes
y′=ky(1−y/M)y' = ky(1 - y/M) 1 no (y2y^2 term)
mx′′=−kxm x'' = -k x 2 yes
y′=t2+y2y' = t^2 + y^2 1 no

A solution on an interval II is a function that satisfies the equation at every point of II. A first-order equation typically has a one-parameter family of solutions (the general solution); adding an initial condition y(t0)=y0y(t_0) = y_0 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": dPdt=kP\frac{dP}{dt} = kP. Its solutions are P=P0ektP = P_0 e^{kt} (Exponential Growth and Decay).

Logistic growth. "Growth is proportional to the size and slows to zero as the size reaches a carrying capacity MM": dPdt=kP(1−PM)\frac{dP}{dt} = kP\left(1 - \frac{P}{M}\right). For small PP it behaves like kPkP; for PP near MM the rate goes to 0; for P>MP > M it is negative. The constant functions P=0P = 0 and P=MP = M are equilibrium solutions (the rate is 0), and every other positive solution moves toward MM. Solved in Models for Population Growth.

Spring. Hooke's law plus Newton's second law: force=−kx\text{force} = -kx and force=mx′′\text{force} = m x'', so x′′=−(k/m)xx'' = -(k/m) x. 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": dTdt=−k(T−Ts)\frac{dT}{dt} = -k(T - T_s) with k>0k > 0.

Checking that a function solves an equation is just differentiation and substitution:

python · try it yourself
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))

y′=y2y' = y^2 already shows a non-linear surprise: the solution −1/(C1+t)-1/(C_1 + t) blows up at the finite time t=−C1t = -C_1. 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 T(t)=20+70e−ktT(t) = 20 + 70 e^{-kt}. The second reading fixes kk, and then the model predicts the future:

python · try it yourself
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 f(t,y)f(t, y) and ∂f/∂y\partial f/\partial y are continuous on a rectangle around (t0,y0)(t_0, y_0), the IVP y′=f(t,y)y' = f(t, y), y(t0)=y0y(t_0) = y_0 has exactly one solution on some interval around t0t_0.

Proof idea. The IVP is equivalent to the integral equation y(t)=y0+∫t0tf(s,y(s)) dsy(t) = y_0 + \int_{t_0}^{t} f(s, y(s))\,ds. Start with y0(t)≡y0y_0(t) \equiv y_0 and iterate yn+1(t)=y0+∫f(s,yn(s)) dsy_{n+1}(t) = y_0 + \int f(s, y_n(s))\,ds. A bound ∣∂f/∂y∣≤L\lvert \partial f/\partial y \rvert \le L (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 (y′=y2y' = y^2 blows up). And if ∂f/∂y\partial f/\partial y is not continuous, uniqueness can fail: for y′=2∣y∣y' = 2\sqrt{\lvert y \rvert}, y(0)=0y(0) = 0, both y=0y = 0 and y=t2y = t^2 (for t≥0t \ge 0) are solutions, because ∂f/∂y=1/y\partial f/\partial y = 1/\sqrt{y} is infinite at y=0y = 0.

python · try it yourself
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. x′′=F(x,x′)/mx'' = F(x, x')/m 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" dθdt=−∇L(θ)\frac{d\theta}{dt} = -\nabla L(\theta) has the loss as a decreasing quantity; one step θ←θ−η∇L\theta \leftarrow \theta - \eta \nabla L 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) y′′+ty′=sin⁡ty'' + t y' = \sin t, b) y′=y(2−y)y' = y(2 - y), c) t2y′′′−y=0t^2 y''' - y = 0.

Answer

a) order 2, linear; b) order 1, non-linear; c) order 3, linear.

Solution

A linear equation has yy and its derivatives only to the first power, multiplied by functions of tt. In b) the y2y^2 term breaks linearity; the coefficients tt and t2t^2 in a) and c) are fine.

Exercise 2: Find the exponents

For which rr does y=erty = e^{rt} solve y′′+y′−6y=0y'' + y' - 6y = 0?

Answer

r=2r = 2 and r=−3r = -3.

Solution
y′′+y′−6y=(r2+r−6)ert=0⇒r2+r−6=(r−2)(r+3)=0y'' + y' - 6y = (r^2 + r - 6) e^{rt} = 0 \quad \Rightarrow \quad r^2 + r - 6 = (r - 2)(r + 3) = 0

Exercise 3: Equilibria and direction

For dPdt=0.3P(1−P500)\frac{dP}{dt} = 0.3 P \left(1 - \frac{P}{500}\right), list the equilibria and say whether PP increases or decreases when P=100P = 100, P=700P = 700.

Answer

Equilibria P=0P = 0 and P=500P = 500; increasing at 100 (rate 24), decreasing at 700 (rate −84).

Solution
0.3P(1−P/500)=0⇒P=0 or P=500P=100:0.3⋅100⋅(0.8)=24>0P=700:0.3⋅700⋅(−0.4)=−84<0\begin{aligned} &0.3P(1 - P/500) = 0 \quad \Rightarrow \quad P = 0 \text{ or } P = 500 \\ &P = 100{:}\quad 0.3 \cdot 100 \cdot (0.8) = 24 > 0 \\ &P = 700{:}\quad 0.3 \cdot 700 \cdot (-0.4) = -84 < 0 \end{aligned}

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 T(t)=25+375e−ktT(t) = 25 + 375 e^{-kt}, find when it is safe to touch (50 °C).

Answer

k=ln⁡(375/275)/5≈0.0620k = \ln(375/275)/5 \approx 0.0620, and t≈43.66t \approx 43.66 minutes.

Solution
300=25+375e−5k⇒k=ln⁡(375/275)/550=25+375e−kt⇒t=ln⁡(15)/k\begin{aligned} 300 &= 25 + 375 e^{-5k} && \Rightarrow \quad k = \ln(375/275)/5 \\ 50 &= 25 + 375 e^{-kt} && \Rightarrow \quad t = \ln(15)/k \end{aligned}
python · try it yourself
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 mx′′=−kxm x'' = -kx need to determine a unique motion?

  1. None
  2. One: the starting position
  3. Two: starting position and velocity
  4. Three

The IVP y′=2∣y∣y' = 2\sqrt{\lvert y \rvert}, y(0)=0y(0) = 0 has two solutions. Which hypothesis of the uniqueness theorem fails?

  1. ff is not continuous
  2. ∂f/∂y\partial f/\partial y is not continuous at y=0y = 0
  3. The interval is too short
  4. The equation is second order

One step of gradient descent θ←θ−η∇L(θ)\theta \leftarrow \theta - \eta \nabla L(\theta) is

  1. Euler's method with step η\eta on dθ/dt=−∇L(θ)d\theta/dt = -\nabla L(\theta)
  2. The exact solution of the gradient flow
  3. Newton's method
  4. A second-order ODE