home
PDF (letter size)
PDF (legal size)

HowTo, Matlab/Simulink for basic modeling

July 2, 2015   Compiled on November 11, 2018 at 11:51pm

1 How to solve ﬁrst order ode with zero initial conditions?

Solve $$y'(t)+3 y(t)=e^{-t}$$ with $$y(0)=0$$

2 How to solve ﬁrst order ode with non-zero initial conditions?

Solve $$y'(t)+3 y(t)=e^{-t}$$ with $$y(0)=1$$

3 How to solve second order ode with zero initial conditions?

Solve $$y''(t)+5 y'(t)+4 y(t)=5 \cos (2 t)$$ with $$y(0)=0,y'(0)=0$$

4 How to solve second order ode with non-zero initial conditions?

Solve $$y''(t)+5 y'(t)+4 y(t)=5 \cos (2 t)$$ with $$y(0)=1,y'(0)=0.5$$

The initial conditions are set up by modifying the integator $$\frac{1}{s}$$ block as shown below.

5 How to solve mass/spring system with unit step input?

From HW problem I did for ECE 717, to be solved using Simulink

Starting with the assumption that the ground surface is smooth and there is no friction. Assuming that all parts are moving in the positive direction to the right. Taking a snap shot when $$s_{2}>s_{1}$$ so that the spring $$k_{2}$$ is in compression. Spring $$k_{1}$$ is in compression by also assuming that $$s_{1}>u$$ at this instance.

Any other assumptions will also lead to the same set of equations as long as they are used in consistent way when ﬁnding the forces in the springs.

Starting with drawing a free body diagram of each body showing all forces acting on them based on the above assumption, and then using $$F=ma$$ to ﬁnd the equation of motion of each body $$m_{1},m_{2}$$. The free body diagrams is shown below

Now $$F=ma$$ is applied to each body to obtain the equation of motions. For mass $$m_{2}$$ \begin{align*} m_{2}s_{2}^{\prime \prime } & =-k_{2}\left ( s_{2}-s_{1}\right ) \\ s_{2}^{\prime \prime } & =-\frac{k_{2}}{m_{2}}\left ( s_{2}-s_{1}\right ) \end{align*}

And for mass $$m_{1}$$ \begin{align*} m_{1}s_{1}^{\prime \prime } & =k_{2}\left ( s_{2}-s_{1}\right ) -k_{1}\left ( s_{1}-u\right ) \\ s_{1}^{\prime \prime } & =\frac{k_{2}}{m_{1}}\left ( s_{2}-s_{1}\right ) -\frac{k_{1}}{m_{1}}\left ( s_{1}-u\right ) \end{align*}

Now the state space equations are found. \begin{align*} \begin{pmatrix} x_{1}=s_{1}\\ x_{2}=s_{2}\\ x_{3}=s_{1}^{\prime }\\ x_{4}=s_{2}^{\prime }\end{pmatrix} & \overset{\frac{d}{dt}}{\Longrightarrow }\begin{pmatrix} x_{1}^{\prime }=s_{1}^{\prime }=x_{3}\\ x_{2}^{\prime }=s_{2}^{\prime }=x_{4}\\ x_{3}^{\prime }=s_{1}^{\prime \prime }=\frac{k_{2}}{m_{1}}\left ( s_{2}-s_{1}\right ) -\frac{k_{1}}{m_{1}}\left ( s_{1}-u\right ) =\frac{k_{2}}{m_{1}}\left ( x_{2}-x_{1}\right ) -\frac{k_{1}}{m_{1}}\left ( x_{1}-u\right ) \\ x_{4}^{\prime }=s_{2}^{\prime \prime }=-\frac{k_{2}}{m_{2}}\left ( s_{2}-s_{1}\right ) =-\frac{k_{2}}{m_{2}}\left ( x_{2}-x_{1}\right ) \end{pmatrix} \\ & =\begin{pmatrix} x_{3}\\ x_{4}\\ x_{1}\left ( -\frac{k_{2}}{m_{1}}-\frac{k_{1}}{m_{1}}\right ) +\frac{k_{2}}{m_{1}}x_{2}+\frac{k_{1}}{m_{1}}u\\ \frac{k_{2}}{m_{2}}x_{1}-\frac{k_{2}}{m_{2}}x_{2}\end{pmatrix} \end{align*}

Hence\begin{align*} \begin{pmatrix} x_{1}^{\prime }\\ x_{2}^{\prime }\\ x_{3}^{\prime }\\ x_{4}^{\prime }\end{pmatrix} & =\overset{A\left ( n\times n\right ) }{\overbrace{\begin{pmatrix} 0 & 0 & 1 & 0\\ 0 & 0 & 0 & 1\\ -\left ( \frac{k_{2}}{m_{1}}+\frac{k_{1}}{m_{1}}\right ) & \frac{k_{2}}{m_{1}} & 0 & 0\\ \frac{k_{2}}{m_{2}} & -\frac{k_{2}}{m_{2}} & 0 & 0 \end{pmatrix} }}\begin{pmatrix} x_{1}\\ x_{2}\\ x_{3}\\ x_{4}\end{pmatrix} +\overset{B\left ( n\times m\right ) }{\overbrace{\begin{pmatrix} 0\\ 0\\ \frac{k_{1}}{m_{1}}\\ 0 \end{pmatrix} }}u\left ( t\right ) \\\begin{pmatrix} y_{1}\\ y_{2}\end{pmatrix} & =\overset{C\left ( r\times n\right ) }{\overbrace{\begin{pmatrix} 1 & 0 & 0 & 0\\ 0 & 1 & 0 & 0 \end{pmatrix} }}\begin{pmatrix} x_{1}\\ x_{2}\\ x_{3}\\ x_{4}\end{pmatrix} +\overset{D\left ( r\times m\right ) }{\overbrace{\begin{pmatrix} 0\\ 0 \end{pmatrix} }}u\left ( t\right ) \end{align*}

The above is in the form of $$x^{\prime }=Ax+Bu$$ and $$y=Cx+Du$$ where $$r=2$$ is number of outputs, $$m=1$$ is the number of input and $$n=4$$ is the number of states.

Using $$k_{1}=k_{2}=0.5,m_{1}=1,m_{2}=2$$ and $$x\left ( 0\right ) =0$$ now the unit step response for $$y_{1},y_{2}$$ is found using Simulink. With the above values the system becomes\begin{align*} \begin{pmatrix} x_{1}^{\prime }\\ x_{2}^{\prime }\\ x_{3}^{\prime }\\ x_{4}^{\prime }\end{pmatrix} & =\begin{pmatrix} 0 & 0 & 1 & 0\\ 0 & 0 & 0 & 1\\ -1 & 0.5 & 0 & 0\\ 0.25 & -0.25 & 0 & 0 \end{pmatrix}\begin{pmatrix} x_{1}\\ x_{2}\\ x_{3}\\ x_{4}\end{pmatrix} +\begin{pmatrix} 0\\ 0\\ 0.5\\ 0 \end{pmatrix} u\left ( t\right ) \\\begin{pmatrix} y_{1}\\ y_{2}\end{pmatrix} & =\begin{pmatrix} 1 & 0 & 0 & 0\\ 0 & 1 & 0 & 0 \end{pmatrix}\begin{pmatrix} x_{1}\\ x_{2}\\ x_{3}\\ x_{4}\end{pmatrix} +\begin{pmatrix} 0\\ 0 \end{pmatrix} u\left ( t\right ) \end{align*}

Using simulink, state space block was used to implement the above. A step input source was used. Demux was used to send the $$y_{1}$$ and $$y_{2}$$ responses to two diﬀerent time scopes. Simulation was set for 40 seconds to obtain long enough view of the response. The following ﬁgure shows the step response and the model used.