# MATLAB & Simulink for Dynamic System Analysis & Control

### Arash Mohtat (view profile)

19 Dec 2007 (Updated )

An instruction package for Control Toolbox, Simulink and SimMechanics in Mech. Eng. Control Course

session1_control.m
```clear,clc
echo on
%% Part I:  Finding the step response of a second order LTI sys using different methods

% Mass-Spring-Damper System
m = 1.5;
k = 50;
c = 1.7;
pause

%% Solving the DE: md2x/dt2+cdx/dt+kx=f
f=1;
DE=@(t,x)[x(2);1/m*(f-k*x(1)-c*x(2))];
ode45(DE,[0 10],[0 0]);

pause
clc

%% Using Control Toolbox

% Transfer Fcn
G=tf(1,[m c k])
pause
help tf
pause
clc

% Transfer Fcn Alternative Definition
s=tf('s')
G=1/(m*s^2+c*s+k)

pause,
figure,step(G)

pause
clc
help step
pause
clc

% State Space Modeling
A=[0 1
-k/m -c/m];
B=[0
1/m];
C=[1 0];
D=0;

H=ss(A,B,C,D)

pause
clc
help ss

pause
clc

% State Space Modeling Alternative: Conversion from Transfer Fcn
H=ss(G);

[A,B,C,D]=ssdata(G)

pause,
figure,step(H)

pause
clc

% Characteristic Eq
poly(A)

% System Poles
roots(poly(A))

pause
% Ploting System Poles & zeros
pzmap(G)
pause

% Determining Natural Frequency & Damping
damp(G)
pause
clc

pause
clc

%% Part II:  System Interconnections Example

% Transfer Fcn of Blocks

G1=1/(s+10);
G2=1/(s+1);
G3=(s^2+1)/(s^2+4*s+4);
G4=(s+1)/(s+6);
G5=(s+1)/(s+2);
G6=tf(2,1);
G7=tf(1,1);

pause

%% Method#1
temp1=feedback(G3*G4,G5,1);
temp2=feedback(G2*temp1,G6/G4,-1);
sys1=feedback(G1*temp2,G7,-1)

pause

%% Method#2
temp=append(G1,G2,G3,G4,G5,G6,G7,tf(1,1));
Q=[1 8 -7
2 1 -6
3 2 5
4 3 0
5 4 0
6 4 0
7 4 0];
echo off
disp('sys2=tf(connect(temp,Q,8,4))')
sys2=sys1

```