TExcelTAD
Home › Calculus Essentials › First-Order Linear Equations

First-Order Linear Equations

A first-order linear equation has the form y′+P(x) y=Q(x)y' + P(x)\,y = Q(x). 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

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

yy and y′y' appear only to the first power and are never multiplied together; PP and QQ may be any functions of xx. If Q=0Q = 0 the equation is called homogeneous and it is also separable.

Examples: y′+2y=4y' + 2y = 4, xy′+y=x3xy' + y = x^3 (divide by xx first: y′+y/x=x2y' + y/x = x^2), y′−ytan⁡x=cos⁡xy' - y\tan x = \cos x.

The integrating factor

Multiply the whole equation by

μ(x)=e∫P(x) dx\mu(x) = e^{\int P(x)\,dx}

Because μ′=Pμ\mu' = P\mu, the product rule shows that the new left side is exactly a derivative:

μy′+μPy=(μy)′(μy)′=μQμy=∫μQ dx+Cy=∫μQ dx+Cμ\begin{aligned} \mu y' + \mu P y &= (\mu y)' \\ (\mu y)' &= \mu Q \\ \mu y &= \int \mu Q\,dx + C \\ y &= \frac{\int \mu Q\,dx + C}{\mu} \end{aligned}

Worked example, y′+y/x=x2y' + y/x = x^2 for x>0x > 0:

P=1/x,μ=eln⁡x=x(xy)′=x⋅x2=x3xy=x44+Cy=x34+Cx\begin{aligned} &P = 1/x, \qquad \mu = e^{\ln x} = x \\ (xy)' &= x \cdot x^2 = x^3 \\ xy &= \frac{x^4}{4} + C \\ y &= \frac{x^3}{4} + \frac{C}{x} \end{aligned}

Check: y′=3x2/4−C/x2y' = 3x^2/4 - C/x^2, and y/x=x2/4+C/x2y/x = x^2/4 + C/x^2, so y′+y/x=x2y' + y/x = x^2 ✓.

SymPy does exactly this (its hint is called 1st_linear):

python · try it yourself
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 P=kP = k and constant Q=kLQ = kL:

y′+ky=kL  ⟺  y′=−k(y−L)solution: y(t)=L+(y0−L)⋅e−kt\begin{aligned} &y' + ky = kL \quad\iff\quad y' = -k(y - L) \\ &\text{solution: } y(t) = L + (y_0 - L) \cdot e^{-kt} \end{aligned}

yy slides exponentially from y0y_0 to the level LL. After t=1/kt = 1/k (the time constant τ\tau) it has covered 1−e−1≈63%1 - e^{-1} \approx 63\% of the way; after 5τ5\tau, 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 S(t)S(t) be the grams of salt in the tank:

rate in=10 g/L⋅4 L/min=40 g/minrate out=(S/200) g/L⋅4 L/min=S/50 g/minS′=40−S/50,S(0)=0\begin{aligned} \text{rate in} &= 10\ \text{g/L} \cdot 4\ \text{L/min} = 40\ \text{g/min} \\ \text{rate out} &= (S/200)\ \text{g/L} \cdot 4\ \text{L/min} = S/50\ \text{g/min} \\ S' &= 40 - S/50, \qquad S(0) = 0 \end{aligned}

That is S′+S/50=40S' + S/50 = 40, with k=1/50k = 1/50 and L=2000L = 2000 g (the salt in a tank full of 10 g/L brine). So S(t)=2000(1−e−t/50)S(t) = 2000(1 - e^{-t/50}). Let's confirm with SymPy and with SciPy's numeric solver:

python · try it yourself
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 t=50t = 50 min, one time constant, the tank holds 63.2% of its final 2000 g.

An RC circuit

A capacitor CC charging through a resistor RR from a battery of voltage V0V_0 obeys RCv′+v=V0RCv' + v = V_0. With τ=RC\tau = RC this is the same equation again: v(t)=V0(1−e−t/τ)v(t) = V_0(1 - e^{-t/\tau}). Engineers say "it takes about 5τ5\tau to charge" for the same reason the tank takes about 250 minutes to fill with salt.

In software

y′=−k(y−L)y' = -k(y - L) 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 y=L+(y−L)e−kΔty = L + (y - L)e^{-k\Delta t} 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 PP or QQ are messy, or the equation is not linear at all, switch to a numerical solver.

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
μ=e3x(e3xy)′=6e3xe3xy=2e3x+Cy=2+Ce−3x;y(0)=2+C=5  ⇒  C=3\begin{aligned} \mu &= e^{3x} \\ (e^{3x} y)' &= 6e^{3x} \\ e^{3x} y &= 2e^{3x} + C \\ y &= 2 + Ce^{-3x}; \qquad y(0) = 2 + C = 5 \;\Rightarrow\; C = 3 \end{aligned}

Equivalently, y′=−3(y−2)y' = -3(y - 2): level L=2L = 2, start 5, so y=2+(5−2)e−3xy = 2 + (5 - 2)e^{-3x}.

Exercise 2: A variable coefficient

Solve y′+2xy=xy' + 2xy = x.

Answer

y=12+Ce−x2y = \frac{1}{2} + Ce^{-x^2}.

Solution
P=2x,μ=ex2(ex2y)′=xex2ex2y=12ex2+C(substitute u=x2)y=12+Ce−x2\begin{aligned} &P = 2x, \qquad \mu = e^{x^2} \\ (e^{x^2} y)' &= xe^{x^2} \\ e^{x^2} y &= \tfrac{1}{2} e^{x^2} + C && \text{(substitute } u = x^2\text{)} \\ y &= \tfrac{1}{2} + Ce^{-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), 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 t=0t = 0, with y′=−8(y−100)y' = -8(y - 100) (tt in seconds). How long until yy reaches 95?

Answer

t=ln⁡(20)/8≈0.374t = \ln(20)/8 \approx 0.374 s.

Solution
y=100−100e−8t95=100−100e−8t  ⇒  e−8t=0.05  ⇒  t=ln⁡20/8≈2.996/8≈0.374 s\begin{aligned} &y = 100 - 100e^{-8t} \\ &95 = 100 - 100e^{-8t} \;\Rightarrow\; e^{-8t} = 0.05 \;\Rightarrow\; t = \ln 20 / 8 \approx 2.996 / 8 \approx 0.374\ \text{s} \end{aligned}

That is about 3 time constants (τ=1/8\tau = 1/8 s), matching the rule "95% after 3τ3\tau".

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
S′=40−S/50,L=2000,S(0)=600S(t)=2000+(600−2000)e−t/50S(30)=2000−1400e−0.6=2000−1400⋅0.5488≈1231.7 g\begin{aligned} &S' = 40 - S/50, \quad L = 2000, \quad S(0) = 600 \\ &S(t) = 2000 + (600 - 2000)e^{-t/50} \\ &S(30) = 2000 - 1400e^{-0.6} = 2000 - 1400 \cdot 0.5488 \approx 1231.7\ \text{g} \end{aligned}
python · try it yourself
import math

print(f"{2000 - 1400 * math.exp(-30 / 50):.1f} g")
1231.7 g

Test yourself

What is the integrating factor for y′+(2/x)y=1y' + (2/x)y = 1 (x>0x > 0)?

  1. e2xe^{2x}
  2. x2x^2
  3. 2/x2/x
  4. ln⁡x2\ln x^2

The solution of y′=−k(y−L)y' = -k(y - L) approaches what as t→∞t \to \infty (k>0k > 0)?

  1. 0
  2. y0y_0
  3. LL
  4. Infinity

After one time constant τ=1/k\tau = 1/k, what fraction of the way from y0y_0 to LL has the solution covered?

  1. 50%
  2. About 63%
  3. About 95%
  4. 100%