It is nonhomogenous differential equation (NH) with constant coefficients (\(\displaystyle y''+p(x)y'+q(x)y=g(x)\)).
The general solution is \(\displaystyle y=y_h+y_p\), where \(\displaystyle y_h\) - general solution of the homogeneous associated equation (H), and \(\displaystyle y_p\) - particular solution of the equation (NH).
(H): \(\displaystyle y''+y=0\)
Characteristic equation: \(\displaystyle r^2+1=0\), its solution \(\displaystyle r=\alpha+i\beta=i\)
\(\displaystyle y_h=c_1y_1+c_2y_2\), where \(\displaystyle y_1=e^{\alpha x}\cos{(\beta x)}=\cos x\), \(\displaystyle y_2=e^{\alpha x}\sin{(\beta x)}=\sin x\)
\(\displaystyle y_h=c_1\cos x+c_2\sin x\)
To find \(\displaystyle y_p\) we will use method of variation of parameters:
\(\displaystyle y_p=u_1(x)y_1(x)+u_2(x)y_2(x)\)
\(\displaystyle \left\{\begin{matrix}
u_1'y_1+u_2'y_2=0\\
u_1'y_1'+u_2'y_2'=g(x)
\end{matrix}\right.\)
\(\displaystyle \left\{\begin{matrix}
u_1'\cos x+u_2'\sin x=0\\
-u_1'\sin x+u_2'\cos x=-2
\end{matrix}\right.\)
\(\displaystyle u_1'=2\sin x\), \(\displaystyle u_2'=-2\cos x\)
\(\displaystyle u_1=-2\cos x\), \(\displaystyle u_2=-2\sin x\)
\(\displaystyle y_p=-2\)
\(\displaystyle y=c_1\cos x+c_2\sin x-2\)