CS Matlab Exercises
CS Matlab Exercises
» prg1
num =
18 360
den =
1.0000 40.4000 391.0000 150.0000
num/den =
18 s + 360
----------------------------
s^3 + 40.4 s^2 + 391 s + 150
numol =
18 360
denol =
1.0000 40.4000 391.0000 150.0000
numcl =
0 0 18 360
dencl =
1.0000 40.4000 409.0000 510.0000
num/den =
18 s + 360
----------------------------
s^3 + 40.4 s^2 + 409 s + 510
1
-K -
S te p s 2+4 s
Sum Gain T ra n sfe r Fcn
C (s ) G H =1 C (s ) K
The CLTF is = ⇔ = 2
R(s ) 1 + GH G = 1 R(s ) s + 4s + K
s 2 + 4s
So if K=1:
% prg6
% step 1
% This m-file finds the step response
num=[1];
den=[1 4 1];
step(num,den);
grid
xlabel('time')
ylabel('output')
title('step response')
S tep Res pons e
s tep res ponse
1
0.9
0.8
0.7
0.6
Am plitude
output
0.5
0.4
0.3
0.2
0.1
0
0 5 10 15 20 25
Tim e tim
(sece .)
The command step takes a default time for the transient response to be finished (i.e. 25s here)
If we want to define the time we can use the next m-file:
% prg7
% step 2
% This m-file finds the step response
t=0:0.1:30;
0.9
0.8
0.7
0.6
A m plitude
output
0.5
0.4
0.3
0.2
0.1
0
0 5 10 15 20 25 30
Tim e tim
(sece .)
For more complex systems it is possible to combine the commands step & plot :
% prg8
% step 3
% This m-file finds the step response
t=0:0.1:30;
num=[1];
den=[1 4 1];
c=step(num,den,t);
plot(t,c,'or')
grid
xlabel('time')
ylabel('output')
title('step response')
s tep response
1
0.9
0.8
0.7
0.6
output
0.5
0.4
0.3
0.2
0.1
0
0 5 10 15 20 25 30
tim e
More than one graph can be plotted on the same figure using a combination of plot and step:
s tep response
1.6
1.4
1.2
1
output
0.8
0.6
0.4
0.2
0
0 5 10 15 20
tim e
To find the impulse response we use the command impulse. This is used exactly as the command
step. To find the ramp response we have to find the step response of the function G ( s)
s
B ode Diagram s
50
-50
-150
-100
-200
-300
-2 -1 0 1 2
10 10 10 10 10
Frequency (rad/sec)
100
-100
Phas e (deg); M agnitude (dB)
-200
-300
-100
-200
-300
-1 0 1 2 3 4
10 10 10 10 10 10
Frequency (rad/s ec )
To find the gain and phase margins we use the command margin(num,den) since it is very difficult
to calculated directly from the above figures with any accuracy. This command plots the Bode
diagram and calculates the margins:
% prg18
%bode 3
clc
clear all
clf
num=10;
den=[1 5 5 0];
margin(num,den);
B ode Diagram s
-50
-150
-100
-200
-300
-2 -1 0 1 2
10 10 10 10 10
Frequency (rad/sec)
Nichols diagrams
60
O 40
pe 0
n- 0.25
Lo 0.5
20 1 -1
op
G 3 -3
6
ai 0 -6
Pm
-12
-20 -20
Gm
-40 -40
- - - - - - -50
Open-Loop Phase
With the Nichols plots we use the ngrid command.
Ny quis t Diagram s
250
200
150
100
Im aginary A x is
50
-50
-100
-150
-200
-250
-2.5 -2 -1.5 -1 -0.5 0
Real A xis
The command nyquist takes both negative and positive values for the frequency. For this reason we
see a curve which is symmetrical to the x-axis. If we want only positive frequencies then:
% prg22
%nyquist 3
clc
clear all
clf
num=10;
den=[1 5 5 0];
w=0:0.1:100;
[re,im,w]=nyquist(num,den,w);
plot(re,im);
grid
A 0
0
-5
1
-10 Gm =
0A
-15
-20
-2 -1.8 -1.6 -1.4 -1.2 -1 -0.8 -0.6 -0.4 -0.2 0
All the above commands can be used for systems that are described in State Space form – see later
Root Locus
The design of the root locus plot of a system is very easy and fast with the use of Matlab. This is
more obvious in more complex systems than the examples used here. To find the root locus we use
the command rlocus(xxx,yyy) where xxx is the numerator and yyy is the denominator of the OLTF
e.g.:
1
k
s 2+5 s
Sum Gain T ra n sfe r Fcn
% prg12
% rlocus 1
clc
clear all
num=1;
den=[1 5 0];
rlocus(num,den)
0.5
Im ag A x is
0
-0.5
-1
-1.5
-6 -5 -4 -3 -2 -1 0 1 2
Real Ax is
The above root locus plot is for values of gain from 0 to a specific value to allow conclusions about
the behaviour and stability of the system. If we would like to define the values of gain then:
% prg13
%rlocus 2
clc
clear all
clf
k=0:1:100;
num=1;
den=[1 5 5 0];
rlocus(num,den,k)
4
1
Im ag A x is
-1
-2
-3
-4
-5 -4 -3 -2 -1 0 1 2
Real Axis
or
% prg14
%rlocus 3
clc
clear all
clf
k1=0:2:10;
k2=11:0.5:90;
k3=91:10:500;
k=[k1 k2 k3];
num=1;
den=[1 5 5 0];
rlocus(num,den,k)
Im ag Ax is
0
-2
-4
-6
-8
-5 -4 -3 -2 -1 0 1 2
Real Ax is
To find the poles and a specific point of the locus we use the command rlocfind(xxx,yyy)
% prg15
%rlocus 4
clc
clear all
clf
k1=0:2:10;
k2=11:0.5:90;
k3=91:10:500;
k=[k1 k2 k3];
num=1;
den=[1 5 5 0];
rlocus(num,den,k);
[k,poles]=rlocfind(num,den)
After the execution of the m-file the point of interest can be defined with the mouse:
» prg15
Select a point in the graphics window
selected_point =
-0.4330+ 1.1730i
k =
6.4034
poles =
-4.1691
-0.4154+ 1.1676i
-0.4154- 1.1676i
2
Im ag A x is
-2
-4
-6
-8
-5 -4 -3 -2 -1 0 1 2
Real A x is
If that point were on the imaginary axis then this would produce the maximum stable gain.
The commands rlocus, rlocfind can be used for systems that are described in State Space form – see
later
State Space
To describe a system in State Space we must define the matrices A,B,C,D:
% prg2
% This m-file creates and prints a system, which is defined in state %
space
a=[-40.4 -391 -150; 1 0 0; 0 1 0];
b=[1 0 0]';
c=[0 18 360];
d=[0];
printsys(a,b,c,d)
» prg2
a =
x1 x2 x3
x1 -40.40000 -391.00000 -150.00000
x2 1.00000 0 0
x3 0 1.00000 0
b =
u1
x1 1.00000
x2 0
x3 0
c =
x1 x2 x3
y1 0 18.00000 360.00000
d =
u1
y1 0
» prg4
num/den =
s - 5
-------------
s^2 - 6 s – 3
From the TF can find the matrices A,B,C,D with the command tf2ss(num,den)
% prg5
% This m-file converts a second order system
% form transfer function to state space
% num stands for numenator
% den stands for denominator
num=[1 -5];
den=[1 -6 -3];
[A,B,C,D]=tf2ss(num,den);
printsys(A,B,C,D)
prg5
a =
x1 x2
x1 6.00000 3.00000
x2 1.00000 0
b =
u1
x1 1.00000
x2 0
c =
x1 x2
y1 1.00000 -5.00000
d =
u1
y1 0
4
A m plitude
0
0 0.16 0.32 0.48 0.64 0.8
Tim e (sec .)
From: U1 From: U2
0.4
0.2
To: Y 1 0
-0.2
A mplitude
-0.4
1.5
To: Y 2
0.5
0
0 4 8 120 4 8 12
Time (sec.)
0.3
Step Response
0.2
0.4
0.1
To: Y 1
0.2
0
To: Y 1
0
-0.1
A mplitude
-0.2 -0.2
Amplitude
-0.4 2
2
1.5
1.5
To: Y 2
1
To: Y 2
1
0.5
0.5
0
0 0 2 4 6 8 10 12
0 2 4 6 8 10 12
1
k
s 3+6 s 2+1 1 s+6
Sum Gain
T ra n sfe r Fcn
2.1 Find the root locus plot and the maximum stable gain. Relate the step response to the CL
pole locations.
1
k
s 3+6 s 2+1 1 s+6
Sum Gain
T ra n sfe r Fcn
2.2 Plot the root locus and find the maximum stable gain. Relate the step response to the CL
pole locations.
1
k
s 4+1 .1 s 3+1 0 .3 s 2+5 s
Sum Gain
T ra n sfe r Fcn
s 2+5 s+6
k
s 2+s
Sum Gain
T ra n sfe r Fcn
2.4 Plot the root locus and find the maximum stable gain. Relate the step response to the CL
pole locations:
1
k
s 3+6 s 2+2 5 s
Sum Gain
T ra n sfe r Fcn
2.5 Find the gain and phase margin of the above OLTF
3 Produce the Bode, Nyquist and Nichols diagrams for the following systems.
320(s + 1)
G1(s ) =
s(s + 2)(s 2 + 4s + 16)
k
G2 (s ) = , k = 4, 1.5, 0.4
s(s + 1)(s + 2)
k
G3 (s ) = , k = 10, 100
s(s + 1)(s + 5)