<s>[INST] How to solve the given initial-value problem? Solve the given problem which the input function $g(x)$ is discontinuous?
$y''+4y = g(x)$, $y(0) = 1$, $y'(0) = 2$, where
$$g(x) = \begin{cases} \sin x, & 0\leq x\leq\frac{\pi}{2}\\
0,& x>\frac{\pi}{2} \end{cases}$$
And the given answer is,
$$y = \begin{cases} \cos 2x+\frac56\sin2x+\frac13\sin x, & 0\leq x\leq\frac{\pi}{2}\\
\frac23\cos 2x+\frac56\sin2x,& x>\frac{\pi}{2} \end{cases}$$
 [/INST] First let $g(x)=\sin(x)$.
$$
\begin{align*}
y'' + 4y &= \sin(x)
\end{align*}
$$
Applying the Laplace transform to both sides gives
\begin{align*}
s^2Y(s)-s-2+4Y(s) &= \dfrac{1}{s^2+1}\\
Y(s)(s^2+4) &= \dfrac{1}{s^2+1}+s+2\\
Y(s) &= \dfrac{1}{(s^2+1)(s^2+4)}+\dfrac{s}{s^2+4}+\dfrac{2}{s^2+4}\\
\end{align*}
Via partial fraction decomposition we get
\begin{align*}
Y(s) &= \dfrac{1}{3(s^2+1)}-\dfrac{1}{3(s^2+4)}+\dfrac{s}{s^2+4}+\dfrac{2}{s^2+4}\\
\end{align*}
Thus the inverse Laplace transform gives the desired solution:
$$y(x) = \frac{1}{3}\sin(x) - \frac{5}{6}\sin(2x)+\cos(2x)$$
For $g(x)=0$, we have
$$\begin{align*}y'' + 4y &= 0\\
\end{align*}$$
Let us make the ansatz that $y(x)=e^{\lambda x}$. Thus,
$$\begin{align*}
\lambda^2e^{\lambda x} + 4e^{\lambda x} &= 0\\
\lambda^2 + 4 &= 0\\
\end{align*}$$
Hence $\lambda = \pm 2i$, and the general solution is:
$$\begin{align*}
y(x) &= c_1 e^{2ix} + c_2e^{-2ix}\\
&=c_1(\cos(2x)+i\sin(x))+c_2(\cos(2x)-i\sin(2x))\\
&=(c_1+c_2)\cos(2x)+i(c_1-c_2)\sin(2x)\\
&=k_1\cos(2x)+k_2\sin(2x)
\end{align*}$$
where the last line simply redefines our constants, since they are already arbitrary. To satisfy initial conditions, $k_1=k_2=1$, which gives the specific solution
$$ y(x) = \cos(2x)+\sin(2x) $$
In conclusion, the piecewise function is
$$y(x) =
  \begin{cases}
   \frac{1}{3}\sin(x) - \frac{5}{6}\sin(2x)+\cos(2x) & \text{if } 0 \leq x \leq \frac{\pi}{2} \\
   \cos(2x)+\sin(2x)       & \text{if } x > \frac{\pi}{2}
  \end{cases}$$
</s>