Nonhomogeneous Linear Equations
When a second-order linear equation has a forcing term, , 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 and let be any one solution of . Then every solution is
Proof: if is any solution, by linearity, so is a homogeneous solution, hence of the form . Conversely every such sum solves . The same argument gives superposition of forcing: if and , then , 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 have the same shape as . So guess of that shape with unknown coefficients and solve for them.
| Trial | |
|---|---|
| polynomial of degree | |
| or | |
| (or ) |
Modification rule: if any term of the trial already solves the homogeneous equation, multiply the whole trial by (by if that is still the case). This happens exactly when (or ) is a root of the characteristic equation, with multiplicity 1 or 2.
Example: . The characteristic roots are 2 and , and 0 is not a root, so try :
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 . The roots are 1 and , so is already a homogeneous solution and gives , never . Multiply by : with ,
SymPy's dsolve applies the same rule automatically:
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 , or , whose derivatives keep producing new shapes. Variation of parameters works for every continuous . Look for with functions , instead of constants, and impose the extra condition to keep free of terms. Substituting into leaves a linear system whose solution is
Example: on . Here , , , :
The function below does this for any , , . (SymPy writes the natural logarithm as log.)
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 . The complementary part dies out when damping is present (the transient) and is what you see after a while (the steady state).
- Frequency response. For the particular solution is a cosine of the same frequency with a new amplitude and phase. Plotting that amplitude against 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 terms.
Exercises
Exercise 1: Constant forcing
Find the general solution of .
Answer
.
Solution
The trial for a constant is . Then , so and . Add the complementary solution of ().
Exercise 2: A double root resonance
Find a particular solution of .
Answer
.
Solution
The characteristic polynomial has 2 as a double root, so both and are homogeneous solutions. The trial is .
Exercise 3: Pure resonance
Find a particular solution of .
Answer
.
Solution
and solve , so the trial is .
The amplitude 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 on , and check it.
Answer
.
Solution
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 with , , using the particular solution found in the lesson.
Answer
.
Solution
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 ?
Why is a solution of the homogeneous equation whenever and both solve ?
- Because is zero
- Because is linear, so
- Because the Wronskian is non-zero
- Because is unique
Which forcing term cannot be handled by undetermined coefficients?