TExcelTAD
Home › Differential Equations › Linear Equations and the Integrating Factor

Linear Equations and the Integrating Factor

A first-order linear equation has the form

y′+p(x) y=q(x).y' + p(x)\,y = q(x).

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 r(x)=e∫p(x) dxr(x) = e^{\int p(x)\,dx}. Because r′=p rr' = p\,r, the left side becomes an exact derivative by the product rule:

ry′+rpy=(ry)′.r y' + r p y = (r y)'.

So the equation turns into (ry)′=rq(ry)' = rq, and one integration solves it:

y(x)=1r(x)(∫r(x) q(x) dx+C),r(x)=e∫p(x) dx.y(x) = \frac{1}{r(x)}\left(\int r(x)\,q(x)\,dx + C\right), \qquad r(x) = e^{\int p(x)\,dx}.

Any antiderivative of pp works for rr (the constant cancels), and with an initial condition you can use definite integrals from x0x_0.

Example

Solve y′+2y=e−xy' + 2y = e^{-x}, y(0)=3y(0) = 3.

r=e2x(e2xy)′=e2xe−x=exe2xy=ex+Cy=e−x+Ce−2x,y(0)=1+C=3  ⇒  C=2.\begin{aligned} r &= e^{2x} \\ (e^{2x}y)' &= e^{2x}e^{-x} = e^{x} \\ e^{2x}y &= e^{x} + C \\ y &= e^{-x} + Ce^{-2x}, \qquad y(0) = 1 + C = 3 \;\Rightarrow\; C = 2. \end{aligned}
python · try it yourself
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 (1+2e−x)e−x(1 + 2e^{-x})e^{-x}, which expands to e−x+2e−2xe^{-x} + 2e^{-2x}.

Structure: homogeneous plus particular

The answer y=e−x+2e−2xy = e^{-x} + 2e^{-2x} has two parts. The term Ce−2xCe^{-2x} solves the homogeneous equation y′+2y=0y' + 2y = 0; the term e−xe^{-x} 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 xy′+2y=4x2x y' + 2y = 4x^2 for x>0x > 0, y(1)=2y(1) = 2. First divide by xx to get the standard form y′+2xy=4xy' + \frac{2}{x}y = 4x. Then r=e2ln⁡x=x2r = e^{2\ln x} = x^2 and (x2y)′=4x3(x^2y)' = 4x^3, so x2y=x4+Cx^2y = x^4 + C, y=x2+Cx−2y = x^2 + Cx^{-2}, and y(1)=1+C=2y(1) = 1 + C = 2 gives C=1C = 1.

python · try it yourself
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: p(x)=2/xp(x) = 2/x is undefined at x=0x = 0, so the solution is only guaranteed on an interval that avoids 0. Indeed x−2x^{-2} blows up there.

In software

  • A first-order low-pass filter (smoothing a sensor signal, an audio "one-pole" filter) is the linear ODE τy′+y=u(t)\tau y' + y = u(t) with input uu. Its discrete version y += (u - y) * dt / tau is 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 y′+3y=6y' + 3y = 6, y(0)=0y(0) = 0.

Answer

y=2−2e−3xy = 2 - 2e^{-3x}.

Solution

r=e3xr = e^{3x}, (e3xy)′=6e3x(e^{3x}y)' = 6e^{3x}, e3xy=2e3x+Ce^{3x}y = 2e^{3x} + C, so y=2+Ce−3xy = 2 + Ce^{-3x}; y(0)=2+C=0y(0) = 2 + C = 0 gives C=−2C = -2. The solution rises to the equilibrium y=2y = 2.

Exercise 2: Find the integrating factor

What is the integrating factor of y′+tan⁡(x) y=cos⁡xy' + \tan(x)\,y = \cos x on (−π/2,π/2)(-\pi/2, \pi/2), and what is the general solution?

Answer

r=sec⁡xr = \sec x; y=(x+C)cos⁡xy = (x + C)\cos x.

Solution

∫tan⁡x dx=−ln⁡cos⁡x\int \tan x\,dx = -\ln\cos x, so r=e−ln⁡cos⁡x=1cos⁡xr = e^{-\ln\cos x} = \frac{1}{\cos x}. Then (ycos⁡x)′=cos⁡xcos⁡x=1\left(\frac{y}{\cos x}\right)' = \frac{\cos x}{\cos x} = 1, so ycos⁡x=x+C\frac{y}{\cos x} = x + C.

Exercise 3: A cooling cup in a warming room (code)

Coffee at 90 °C sits in a room whose temperature is 20+t20 + t (°C, tt in minutes). With Newton's law T′=−0.1(T−20−t)T' = -0.1(T - 20 - t), find T(10)T(10) with SymPy.

Answer

T(10)=80e−1+20≈49.43T(10) = 80e^{-1} + 20 \approx 49.43 °C.

Solution

The equation is T′+0.1T=2+0.1tT' + 0.1T = 2 + 0.1t, linear. SymPy gives T=t+10+80e−t/10T = t + 10 + 80e^{-t/10}, so T(10)=20+80e−1T(10) = 20 + 80e^{-1}.

python · try it yourself
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 y′+p(x)y=q(x)y' + p(x)y = q(x) is…

  1. e∫q dxe^{\int q\,dx}
  2. e∫p dxe^{\int p\,dx}
  3. p(x)p(x)
  4. 1/p(x)1/p(x)

Before computing the integrating factor of xy′+2y=4x2x y' + 2y = 4x^2 you must…

  1. differentiate both sides
  2. divide by xx to get the form y′+p(x)y=q(x)y' + p(x)y = q(x)
  3. square both sides
  4. set x=1x = 1

The general solution of a linear equation is…

  1. one particular solution
  2. the homogeneous solution only
  3. the homogeneous solution plus one particular solution
  4. always a polynomial