Method of Undermined coefficients The following equation:
$y''+y=\cos(x)$
Solving $y''+y=0$  gives me $c_1\cos(x)+c_2\sin(x)$
Using the particular integral yp form of $a\cos(x)+b\sin(x)$ and substituting this back into the ODE
$y = a\cos(x)+b\sin(x)$,    $y'=-a\sin(x)+b\cos(x)$,  $y''=-a\cos(x)-b\sin(x)$
Plugging this back into the ODE leaves me with $0=\cos(x)$
So how is the particular solution $y=1/2x\sin(x)$ with the coefficients value of $c_1=0$ and $c_2=1/2$, and where does the $x$ come from?
 A: Observe that $y''+y = 0 \Rightarrow y=c_1 \cos(x) + c_2 \sin(x)$. So your forcing term $f(x) = \cos(x)$ is a solution to the homogenous solution, so we need to come up with a new basis for the method of undetermined coefficients. Since equating coefficients will fail for this basis since if $y=c_1 \cos(x) + c_2 \sin(x)$, the left hand side will always be a $0$ sicne it's a solution to the homogenous equation .The main way is to differentiate the homogenous solution $y_h$ with respect to $r$ where $r$ is the root of the characteristic polynomial. For this case,
$y(x) = c_1e^{rx} + c_2e^{-rx}, r = i$, so $\frac{dy}{dr} = xy_h(x) = x(\alpha_1\cos(x) + \alpha_2\sin(x))$ after the usual linear combination rearrangement trick for $e^{ix},e^{-ix}$ using Euler's identity. So our new basis for trial solutions $x(\alpha_1\cos(x) + \alpha_2\sin(x))$ which after equating coefficients will give you $\alpha_1 = 0, \alpha_2 = \frac{1}{2}$. Remember to add in the homogenous solution at the end.
A: Let's choose a more general particular solution for $y''+y=\cos(nx)$ in the form $y_p=(ax+b)\sin(nx)+(cx+d)\cos(nx)$, with $n>0$. You can easily show
$$y_p''=−n((anx+bn+2c)\sin(nx)+(cnx+dn−2a)\cos(nx))$$
Let's plug these two into the original equation, and you get $$x\sin(nx)(-an^2+a)+\sin(nx)(-bn^2-2nc+b)+x\cos(nx)(-cn^2+c)+\cos(nx)(-dn^2+2an+d)=\cos(nx)$$
In case $n=1$, you have $1-n^2=0$, so your equation simplifies to $$-2c\sin(x)+2a\cos(x)=\cos(x)$$
The solutions are therefore $c=0$ and $a=1/2$, so your solution is $$y_p=\frac 12x\sin s$$
It is interesting to see what happens if $n\ne 1$. Then if you kook at just the coefficients of $x\sin(nx)$ you get $a(1-n^2)=0$. That means $a=0$. Similarly, for $x\cos(nx)$ you get $c=0$. You equation reduces to $$b(1-n^2)\sin(nx)+d(1-n^2)\cos(nx)=\cos(nx)$$
You have therefore $b=0$ and $d=\frac1{1-n^2}$. So no terms with $x$ multiplied with a trigonometric function. 
So the conclusion is that if the free term in the ODE is the same type as the homogeneous solution, the particular solution is the form $ax\sin x+bx\cos x$, otherwise it does not contain $x$. 
