### 4.5 Solve non-homogeneous 2nd order ODE, constant coeﬃcients

Problem: Solve $y^{\prime \prime }\left ( t\right ) -1.5y^{\prime }\left ( t\right ) +5y\left ( t\right ) =4\cos \left ( t\right )$ with initial conditions $y\left ( 0\right ) =1 , y^{\prime }\left ( 0\right ) =0$ To use Matlab ode45, the second order ODE is converted to state space as follows

Given $$y^{\prime \prime }\left ( t\right ) -1.5y^{\prime }\left ( t\right ) +5y\left ( t\right ) =4\cos \left ( t\right )$$, let \begin {align*} x_{1} & =y\\ x_{2} & =y^{\prime }\\ & =x_{1}^{\prime } \end {align*}

hence $x_{1}^{\prime }=x_{2}$ and \begin {align*} x_{2}^{\prime } & =y^{\prime \prime }\\ & =1.5y^{\prime }-5y+4\cos \left ( t\right ) \\ & =1.5x_{2}-5x_{1}+4\cos \left ( t\right ) \end {align*}

Hence we can now write $\begin {bmatrix} x_{1}^{\prime }\\ x_{2}^{\prime }\end {bmatrix} =\begin {bmatrix} 0 & 1\\ -5 & 1.5 \end {bmatrix}\begin {bmatrix} x_{1}\\ x_{2}\end {bmatrix} +\begin {bmatrix} 0\\ 4 \end {bmatrix} \cos \left ( t\right )$

Mathematica

Remove["Global*"];
eq  = y''[t]-15/10y'[t]+5y[t]==4  Cos[t];
ic  = {y'[0]==0,y[0]== 1};
sol = First@DSolve[{eq,ic},y[t],t];



$\frac {1}{5183}( -1704 \sin (t) \sin ^2\left (\frac {\sqrt {71} t}{4}\right )+69 \sqrt {71} e^{3 t/4} \sin \left (\frac {\sqrt {71} t}{4}\right )+4544 \cos (t) \cos ^2\left (\frac {\sqrt {71} t}{4}\right )+639 e^{3 t/4} \cos \left (\frac {\sqrt {71} t}{4}\right )-1704 \sin (t) \cos ^2\left (\frac {\sqrt {71} t}{4}\right )+4544 \sin ^2\left (\frac {\sqrt {71} t}{4}\right ) \cos (t)))$

Plot[y,{t,0,10},
FrameLabel->{{"y(t)",None},
{"t","Solution"}},
Frame->True,
GridLines->Automatic,
GridLinesStyle->Automatic,
RotateLabel->False,
ImageSize->300,
AspectRatio->1,
PlotRange->All,
PlotStyle->{Thick,Red}]



Matlab

function e55
t0 = 0; %initial time
tf = 10; %final time
%initial conditions [y(0)  y'(0)]
ic =[1 0]';

[t,y] = ode45(@rhs, [t0 tf], ic);

plot(t,y(:,1),'r')
title('Solution using ode45');
xlabel('time');
ylabel('y(t)');
grid on
set(gcf,'Position',[10,10,320,320]);

function dydt=rhs(t,y)
dydt=[y(2) ;
-5*y(1)+1.5*y(2)+4*cos(t)];
end
end

`