###
Subsection2.5.1Solving nonhomogeneous equations

We have solved linear constant coefficient homogeneous equations. What about nonhomogeneous linear ODEs? For example, the equations for forced mechanical vibrations. That is, suppose we have an equation such as

\begin{equation}
y'' + 5y'+ 6y = 2x+1 .\label{eq3_5_nh}\tag{6}
\end{equation}
We will write \(Ly = 2x+1\) when the exact form of the operator is not important. We solve (6) in the following manner. First, we find the general solution \(y_c\) to the *associated homogeneous equation*

\begin{equation}
y'' + 5y'+ 6y = 0 .\label{eq3_5_h}\tag{7}
\end{equation}
We call \(y_c\) the *complementary solution*. Next, we find a single *particular solution* \(y_p\) to (6) in some way. Then

\begin{equation*}
y = y_c + y_p
\end{equation*}
is the general solution to (6). We have \(L y_c = 0\) and \(L y_p = 2x+1\text{.}\) As \(L\) is a *linear operator* we verify that \(y\) is a solution, \(L y = L ( y_c + y_p) = L y_c + L y_p = 0
+ (2x+1)\text{.}\) Let us see why we obtain the *general* solution.

Let \(y_p\) and \(\tilde{y}_p\) be two different particular solutions to (6). Write the difference as \(w = y_p - \tilde{y}_p\text{.}\) Then plug \(w\) into the left hand side of the equation to get

\begin{equation*}
w'' + 5w'+ 6w =
(y_p'' + 5y_p'+ 6y_p) -
(\tilde{y}_p'' + 5\tilde{y}_p'+ 6\tilde{y}_p) =
(2x+1) - (2x+1) = 0 .
\end{equation*}
Using the operator notation the calculation becomes simpler. As \(L\) is a linear operator we write

\begin{equation*}
Lw = L(y_p - \tilde{y}_p) =
Ly_p - L\tilde{y}_p =
(2x+1)-(2x+1) = 0 .
\end{equation*}
So \(w = y_p - \tilde{y}_p\) is a solution to (7), that is \(Lw = 0\text{.}\) Any two solutions of (6) differ by a solution to the homogeneous equation (7). The solution \(y = y_c + y_p\) includes *all* solutions to (6), since \(y_c\) is the general solution to the associated homogeneous equation.

######
Theorem2.5.1

Let \(Ly=f(x)\) be a linear ODE (not necessarily constant coefficient). Let \(y_c\) be the complementary solution (the general solution to the associated homogeneous equation \(Ly = 0\)) and let \(y_p\) be any particular solution to \(Ly=f(x)\text{.}\) Then the general solution to \(Ly=f(x)\) is

\begin{equation*}
y = y_c + y_p.
\end{equation*}
The moral of the story is that we can find the particular solution in any old way. If we find a different particular solution (by a different method, or simply by guessing), then we still get the same general solution. The formula may look different, and the constants we will have to choose to satisfy the initial conditions may be different, but it is the same solution.

###
Subsection2.5.2Undetermined coefficients

The trick is to somehow, in a smart way, guess one particular solution to (6). Note that \(2x+1\) is a polynomial, and the left hand side of the equation will be a polynomial if we let \(y\) be a polynomial of the same degree. Let us try

\begin{equation*}
y_p = Ax + B .
\end{equation*}
We plug in to obtain

\begin{equation*}
y_p'' + 5y_p'+ 6y_p =
(Ax+B)'' + 5(Ax+B)' + 6(Ax+B) =
0 + 5A + 6Ax + 6B = 6Ax+ (5A+6B) .
\end{equation*}
So \(6Ax+(5A+6B) = 2x+1\text{.}\) Therefore, \(A = \nicefrac{1}{3}\) and \(B = \nicefrac{-1}{9}\text{.}\) That means \(y_p = \frac{1}{3}\, x - \frac{1}{9} = \frac{3x-1}{9}\text{.}\) Solving the complementary problem (exercise!) we get

\begin{equation*}
y_c = C_1 e^{-2x} + C_2 e^{-3x}.
\end{equation*}
Hence the general solution to (6) is

\begin{equation*}
y = C_1 e^{-2x} + C_2 e^{-3x} + \frac{3x-1}{9} .
\end{equation*}
Now suppose we are further given some initial conditions. For example, \(y(0) = 0\) and \(y'(0) = \nicefrac{1}{3}\text{.}\) First find \(y' = - 2C_1 e^{-2x} - 3C_2 e^{-3x}
+ \nicefrac{1}{3}\text{.}\) Then

\begin{equation*}
0 = y(0) = C_1 + C_2 -\frac{1}{9} , \qquad
\frac{1}{3} = y'(0) = - 2C_1 - 3C_2 + \frac{1}{3} .
\end{equation*}
We solve to get \(C_1 = \nicefrac{1}{3}\) and \(C_2 = \nicefrac{-2}{9}\text{.}\) The particular solution we want is

\begin{equation*}
y(x) = \frac{1}{3} e^{-2x} - \frac{2}{9} e^{-3x} + \frac{3x-1}{9} =
\frac{3 e^{-2x} - 2 e^{-3x} + 3x-1}{9} .
\end{equation*}
######
Exercise2.5.1

Check that \(y\) really solves the equation (6) and the given initial conditions.

Note: A common mistake is to solve for constants using the initial conditions with \(y_c\) and only add the particular solution \(y_p\) after that. That will *not* work. You need to first compute \(y = y_c + y_p\) and *only then* solve for the constants using the initial conditions.

A right hand side consisting of exponentials, sines, and cosines can be handled similarly. For example,

\begin{equation*}
y''+2y'+2y = \cos (2x) .
\end{equation*}
Let us find some \(y_p\text{.}\) We start by guessing the solution includes some multiple of \(\cos(2x)\text{.}\) We may have to also add a multiple of \(\sin (2x)\) to our guess since derivatives of cosine are sines. We try

\begin{equation*}
y_p = A \cos (2x) + B \sin (2x) .
\end{equation*}
We plug \(y_p\) into the equation and we get

\begin{equation*}
-4 A \cos (2x) - 4 B \sin (2x)
-4A \sin (2x) + 4B \cos (2x)
+ 2A \cos (2x) + 2B \sin (2x)
= \cos (2x) .
\end{equation*}
The left hand side must equal to right hand side. We group terms and we get that \(-4A + 4B + 2A = 1\) and \(-4B - 4A + 2B = 0\text{.}\) So \(-2A+4B =1\) and \(2A+B=0\) and hence \(A=\nicefrac{-1}{10}\) and \(B=\nicefrac{1}{5}\text{.}\) So

\begin{equation*}
y_p = A \cos (2x) + B \sin (2x) = \frac{-\cos (2x) + 2 \sin (2x)}{10} .
\end{equation*}
Similarly, if the right hand side contains exponentials we try exponentials. For example, for

\begin{equation*}
Ly = e^{3x},
\end{equation*}
we will try \(y = A e^{3x}\) as our guess and try to solve for \(A\text{.}\)

When the right hand side is a multiple of sines, cosines, exponentials, and polynomials, we can use the product rule for differentiation to come up with a guess. We need to guess a form for \(y_p\) such that \(Ly_p\) is of the same form, and has all the terms needed to for the right hand side. For example,

\begin{equation*}
Ly = (1+3x^2)\,e^{-x}\cos (\pi x) .
\end{equation*}
For this equation, we will guess

\begin{equation*}
y_p = (A + Bx + Cx^2)\,e^{-x} \cos (\pi x) +
(D + Ex + Fx^2)\,e^{-x} \sin (\pi x) .
\end{equation*}
We will plug in and then hopefully get equations that we can solve for \(A,B,C,D,E,\) and \(F\text{.}\) As you can see this can make for a very long and tedious calculation very quickly. C'est la vie!

There is one hiccup in all this. It could be that our guess actually solves the associated homogeneous equation. That is, suppose we have

\begin{equation*}
y'' - 9y = e^{3x} .
\end{equation*}
We would love to guess \(y = Ae^{3x}\text{,}\) but if we plug this into the left hand side of the equation we get

\begin{equation*}
y''-9y = 9Ae^{3x} - 9Ae^{3x} = 0 \not= e^{3x} .
\end{equation*}
There is no way we can choose \(A\) to make the left hand side be \(e^{3x}\text{.}\) The trick in this case is to multiply our guess by \(x\) to get rid of duplication with the complementary solution. That is first we compute \(y_c\) (solution to \(Ly =
0\))

\begin{equation*}
y_c = C_1 e^{-3x} + C_2 e^{3x} ,
\end{equation*}
and we note that the \(e^{3x}\) term is a duplicate with our desired guess. We modify our guess to \(y = Axe^{3x}\) and notice there is no duplication anymore. Let us try. Note that \(y' = Ae^{3x} + 3Axe^{3x}\) and \(y'' = 6Ae^{3x} + 9Axe^{3x}\text{.}\) So

\begin{equation*}
y'' -9y = 6Ae^{3x} + 9Axe^{3x} - 9Axe^{3x} =
6Ae^{3x} .
\end{equation*}
Thus \(6Ae^{3x}\) is supposed to equal \(e^{3x}\text{.}\) Hence, \(6A = 1\) and so \(A=\nicefrac{1}{6}\text{.}\) We can now write the general solution as

\begin{equation*}
y = y_c + y_p =
C_1 e^{-3x} + C_2 e^{3x} + \frac{1}{6}\,xe^{3x} .
\end{equation*}
It is possible that multiplying by \(x\) does not get rid of all duplication. For example,

\begin{equation*}
y''-6y'+9y = e^{3x} .
\end{equation*}
The complementary solution is \(y_c = C_1 e^{3x} + C_2 x e^{3x}\text{.}\) Guessing \(y=A xe^{3x}\) would not get us anywhere. In this case we want to guess \(y_p = Ax^2e^{3x}\text{.}\) Basically, we want to multiply our guess by \(x\) until all duplication is gone. *But no more!* Multiplying too many times will not work.

Finally, what if the right hand side has several terms, such as

\begin{equation*}
Ly = e^{2x} + \cos x .
\end{equation*}
In this case we find \(u\) that solves \(Lu = e^{2x}\) and \(v\) that solves \(Lv = \cos x\) (that is, do each term separately). Then note that if \(y = u+ v\text{,}\) then \(Ly = e^{2x} + \cos x\text{.}\) This is because \(L\) is linear; we have \(Ly = L(u+v) = Lu + Lv = e^{2x} + \cos x\text{.}\)

###
Subsection2.5.3Variation of parameters

The method of undetermined coefficients will work for many basic problems that crop up. But it does not work all the time. It only works when the right hand side of the equation \(Ly = f(x)\) has only finitely many linearly independent derivatives, so that we can write a guess that consists of them all. Some equations are a bit tougher. Consider

\begin{equation*}
y''+y = \tan x .
\end{equation*}
Note that each new derivative of \(\tan x\) looks completely different and cannot be written as a linear combination of the previous derivatives. We get \(\sec^2 x\text{,}\) \(2\sec^2 x \tan x\text{,}\) etc.

This equation calls for a different method. We present the method of *variation of parameters*, which will handle any equation of the form \(Ly = f(x)\text{,}\) provided we can solve certain integrals. For simplicity, we restrict ourselves to second order constant coefficient equations, but the method works for higher order equations just as well (the computations become more tedious). The method also works for equations with nonconstant coefficients, provided we can solve the associated homogeneous equation.

Perhaps it is best to explain this method by example. Let us try to solve the equation

\begin{equation*}
Ly = y''+y = \tan x .
\end{equation*}
First we find the complementary solution (solution to \(Ly_c = 0\)). We get \(y_c = C_1 y_1 + C_2 y_2\text{,}\) where \(y_1 = \cos x\) and \(y_2 = \sin x\text{.}\) To find a particular solution to the nonhomogeneous equation we try

\begin{equation*}
y_p = y = u_1 y_1 + u_2 y_2 ,
\end{equation*}
where \(u_1\) and \(u_2\) are *functions* and not constants. We are trying to satisfy \(Ly = \tan x\text{.}\) That gives us one condition on the functions \(u_1\) and \(u_2\text{.}\) Compute (note the product rule!)

\begin{equation*}
y' = (u_1' y_1 + u_2' y_2) + (u_1 y_1' + u_2 y_2').
\end{equation*}
We can still impose one more condition at our discretion to simplify computations (we have two unknown functions, so we should be allowed two conditions). We require that \((u_1' y_1 + u_2' y_2) = 0\text{.}\) This makes computing the second derivative easier.

\begin{equation*}
\begin{aligned}
& y' = u_1 y_1' + u_2 y_2' , \\
& y'' = (u_1' y_1' + u_2' y_2') + (u_1 y_1'' + u_2 y_2'') .
\end{aligned}
\end{equation*}
Since \(y_1\) and \(y_2\) are solutions to \(y''+y = 0\text{,}\) we know that \(y_1'' = - y_1\) and \(y_2'' = - y_2\text{.}\) (Note: If the equation was instead \(y''+p(x)y' +q(x)y = 0\) we would have \(y_i'' = -p(x)y_i' -q(x)y_i\text{.}\)) So

\begin{equation*}
y'' = (u_1' y_1' + u_2' y_2') - (u_1 y_1 + u_2 y_2) .
\end{equation*}
We have \((u_1 y_1 + u_2 y_2) = y\) and so

\begin{equation*}
y'' = (u_1' y_1' + u_2' y_2') - y ,
\end{equation*}
and hence

\begin{equation*}
y'' + y = Ly = u_1' y_1' + u_2' y_2' .
\end{equation*}
For \(y\) to satisfy \(Ly = f(x)\) we must have \(f(x) = u_1' y_1' + u_2' y_2'\text{.}\)

So what we need to solve are the two equations (conditions) we imposed on \(u_1\) and \(u_2\text{:}\)

\begin{equation*}
\boxed{~~
\begin{aligned}
& u_1' y_1 + u_2' y_2 = 0 ,\\
& u_1' y_1' + u_2' y_2' = f(x) .
\end{aligned}
~~}
\end{equation*}
We now solve for \(u_1'\) and \(u_2'\) in terms of \(f(x)\text{,}\) \(y_1\) and \(y_2\text{.}\) We always get these formulas for any \(Ly = f(x)\text{,}\) where \(Ly =
y''+p(x)y'+q(x)y\text{.}\) There is a general formula for the solution we can just plug into, but it is better to just repeat what we do below. In our case the two equations become

\begin{equation*}
\begin{aligned}
u_1' \cos (x) + u_2' \sin (x) &= 0 ,\\
-u_1' \sin (x) + u_2' \cos (x) &= \tan (x) .
\end{aligned}
\end{equation*}
Hence

\begin{equation*}
\begin{aligned}
u_1' \cos (x) \sin (x) + u_2' \sin^2 (x) & = 0 ,\\
-u_1' \sin (x) \cos (x) + u_2' \cos^2 (x) & = \tan (x) \cos (x) = \sin (x) .
\end{aligned}
\end{equation*}
And thus

\begin{equation*}
\begin{aligned}
& u_2' \bigl(\sin^2 (x) + \cos^2 (x)\bigr) = \sin (x) , \\
& u_2' = \sin (x) , \\
& u_1' = \frac{- \sin^2 (x)}{\cos (x)} = - \tan (x) \sin (x) .
\end{aligned}
\end{equation*}
Now we need to integrate \(u_1'\) and \(u_2'\) to get \(u_1\) and \(u_2\text{.}\)

\begin{equation*}
\begin{aligned}
& u_1 = \int u_1'~dx
= \int - \tan (x) \sin (x)~dx
= \frac{1}{2}
\ln \left\lvert \frac{\sin (x)-1}{\sin (x) + 1} \right\rvert
+ \sin (x) , \\
& u_2 = \int u_2'~dx
= \int \sin (x)~dx = -\cos (x) .
\end{aligned}
\end{equation*}
So our particular solution is

\begin{multline*}
y_p = u_1 y_1 + u_2 y_2 =
\frac{1}{2} \cos (x) \ln \left\lvert \frac{\sin (x)-1}{\sin (x) + 1}
\right\rvert
+ \cos (x) \sin (x)
-\cos (x) \sin (x)
= \\ =
\frac{1}{2} \cos (x) \ln \left\lvert \frac{\sin (x)-1}{\sin (x) + 1}
\right\rvert .
\end{multline*}
The general solution to \(y'' + y = \tan x\) is, therefore,

\begin{equation*}
y = C_1 \cos (x) + C_2 \sin (x) +
\frac{1}{2} \cos (x) \ln \left\lvert \frac{\sin (x)-1}{\sin (x) + 1}
\right\rvert .
\end{equation*}