First-Order Linear Equations
A first-order linear equation has the form . It is usually not separable, but one clever multiplication, the integrating factor, turns its left side into the derivative of a product, and then a single integration solves it. Mixing tanks, RC circuits and exponential smoothing all lead to this form.
The standard form
and appear only to the first power and are never multiplied together; and may be any functions of . If the equation is called homogeneous and it is also separable.
Examples: , (divide by first: ), .
The integrating factor
Multiply the whole equation by
Because , the product rule shows that the new left side is exactly a derivative:
Worked example, for :
Check: , and , so ✓.
SymPy does exactly this (its hint is called 1st_linear):
import sympy as sp
x = sp.symbols("x", positive=True)
y = sp.Function("y")
ode = sp.Eq(y(x).diff(x) + y(x) / x, x**2)
print("1st_linear" in sp.classify_ode(ode))
sol = sp.dsolve(ode)
print(sol)
print(sp.checkodesol(ode, sol))
print(sp.dsolve(ode, ics={y(1): 1}))True
Eq(y(x), (C1 + x**4/4)/x)
(True, 0)
Eq(y(x), (x**4/4 + 3/4)/x)
Constant coefficients: approach to equilibrium
The most common case has constant and constant :
slides exponentially from to the level . After (the time constant ) it has covered of the way; after , more than 99%.
A mixing tank
A 200-litre tank holds clean water. Brine with 10 g of salt per litre flows in at 4 L/min, and the well-mixed liquid flows out at the same rate. Let be the grams of salt in the tank:
That is , with and g (the salt in a tank full of 10 g/L brine). So . Let's confirm with SymPy and with SciPy's numeric solver:
import numpy as np
import sympy as sp
from scipy.integrate import solve_ivp
t = sp.symbols("t")
S = sp.Function("S")
sol = sp.dsolve(sp.Eq(S(t).diff(t), 40 - S(t) / 50), ics={S(0): 0})
print(sol)
f = sp.lambdify(t, sol.rhs)
num = solve_ivp(lambda t, s: 40 - s / 50, (0, 200), [0],
t_eval=[0, 50, 100, 200], rtol=1e-10, atol=1e-8)
for ti, si in zip(num.t, num.y[0]):
print(f"t = {ti:5.1f} min: formula {f(ti):8.2f} g numeric {si:8.2f} g")Eq(S(t), 2000 - 2000*exp(-t/50))
t = 0.0 min: formula 0.00 g numeric 0.00 g
t = 50.0 min: formula 1264.24 g numeric 1264.24 g
t = 100.0 min: formula 1729.33 g numeric 1729.33 g
t = 200.0 min: formula 1963.37 g numeric 1963.37 g
At min, one time constant, the tank holds 63.2% of its final 2000 g.
An RC circuit
A capacitor charging through a resistor from a battery of voltage obeys . With this is the same equation again: . Engineers say "it takes about to charge" for the same reason the tank takes about 250 minutes to fill with salt.
In software
is everywhere in code, often without anyone writing it down:
- Exponential moving averages (
avg += α·(sample − avg)) of frame times, latencies or stock prices are one Euler step of this equation per sample. - Following a target in games and UIs: the exact step is the frame-rate-independent version, derived in easing and smoothing.
- Momentum in optimisers: the velocity term in SGD with momentum and Adam's moving averages are first-order linear recurrences.
- Low-pass filters in audio and sensor code are discretised RC circuits.
When or are messy, or the equation is not linear at all, switch to a numerical solver.
Exercises
Exercise 1: Constant coefficients
Solve with .
Answer
.
Solution
Equivalently, : level , start 5, so .
Exercise 2: A variable coefficient
Solve .
Answer
.
Solution
import sympy as sp
x = sp.symbols("x")
y = sp.Function("y")
print(sp.dsolve(sp.Eq(y(x).diff(x) + 2 * x * y(x), x)))Eq(y(x), C1*exp(-x**2) + 1/2)
Exercise 3: Time constant
A smoothing filter follows a signal that jumps from 0 to 100 at , with ( in seconds). How long until reaches 95?
Answer
s.
Solution
That is about 3 time constants ( s), matching the rule "95% after ".
Exercise 4: A leaky tank
Same 200 L tank and 10 g/L brine at 4 L/min, but the tank starts with 600 g of salt and the outflow is 4 L/min. How much salt is in it after 30 minutes?
Answer
About 1231.7 g.
Solution
import math
print(f"{2000 - 1400 * math.exp(-30 / 50):.1f} g")1231.7 g
Test yourself
What is the integrating factor for ()?
The solution of approaches what as ()?
- 0
- Infinity
After one time constant , what fraction of the way from to has the solution covered?
- 50%
- About 63%
- About 95%
- 100%