First-Order Models: Mixing, Cooling, Interest
Most first-order models come from one principle:
This lesson builds three classic models from it, solves them with the methods of this chapter and checks them numerically. Each is a template for many others.
Mixing tanks
A tank holds 100 L of water with 5 kg of salt dissolved. Brine with 0.2 kg/L flows in at 3 L/min, and the well-stirred mixture flows out at 3 L/min. Let be the salt in kg.
- Rate in: kg/min.
- Rate out: concentration times outflow, .
This is linear with equilibrium kg (the inflow concentration times the volume), and the solution is .
If the outflow differs from the inflow, the volume changes, , and the equation gets a variable coefficient:
import sympy as sp
t = sp.symbols("t", nonnegative=True)
x = sp.Function("x")
V = 100 + (3 - 2) * t # in 3 L/min, out 2 L/min
ode = sp.Eq(x(t).diff(t), sp.Rational(6, 10) - 2 * x(t) / V)
sol = sp.dsolve(ode, ics={x(0): 5}).rhs
print("x(t) =", sp.simplify(sol))
print("salt after 60 min:", round(float(sol.subs(t, 60)), 4), "kg in", 100 + 60, "L")x(t) = (t**3 + 300*t**2 + 30000*t + 250000)/(5*(t**2 + 200*t + 10000))
salt after 60 min: 26.1406 kg in 160 L
Newton's law of cooling
An object's temperature changes at a rate proportional to its difference from the surroundings: . The solution is . Two measurements determine .
Example. A pie comes out of the oven at 180 °C into a 20 °C kitchen and is 120 °C after 10 minutes. Then , . When is it 40 °C?
import math
Ta, T0 = 20.0, 180.0
k = math.log(160 / 100) / 10
t40 = math.log((T0 - Ta) / (40 - Ta)) / k
print(f"k = {k:.5f} per minute, 40 degrees after {t40:.1f} minutes")k = 0.04700 per minute, 40 degrees after 44.2 minutes
Continuous interest with deposits
A balance grows at rate and receives continuous deposits of per year: . With :
For a loan, is negative (payments). The loan is paid off when ; payments must exceed or the debt grows forever (the equilibrium is unstable).
import numpy as np
from scipy.integrate import solve_ivp
r, d, B0 = 0.05, -12_000.0, 150_000.0 # 5 % loan, 12,000 per year in payments
paid = lambda t, B: B[0]
paid.terminal = True
sol = solve_ivp(lambda t, B: r * B + d, (0, 100), [B0], events=paid, rtol=1e-10, atol=1e-6)
T_exact = np.log((d / r) / (B0 + d / r)) / r
print(f"paid off after {sol.t_events[0][0]:.3f} years (formula {T_exact:.3f})")
print(f"total paid: {-d * T_exact:,.0f}")paid off after 19.617 years (formula 19.617)
total paid: 235,399
Checklist for building a model
- Name the unknown and its units.
- Write rate in and rate out; check that the units of every term match.
- Identify the equilibrium and whether it is stable (it is often the physically obvious long-run value).
- Solve, then sanity-check limits: and .
In software
- Compartment models (drug dosing in pharmacology, inventory and queue models, the SIR model) are systems of "in minus out" equations.
- Thermal throttling in CPUs and GPUs uses exactly the cooling model: the chip's temperature relaxes toward a target set by power draw, and controllers predict it with .
Exercises
Exercise 1: Mixing equilibrium
A 50 L tank of pure water receives brine of 0.1 kg/L at 2 L/min and drains at 2 L/min. How much salt is there after a long time, and after 25 minutes?
Answer
5 kg in the long run; kg at 25 minutes.
Solution
, , so . At : .
Exercise 2: Cooling constant
A drink at 4 °C warms to 10 °C after 30 minutes in a 25 °C room. Find .
Answer
per minute.
Solution
; at 30 minutes, , so and .
Exercise 3: Minimum payment (code)
For the loan in the lesson, what is the smallest yearly payment that ever pays it off? How long does 15,000 per year take?
Answer
More than 7,500 per year; about 13.86 years at 15,000.
Solution
The balance falls only if , i.e. payments above . With : .
import math
r, d, B0 = 0.05, -15_000.0, 150_000.0
print(round(math.log((d / r) / (B0 + d / r)) / r, 2), round(20 * math.log(2), 2))13.86 13.86
Test yourself
In a well-stirred tank of volume with outflow rate , the salt leaves at rate…
In Newton's law of cooling , the long-run temperature is…
- 0
- it oscillates
For with , the equilibrium is…
- stable
- unstable
- semistable
- not an equilibrium