TExcelTAD
Home › Differential Equations › Variation of Parameters

Variation of Parameters

Undetermined coefficients only works for special right-hand sides. Variation of parameters finds a particular solution for any continuous forcing ff, and even for variable coefficients, as long as you know the homogeneous solutions. The price is two integrals that may be hard.

The idea

For y′′+p(x)y′+q(x)y=f(x)y'' + p(x)y' + q(x)y = f(x) with homogeneous solutions y1y_1, y2y_2, replace the constants in c1y1+c2y2c_1y_1 + c_2y_2 by functions:

yp=u1(x) y1(x)+u2(x) y2(x).y_p = u_1(x)\,y_1(x) + u_2(x)\,y_2(x).

Two unknown functions but one equation leave freedom for an extra condition. Choosing u1′y1+u2′y2=0u_1'y_1 + u_2'y_2 = 0 keeps yp′y_p' free of u′′u'' terms, and substituting into the equation gives the linear system

[y1y2y1′y2′][u1′u2′]=[0f],\begin{bmatrix} y_1 & y_2 \\ y_1' & y_2' \end{bmatrix} \begin{bmatrix} u_1' \\ u_2' \end{bmatrix} = \begin{bmatrix} 0 \\ f \end{bmatrix},

whose matrix is the Wronskian matrix. By Cramer's rule, with W=y1y2′−y1′y2W = y_1y_2' - y_1'y_2:

u1=−∫y2fW dx,u2=∫y1fW dx.u_1 = -\int \frac{y_2 f}{W}\,dx, \qquad u_2 = \int \frac{y_1 f}{W}\,dx.

The equation must be in standard form (coefficient of y′′y'' equal to 1) for ff to be the right function here.

Example: y′′+y=tan⁡xy'' + y = \tan x

On (−π/2,π/2)(-\pi/2, \pi/2): y1=cos⁡xy_1 = \cos x, y2=sin⁡xy_2 = \sin x, W=1W = 1.

u1=−∫sin⁡xtan⁡x dx=sin⁡x−ln⁡∣sec⁡x+tan⁡x∣u2=∫cos⁡xtan⁡x dx=−cos⁡xyp=u1cos⁡x+u2sin⁡x=−cos⁡x ln⁡∣sec⁡x+tan⁡x∣.\begin{aligned} u_1 &= -\int \sin x\tan x\,dx = \sin x - \ln\lvert\sec x + \tan x\rvert \\ u_2 &= \int \cos x\tan x\,dx = -\cos x \\ y_p &= u_1\cos x + u_2\sin x = -\cos x\,\ln\lvert\sec x + \tan x\rvert. \end{aligned}
python · try it yourself
import sympy as sp

x = sp.symbols("x")
y1, y2, f = sp.cos(x), sp.sin(x), sp.tan(x)
W = sp.simplify(sp.wronskian([y1, y2], x))
u1 = sp.integrate(-y2 * f / W, x)
u2 = sp.integrate(y1 * f / W, x)
yp = sp.simplify(u1 * y1 + u2 * y2)
print("W =", W)
print("y_p =", yp)
print("check:", sp.simplify(yp.diff(x, 2) + yp - f))
W = 1
y_p = (log(sin(x) - 1) - log(sin(x) + 1))*cos(x)/2
check: 0

SymPy's antiderivative of sec⁡x\sec x may look different from ln⁡∣sec⁡x+tan⁡x∣\ln\lvert\sec x + \tan x\rvert, but the check confirms it.

A formula with a definite integral: Green's function

Using definite integrals from x0x_0 gives the particular solution with yp(x0)=yp′(x0)=0y_p(x_0) = y_p'(x_0) = 0:

yp(x)=∫x0xy1(s) y2(x)−y1(x) y2(s)W(s) f(s) ds.y_p(x) = \int_{x_0}^{x} \frac{y_1(s)\,y_2(x) - y_1(x)\,y_2(s)}{W(s)}\,f(s)\,ds.

For y′′+ω2y=fy'' + \omega^2y = f the kernel is sin⁡ω(x−s)ω\frac{\sin\omega(x - s)}{\omega}: the response at time xx to a kick at time ss. This is the convolution form you will meet again with the Laplace transform. It also works numerically for any ff, even measured data:

python · try it yourself
import numpy as np
from scipy.integrate import quad, solve_ivp

w = 2.0
f = lambda s: np.exp(-s) * np.sqrt(1 + s)          # a forcing with no nice guess
yp = lambda x: quad(lambda s: np.sin(w * (x - s)) / w * f(s), 0, x)[0]

sol = solve_ivp(lambda t, Y: [Y[1], f(t) - w**2 * Y[0]], (0, 3), [0.0, 0.0],
                t_eval=[1.0, 2.0, 3.0], rtol=1e-10, atol=1e-12)
for x, y in zip(sol.t, sol.y[0]):
    print(f"x = {x}: Green's formula {yp(x):.8f}, ODE solver {y:.8f}")
x = 1.0: Green's formula 0.28467486, ODE solver 0.28467486
x = 2.0: Green's formula 0.14711448, ODE solver 0.14711448
x = 3.0: Green's formula -0.23016075, ODE solver -0.23016075

Comparing the two methods

Undetermined coefficients Variation of parameters
Coefficients constant only any, if y1y_1, y2y_2 are known
Forcing polynomials, exponentials, sines and products any continuous ff
Work linear algebra two integrals (possibly hard)

In software

  • The Green's-function formula is how linear systems are simulated with measured inputs: the impulse response is convolved with the input signal. Audio reverb ("convolution reverb") does exactly this with a recorded impulse response of a room.
  • scipy.signal.lsim simulates a linear system's response to an arbitrary input in the same spirit.

Exercises

Exercise 1: Wronskian first

For y′′−y=f(x)y'' - y = f(x) with y1=exy_1 = e^x, y2=e−xy_2 = e^{-x}, compute WW and the formulas for u1′u_1' and u2′u_2'.

Answer

W=−2W = -2; u1′=12e−xfu_1' = \frac{1}{2}e^{-x}f, u2′=−12exfu_2' = -\frac{1}{2}e^{x}f.

Solution

W=ex(−e−x)−exe−x=−2W = e^x(-e^{-x}) - e^xe^{-x} = -2. u1′=−y2fW=e−xf2u_1' = -\frac{y_2f}{W} = \frac{e^{-x}f}{2} and u2′=y1fW=−exf2u_2' = \frac{y_1f}{W} = -\frac{e^xf}{2}.

Exercise 2: Check against undetermined coefficients

Use variation of parameters for y′′−y=e2xy'' - y = e^{2x} and compare with the guess Ae2xAe^{2x}.

Answer

yp=13e2xy_p = \frac{1}{3}e^{2x} either way.

Solution

u1=∫12ex dx=12exu_1 = \int \frac{1}{2}e^{x}\,dx = \frac{1}{2}e^x, u2=−∫12e3x dx=−16e3xu_2 = -\int \frac{1}{2}e^{3x}\,dx = -\frac{1}{6}e^{3x}. Then yp=12e2x−16e2x=13e2xy_p = \frac{1}{2}e^{2x} - \frac{1}{6}e^{2x} = \frac{1}{3}e^{2x}. The guess gives 4A−A=14A - A = 1, the same.

Exercise 3: Secant forcing (code)

Find a particular solution of y′′+y=sec⁡xy'' + y = \sec x on (−π/2,π/2)(-\pi/2, \pi/2).

Answer

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

Solution

u1=−∫sin⁡xsec⁡x dx=ln⁡cos⁡xu_1 = -\int \sin x\sec x\,dx = \ln\cos x and u2=∫1 dx=xu_2 = \int 1\,dx = x.

python · try it yourself
import sympy as sp
x = sp.symbols("x")
yp = x * sp.sin(x) + sp.cos(x) * sp.log(sp.cos(x))
print(sp.simplify(yp.diff(x, 2) + yp - 1 / sp.cos(x)))
0

Test yourself

Variation of parameters looks for ypy_p of the form…

  1. AekxAe^{kx}
  2. u1(x)y1+u2(x)y2u_1(x)y_1 + u_2(x)y_2 with unknown functions u1,u2u_1, u_2
  3. a polynomial
  4. c1y1+c2y2c_1y_1 + c_2y_2 with constants

Before applying the formulas to x2y′′+xy′−y=xx^2y'' + xy' - y = x, you must…

  1. nothing
  2. divide by x2x^2 so that f=1/xf = 1/x
  3. multiply by xx
  4. differentiate

Which advantage does variation of parameters have over undetermined coefficients?

  1. It never needs integrals
  2. It works for any continuous forcing ff
  3. It doesn't need the homogeneous solutions
  4. It only uses algebra