# This is a code cell that imports the necessary libraries for our session.import numpy as np # NumPy for numerical computationsimport sympy as sym # SymPy for symbolic mathematicsimport matplotlib as mpl # Matplotlib for plottingimport matplotlib.pyplot as plt # Matplotlib pyplot interfacefrom IPython.display import Math, displaympl.rcParams['figure.dpi'] =150mpl.rcParams['axes.spines.top'] =Falsempl.rcParams['axes.spines.right'] =False
In this document we work through fully solved examples for the topic covered in Section 2.4.2 of the text: the method of variation of parameters for nonhomogeneous second-order linear ODEs.
The examples progress from a gentle warm-up (forcing by a simple exponential) through cases involving rational, trigonometric, and logarithmic forcing functions — precisely the situations where the method of undetermined coefficients does not apply. Each example is worked completely by hand and then verified using SymPy.
The Method — A Brief Review
Consider the nonhomogeneous second-order linear ODE written in standard (divided) form: \[
y'' + p(t)\,y' + q(t)\,y = f(t).
\]
Step 0. Solve the associated homogeneous equation \(y'' + p(t)y' + q(t)y = 0\) to obtain two linearly independent solutions \(y_1\) and \(y_2\). The general homogeneous solution is \(y_h = C_1 y_1 + C_2 y_2\).
Step 3 — Integrate.\[
u_1(t) = \int u_1'(t)\,dt, \qquad u_2(t) = \int u_2'(t)\,dt.
\] (Omit the constants of integration; they fold into \(C_1\) and \(C_2\) of \(y_h\).)
Why variation of parameters? The method of undetermined coefficients is restricted to forcing functions that are polynomials, exponentials, sines, cosines, or products of these. Variation of parameters works for any forcing function \(f(t)\) for which the integrals in Step 3 can be evaluated, including rational functions, \(\sec t\), \(\ln t\), and so on.
Example 1 — Exponential Forcing, Constant-Coefficient ODE
Use the method of variation of parameters to find the general solution of \[y'' - 5y' + 6y = e^t.\]
This example uses a forcing function that could be handled by undetermined coefficients (since \(e^t\) is not a homogeneous solution), but it makes an ideal first example for learning the variation-of-parameters procedure.
Cross-check with undetermined coefficients. Since \(e^t\) is not a solution of the homogeneous equation, we could try \(y_p = Ae^t\). Then \(y_p'' - 5y_p' + 6y_p =
Ae^t - 5Ae^t + 6Ae^t = 2Ae^t = e^t\), giving \(A = \tfrac{1}{2}\). This confirms \(y_p = \tfrac{1}{2}e^t\).
For \(u_1\), recall that \(\displaystyle\int \tan t\,dt = -\ln|\cos t| + C\): \[
u_1 = \int -\tan t\,dt = \ln|\cos t| = \ln(\cos t)
\quad\bigl(\text{valid for }|t| < \tfrac{\pi}{2}\bigr).
\]\[
u_2 = \int 1\,dt = t.
\]
Step 4 — Particular solution.\[
y_p = u_1 y_1 + u_2 y_2
= \ln(\cos t)\cdot\cos t + t\sin t.
\]
Step 5 — General solution.\[
\boxed{y(t) = C_1\cos t + C_2\sin t + \cos t\,\ln(\cos t) + t\sin t,}
\qquad |t| < \frac{\pi}{2}.
\]
Note
The term \(\cos t\,\ln(\cos t)\) could never appear as a trial function in the method of undetermined coefficients, which is why variation of parameters is necessary here.
Figure 1: Several solution curves of \(y'' + y = \sec t\) on \((-\pi/2,\,\pi/2)\). The particular solution (red, \(C_1=C_2=0\)) \(y_p = \cos t\ln(\cos t) + t\sin t\) is highlighted.
Example 3 — Rational Forcing: \(1/t^2\)
Use variation of parameters to find the general solution of \[y'' - \frac{2}{t^2}\,y = 3, \qquad t > 0,\] given that \(y_1 = t^2\) and \(y_2 = t^{-1}\) are solutions of the associated homogeneous equation.
Note
This is an Euler–Cauchy type equation (variable coefficients), not a constant-coefficient ODE. Variation of parameters applies to any second-order linear ODE once a fundamental set of solutions is known — the coefficients need not be constant.
By Hand
Step 0 — Homogeneous solution.
We are given \(y_1 = t^2\) and \(y_2 = t^{-1}\). (One may verify by substitution that each satisfies \(y'' - \frac{2}{t^2}y = 0\).)
\[
y_h = C_1 t^2 + C_2 t^{-1}.
\]
We must write the ODE in standard form by dividing through by the leading coefficient (here it is already \(1\) for \(y''\)): \[
y'' + 0 \cdot y' - \frac{2}{t^2}\,y = 3.
\]
The term \(-\dfrac{t^2}{3}\) is a constant multiple of the homogeneous solution \(y_1 = t^2\) and can be absorbed into \(C_1 t^2\). We therefore simplify the particular solution to \[y_p = t^2 \ln t.\] This is a standard simplification: any part of \(y_p\) that duplicates a homogeneous solution is redundant and may be dropped.
Step 5 — General solution.\[
\boxed{y(t) = C_1 t^2 + C_2 t^{-1} + t^2\ln t, \qquad t > 0.}
\]
Verification. Compute \(y_p' = 2t\ln t + t\) and \(y_p'' = 2\ln t + 3\). Then \[
y_p'' - \frac{2}{t^2}y_p = (2\ln t + 3) - \frac{2}{t^2}(t^2\ln t)
= 2\ln t + 3 - 2\ln t = 3. \checkmark
\]
Using SymPy
Because the coefficient of \(y\) depends on \(t\), we pass the full ODE to dsolve; SymPy recognises it as an Euler–Cauchy equation and handles it symbolically.
The particular solution to the IVP is \[
\boxed{y(t) = te^t + e^t\!\left[t\arctan t - \frac{1}{2}\ln(t^2+1)\right]
= e^t\!\left[t + t\arctan t - \frac{1}{2}\ln(t^2+1)\right].}
\]
Using SymPy
SymPy’s dsolve cannot automatically evaluate the variation-of-parameters integrals that produce \(\arctan t\) and \(\ln(t^2+1)\), so we carry out each step of the method explicitly in SymPy instead.
Figure 4: IVP solution \(y(t) = e^t[t + t\arctan t - \tfrac{1}{2}\ln(t^2+1)]\) for \(y'' - 2y' + y = e^t/(t^2+1)\), \(y(0)=0\), \(y'(0)=1\). The homogeneous solutions \(e^t\) (dashed) and \(te^t\) (dotted) are shown for scale.
TipSummary: When to Use Variation of Parameters vs. Undetermined Coefficients
Situation
Method
\(f(t)\) is a polynomial, \(e^{at}\), \(\sin(bt)\), \(\cos(bt)\), or products/sums of these
Undetermined coefficients (simpler algebra)
\(f(t)\) involves \(\sec t\), \(\csc t\), \(\tan t\), \(\ln t\), \(1/p(t)\), or other non-elementary forms
Variation of parameters (always works when the integrals are tractable)
Variable-coefficient ODE (e.g. Euler–Cauchy) with known \(y_1\), \(y_2\)
Variation of parameters
Either method could work
Undetermined coefficients is usually less labor-intensive
The key formula to memorize is the Wronskian-based pair: \[
u_1' = -\frac{y_2 f}{W}, \qquad u_2' = \frac{y_1 f}{W}, \qquad
W = y_1 y_2' - y_2 y_1'.
\] Always divide the ODE by the leading coefficient first to put it in standard form before reading off \(f(t)\).
TipExpand for Session Info
import sysprint("Python version:", sys.version)print('\n'.join(f'{m.__name__}=={m.__version__}'for m inglobals().values()ifgetattr(m, '__version__', None)))