<s>[INST] Solve $y''+4y=(1+\sin{x})^2$. My attempt: The null-solution is $A\cos{2x}+B\sin{2x}.$ Let's start by rewriting RHS by 
$$(1+\sin{x})^2=1+2\sin{x}+\sin^2{x}=\frac{3}{2}+2\sin{x}-\frac{1}{2}\cos{x}.$$ So we can now look at too differential equations:
$$\left\{
  \begin{array}{rcr}
    y_1''+4y_1 & = & \frac{3}{2}+2\sin{x} \quad \quad \quad \quad (1)\\
    y_2''+4y_2 & = & -\frac{1}{2}\cos{x} \quad \quad \quad \quad (2)\\
  \end{array}
\right.$$

For (1) we can assume that the particular solution is of the form $y_1=a+b\cos{x}+c\sin{x}$ and then $y_1'=-b\sin{x}+c\cos{x}$ and $y_1''=-b\cos{x}-c\sin{x}$ so 
$$y_1''+4y_1= -b\cos{x}-c\sin{x} + 4(a+b\cos{x}+c\sin{x})=4a+3b\cos{x}+3c\sin{x}.$$
Identifying and solving coefficients gives $(a,b,c)=(\frac{3}{8},0,\frac{2}{3}),$ so I have that $y_{1p}=\frac{3}{8}+\frac{2}{3}\sin{x}.$

For (2) we can assume the same form for the particular solution. I get that
\begin{array}{lcl}
y_2  & = & a+b\cos{x}+c\sin{x} \\
y_2' & = & -b\sin{x}+c\cos{x} \\
y_2''& = & -b\cos{x}-c\sin{x}
\end{array}
Substituting in equation (2) gives $3a\cos{x}+3b\sin{x}=-\frac{1}{2}\cos{x}$, thus $(a,b)=(-\frac{1}{6},0)$ so $y_{2p}=-\frac{1}{6}\cos{x}.$

According to the law of super position I have that $$y_p=y_{1p}+y_{2p}=\frac{3}{8}+\frac{2}{3}\sin{x}-\frac{1}{6}\cos{x},$$
which finally gives
$$y(x)=y_h+y_p=A\cos{2x}+B\sin{2x}+\frac{2}{3}\sin{x}-\frac{1}{6}\cos{x}+\frac{3}{8}$$
Correct answer: $$y(x)=A\cos{2x}+B\sin{2x}+\frac{2}{3}\sin{x}-\frac{x}{8}\sin{2x}+\frac{3}{8}.$$
One term difference...why? Please note that I'm not interested in other solutions, I just want to know why my $y_{p2}$ is incorrect.
 [/INST] For a particular solution, you can solve for every term independently
$$y''+4y=1\to y=\frac 14.$$
$$y''+4y=\sin x\to y=\frac23\sin x$$ (by indeterminate coefficients).
Then $$y''+4y=\cos2x\to ???$$ (indeterminate coefficients don't seem to work).
It turns out that this RHS can be expressed as an instance of the homogenous solution, so that you need another ansatz.
With $x(a\cos2x+\sin2x)$, you have
$$x(-4a\cos2x-4b\sin2x+4a\cos2x+4b\sin2x)+2(2a\sin2x-2b\cos2x)=\cos2x,$$ giving
$$y''+4y=\cos2x\to \frac{x\sin 2x}4.$$
</s>