Linear Equations and the Integrating Factor
A first-order linear equation has the form
Most first-order models that are not separable are linear: a tank with a time-varying inflow, a circuit with a time-varying voltage, an object cooling in a room whose temperature changes. There is a formula for every such equation, found with one trick.
The integrating factor
Multiply the equation by . Because , the left side becomes an exact derivative by the product rule:
So the equation turns into , and one integration solves it:
Any antiderivative of works for (the constant cancels), and with an initial condition you can use definite integrals from .
Example
Solve , .
import sympy as sp
x = sp.symbols("x")
y = sp.Function("y")
ode = sp.Eq(y(x).diff(x) + 2 * y(x), sp.exp(-x))
sol = sp.dsolve(ode, ics={y(0): 3})
print(sol)
print(sp.checkodesol(ode, sol))Eq(y(x), (1 + 2*exp(-x))*exp(-x))
(True, 0)
SymPy factors the answer as , which expands to .
Structure: homogeneous plus particular
The answer has two parts. The term solves the homogeneous equation ; the term is one particular solution of the full equation. This "general = homogeneous + particular" structure holds for every linear equation, of any order, and returns in Undetermined Coefficients and Nonhomogeneous Systems.
Example with a variable coefficient
Solve for , . First divide by to get the standard form . Then and , so , , and gives .
import numpy as np
from scipy.integrate import solve_ivp
sol = solve_ivp(lambda x, y: 4 * x - 2 * y / x, (1, 3), [2.0], t_eval=[1.5, 2, 3], rtol=1e-10, atol=1e-12)
for xv, yv in zip(sol.t, sol.y[0]):
print(f"x = {xv}: numeric {yv:.8f}, formula {xv**2 + xv**-2:.8f}")x = 1.5: numeric 2.69444444, formula 2.69444444
x = 2.0: numeric 4.25000000, formula 4.25000000
x = 3.0: numeric 9.11111111, formula 9.11111111
Note the domain: is undefined at , so the solution is only guaranteed on an interval that avoids 0. Indeed blows up there.
In software
- A first-order low-pass filter (smoothing a sensor signal, an audio "one-pole" filter) is the linear ODE with input . Its discrete version
y += (u - y) * dt / tauis the exponential moving average used everywhere from stock charts to the Adam optimizer's moment estimates. - An RC circuit charging through a resistor obeys the same equation (RLC Circuits).
Exercises
Exercise 1: Constant forcing
Solve , .
Answer
.
Solution
, , , so ; gives . The solution rises to the equilibrium .
Exercise 2: Find the integrating factor
What is the integrating factor of on , and what is the general solution?
Answer
; .
Solution
, so . Then , so .
Exercise 3: A cooling cup in a warming room (code)
Coffee at 90 °C sits in a room whose temperature is (°C, in minutes). With Newton's law , find with SymPy.
Answer
°C.
Solution
The equation is , linear. SymPy gives , so .
import sympy as sp
t = sp.symbols("t"); T = sp.Function("T")
sol = sp.dsolve(sp.Eq(T(t).diff(t), -sp.Rational(1, 10) * (T(t) - 20 - t)), ics={T(0): 90}).rhs
print(sp.expand(sol), "|", sp.simplify(sol.subs(t, 10)), round(float(sol.subs(t, 10)), 2))t + 10 + 80*exp(-t/10) | 20 + 80*exp(-1) 49.43
Test yourself
The integrating factor for is…
Before computing the integrating factor of you must…
- differentiate both sides
- divide by to get the form
- square both sides
- set
The general solution of a linear equation is…
- one particular solution
- the homogeneous solution only
- the homogeneous solution plus one particular solution
- always a polynomial