Linear Equations
A first-order linear equation can always be solved by one trick, the integrating factor, which turns its left-hand side into the derivative of a product. This lesson proves the method works, states the existence and uniqueness theorem that comes with it, and applies it to electric circuits, Bernoulli equations and the exponential smoothing found in game cameras and signal filters.
The survey version is First-Order Linear Equations.
Standard form
A first-order equation is linear if it can be written as
with and continuous on an interval . "Linear" refers to and : they appear to the first power, never multiplied together or inside other functions. is linear (divide by ); and are not. When the equation is homogeneous.
The integrating factor
Multiply the equation by . Because , the product rule gives
so integrating both sides,
Theorem (existence and uniqueness). If and are continuous on an open interval containing , then for every the problem , has exactly one solution, and it is defined on all of .
Proof sketch. Existence: the formula above with definite integrals from , with , is differentiable on and satisfies both conditions. Uniqueness: if and are solutions, solves , , so and is constant, equal to . Since , . ∎
Contrast this with nonlinear equations such as , whose solutions can blow up in finite time (see Separable Equations): a linear equation's solutions live as long as its coefficients are continuous.
The solution formula also shows the structure , where solves . This superposition principle is what makes linear equations, linear algebra and linear systems so tractable.
import sympy as sp
x = sp.symbols("x", positive=True)
y = sp.Function("y")
# x y' + 2y = 5x³ ⇒ y' + (2/x) y = 5x², I = x²
ode = sp.Eq(x * y(x).diff(x) + 2 * y(x), 5 * x**3)
print(sp.dsolve(ode, y(x)))
I = sp.exp(sp.integrate(2 / x, x))
print("integrating factor:", I)
print("by hand:", sp.expand((sp.integrate(I * 5 * x**2, x) + sp.Symbol("C")) / I))Eq(y(x), (C1 + x**5)/x**2)
integrating factor: x**2
by hand: C/x**2 + x**3
An RL circuit
A circuit with resistance (ohms), inductance (henries) and a voltage source obeys Kirchhoff's law . Take , and a switch closed at onto a sinusoidal source , with :
The solution splits into a transient term proportional to that dies off and a steady-state sinusoid with the source's frequency, smaller amplitude and a phase lag.
import sympy as sp
t = sp.symbols("t", nonnegative=True)
I = sp.Function("I")
sol = sp.dsolve(sp.Eq(2 * I(t).diff(t) + 8 * I(t), 20 * sp.sin(3 * t)), I(t), ics={I(0): 0})
print(sp.simplify(sol.rhs))
steady = sol.rhs.subs(sp.exp(-4 * t), 0)
amp = sp.sqrt(sum(c**2 for c in sp.Poly(steady, sp.sin(3*t), sp.cos(3*t)).coeffs()))
print("steady-state amplitude:", sp.nsimplify(amp), "=", float(amp), "A")
print("I(1) ≈", round(float(sol.rhs.subs(t, 1)), 5))8*sin(3*t)/5 - 6*cos(3*t)/5 + 6*exp(-4*t)/5
steady-state amplitude: 2 = 2.0 A
I(1) ≈ 1.43576
The amplitude is : the circuit attenuates the input, and the attenuation grows with frequency. That is a low-pass filter.
Bernoulli equations
() is nonlinear, but the substitution makes it linear: , so dividing the equation by gives .
In software: exponential smoothing
The equation says "move toward the target at a rate proportional to the gap". It is linear with . For a constant target, . Programmers use its exact discrete update everywhere:
- Camera follow and UI motion: the update above is frame-rate independent, unlike the common
y += (u − y)·0.1, which depends on how often it runs (see Damping). - Signal processing and metrics: an exponential moving average (load averages in
top, EMA indicators in trading, Adam's moment estimates in deep learning) is the same filter sampled at fixed steps. - Thermal and battery models in firmware are RC circuits, i.e. first-order linear ODEs.
import math
def follow(y, target, dt, tau):
return y + (target - y) * (1 - math.exp(-dt / tau))
for fps in (30, 144):
y, dt = 0.0, 1 / fps
for _ in range(fps): # simulate one second
y = follow(y, 10.0, dt, tau=0.25)
print(f"{fps:3d} fps: y(1 s) = {y:.6f}")
print(f"exact: y(1 s) = {10 * (1 - math.exp(-4)):.6f}") 30 fps: y(1 s) = 9.816844
144 fps: y(1 s) = 9.816844
exact: y(1 s) = 9.816844
Exercises
Exercise 1: Constant coefficients
Solve with .
Answer
Solution
Exercise 2: Variable coefficient
Solve with .
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), 4 * x), y(x), ics={y(0): 0}))Eq(y(x), 2 - 2*exp(-x**2))
Exercise 3: A Bernoulli equation
Solve with .
Answer
Solution
Exercise 4: Frame-rate dependence
Run the naive update y += (10 − y)·0.1 once per frame for one second at 30 fps and at 144 fps, starting from 0. Compare with the frame-rate-independent update.
Answer
Naive: about at 30 fps but at 144 fps, so the motion speed depends on the frame rate. The exact update gives at both.
Solution
The naive factor 0.1 per frame equals only for one particular . After frames the gap is : and .
for fps in (30, 144):
y = 0.0
for _ in range(fps):
y += (10 - y) * 0.1
print(f"{fps:3d} fps: {y:.4f}") 30 fps: 9.5761
144 fps: 10.0000
Test yourself
What is the integrating factor of ()?
Where is the solution of , , guaranteed to exist?
- Only near
- On the whole interval containing where and are continuous
- Only for
- Everywhere on
Which equation is linear in ?