TExcelTAD
Home › Calculus in Depth › Nonhomogeneous Linear Equations

Nonhomogeneous Linear Equations

When a second-order linear equation has a forcing term, ay′′+by′+cy=G(x)ay'' + by' + cy = G(x), its solutions are the homogeneous solutions shifted by one particular solution. This lesson proves that structure, then gives the two standard ways to find the particular solution: the method of undetermined coefficients (fast, for exponential, polynomial and sinusoidal forcing) and variation of parameters (general, at the price of integrals). Both are carried out in SymPy.

It continues Second-Order Linear Equations; the survey version is Second-order equations.

The structure theorem

Let L[y]=ay′′+by′+cyL[y] = ay'' + by' + cy and let ypy_p be any one solution of L[y]=GL[y] = G. Then every solution is

y=yc+yp,yc=c1y1+c2y2(the complementary solution, L[yc]=0)y = y_c + y_p, \qquad y_c = c_1y_1 + c_2y_2 \quad (\text{the complementary solution, } L[y_c] = 0)

Proof: if yy is any solution, L[y−yp]=L[y]−L[yp]=G−G=0L[y - y_p] = L[y] - L[y_p] = G - G = 0 by linearity, so y−ypy - y_p is a homogeneous solution, hence of the form c1y1+c2y2c_1y_1 + c_2y_2. Conversely every such sum solves L[y]=GL[y] = G. The same argument gives superposition of forcing: if L[yA]=GAL[y_A] = G_A and L[yB]=GBL[y_B] = G_B, then L[yA+yB]=GA+GBL[y_A + y_B] = G_A + G_B, so a sum of forcing terms can be handled one term at a time.

Undetermined coefficients

For forcing terms built from polynomials, exponentials, sines and cosines, the derivatives of GG have the same shape as GG. So guess ypy_p of that shape with unknown coefficients and solve for them.

G(x)G(x) Trial ypy_p
polynomial of degree nn Anxn+⋯+A1x+A0A_nx^n + \cdots + A_1x + A_0
ekxe^{kx} AekxAe^{kx}
cos⁡kx\cos kx or sin⁡kx\sin kx Acos⁡kx+Bsin⁡kxA\cos kx + B\sin kx
ekxPn(x)cos⁡mxe^{kx}P_n(x)\cos mx (or sin⁡mx\sin mx) ekx[Qn(x)cos⁡mx+Rn(x)sin⁡mx]e^{kx}[Q_n(x)\cos mx + R_n(x)\sin mx]

Modification rule: if any term of the trial already solves the homogeneous equation, multiply the whole trial by xx (by x2x^2 if that is still the case). This happens exactly when kk (or k±mik \pm mi) is a root of the characteristic equation, with multiplicity 1 or 2.

Example: y′′−y′−2y=4x2y'' - y' - 2y = 4x^2. The characteristic roots are 2 and −1-1, and 0 is not a root, so try yp=Ax2+Bx+Cy_p = Ax^2 + Bx + C:

yp′′−yp′−2yp=2A−(2Ax+B)−2(Ax2+Bx+C)=−2A x2+(−2A−2B) x+(2A−B−2C)match with 4x2:−2A=4,−2A−2B=0,2A−B−2C=0A=−2,B=2,C=−3\begin{aligned} y_p'' - y_p' - 2y_p &= 2A - (2Ax + B) - 2(Ax^2 + Bx + C) \\ &= -2A\,x^2 + (-2A - 2B)\,x + (2A - B - 2C) \\[1ex] \text{match with } 4x^2\text{:} \quad &-2A = 4, \quad -2A - 2B = 0, \quad 2A - B - 2C = 0 \\ &A = -2, \quad B = 2, \quad C = -3 \end{aligned}
python · try it yourself
import sympy as sp

x, A, B, C = sp.symbols('x A B C')
L = lambda f: f.diff(x, 2) - f.diff(x) - 2*f
trial = A*x**2 + B*x + C
residual = sp.expand(L(trial) - 4*x**2)
equations = sp.Poly(residual, x).coeffs()          # every coefficient must vanish
coeffs = sp.solve(equations, [A, B, C])
print("coefficients:", coeffs)
yp = trial.subs(coeffs)
print("y_p =", yp, "   check L[y_p] =", sp.expand(L(yp)))
coefficients: {A: -2, B: 2, C: -3}
y_p = -2*x**2 + 2*x - 3    check L[y_p] = 4*x**2

Resonance: when the guess fails

Take y′′+y′−2y=3exy'' + y' - 2y = 3e^x. The roots are 1 and −2-2, so exe^x is already a homogeneous solution and AexAe^x gives L[Aex]=0L[Ae^x] = 0, never 3ex3e^x. Multiply by xx: with yp=Axexy_p = Axe^x,

yp′=A(1+x)ex,yp′′=A(2+x)exyp′′+yp′−2yp=A[(2+x)+(1+x)−2x]ex=3Aex  →  A=1,yp=xex\begin{aligned} &y_p' = A(1 + x)e^x, \quad y_p'' = A(2 + x)e^x \\ &y_p'' + y_p' - 2y_p = A[(2 + x) + (1 + x) - 2x]e^x = 3Ae^x \;\to\; A = 1, \quad y_p = xe^x \end{aligned}

SymPy's dsolve applies the same rule automatically:

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).diff(x) - 2*y(x), 3*sp.exp(x))
print(sp.dsolve(ode))
print(sp.dsolve(ode, hint='nth_linear_constant_coeff_undetermined_coefficients').rhs.expand())
Eq(y(x), C2*exp(-2*x) + (C1 + x)*exp(x))
C1*exp(x) + C2*exp(-2*x) + x*exp(x)

Variation of parameters

Undetermined coefficients cannot handle G=tan⁡xG = \tan x, 1/x1/x or csc⁡2x\csc 2x, whose derivatives keep producing new shapes. Variation of parameters works for every continuous GG. Look for yp=u1y1+u2y2y_p = u_1y_1 + u_2y_2 with functions u1u_1, u2u_2 instead of constants, and impose the extra condition u1′y1+u2′y2=0u_1'y_1 + u_2'y_2 = 0 to keep yp′y_p' free of u′u' terms. Substituting into ay′′+by′+cy=Gay'' + by' + cy = G leaves a linear system whose solution is

u1′=−y2GaW,u2′=y1GaW,W=y1y2′−y2y1′u_1' = -\frac{y_2G}{aW}, \qquad u_2' = \frac{y_1G}{aW}, \qquad W = y_1y_2' - y_2y_1'

Example: y′′+4y=csc⁡2xy'' + 4y = \csc 2x on 0<x<π/20 < x < \pi/2. Here y1=cos⁡2xy_1 = \cos 2x, y2=sin⁡2xy_2 = \sin 2x, W=2W = 2, a=1a = 1:

u1′=−sin⁡2x⋅csc⁡2x/2=−1/2→ u1=−x/2u2′=cos⁡2x⋅csc⁡2x/2=cot⁡2x/2→ u2=14ln⁡(sin⁡2x)yp=−(x/2)cos⁡2x+14sin⁡2x ln⁡(sin⁡2x)\begin{aligned} u_1' &= -\sin 2x \cdot \csc 2x / 2 = -1/2 && \to\ u_1 = -x/2 \\ u_2' &= \cos 2x \cdot \csc 2x / 2 = \cot 2x / 2 && \to\ u_2 = \tfrac{1}{4}\ln(\sin 2x) \\ y_p &= -(x/2)\cos 2x + \tfrac{1}{4}\sin 2x\,\ln(\sin 2x) \end{aligned}

The function below does this for any y1y_1, y2y_2, GG. (SymPy writes the natural logarithm ln⁡\ln as log.)

python · try it yourself
import sympy as sp

x = sp.symbols('x', positive=True)

def variation_of_parameters(y1, y2, G, a=1):
    W = sp.simplify(y1*y2.diff(x) - y2*y1.diff(x))
    u1 = sp.integrate(sp.simplify(-y2*G/(a*W)), x)
    u2 = sp.integrate(sp.simplify(y1*G/(a*W)), x)
    return sp.simplify(u1*y1 + u2*y2), W

yp, W = variation_of_parameters(sp.cos(2*x), sp.sin(2*x), 1/sp.sin(2*x))
print("W   =", W)
print("y_p =", yp)
print("check y'' + 4y - csc 2x =", sp.simplify(yp.diff(x, 2) + 4*yp - 1/sp.sin(2*x)))
W   = 2
y_p = -x*cos(2*x)/2 + log(sin(2*x))*sin(2*x)/4
check y'' + 4y - csc 2x = 0

Tip: Use undetermined coefficients whenever the table applies: it needs only algebra. Variation of parameters always works but needs two integrals, which may not have closed forms (then integrate numerically).

In software

  • Forced systems. A game engine's spring attached to a moving target, a vehicle on a bumpy road, an audio oscillator driven by an input signal: all are L[y]=G(t)L[y] = G(t). The complementary part ycy_c dies out when damping is present (the transient) and ypy_p is what you see after a while (the steady state).
  • Frequency response. For G=cos⁡ωtG = \cos\omega t the particular solution is a cosine of the same frequency with a new amplitude and phase. Plotting that amplitude against ω\omega is a Bode plot, the standard way audio and control engineers describe a filter.
  • Computer algebra. SymPy, Mathematica and Maple choose between these methods with "hints"; knowing the methods tells you why a CAS returns an answer with unexpected integrals or xexxe^x terms.

Exercises

Exercise 1: Constant forcing

Find the general solution of y′′+4y=8y'' + 4y = 8.

Answer

y=c1cos⁡2x+c2sin⁡2x+2y = c_1\cos 2x + c_2\sin 2x + 2.

Solution

The trial for a constant is yp=Ay_p = A. Then yp′′=0y_p'' = 0, so 4A=84A = 8 and A=2A = 2. Add the complementary solution of r2+4=0r^2 + 4 = 0 (r=±2ir = \pm 2i).

Exercise 2: A double root resonance

Find a particular solution of y′′−4y′+4y=e2xy'' - 4y' + 4y = e^{2x}.

Answer

yp=12x2e2xy_p = \frac{1}{2}x^2e^{2x}.

Solution

The characteristic polynomial (r−2)2(r - 2)^2 has 2 as a double root, so both e2xe^{2x} and xe2xxe^{2x} are homogeneous solutions. The trial is Ax2e2xAx^2e^{2x}.

yp=Ax2e2xyp′=A(2x+2x2)e2xyp′′=A(2+8x+4x2)e2xyp′′−4yp′+4yp=A[(2+8x+4x2)−(8x+8x2)+4x2]e2x=2Ae2x2A=1  →  A=12\begin{aligned} y_p &= Ax^2e^{2x} \\ y_p' &= A(2x + 2x^2)e^{2x} \\ y_p'' &= A(2 + 8x + 4x^2)e^{2x} \\ y_p'' - 4y_p' + 4y_p &= A[(2 + 8x + 4x^2) - (8x + 8x^2) + 4x^2]e^{2x} = 2Ae^{2x} \\ 2A &= 1 \;\to\; A = \tfrac{1}{2} \end{aligned}

Exercise 3: Pure resonance

Find a particular solution of y′′+y=2cos⁡xy'' + y = 2\cos x.

Answer

yp=xsin⁡xy_p = x\sin x.

Solution

cos⁡x\cos x and sin⁡x\sin x solve y′′+y=0y'' + y = 0, so the trial is x(Acos⁡x+Bsin⁡x)x(A\cos x + B\sin x).

yp=x(Acos⁡x+Bsin⁡x)yp′′=2(−Asin⁡x+Bcos⁡x)−x(Acos⁡x+Bsin⁡x)yp′′+yp=−2Asin⁡x+2Bcos⁡x=2cos⁡x  →  A=0, B=1\begin{aligned} y_p &= x(A\cos x + B\sin x) \\ y_p'' &= 2(-A\sin x + B\cos x) - x(A\cos x + B\sin x) \\ y_p'' + y_p &= -2A\sin x + 2B\cos x = 2\cos x \;\to\; A = 0,\ B = 1 \end{aligned}

The amplitude xx grows without bound: forcing at the natural frequency of an undamped oscillator makes it resonate.

Exercise 4: Variation of parameters (code)

Use the variation_of_parameters function from the lesson to find a particular solution of y′′+y=sec⁡xy'' + y = \sec x on −π/2<x<π/2-\pi/2 < x < \pi/2, and check it.

Answer

yp=xsin⁡x+cos⁡x ln⁡(cos⁡x)y_p = x\sin x + \cos x\,\ln(\cos x).

Solution
y1=cos⁡x, y2=sin⁡x, W=1u1′=−sin⁡x⋅sec⁡x=−tan⁡x→ u1=ln⁡(cos⁡x)u2′=cos⁡x⋅sec⁡x=1→ u2=xyp=cos⁡x⋅ln⁡(cos⁡x)+x⋅sin⁡x\begin{aligned} &y_1 = \cos x,\ y_2 = \sin x,\ W = 1 \\ u_1' &= -\sin x \cdot \sec x = -\tan x && \to\ u_1 = \ln(\cos x) \\ u_2' &= \cos x \cdot \sec x = 1 && \to\ u_2 = x \\ y_p &= \cos x \cdot \ln(\cos x) + x \cdot \sin x \end{aligned}
python · try it yourself
import sympy as sp

x = sp.symbols('x')
y1, y2, G = sp.cos(x), sp.sin(x), 1/sp.cos(x)
W = sp.simplify(y1*y2.diff(x) - y2*y1.diff(x))
u1 = sp.integrate(-y2*G/W, x)
u2 = sp.integrate(y1*G/W, x)
yp = u1*y1 + u2*y2
print("u1 =", u1, "  u2 =", u2)
print("check:", sp.simplify(yp.diff(x, 2) + yp - G))
u1 = log(cos(x))   u2 = x
check: 0

Exercise 5: Initial values

Solve y′′−y′−2y=4x2y'' - y' - 2y = 4x^2 with y(0)=0y(0) = 0, y′(0)=0y'(0) = 0, using the particular solution found in the lesson.

Answer

y=13e2x+83e−x−2x2+2x−3y = \frac{1}{3}e^{2x} + \frac{8}{3}e^{-x} - 2x^2 + 2x - 3.

Solution
y=c1e2x+c2e−x−2x2+2x−3y(0)=c1+c2−3=0y′(0)=2c1−c2+2=0add: 3c1−1=0  →  c1=1/3,c2=8/3\begin{aligned} y &= c_1e^{2x} + c_2e^{-x} - 2x^2 + 2x - 3 \\ y(0) &= c_1 + c_2 - 3 = 0 \\ y'(0) &= 2c_1 - c_2 + 2 = 0 \\ \text{add: } 3c_1 - 1 &= 0 \;\to\; c_1 = 1/3, \quad c_2 = 8/3 \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).diff(x) - 2*y(x), 4*x**2)
print(sp.dsolve(ode, ics={y(0): 0, y(x).diff(x).subs(x, 0): 0}))
Eq(y(x), -2*x**2 + 2*x + exp(2*x)/3 - 3 + 8*exp(-x)/3)

Test yourself

What trial solution should you use for y′′−3y′+2y=5exy'' - 3y' + 2y = 5e^x?

  1. AexAe^x
  2. AxexAxe^x
  3. Ax2exAx^2e^x
  4. Ae2xAe^{2x}

Why is y−ypy - y_p a solution of the homogeneous equation whenever yy and ypy_p both solve L[y]=GL[y] = G?

  1. Because GG is zero
  2. Because LL is linear, so L[y−yp]=G−G=0L[y - y_p] = G - G = 0
  3. Because the Wronskian is non-zero
  4. Because ypy_p is unique

Which forcing term cannot be handled by undetermined coefficients?

  1. x3x^3
  2. e−2xsin⁡3xe^{-2x}\sin 3x
  3. cos⁡5x\cos 5x
  4. ln⁡x\ln x