How to solve $y''+y=\cos x$? 
Solve $y''+y=\cos x$.

After first solving the homogeneous equation we know that the solution to it is $y=c_1\cos x+c_2\sin x$.
We can guess that the private solution to non-homogeneous equation will be of form: $y_p=x(A_1\cos x+A_2\sin x)$.
Then:
$$
y_p'=A_1\cos x-A_1x\sin x+A_2\sin x+A_2x\cos x\\
y_p''=-2A_1\sin x-A_1x\cos x+2A_2\cos x-A_2x\sin x
$$
If we plug these into the original equation we get:
$$
\cos x(A_1+A_2x-A_1x+2A_2)+\sin x(A_2-A_1-2A_2-A_2x)=\cos x \quad\ast
$$
We can try to solve the system:
$$
\begin{cases}
x(A_2-A_1)+A_1+2A_2=1\\
x(-A_1-A_2)+A_2-2A_1=0
\end{cases}
$$
But there're 3 unknowns in the system so I don't see how to find out the values of $A_1$ and $A_2$.
The solution says that we get $2(A_2\cos x-A_1\sin x)=\cos x$ from which is follows that $A_1=0$ and $A_2=0.5$. But how do we get to this conclusion? Is there some trick I missed?
I checked my calculations in Wolfram Alpha and they match.
 A: Rewrite your last equation 
$$2(A_2\cos x-A_1\sin x)=\cos x$$
as
$$2A_2 \cos x - 2 A_1 \sin x = 1 \cdot \cos x + 0 \cdot \sin x$$
From here equate coefficients of like terms on both sides: $2 A_2 = 1$ and $-2A_1 = 0$.
A: So the corresponding auxiliary equation to $y''+y=\cos x$ is $m^2+1=0$, so $$y_c=c_1 \cos x + c_2 \sin x,$$
 so things are fine so far. Now since our RHS is $\cos x$, like you said, we assume that the particular solution is of the form $A \sin x+B \cos x$. But since $A\sin x$ is already accounted for in $y_c$, we take $Ax \sin x+Bx \cos x$. Thus,
\begin{cases} y_p=Ax \sin x+Bx \cos x \\ {y_p}'=Ax \cos x+B \cos x-Bx \sin x +A \sin x \\ {y_p}''= -Bx \cos x+2A \cos x-Ax\sin x-2B \sin x \end{cases}
$\implies \left( -Bx \cos x+2A \cos x-Ax\sin x-2B \sin x \right) + \left( Ax \sin x+Bx \cos x \right) = \cos x$
$\implies 2A \cos x - 2B \sin x = \cos x \implies \begin{cases} A=\frac{1}{2} \\ B =0 \end{cases}$
Therefore our solution $y=y_c + y_p$ is 
$$y=c_1 \cos x + c_2 \sin x+\frac{1}{2}x \sin x.$$
A: Your starred equation is not correct.  You plugged in the terms from $y'_p$ instead of $y_p$.  If you plug into the original equation, you should get $$x(A_1\cos x + A_2 \sin x)-2A_1\sin x-A_1x\cos x+2A_2\cos x-A_2x\sin x=\cos x$$ from which the terms proportional to $x$ cancel, leaving 
$$-2A_1 \sin x+2A_2\cos x =\cos x$$ as desired.
