# 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.

• There are two unknowns, $A_{1}$ and $A_{2}$. $x$ is the parameter of your function. It's not an unknown, because it doesn't have a definite value. – T. Linnell Aug 4 '17 at 13:39
• try it with $$A\cos(x)+Bx\sin(x)$$ – Dr. Sonnhard Graubner Aug 4 '17 at 13:39
• @T.Linnell does this fact let me solve the system? – Yos Aug 4 '17 at 13:41
• Yes. You have two independent equations and two unknowns, so you can find both. However, I'm not sure $x$ should even be in those equations. You should treat the $\cos{x}$, $x\cos{x}$, $\sin{x}$, and $x\sin{x}$ terms all differently, which would give you 4 equations. And looking at those 4 equations, it looks like something's gone wrong in your calculations. I think you've written $y''+y'=\cos{x}$, but isn't the original ODE $y''+y=\cos{x}$? – T. Linnell Aug 4 '17 at 13:42
• Just as an aside, it is a lot easier to identify $y$ here as $Re(z)$ where $z''+z=e^{ix}$. – Ian Aug 4 '17 at 14:36

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.

$$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$.

• OP is asking how to derive your first equation. – Ross Millikan Aug 4 '17 at 14:35
• What is the equation you're referring to, the one with the asterisk? My mistake was calculating $y''+y'=\cos x$ instead of calculating $y''+y=\cos x$, please mention this in your answer. – Yos Aug 4 '17 at 14:35

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.$$