Depending on $g(t)$ often it is difficult to come up with a suitable trial solution of a given non-homogeneous equation in the method of undetermined coefficient discussed here. In this note, we discuss an alternative method called the method of variation of parameters. First assume that we know the general solution
$$x_{\mathrm{hom}}(t)=c_1x_1(t)+c_2x_2(t)$$
of the homogeneous equation $$\ddot{x}+p(t)\dot{x}+q(t)x=0$$ Let
\begin{equation}
\label{eq:vp}
x(t)=u_1(t)x_1(t)+u_2(t)x_2(t)
\end{equation}
be a solution of the non-homogeneous equation $$\ddot{x}+p(t)\dot{x}+q(t)x=g(t)$$ Differentiating \eqref{eq:vp}
$$\dot{x}(t)=\dot{u}_1x_1+u_1\dot{x}_1+\dot{u}_2x_2+u_2\dot{x}_2$$
Require that
\begin{equation}
\label{eq:vp2}
\dot{u}_1x_1+\dot{u}_2x_2=0
\end{equation}
so that we have
\begin{equation}
\label{eq:vp3}
\dot{x}(t)=u_1\dot{x}_1+u_2\dot{x}_2
\end{equation}
Differentiating \eqref{eq:vp3}
\begin{equation}
\label{eq:vp4}
\ddot{x}(t)=\dot{u}_1\dot{x}_1+u_1\ddot{x}_1+\dot{u}_2\dot{x}_2+u_2\ddot{x}_2
\end{equation}
Substituting $\ddot{x}$, $\dot{x}$, $x$ in the non-homogeneous equation with the corresponding expressions in \eqref{eq:vp}, \eqref{eq:vp3} and \eqref{eq:vp4}, respectively results in the equation
\begin{equation}
\label{eq:vp5}
\dot{u}_1\dot{x}_1+\dot{u}_2\dot{x}_2=g(t)
\end{equation}
Let
$$W(x_1,x_2)(t):=\begin{vmatrix}
x_1(t) & x_2(t)\\
\dot{x}_1(t) & \dot{x}_2(t)
\end{vmatrix}=x_1(t)\dot{x}_2(t)-x_2(t)\dot{x}_1(t)$$
$W(x_1,x_2)(t)$ is called the Wronskian of $x_1(t)$ and $x_2(t)$. If $x_1(t)$ and $x_2(t)$ are linearly dependent, so are the columns of $W(x_1,x_2)(t)$ hence $W(x_1,x_2)(t)=0$ for all $t$. This means that If $W(x_1,x_2)(t)\ne 0$ for some $t$, $x_1(t)$ and $x_2(t)$ are linearly independent. Also we have the following theorem holds.
Theorem. Let $x_1(t)$ and $x_2(t)$ be solutions of a homogeneous second-order linear differential equation. If $x_1(t)$ and $x_2(t)$ are linearly independent, then $W(x_1,x_2)(t)\ne 0$ for all $t$.
Since $x_1(t)$ and $x_2(t)$ are linearly independent, $W(x_1,x_2)\ne 0$ for all $t$ so by Cramer’s rule the solution of the system of linear equations \eqref{eq:vp2}, \eqref{eq:vp5} in $\dot{u}_1$ and $\dot{u}_2$ is given by
\begin{equation}
\begin{aligned}
\dot{u}_1(t)&=\frac{\begin{vmatrix}
0 & x_2\\
g(t) & \dot{x}_2
\end{vmatrix}
}{W(x_1,x_2)(t)}=-\frac{g(t)x_2(t)}{W(x_1,x_2)(t)}\\
\dot{u}_2(t)&=\frac{\begin{vmatrix}
x_1 & 0\\
\dot{x}_1 & g(t)
\end{vmatrix}
}{W(x_1,x_2)(t)}=\frac{g(t)x_1}{W(x_1,x_2)(t)}
\end{aligned}\label{eq:vp6}
\end{equation}
Integrating \eqref{eq:vp6}, $u_1(t)$ and $u_2(t)$ are determined to be
\begin{equation}
\label{eq:vp7}
\begin{aligned}
u_1(t)&=-\int\frac{x_2(t)g(t)}{W(x_1,x_2)(t)}dt+c_1\\
u_2(t)&=\int\frac{x_1(t)g(t)}{W(x_1,x_2)(t)}dt+c_2
\end{aligned}
\end{equation}
where $c_1$ and $c_2$ are constant.
Therefore
\begin{equation}
\label{eq:vp8}
\begin{aligned}
x(t)=c_1x_1(t)+&c_2x_2(t)+\left\{-x_1(t)\int\frac{x_2(t)g(t)}{W(x_1,x_2)(t)}dt+\right.\\
&\left.x_2(t)\int\frac{x_1(t)g(t)}{W(x_1,x_2)(t)}dt\right\}
\end{aligned}
\end{equation}
is indeed the general solution of the non-homegeneous equation for
\begin{equation}
\label{eq:vp9}
X(t)=-x_1(t)\int\frac{x_2(t)g(t)}{W(x_1,x_2)(t)}dt+x_2(t)\int\frac{x_1(t)g(t)}{W(x_1,x_2)(t)}dt
\end{equation}
being a particular solution of the non-homogeneous equation.
Example. Solve the non-homogeneous equation $\ddot{x}-3\dot{x}-4x=2\sin t$.
Solution. Recall that $x_1(t)=e^{-t}$, $x_2(t)=e^{4t}$. The Wronskian is
$$W(x_1,x_2)(t)=\begin{vmatrix}
e^{-t} & e^{4t}\\
-e^{-t} & 4e^{4t}
\end{vmatrix}=5e^{3t}$$
\begin{align*}
\int\frac{x_2(t)g(t)}{W(x_1,x_2)(t)}dt&=\int\frac{e^{4t}(2\sin t)}{5e^{3t}}dt\\
&=-\frac{1}{5}e^t\cos t+\frac{1}{5}e^t\sin t
\end{align*}
and
\begin{align*}
\int\frac{x_1(t)g(t)}{W(x_1,x_2)(t)}dt&=\int\frac{e^{-t}(2\sin t)}{5e^{3t}}dt\\
&=-\frac{2}{85}e^{-4t}\cos t-\frac{8}{85}e^{-4t}\sin t
\end{align*}
The general solution is
\begin{align*}
x(t)&=c_1e^{-t}+c_2e^{4t}\\
&-e^{-t}\left(-\frac{1}{5}e^t\cos t+\frac{1}{5}e^t\sin t\right)+e^{4t}\left(-\frac{2}{85}e^{-4t}\cos t-\frac{8}{85}e^{-4t}\sin t\right)\\
&=c_1e^{-t}+c_2e^{4t}+\frac{3}{17}\cos t-\frac{5}{17}\sin t.
\end{align*}
Example. Solve the non-homogeneous $\ddot{x}+4x=8\tan t$, $-\frac{\pi}{2}<t<\frac{\pi}{2}$
Solution. $x_1(t)=\cos(2t)$, $x_2(t)=\sin(2t)$. The Wronskian is
$$W(x_1,x_2)(t)=\begin{vmatrix}
\cos(2t) & \sin(2t)\\
-2\sin(2t) & 2\cos(2t)
\end{vmatrix}=2$$
\begin{align*}
\int\frac{x_2(t)g(t)}{W(x_1,x_2)(t)}dt&=\int\frac{\sin(2t)(8\tan t)}{2}dt\\
&=4t-2\sin(2t)
\end{align*}
and
\begin{align*}
\int\frac{x_1(t)g(t)}{W(x_1,x_2)(t)}dt&=\int\frac{\cos(2t)(8\tan t)}{2}dt\\
&=-2\cos(2t)+4\ln(\cos t)
\end{align*}
Thus the general solution is
$$x(t)=c_1\cos(2t)+c_2\sin(2t)-4t\cos(2t)+4\sin(2t)\ln(\cos t)$$