Solution (source code)

= Solution

Use $\cos^2x=(1+\cos2x)/2$. The constant forcing has <particular solution> $1/8$, while the $\cos2x$ term satisfies the <resonance condition> and needs the trial $Cx\sin2x$. Since $(d^2/dx^2+4)(x\sin2x)=4\cos2x$, the forced term is $x\sin2x/8$. Adding the <homogeneous solution> and imposing the initial conditions gives
$$
\boxed{y(x)=\frac{1-\cos2x+x\sin2x}{8}.}
$$