% =============================================================================
% ch05_second_order_nonhomogeneous.tex
% Chapter 5: Second-Order Linear Nonhomogeneous Equations
% =============================================================================

\section{Second-Order Nonhomogeneous}
\label{ch:second_order_nonhomogeneous}

We now extend the theory from \cref{ch:second_order_homogeneous} to the nonhomogeneous case. The general equation is
\begin{equation}
  \label{eq:nonhomogeneous_general}
  a\,y'' + b\,y' + c\,y = g(x),
  \qquad a, b, c \in \R,\;\; a \neq 0,
\end{equation}
where $g(x)$ is a given \textbf{forcing function} (also called the \textbf{source term} or \textbf{nonhomogeneous term}). When $g(x) \equiv 0$, we recover the homogeneous equation \cref{eq:second_order_homogeneous}. The presence of $g(x)$ models external driving forces in physical systems --- a periodically driven pendulum, a forced electrical circuit, or a building shaken by an earthquake.

The general strategy in all cases is the same: first solve the associated homogeneous equation, then find one particular solution of the nonhomogeneous equation. The two parts combine to give the complete solution.

\subsection{General Solution Structure}
\label{sec:ch05_general_solution}

\begin{theorem}[Structure of the General Solution]
  \label{thm:nonhomogeneous_structure}
  Let $L[y] = ay'' + by' + cy$ with constant $a, b, c$ and $a \neq 0$. If $y_h(x)$ is the general solution of the associated homogeneous equation $L[y] = 0$, and $y_p(x)$ is \emph{any} particular solution of the nonhomogeneous equation $L[y] = g(x)$, then the general solution of $L[y] = g(x)$ is
  \[
    y(x) = y_h(x) + y_p(x).
  \]
\end{theorem}

\begin{proof}
  Define the linear operator $L[y] = ay'' + by' + cy$. We need to show that $y = y_h + y_p$ satisfies $L[y] = g(x)$ for \emph{every} choice of the constants in $y_h$.

  By linearity of differentiation (the \textbf{superposition principle}, \cref{thm:superposition}):
  \[
    L[y_h + y_p] = L[y_h] + L[y_p].
  \]
  Since $y_h$ solves the homogeneous equation, $L[y_h] = 0$. Since $y_p$ is a particular solution, $L[y_p] = g(x)$. Therefore:
  \[
    L[y_h + y_p] = 0 + g(x) = g(x).
  \]
  Thus $y_h + y_p$ is a solution of the nonhomogeneous equation. Conversely, if $y$ is any solution of $L[y] = g(x)$, then $L[y - y_p] = L[y] - L[y_p] = g(x) - g(x) = 0$, so $y - y_p = y_h$ for some homogeneous solution $y_h$, meaning $y = y_h + y_p$.
\end{proof}

The homogeneous part $y_h(x)$ retains two arbitrary constants $c_1, c_2$ from the general solution found in \cref{ch:second_order_homogeneous}. The particular solution $y_p(x)$ contains no free parameters --- we only need \emph{one} specific function that works.

\begin{hintbox}
  \textbf{Two-step workflow for every nonhomogeneous problem.}
  \begin{enumerate}
    \item \textbf{Solve the homogeneous equation} $ay'' + by' + cy = 0$ using the characteristic equation method from \cref{ch:second_order_homogeneous}. Obtain $y_h(x) = c_1 y_1(x) + c_2 y_2(x)$.
    \item \textbf{Find one particular solution} $y_p(x)$ using either the method of undetermined coefficients or variation of parameters.
  \end{enumerate}
  Combine: $y(x) = y_h(x) + y_p(x)$.
\end{hintbox}

We now develop the two main methods for finding $y_p(x)$.

\subsection{Method of Undetermined Coefficients}
\label{sec:ch05_undetermined_coefficients}

The method of \textbf{undetermined coefficients} (UC) is an algebraic technique that works when the forcing function $g(x)$ has a \textbf{simple form}. The idea is straightforward: guess the \emph{shape} of $y_p$ based on the form of $g(x)$, but leave the coefficients unknown. Then substitute the guess into the ODE and solve for those coefficients.

\paragraph{When to use.} Undetermined coefficients applies only when $g(x)$ is one of the following (or a sum/product of them):
\begin{itemize}
  \item A polynomial $P_n(x)$ of degree $n$.
  \item An exponential $e^{\alpha x}$.
  \item A sine or cosine function $\sin(\beta x)$ or $\cos(\beta x)$.
  \item Products of the above, such as $e^{\alpha x} P_n(x)$ or $e^{\alpha x}\sin(\beta x)$.
\end{itemize}
If $g(x) = \ln(x)$, $1/x$, $\tan(x)$, or any other function outside this family, undetermined coefficients \textbf{will not work}. Use variation of parameters instead (\cref{sec:ch05_variation_of_parameters}).

\begin{hintbox}
  \textbf{Limitation warning.} Undetermined coefficients is an \emph{educated guessing} method. It exploits the fact that polynomials, exponentials, and trigonometric functions are closed under differentiation. Functions like $\ln x$ and $\tan x$ do not share this property, so the method breaks down.
\end{hintbox}

\subsubsection{Guess Table}
\label{sec:ch05_guess_table}

The following table gives the initial guess for $y_p$ based on the form of $g(x)$. The constants $A_k, B_k$ are the ``undetermined coefficients'' to be found by substitution.

\begin{keyresult}
  \textbf{Guess table for the method of undetermined coefficients.}

  \begin{center}
  \begin{tabular}{l l}
    \toprule
    \textbf{Form of $g(x)$} & \textbf{Initial guess for $y_p(x)$} \\
    \midrule
    $P_n(x) = a_n x^n + \cdots + a_0$ & $A_n x^n + A_{n-1} x^{n-1} + \cdots + A_0$ \\[8pt]
    $e^{\alpha x}$ & $A\,e^{\alpha x}$ \\[8pt]
    $\sin(\beta x)$ \textbf{or} $\cos(\beta x)$ & $A\sin(\beta x) + B\cos(\beta x)$ \\[8pt]
    $e^{\alpha x} P_n(x)$ & $e^{\alpha x}(A_n x^n + \cdots + A_0)$ \\[8pt]
    $e^{\alpha x}\sin(\beta x)$ \textbf{or} $e^{\alpha x}\cos(\beta x)$ &
    \begin{tabular}[t]{@{}l@{}}
      $e^{\alpha x}\bigl[(A_n x^n + \cdots + A_0)\cos(\beta x)$ \\
      \quad $+ (B_n x^n + \cdots + B_0)\sin(\beta x)\bigr]$
    \end{tabular} \\[12pt]
    Product of two forms & Multiply the individual guesses \\[8pt]
    Sum of two forms $g_1 + g_2$ & Sum the individual guesses \\
    \bottomrule
  \end{tabular}
  \end{center}
\end{keyresult}

\subsubsection{Modification Rule}
\label{sec:ch05_modification_rule}

There is one important caveat: if any term of your initial guess \textbf{already appears in the homogeneous solution} $y_h$, the guess will fail (substitution leads to $0 = g(x)$, a contradiction). In this case, you must \textbf{modify} the guess.

\begin{hintbox}
  \textbf{Modification rule.} If a term in the initial guess for $y_p$ is also a solution of the homogeneous equation:
  \begin{enumerate}
    \item Multiply that term by $x$.
    \item If the modified term \emph{still} appears in $y_h$, multiply by $x$ again (i.e.\ by $x^2$).
  \end{enumerate}
  For a second-order equation, $x^2$ is always sufficient, because the homogeneous solution space has dimension~2.
\end{hintbox}

\paragraph{Why this works.} The modification multiplies the guess by $x$ (or $x^2$), which introduces new functional forms (like $x e^{rx}$ or $x^2 e^{rx}$) that are \emph{not} solutions of the homogeneous equation, while preserving the algebraic structure needed for substitution.

\subsubsection{Worked Examples}
\label{sec:ch05_uc_examples}

\paragraph{Example 1: Polynomial forcing.}

\begin{workedexample}
  Solve $y'' - 3y' + 2y = 4x + 1$.

  \textbf{Solution.} \textit{Step 1: Homogeneous solution.} The characteristic equation is
  \[
    r^2 - 3r + 2 = 0 \quad\Longrightarrow\quad (r-1)(r-2) = 0.
  \]
  Roots: $r_1 = 1$, $r_2 = 2$. The homogeneous solution is
  \[
    y_h(x) = c_1 e^x + c_2 e^{2x}.
  \]

  \textit{Step 2: Particular solution.} Here $g(x) = 4x + 1$, a first-degree polynomial. From the guess table, we try
  \[
    y_p(x) = Ax + B.
  \]
  No term overlaps with $y_h$ (exponentials vs.\ polynomials), so no modification is needed. Differentiate:
  \[
    y_p' = A, \qquad y_p'' = 0.
  \]
  Substitute into the ODE:
  \[
    0 - 3A + 2(Ax + B) = 4x + 1.
  \]
  Collect terms:
  \[
    2A\,x + (2B - 3A) = 4x + 1.
  \]
  Equate coefficients of like powers of $x$:
  \[
    \begin{cases}
      2A = 4 \quad\Longrightarrow\quad A = 2, \\
      2B - 3A = 1 \quad\Longrightarrow\quad 2B - 6 = 1 \quad\Longrightarrow\quad B = \dfrac{7}{2}.
    \end{cases}
  \]
  The particular solution is $y_p(x) = 2x + \dfrac{7}{2}$.

  \textit{Step 3: General solution.}
  \[
    y(x) = c_1 e^x + c_2 e^{2x} + 2x + \frac{7}{2}.
  \]
\end{workedexample}

\paragraph{Example 2: Exponential forcing.}

\begin{workedexample}
  Solve $y'' + y' - 6y = 12e^{3x}$.

  \textbf{Solution.} \textit{Step 1: Homogeneous solution.} The characteristic equation is
  \[
    r^2 + r - 6 = 0 \quad\Longrightarrow\quad (r-2)(r+3) = 0.
  \]
  Roots: $r_1 = 2$, $r_2 = -3$. Thus
  \[
    y_h(x) = c_1 e^{2x} + c_2 e^{-3x}.
  \]

  \textit{Step 2: Particular solution.} Here $g(x) = 12e^{3x}$. From the guess table, we try
  \[
    y_p(x) = A\,e^{3x}.
  \]
  Check for overlap: $e^{3x}$ is not in $y_h$ (which contains $e^{2x}$ and $e^{-3x}$), so no modification needed. Differentiate:
  \[
    y_p' = 3A\,e^{3x}, \qquad y_p'' = 9A\,e^{3x}.
  \]
  Substitute:
  \[
    9A\,e^{3x} + 3A\,e^{3x} - 6A\,e^{3x} = 12e^{3x}.
  \]
  Factor out $e^{3x}$ (never zero):
  \[
    (9 + 3 - 6)A = 12 \quad\Longrightarrow\quad 6A = 12 \quad\Longrightarrow\quad A = 2.
  \]
  So $y_p(x) = 2e^{3x}$.

  \textit{Step 3: General solution.}
  \[
    y(x) = c_1 e^{2x} + c_2 e^{-3x} + 2e^{3x}.
  \]
\end{workedexample}

\paragraph{Example 3: Trigonometric forcing.}

\begin{workedexample}
  Solve $y'' + 4y = 8\cos(2x)$.

  \textbf{Solution.} \textit{Step 1: Homogeneous solution.} The characteristic equation is
  \[
    r^2 + 4 = 0 \quad\Longrightarrow\quad r = \pm 2i.
  \]
  Thus
  \[
    y_h(x) = c_1 \cos(2x) + c_2 \sin(2x).
  \]

  \textit{Step 2: Particular solution.} Here $g(x) = 8\cos(2x)$. From the guess table, we try
  \[
    y_p(x) = A\cos(2x) + B\sin(2x).
  \]
  \textbf{Overlap detected!} Both $\cos(2x)$ and $\sin(2x)$ appear in $y_h$. We must apply the modification rule: multiply the entire guess by $x$:
  \[
    y_p(x) = x\bigl[A\cos(2x) + B\sin(2x)\bigr] = Ax\cos(2x) + Bx\sin(2x).
  \]
  Check again: $x\cos(2x)$ and $x\sin(2x)$ are \emph{not} in $y_h$, so we are good. Differentiate (product rule):
  \[
    \begin{aligned}
      y_p' &= A\cos(2x) - 2Ax\sin(2x) + B\sin(2x) + 2Bx\cos(2x), \\
      y_p'' &= -2A\sin(2x) - 2A\sin(2x) - 4Ax\cos(2x) + 2B\cos(2x) + 2B\cos(2x) - 4Bx\sin(2x) \\
            &= -4A\sin(2x) - 4Ax\cos(2x) + 4B\cos(2x) - 4Bx\sin(2x).
    \end{aligned}
  \]
  Substitute into $y'' + 4y = 8\cos(2x)$:
  \[
    \bigl[-4A\sin(2x) - 4Ax\cos(2x) + 4B\cos(2x) - 4Bx\sin(2x)\bigr]
    + 4\bigl[Ax\cos(2x) + Bx\sin(2x)\bigr] = 8\cos(2x).
  \]
  The $x$-terms cancel:
  \[
    -4A\sin(2x) + 4B\cos(2x) = 8\cos(2x).
  \]
  Equate coefficients:
  \[
    \begin{cases}
      -4A = 0 \quad\Longrightarrow\quad A = 0, \\
      4B = 8 \quad\Longrightarrow\quad B = 2.
    \end{cases}
  \]
  So $y_p(x) = 2x\sin(2x)$.

  \textit{Step 3: General solution.}
  \[
    y(x) = c_1 \cos(2x) + c_2 \sin(2x) + 2x\sin(2x).
  \]
\end{workedexample}

\paragraph{Example 4: Product of exponential and trigonometric.}

\begin{workedexample}
  Solve $y'' - 2y' + 5y = 10e^x\cos(2x)$.

  \textbf{Solution.} \textit{Step 1: Homogeneous solution.} The characteristic equation is
  \[
    r^2 - 2r + 5 = 0.
  \]
  Discriminant: $\Delta = 4 - 20 = -16 < 0$. Roots:
  \[
    r = \frac{2 \pm \sqrt{-16}}{2} = 1 \pm 2i.
  \]
  Thus
  \[
    y_h(x) = e^x\bigl[c_1 \cos(2x) + c_2 \sin(2x)\bigr].
  \]

  \textit{Step 2: Particular solution.} Here $g(x) = 10e^x\cos(2x)$. From the guess table for $e^{\alpha x}\cos(\beta x)$ with $\alpha = 1, \beta = 2$, we try
  \[
    y_p(x) = e^x\bigl[A\cos(2x) + B\sin(2x)\bigr].
  \]
  \textbf{Overlap detected!} Both $e^x\cos(2x)$ and $e^x\sin(2x)$ appear in $y_h$. Apply the modification rule: multiply by $x$:
  \[
    y_p(x) = x\,e^x\bigl[A\cos(2x) + B\sin(2x)\bigr].
  \]
  Now differentiate. Let $y_p = x\,e^x\,(A\cos(2x) + B\sin(2x))$. Set $u = x\,e^x$ and $v = A\cos(2x) + B\sin(2x)$ for clarity. Then $u' = e^x(1 + x)$ and $v' = -2A\sin(2x) + 2B\cos(2x)$.
  \[
    \begin{aligned}
      y_p' &= u'v + uv' = e^x(1+x)\bigl[A\cos(2x) + B\sin(2x)\bigr] + x\,e^x\bigl[-2A\sin(2x) + 2B\cos(2x)\bigr], \\
      y_p'' &= e^x(1+x)\bigl[-2A\sin(2x) + 2B\cos(2x)\bigr] + e^x\bigl[A\cos(2x) + B\sin(2x)\bigr] \\
        &\quad + e^x(1+x)\bigl[-2A\sin(2x) + 2B\cos(2x)\bigr] + x\,e^x\bigl[-4A\cos(2x) - 4B\sin(2x)\bigr] \\
        &= 2e^x(1+x)\bigl[-2A\sin(2x) + 2B\cos(2x)\bigr] + e^x\bigl[A\cos(2x) + B\sin(2x)\bigr] \\
        &\quad - 4x\,e^x\bigl[A\cos(2x) + B\sin(2x)\bigr].
    \end{aligned}
  \]
  Substitute $y_p$, $y_p'$, and $y_p''$ into the ODE $y'' - 2y' + 5y$. Collect the coefficient of $e^x\cos(2x)$ and $e^x\sin(2x)$ (the $x$-terms will cancel due to the modification rule):

  Coefficient of $e^x\cos(2x)$: $4B + A - 2A + 5A = 4B + 4A$.

  Coefficient of $e^x\sin(2x)$: $-4A + B - 2B + 5B = -4A + 4B$.

  Set equal to the RHS $10e^x\cos(2x)$:
  \[
    \begin{cases}
      4A + 4B = 10, \\
      -4A + 4B = 0 \quad\Longrightarrow\quad A = B.
    \end{cases}
  \]
  From the second equation, $A = B$. Substituting into the first: $8A = 10$, so $A = \dfrac{5}{4}$ and $B = \dfrac{5}{4}$.

  The particular solution is
  \[
    y_p(x) = \frac{5}{4}\,x\,e^x\bigl[\cos(2x) + \sin(2x)\bigr].
  \]

  \textit{Step 3: General solution.}
  \[
    y(x) = e^x\Bigl[c_1\cos(2x) + c_2\sin(2x)\Bigr] + \frac{5}{4}\,x\,e^x\bigl[\cos(2x) + \sin(2x)\bigr].
  \]
\end{workedexample}

\paragraph{Example 5: Modification rule (repeated-root overlap).}

\begin{workedexample}
  Solve $y'' - 4y' + 4y = 6e^{2x}$.

  \textbf{Solution.} \textit{Step 1: Homogeneous solution.} The characteristic equation is
  \[
    r^2 - 4r + 4 = 0 \quad\Longrightarrow\quad (r-2)^2 = 0.
  \]
  Double root: $r = 2$. Thus
  \[
    y_h(x) = c_1 e^{2x} + c_2 x\,e^{2x}.
  \]

  \textit{Step 2: Particular solution.} Here $g(x) = 6e^{2x}$. The initial guess is $y_p = A\,e^{2x}$.

  \textbf{Overlap check:} $e^{2x}$ is in $y_h$. Modify: multiply by $x \Rightarrow y_p = Ax\,e^{2x}$.

  \textbf{Overlap check again:} $x\,e^{2x}$ is \emph{also} in $y_h$. Modify again: multiply by $x$ once more $\Rightarrow y_p = Ax^2 e^{2x}$.

  Now $x^2 e^{2x}$ is \emph{not} in $y_h$, so this is our final guess. Differentiate (product rule):
  \[
    \begin{aligned}
      y_p &= A x^2 e^{2x}, \\
      y_p' &= A(2x e^{2x} + 2x^2 e^{2x}) = 2A x\,e^{2x} + 2A x^2 e^{2x}, \\
      y_p'' &= 2A e^{2x} + 4A x\,e^{2x} + 4A x\,e^{2x} + 4A x^2 e^{2x} \\
            &= 2A e^{2x} + 8A x\,e^{2x} + 4A x^2 e^{2x}.
    \end{aligned}
  \]
  Substitute into $y'' - 4y' + 4y = 6e^{2x}$:
  \[
    \begin{aligned}
      &\bigl[2A e^{2x} + 8A x\,e^{2x} + 4A x^2 e^{2x}\bigr]
      - 4\bigl[2A x\,e^{2x} + 2A x^2 e^{2x}\bigr]
      + 4\bigl[A x^2 e^{2x}\bigr] \\
      &= 2A e^{2x} + (8A - 8A)x\,e^{2x} + (4A - 8A + 4A)x^2 e^{2x} \\
      &= 2A e^{2x}.
    \end{aligned}
  \]
  Set equal to $6e^{2x}$:
  \[
    2A = 6 \quad\Longrightarrow\quad A = 3.
  \]
  The particular solution is $y_p(x) = 3x^2 e^{2x}$.

  \textit{Step 3: General solution.}
  \[
    y(x) = c_1 e^{2x} + c_2 x\,e^{2x} + 3x^2 e^{2x}.
  \]
\end{workedexample}

\paragraph{Example 6: Polynomial times exponential.}

\begin{workedexample}
  Solve $y'' + 2y' + y = 3x\,e^{-x}$.

  \textbf{Solution.} \textit{Step 1: Homogeneous solution.} The characteristic equation is
  \[
    r^2 + 2r + 1 = 0 \quad\Longrightarrow\quad (r+1)^2 = 0.
  \]
  Double root: $r = -1$. Thus
  \[
    y_h(x) = c_1 e^{-x} + c_2 x\,e^{-x}.
  \]

  \textit{Step 2: Particular solution.} Here $g(x) = 3x\,e^{-x} = e^{-x} P_1(x)$ where $P_1(x) = 3x$ is a first-degree polynomial. From the guess table, the initial guess would be
  \[
    y_p(x) = e^{-x}(Ax + B).
  \]
  \textbf{Overlap check:} $e^{-x}$ and $x\,e^{-x}$ are both in $y_h$. We must multiply the entire guess by $x^2$:
  \[
    y_p(x) = x^2 e^{-x}(Ax + B) = A x^3 e^{-x} + B x^2 e^{-x}.
  \]
  Differentiate:
  \[
    \begin{aligned}
      y_p &= (Ax^3 + Bx^2)e^{-x}, \\
      y_p' &= (3Ax^2 + 2Bx)e^{-x} - (Ax^3 + Bx^2)e^{-x}
            = (-Ax^3 + (3A-B)x^2 + 2Bx)\,e^{-x}, \\
      y_p'' &= (-3Ax^2 + 2(3A-B)x + 2B)e^{-x} - (-Ax^3 + (3A-B)x^2 + 2Bx)e^{-x} \\
             &= \bigl[Ax^3 + (-3A + 2(3A-B) - (3A-B))x^2 + \dots\bigr]e^{-x}.
     \end{aligned}
   \]
   Computing $y_p''$ more carefully with the product rule:
   \[
     \begin{aligned}
       y_p' &= e^{-x}\bigl(-Ax^3 + (3A-B)x^2 + 2Bx\bigr), \\
       y_p'' &= e^{-x}\Bigl(Ax^3 - (6A-B)x^2 + (6A-4B)x + 2B\Bigr).
     \end{aligned}
   \]
   Now substitute into $y'' + 2y' + y$:
   \[
     \begin{aligned}
       y_p'' + 2y_p' + y_p &= e^{-x}\Bigl[
         \bigl(Ax^3 - (6A-B)x^2 + (6A-4B)x + 2B\bigr) \\
         &\quad + 2\bigl(-Ax^3 + (3A-B)x^2 + 2Bx\bigr)
         + (Ax^3 + Bx^2)
       \Bigr] \\
       &= e^{-x}\Bigl[
         (A - 2A + A)x^3
         + \bigl(-(6A-B) + 2(3A-B) + B\bigr)x^2 \\
         &\quad + \bigl((6A-4B) + 4B\bigr)x + 2B
       \Bigr] \\
       &= e^{-x}\Bigl[ 0\cdot x^3 + 0\cdot x^2 + 6Ax + 2B \Bigr].
     \end{aligned}
   \]
   Set equal to $g(x) = 3x\,e^{-x}$:
   \[
     \begin{cases}
       6A = 3 \quad\Longrightarrow\quad A = \dfrac{1}{2}, \\
       2B = 0 \quad\Longrightarrow\quad B = 0.
     \end{cases}
   \]
   So $y_p(x) = \dfrac{1}{2}x^3 e^{-x}$.

  \textit{Step 3: General solution.}
  \[
    y(x) = c_1 e^{-x} + c_2 x\,e^{-x} + \frac{1}{2}x^3 e^{-x}.
  \]
\end{workedexample}

\subsection{Variation of Parameters}
\label{sec:ch05_variation_of_parameters}

Variation of parameters (VoP) is a \textbf{universal} method for finding a particular solution. Unlike undetermined coefficients, it works for \emph{any} forcing function $g(x)$, provided we can evaluate the resulting integrals. The trade-off is that VoP typically involves more computation.

\subsubsection{Full Derivation}
\label{sec:ch05_vop_derivation}

\textbf{Setup.} Consider the nonhomogeneous equation in \textbf{normalized form}:
\begin{equation}
  \label{eq:normalized_nonhomogeneous}
  y'' + p(x)\,y' + q(x)\,y = g(x).
\end{equation}
Let $\{y_1(x), y_2(x)\}$ be a fundamental set of solutions for the associated homogeneous equation $y'' + p(x)y' + q(x)y = 0$. Their Wronskian is
\[
  W(x) = W(y_1, y_2)(x) = y_1(x)\,y_2'(x) - y_1'(x)\,y_2(x) \neq 0.
\]

\textbf{Ansatz.} We look for a particular solution of the form
\begin{equation}
  \label{eq:vop_ansatz}
  y_p(x) = u_1(x)\,y_1(x) + u_2(x)\,y_2(x),
\end{equation}
where $u_1(x)$ and $u_2(x)$ are unknown functions to be determined. This is called ``variation of parameters'' because we have replaced the constant coefficients $c_1, c_2$ of the homogeneous solution with variable functions $u_1(x), u_2(x)$.

Differentiate $y_p$:
\begin{equation}
  \label{eq:vop_first_derivative}
  y_p' = u_1' y_1 + u_1 y_1' + u_2' y_2 + u_2 y_2'.
\end{equation}
We have two unknown functions but only one equation (the ODE). To resolve this underdetermination, we impose an \textbf{auxiliary condition}:
\begin{equation}
  \label{eq:vop_constraint}
  u_1' y_1 + u_2' y_2 = 0.
\end{equation}
This is not a restriction --- it is a convenient choice that simplifies the algebra. With this constraint, the first derivative simplifies to
\[
  y_p' = u_1 y_1' + u_2 y_2'.
\]
Differentiate once more:
\begin{equation}
  \label{eq:vop_second_derivative}
  y_p'' = u_1' y_1' + u_1 y_1'' + u_2' y_2' + u_2 y_2''.
\end{equation}

\textbf{Substitution.} Substitute $y_p$, $y_p'$, and $y_p''$ into the ODE \cref{eq:normalized_nonhomogeneous}:
\[
  \bigl[u_1' y_1' + u_1 y_1'' + u_2' y_2' + u_2 y_2''\bigr]
  + p(x)\bigl[u_1 y_1' + u_2 y_2'\bigr]
  + q(x)\bigl[u_1 y_1 + u_2 y_2\bigr] = g(x).
\]
Group terms by $u_1$ and $u_2$:
\[
  u_1'\,y_1' + u_2'\,y_2'
  + u_1\bigl[y_1'' + p(x)y_1' + q(x)y_1\bigr]
  + u_2\bigl[y_2'' + p(x)y_2' + q(x)y_2\bigr] = g(x).
\]
Since $y_1$ and $y_2$ are solutions of the homogeneous equation, the bracketed terms vanish. We are left with
\[
  u_1'\,y_1' + u_2'\,y_2' = g(x).
\]

\textbf{System of equations.} Combining this with the constraint \cref{eq:vop_constraint}, we have a $2 \times 2$ linear system for $u_1'$ and $u_2'$:
\begin{equation}
  \label{eq:vop_system}
  \begin{cases}
    u_1' y_1 + u_2' y_2 = 0, \\[4pt]
    u_1' y_1' + u_2' y_2' = g(x).
  \end{cases}
\end{equation}

\textbf{Solution via Cramer's rule.} Write the system in matrix form:
\[
  \begin{pmatrix} y_1 & y_2 \\ y_1' & y_2' \end{pmatrix}
  \begin{pmatrix} u_1' \\ u_2' \end{pmatrix}
  =
  \begin{pmatrix} 0 \\ g(x) \end{pmatrix}.
\]
The determinant of the coefficient matrix is the Wronskian:
\[
  \det = y_1 y_2' - y_1' y_2 = W(x).
\]
Since $\{y_1, y_2\}$ is a fundamental set, $W(x) \neq 0$ and the system is solvable. By Cramer's rule:
\begin{align}
  u_1'(x) &= \frac{\det\begin{pmatrix} 0 & y_2 \\ g(x) & y_2' \end{pmatrix}}{W(x)}
           = \frac{-y_2(x)\,g(x)}{W(x)}, \label{eq:vop_u1prime} \\
  u_2'(x) &= \frac{\det\begin{pmatrix} y_1 & 0 \\ y_1' & g(x) \end{pmatrix}}{W(x)}
           = \frac{y_1(x)\,g(x)}{W(x)}. \label{eq:vop_u2prime}
\end{align}

Integrate to find $u_1$ and $u_2$ (we may choose any antiderivatives; we set the constants of integration to zero since we need only \emph{one} particular solution):
\begin{align}
  u_1(x) &= -\int \frac{y_2(x)\,g(x)}{W(x)}\,\diff x, \\
  u_2(x) &= \int \frac{y_1(x)\,g(x)}{W(x)}\,\diff x.
\end{align}

\begin{keyresult}
  \textbf{Variation of parameters formula.}

  Given a fundamental set $\{y_1, y_2\}$ of the homogeneous equation associated with \cref{eq:normalized_nonhomogeneous}, a particular solution is
  \begin{equation}
    \label{eq:vop_formula}
    y_p(x) = -y_1(x)\int \frac{y_2(x)\,g(x)}{W(x)}\,\diff x
             + y_2(x)\int \frac{y_1(x)\,g(x)}{W(x)}\,\diff x,
  \end{equation}
  where $W(x) = y_1 y_2' - y_1' y_2$.

  \textbf{CRITICAL:} The equation \emph{must} be in normalized form (coefficient of $y''$ equals~1) before applying this formula. For $a\,y'' + b\,y' + c\,y = \tilde{g}(x)$, first divide by $a$ so that the right-hand side becomes $g(x) = \tilde{g}(x)/a$.
\end{keyresult}

\begin{hintbox}
  \textbf{Normalized form warning.} A very common mistake is to apply the VoP formula to $a\,y'' + b\,y' + c\,y = \tilde{g}(x)$ directly, using $\tilde{g}(x)$ as $g(x)$. This is \textbf{incorrect}. The derivation assumed the coefficient of $y''$ is~1, so you \emph{must} divide the entire equation by $a$ first, making $g(x) = \tilde{g}(x)/a$.
\end{hintbox}

\subsubsection{Worked Examples}
\label{sec:ch05_vop_examples}

\begin{workedexample}
  Solve $y'' + y = \sec(x)$ on the interval $(-\pi/2, \pi/2)$.

  \textbf{Solution.} \textit{Step 1: Homogeneous solution.} The characteristic equation is $r^2 + 1 = 0$, so $r = \pm i$. Thus
  \[
    y_h(x) = c_1 \cos x + c_2 \sin x.
  \]
  We take $y_1(x) = \cos x$ and $y_2(x) = \sin x$.

  \textit{Step 2: Wronskian.}
  \[
    W(x) = \cos x \cdot \cos x - (-\sin x) \cdot \sin x = \cos^2 x + \sin^2 x = 1.
  \]
  The Wronskian is constant, which simplifies the integrals.

  \textit{Step 3: Apply VoP.} The equation is already in normalized form ($g(x) = \sec x$). Compute:
  \[
    \begin{aligned}
      u_1(x) &= -\int \frac{y_2(x)\,g(x)}{W(x)}\,\diff x
              = -\int \frac{\sin x \cdot \sec x}{1}\,\diff x
              = -\int \tan x\,\diff x
              = \ln|\cos x|, \\[8pt]
      u_2(x) &= \int \frac{y_1(x)\,g(x)}{W(x)}\,\diff x
              = \int \frac{\cos x \cdot \sec x}{1}\,\diff x
              = \int 1\,\diff x
              = x.
    \end{aligned}
  \]

  \textit{Step 4: Particular solution.}
  \[
    y_p(x) = u_1(x)\,y_1(x) + u_2(x)\,y_2(x)
           = \ln|\cos x|\,\cos x + x\,\sin x.
  \]

  \textit{Step 5: General solution.}
  \[
    y(x) = c_1 \cos x + c_2 \sin x + \cos x\,\ln|\cos x| + x\,\sin x.
  \]
\end{workedexample}

\begin{workedexample}
  Solve $y'' - 4y' + 3y = \dfrac{e^{2x}}{1 + e^x}$ using variation of parameters.

  \textbf{Solution.} \textit{Step 1: Homogeneous solution.} The characteristic equation is
  \[
    r^2 - 4r + 3 = 0 \quad\Longrightarrow\quad (r-1)(r-3) = 0.
  \]
  Roots: $r_1 = 1$, $r_2 = 3$. Thus
  \[
    y_h(x) = c_1 e^x + c_2 e^{3x}.
  \]
  Take $y_1(x) = e^x$ and $y_2(x) = e^{3x}$.

  \textit{Step 2: Wronskian.}
  \[
    W(x) = e^x \cdot 3e^{3x} - e^x \cdot e^{3x} = 3e^{4x} - e^{4x} = 2e^{4x}.
  \]

  \textit{Step 3: Apply VoP.} The equation is in normalized form with $g(x) = \dfrac{e^{2x}}{1 + e^x}$. Compute:
  \[
    \begin{aligned}
      u_1(x) &= -\int \frac{y_2(x)\,g(x)}{W(x)}\,\diff x
              = -\int \frac{e^{3x} \cdot \dfrac{e^{2x}}{1 + e^x}}{2e^{4x}}\,\diff x \\
              &= -\frac{1}{2}\int \frac{e^{5x}}{e^{4x}(1 + e^x)}\,\diff x
              = -\frac{1}{2}\int \frac{e^x}{1 + e^x}\,\diff x.
    \end{aligned}
  \]
  Substituting $u = 1 + e^x$, $\diff u = e^x\,\diff x$:
  \[
    u_1(x) = -\frac{1}{2}\int \frac{1}{u}\,\diff u = -\frac{1}{2}\ln|u|
           = -\frac{1}{2}\ln(1 + e^x).
  \]

  Next:
  \[
    \begin{aligned}
      u_2(x) &= \int \frac{y_1(x)\,g(x)}{W(x)}\,\diff x
              = \int \frac{e^x \cdot \dfrac{e^{2x}}{1 + e^x}}{2e^{4x}}\,\diff x \\
              &= \frac{1}{2}\int \frac{e^{3x}}{e^{4x}(1 + e^x)}\,\diff x
              = \frac{1}{2}\int \frac{1}{e^x(1 + e^x)}\,\diff x.
    \end{aligned}
  \]
  Use partial fractions: $\dfrac{1}{e^x(1+e^x)} = \dfrac{1}{e^x} - \dfrac{1}{1+e^x} = e^{-x} - \dfrac{1}{1+e^x}$.
  \[
    \begin{aligned}
      u_2(x) &= \frac{1}{2}\int \left(e^{-x} - \frac{1}{1+e^x}\right)\diff x \\
              &= \frac{1}{2}\Bigl(-e^{-x} - x + \ln(1+e^x)\Bigr).
    \end{aligned}
  \]
  (The second integral: $\int \frac{1}{1+e^x}\diff x = x - \ln(1+e^x)$, obtained by writing $\frac{1}{1+e^x} = 1 - \frac{e^x}{1+e^x}$.)

  \textit{Step 4: Particular solution.}
  \[
    \begin{aligned}
      y_p(x) &= u_1(x)\,y_1(x) + u_2(x)\,y_2(x) \\
              &= -\frac{1}{2}\ln(1+e^x)\,e^x + \frac{1}{2}\Bigl(-e^{-x} - x + \ln(1+e^x)\Bigr)e^{3x} \\
              &= -\frac{1}{2}e^x\ln(1+e^x) - \frac{1}{2}e^{2x} + \frac{1}{2}e^{3x}\ln(1+e^x) - \frac{1}{2}x\,e^{3x}.
     \end{aligned}
   \]

  \textit{Step 5: General solution.}
  \[
    y(x) = c_1 e^x + c_2 e^{3x} - \frac{1}{2}e^x\ln(1+e^x)
            + \frac{1}{2}e^{3x}\ln(1+e^x) - \frac{1}{2}e^{2x} - \frac{1}{2}x\,e^{3x}.
  \]
\end{workedexample}

\begin{workedexample}
  Solve $2y'' + 4y' + 2y = e^{-x}$ using variation of parameters. Note that this equation is \textbf{not} in normalized form.

  \textbf{Solution.} \textit{Step 0: Normalize the equation.} Divide the entire equation by $2$:
  \[
    y'' + 2y' + y = \frac{1}{2}\,e^{-x}.
  \]
  Now $g(x) = \dfrac{1}{2}e^{-x}$.

  \textit{Step 1: Homogeneous solution.} The characteristic equation is
  \[
    r^2 + 2r + 1 = 0 \quad\Longrightarrow\quad (r+1)^2 = 0.
  \]
  Double root $r = -1$. Take $y_1(x) = e^{-x}$ and $y_2(x) = x\,e^{-x}$.

  \textit{Step 2: Wronskian.}
  \[
    W(x) = e^{-x}\bigl(e^{-x} - x\,e^{-x}\bigr) - (-e^{-x})(x\,e^{-x})
         = e^{-2x}(1 - x) + x\,e^{-2x} = e^{-2x}.
  \]

  \textit{Step 3: Apply VoP.}
  \[
    \begin{aligned}
      u_1(x) &= -\int \frac{y_2(x)\,g(x)}{W(x)}\,\diff x
              = -\int \frac{x\,e^{-x} \cdot \tfrac{1}{2}e^{-x}}{e^{-2x}}\,\diff x
              = -\frac{1}{2}\int x\,\diff x
              = -\frac{1}{4}x^2, \\[8pt]
      u_2(x) &= \int \frac{y_1(x)\,g(x)}{W(x)}\,\diff x
              = \int \frac{e^{-x} \cdot \tfrac{1}{2}e^{-x}}{e^{-2x}}\,\diff x
              = \frac{1}{2}\int 1\,\diff x
              = \frac{1}{2}x.
    \end{aligned}
  \]

  \textit{Step 4: Particular solution.}
  \[
    y_p(x) = u_1(x)\,y_1(x) + u_2(x)\,y_2(x)
           = -\frac{1}{4}x^2\,e^{-x} + \frac{1}{2}x^2\,e^{-x}
           = \frac{1}{4}x^2\,e^{-x}.
  \]
  This is consistent with the undetermined coefficients result from Example~5 (the modification rule also gave $x^2 e^{-x}$ for this case).

  \textit{Step 5: General solution.}
  \[
    y(x) = c_1 e^{-x} + c_2 x\,e^{-x} + \frac{1}{4}x^2\,e^{-x}.
  \]
\end{workedexample}

\subsection{Method Comparison}
\label{sec:ch05_method_comparison}

Both undetermined coefficients (UC) and variation of parameters (VoP) find particular solutions, but they have different strengths and limitations.

\begin{table}[htbp]
\centering
\caption{Undetermined coefficients vs.\ variation of parameters}
\label{tab:ch05_method_comparison}
\begin{tabular}{p{2.8cm} p{4.8cm} p{6.2cm}}
  \toprule
  \textbf{Aspect} & \textbf{Undetermined Coefficients} & \textbf{Variation of Parameters} \\
  \midrule
  Applicable $g(x)$ & Polynomials, $e^{\alpha x}$, $\sin/\cos(\beta x)$, sums/products & \emph{Any} continuous $g(x)$ (in principle) \\
  Requires $y_h$? & Only for overlap check & Yes --- need a full fundamental set \\
  Requires $W(x)$? & No & Yes \\
  Normalized form? & Not required & \textbf{Required} (coefficient of $y''$ must be~1) \\
  Computation & Algebra (solve linear system) & Integration (may be difficult or non-elementary) \\
  Speed (simple $g(x)$) & Fast --- algebraic, no integrals & Slower --- integrals needed \\
  Speed (complex $g(x)$) & \textbf{N/A} --- method fails & Always applicable (if integrals exist) \\
  \bottomrule
\end{tabular}
\end{table}

\begin{hintbox}
  \textbf{Decision guide.}
  \begin{enumerate}
    \item Is $g(x)$ a polynomial, exponential, sine/cosine, or a product of these? \textbf{Yes} $\Rightarrow$ use undetermined coefficients (simpler algebra).
    \item Is $g(x)$ anything else ($\ln x$, $\tan x$, rational functions, etc.)? \textbf{Yes} $\Rightarrow$ use variation of parameters.
    \item If you are unsure, variation of parameters is always a safe fallback (provided you can compute the integrals).
  \end{enumerate}
\end{hintbox}

\paragraph{Example comparison.} Consider $y'' + y' - 2y = 4x$.

Using \textbf{undetermined coefficients}: guess $y_p = Ax + B$, substitute, solve the linear system. Takes about 5 lines of algebra.

Using \textbf{variation of parameters}: find $y_h$, compute $W(x)$, evaluate two integrals involving $\int x e^x\,\diff x$ and $\int x e^{-2x}\,\diff x$ (by parts). Takes about 15 lines of computation.

Both give the same answer, but UC is clearly faster here. However, if $g(x) = \ln x$, only VoP can be applied.

\subsection{Summary}
\label{sec:ch05_summary}

\begin{table}[htbp]
\centering
\caption{Nonhomogeneous second-order equations: complete reference}
\label{tab:ch05_summary_table}
\begin{tabular}{l p{8cm}}
  \toprule
  \textbf{Concept} & \textbf{Key formula / rule} \\
  \midrule
  General solution & $y(x) = y_h(x) + y_p(x)$ (\cref{thm:nonhomogeneous_structure}) \\
  Homogeneous part & From \cref{ch:second_order_homogeneous}: $y_h = c_1 y_1 + c_2 y_2$ \\
  \midrule
  \textbf{UC: polynomial $P_n(x)$} & $y_p = A_n x^n + A_{n-1} x^{n-1} + \cdots + A_0$ \\
  \textbf{UC: $e^{\alpha x}$} & $y_p = A\,e^{\alpha x}$ \\
  \textbf{UC: $\sin(\beta x)$ or $\cos(\beta x)$} & $y_p = A\sin(\beta x) + B\cos(\beta x)$ \\
  \textbf{UC: $e^{\alpha x} P_n(x)$} & $y_p = e^{\alpha x}(A_n x^n + \cdots + A_0)$ \\
  \textbf{UC: $e^{\alpha x}\sin(\beta x)$ or $e^{\alpha x}\cos(\beta x)$} & $y_p = e^{\alpha x}\bigl[(A_n x^n + \cdots + A_0)\cos(\beta x) + (B_n x^n + \cdots + B_0)\sin(\beta x)\bigr]$ \\
  \midrule
  \textbf{UC modification rule} & If guess term $\in y_h$, multiply by $x$; if still $\in y_h$, multiply by $x^2$ \\
  \midrule
  \textbf{VoP formula} & $y_p = -y_1\displaystyle\int\frac{y_2 g}{W}\,\diff x + y_2\displaystyle\int\frac{y_1 g}{W}\,\diff x$ \\
  \textbf{VoP constraint} & Equation must be normalized: $y'' + py' + qy = g$ (coeff.\ of $y''$ = 1) \\
  \textbf{VoP system} & $u_1' y_1 + u_2' y_2 = 0$, $\; u_1' y_1' + u_2' y_2' = g(x)$ \\
  \textbf{VoP Cramer's rule} & $u_1' = -\dfrac{y_2 g}{W}$, $\; u_2' = \dfrac{y_1 g}{W}$ \\
  \bottomrule
\end{tabular}
\end{table}

\begin{hintbox}
  \textbf{Problem-solving workflow for nonhomogeneous equations.}
  \begin{enumerate}
    \item \textbf{Identify the form of $g(x)$.} If it is a polynomial, exponential, trig function, or their products/sums, prefer undetermined coefficients. Otherwise, use variation of parameters.
    \item \textbf{Solve the homogeneous equation} $ay'' + by' + cy = 0$ and obtain $y_h(x)$.
    \item \textbf{If using UC:} Write the guess based on the table, check for overlap with $y_h$, modify if needed, substitute, and solve for coefficients.
    \item \textbf{If using VoP:} \emph{First} normalize the equation (divide by $a$ if needed). Compute $W(x)$, then evaluate the two integrals for $u_1$ and $u_2$.
    \item \textbf{Combine:} $y(x) = y_h(x) + y_p(x)$. Apply initial conditions if given.
  \end{enumerate}
\end{hintbox}
