ODE No. 1003

\[ -\sin (n x)+y''(x)+y(x)=0 \] Mathematica : cpu = 0.0959813 (sec), leaf count = 45

DSolve[-Sin[n*x] + y[x] + Derivative[2][y][x] == 0,y[x],x]
 

\[\left \{\left \{y(x)\to \frac {\cos ^2(x) (-\sin (n x))-\sin ^2(x) \sin (n x)}{n^2-1}+c_1 \cos (x)+c_2 \sin (x)\right \}\right \}\] Maple : cpu = 0.064 (sec), leaf count = 26

dsolve(diff(diff(y(x),x),x)+y(x)-sin(n*x)=0,y(x))
 

\[y \left (x \right ) = \sin \left (x \right ) c_{2}+\cos \left (x \right ) c_{1}-\frac {\sin \left (n x \right )}{n^{2}-1}\]

Hand solution

\begin {equation} y^{\prime \prime }+y=\sin nx\tag {1} \end {equation} We start by solving the homogeneous equation \[ y^{\prime \prime }+y=0 \] Let \(y=e^{\lambda x}\), substitution in above gives\begin {align*} \lambda ^{2}e^{\lambda x}+e^{\lambda x} & =0\\ \lambda ^{2}+1 & =0 \end {align*}

Hence \(\lambda =\pm i\), therefore the solution is\begin {align*} y_{h} & =Ae^{ix}+Be^{-ix}\\ & =A\left ( \cos x+i\sin x\right ) +B\left ( \cos x-i\sin x\right ) \\ & =\cos x\left ( A+B\right ) +\sin x\left ( Ai-iB\right ) \\ & =\cos x\left ( A+B\right ) +\sin x\left ( i\left ( A-B\right ) \right ) \end {align*}

Let \(A+B=c_{1},i\left ( A-B\right ) =c_{2}\) hence\[ y_{h}=c_{1}\cos x+c_{2}\sin x \] Now we solve for the particular solution using variation of parameters. Let \begin {align*} y_{p} & =u_{1}\left ( x\right ) \cos x+u_{2}\left ( x\right ) \sin x\\ y_{p}^{\prime } & =u_{1}^{\prime }\cos x-u_{1}\sin x+u_{2}^{\prime }\sin x+u_{2}\cos x\\ & =u_{2}\cos x-u_{1}\sin x+u_{1}^{\prime }\cos x+u_{2}^{\prime }\sin x \end {align*}

Let first condition be \begin {equation} u_{1}^{\prime }\cos x+u_{2}^{\prime }\sin x=0\tag {2} \end {equation}

Hence\begin {align*} y_{p}^{\prime } & =u_{2}\cos x-u_{1}\sin x\\ y_{p}^{\prime \prime } & =u_{2}^{\prime }\cos x-u_{2}\sin x-u_{1}^{\prime }\sin x-u_{1}\cos x \end {align*}

Substituting in (1) gives \begin {align} y_{p}^{\prime \prime }+y_{p} & =\sin nx\nonumber \\ u_{2}^{\prime }\cos x-u_{2}\sin x-u_{1}^{\prime }\sin x-u_{1}\cos x+u_{1}\cos x+u_{2}\sin x & =\sin nx\nonumber \\ u_{2}^{\prime }\cos x-u_{1}^{\prime }\sin x & =\sin nx\tag {3} \end {align}

So we have two equations (1)(2) to solve for \(u_{1},u_{2}\)\begin {align*} u_{1}^{\prime }\cos x+u_{2}^{\prime }\sin x & =0\\ u_{2}^{\prime }\cos x-u_{1}^{\prime }\sin x & =\sin nx \end {align*}

From the first equation\begin {equation} u_{1}^{\prime }=-u_{2}^{\prime }\frac {\sin x}{\cos x}\tag {4} \end {equation} Substituting in the second equation\begin {align*} u_{2}^{\prime }\cos x-\left ( -u_{2}^{\prime }\frac {\sin x}{\cos x}\right ) \sin x & =\sin nx\\ u_{2}^{\prime }\left ( \cos x+\frac {\sin x}{\cos x}\sin x\right ) & =\sin nx\\ u_{2}^{\prime }\left ( \frac {\cos ^{2}x+\sin ^{2}x}{\cos x}\right ) & =\sin nx\\ u_{2}^{\prime } & =\cos x\sin nx \end {align*}

Hence \begin {align*} u_{2} & =\int \cos x\sin \left ( nx\right ) dx\\ & =\frac {-n\cos x\cos \left ( nx\right ) -\sin x\sin \left ( nx\right ) }{n^{2}-1} \end {align*}

From (4)\begin {align*} u_{1}^{\prime } & =-\cos x\sin nx\frac {\sin x}{\cos x}\\ u_{1} & =-\int \sin \left ( nx\right ) \sin xdx\\ & =\frac {n\cos \left ( nx\right ) \sin x-\cos x\sin \left ( nx\right ) }{n^{2}-1} \end {align*}

Since \(y_{p}=u_{1}\left ( x\right ) \cos x+u_{2}\left ( x\right ) \sin x\) then\begin {align*} y_{p} & =\left ( \frac {n\cos \left ( nx\right ) \sin x-\cos x\sin \left ( nx\right ) }{n^{2}-1}\right ) \cos x+\left ( \frac {-n\cos x\cos \left ( nx\right ) -\sin x\sin \left ( nx\right ) }{n^{2}-1}\right ) \sin x\\ & =\frac {n\cos \left ( nx\right ) \cos x\sin x-\cos ^{2}x\sin \left ( nx\right ) -n\cos x\sin x\cos \left ( nx\right ) -\sin ^{2}x\sin \left ( nx\right ) }{n^{2}-1}\\ & =\frac {-\sin \left ( nx\right ) \left ( \cos ^{2}x+\sin ^{2}x\right ) }{n^{2}-1}\\ & =\frac {\sin \left ( nx\right ) }{1-n^{2}} \end {align*}

Therefore, the full solution is (for \(n^{2}\neq 1\))\begin {align*} y & =y_{h}+y_{p}\\ & =c_{1}\cos x+c_{2}\sin x+\frac {\sin \left ( nx\right ) }{1-n^{2}} \end {align*}

Solution using undetermined coefficients: Since RHS is \(\sin nx\) we guess \(y_{p}=A\cos \left ( nx\right ) +B\sin \left ( nx\right ) \), therefore

\begin {align*} y_{p}^{\prime } & =-An\sin \left ( nx\right ) +Bn\cos \left ( nx\right ) \\ y_{p}^{\prime \prime } & =-An^{2}\cos \left ( nx\right ) -Bn^{2}\sin \left ( nx\right ) \end {align*}

Plug into the ODE gives

\begin {align*} y_{p}^{\prime \prime }+y_{p} & =\sin nx\\ -An^{2}\cos \left ( nx\right ) -Bn^{2}\sin \left ( nx\right ) +A\cos \left ( nx\right ) +B\sin \left ( nx\right ) & =\sin nx\\ \cos \left ( nx\right ) \left ( -An^{2}+A\right ) +\sin \left ( nx\right ) \left ( -Bn^{2}+B\right ) & =\sin \left ( nx\right ) \end {align*}

Hence \(-Bn^{2}-B=1\) and \(-An^{2}+A=0\). Therefore \(A=0\) and from the first equation

\begin {align*} B\left ( n^{2}+1\right ) & =-1\\ B & =\frac {-1}{n^{2}+1} \end {align*}

Hence \begin {align*} y_{p} & =A\cos \left ( nx\right ) +B\sin \left ( nx\right ) \\ & =\frac {\sin \left ( nx\right ) }{1-n^{2}} \end {align*}

Which is the same as variation of parameters method.

Note: Full solution should also really consider the case for \(n=1\). Will update later.

restart; 
ode:=diff(diff(y(x),x),x)+y(x)-sin(n*x)=0; 
y0:=_C1*cos(x)+_C2*sin(x)+sin(n*x)/(1-n^2); 
odetest(y(x)=y0,ode); 
0