TExcelTAD
Home › Calculus in Depth › Linear Equations

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

y′+P(x)⋅y=Q(x)y' + P(x) \cdot y = Q(x)

with PP and QQ continuous on an interval II. "Linear" refers to yy and y′y': they appear to the first power, never multiplied together or inside other functions. x⋅y′+2y=x3x \cdot y' + 2y = x^3 is linear (divide by xx); y′=y2y' = y^2 and y′+sin⁡y=0y' + \sin y = 0 are not. When Q=0Q = 0 the equation is homogeneous.

The integrating factor

Multiply the equation by I(x)=e∫P(x) dxI(x) = e^{\int P(x)\,dx}. Because I′=P⋅II' = P \cdot I, the product rule gives

(I⋅y)′=I⋅y′+I′⋅y=I⋅y′+P⋅I⋅y=I⋅(y′+P⋅y)=I⋅Q(I \cdot y)' = I \cdot y' + I' \cdot y = I \cdot y' + P \cdot I \cdot y = I \cdot (y' + P \cdot y) = I \cdot Q

so integrating both sides,

I(x)⋅y=∫I(x)⋅Q(x) dx+Cy=1I(x)⋅(∫I⋅Q dx+C)I(x) \cdot y = \int I(x) \cdot Q(x)\,dx + C \qquad y = \frac{1}{I(x)} \cdot \left(\int I \cdot Q\,dx + C\right)

Theorem (existence and uniqueness). If PP and QQ are continuous on an open interval II containing x0x_0, then for every y0y_0 the problem y′+Py=Qy' + Py = Q, y(x0)=y0y(x_0) = y_0 has exactly one solution, and it is defined on all of II.

Proof sketch. Existence: the formula above with definite integrals from x0x_0, y=(y0+∫x0xI⋅Q dt)/I(x)y = \left(y_0 + \int_{x_0}^{x} I \cdot Q\,dt\right)/I(x) with I(x)=e∫x0xPI(x) = e^{\int_{x_0}^{x} P}, is differentiable on II and satisfies both conditions. Uniqueness: if y1y_1 and y2y_2 are solutions, w=y1−y2w = y_1 - y_2 solves w′+Pw=0w' + Pw = 0, w(x0)=0w(x_0) = 0, so (I⋅w)′=0(I \cdot w)' = 0 and I⋅wI \cdot w is constant, equal to I(x0)⋅0=0I(x_0) \cdot 0 = 0. Since I>0I > 0, w≡0w \equiv 0. ∎

Contrast this with nonlinear equations such as y′=xy2y' = xy^2, 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 general solution=particular solution+C⋅(homogeneous solution)\text{general solution} = \text{particular solution} + C \cdot (\text{homogeneous solution}), where C/I(x)=C⋅e−∫PC/I(x) = C \cdot e^{-\int P} solves y′+Py=0y' + Py = 0. This superposition principle is what makes linear equations, linear algebra and linear systems so tractable.

python · try it yourself
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 RR (ohms), inductance LL (henries) and a voltage source E(t)E(t) obeys Kirchhoff's law L⋅dIdt+R⋅I=E(t)L \cdot \frac{dI}{dt} + R \cdot I = E(t). Take L=2L = 2, R=8R = 8 and a switch closed at t=0t = 0 onto a sinusoidal source E=20sin⁡(3t)E = 20 \sin(3t), with I(0)=0I(0) = 0:

I′+4I=10sin⁡(3t),integrating factor e4tI' + 4I = 10 \sin(3t), \qquad \text{integrating factor } e^{4t}

The solution splits into a transient term proportional to e−4te^{-4t} that dies off and a steady-state sinusoid with the source's frequency, smaller amplitude and a phase lag.

python · try it yourself
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 10/42+32=210/\sqrt{4^2 + 3^2} = 2: the circuit attenuates the input, and the attenuation grows with frequency. That is a low-pass filter.

Bernoulli equations

y′+P(x)y=Q(x)yny' + P(x)y = Q(x)y^n (n≠0,1n \ne 0, 1) is nonlinear, but the substitution u=y1−nu = y^{1-n} makes it linear: u′=(1−n)y−ny′u' = (1 - n)y^{-n}y', so dividing the equation by yny^n gives u′/(1−n)+P⋅u=Qu'/(1 - n) + P \cdot u = Q.

In software: exponential smoothing

The equation y′=(u(t)−y)/τy' = (u(t) - y)/\tau says "move yy toward the target uu at a rate proportional to the gap". It is linear with P=1/τP = 1/\tau. For a constant target, y=u+(y0−u)e−t/τy = u + (y_0 - u)e^{-t/\tau}. Programmers use its exact discrete update everywhere:

y←y+(u−y)⋅(1−e−Δt/τ)y \leftarrow y + (u - y) \cdot \left(1 - e^{-\Delta t/\tau}\right)
  • 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.
python · try it yourself
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 y′+3y=6y' + 3y = 6 with y(0)=5y(0) = 5.

Answer

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

Solution
I=e3x:(e3xy)′=6e3x⇒e3xy=2e3x+C⇒y=2+Ce−3xy(0)=2+C=5⇒C=3\begin{aligned} &I = e^{3x}{:}\quad (e^{3x}y)' = 6e^{3x} \quad \Rightarrow \quad e^{3x}y = 2e^{3x} + C \quad \Rightarrow \quad y = 2 + Ce^{-3x} \\ &y(0) = 2 + C = 5 \quad \Rightarrow \quad C = 3 \end{aligned}

Exercise 2: Variable coefficient

Solve y′+2x⋅y=4xy' + 2x \cdot y = 4x with y(0)=0y(0) = 0.

Answer

y=2−2e−x2y = 2 - 2e^{-x^2}

Solution
I=ex2:(ex2y)′=4x⋅ex2⇒ex2y=2ex2+Cy(0)=2+C=0⇒C=−2⇒y=2−2e−x2\begin{aligned} &I = e^{x^2}{:}\quad (e^{x^2}y)' = 4x \cdot e^{x^2} \quad \Rightarrow \quad e^{x^2}y = 2e^{x^2} + C \\ &y(0) = 2 + C = 0 \quad \Rightarrow \quad C = -2 \quad \Rightarrow \quad y = 2 - 2e^{-x^2} \end{aligned}
python · try it yourself
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 y′+y=y2y' + y = y^2 with y(0)=1/2y(0) = 1/2.

Answer

y=1/(1+ex)y = 1/(1 + e^x)

Solution
n=2, u=y−1:u′=−y−2y′.Divide by y2: y−2y′+y−1=1⇒−u′+u=1⇒u′−u=−1I=e−x:(e−xu)′=−e−x⇒e−xu=e−x+C⇒u=1+Cexu(0)=2⇒C=1⇒y=1/(1+ex)\begin{aligned} &n = 2,\ u = y^{-1}{:}\quad u' = -y^{-2}y'. \quad \text{Divide by } y^2{:}\ y^{-2}y' + y^{-1} = 1 \quad \Rightarrow \quad -u' + u = 1 \quad \Rightarrow \quad u' - u = -1 \\ &I = e^{-x}{:}\quad (e^{-x}u)' = -e^{-x} \quad \Rightarrow \quad e^{-x}u = e^{-x} + C \quad \Rightarrow \quad u = 1 + Ce^x \\ &u(0) = 2 \quad \Rightarrow \quad C = 1 \quad \Rightarrow \quad y = 1/(1 + e^x) \end{aligned}

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 9.5769.576 at 30 fps but ≈10.0000\approx 10.0000 at 144 fps, so the motion speed depends on the frame rate. The exact update gives 9.8168449.816844 at both.

Solution

The naive factor 0.1 per frame equals 1−e−Δt/τ1 - e^{-\Delta t/\tau} only for one particular Δt\Delta t. After nn frames the gap is 10⋅0.9n10 \cdot 0.9^n: 0.930≈0.0420.9^{30} \approx 0.042 and 0.9144≈2.6⋅10−70.9^{144} \approx 2.6 \cdot 10^{-7}.

python · try it yourself
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 y′+(1/x)⋅y=xy' + (1/x) \cdot y = x (x>0x > 0)?

  1. exe^x
  2. xx
  3. 1/x1/x
  4. ln⁡x\ln x

Where is the solution of y′+P(x)y=Q(x)y' + P(x)y = Q(x), y(x0)=y0y(x_0) = y_0, guaranteed to exist?

  1. Only near x0x_0
  2. On the whole interval containing x0x_0 where PP and QQ are continuous
  3. Only for x>0x > 0
  4. Everywhere on R\mathbb{R}

Which equation is linear in yy?

  1. y′=y2y' = y^2
  2. y′+ex⋅y=cos⁡xy' + e^x \cdot y = \cos x
  3. y⋅y′=xy \cdot y' = x
  4. y′=sin⁡yy' = \sin y