This preview shows page 1. Sign up to view the full content.
Unformatted text preview: EPM 381 Matlab Tutorial
General commands used in Classical Control System Analysis and Design:
Variable Assignment Commands:
a = 5 or a = 5;
A = [ 1 2 3] ;
B = [ 1 2 3; 4 5 6; 7 8 9] ;
c = B(2,3);
c = B(1:3,2);
d = B(3,:);
C= [0:2:100]
D= linspace (5, 50, 1000);
values from 5 to 50
D= logspace(5, 50, 1000);
values with or without output
one dimensional array assignm ent
two dimensional array assignment
one element from an array
c = column 2 of B
the last row from the array
assigns all even numbers up to 100 in vector C
generates a 1000 element vector D with equally spaced
same as linspace, but with logarithmically, equally spaced General Command Environment:
pwd, cd, ls
directory commands
<up arrow>
last command
abc<up arrow>
last command starting in abc
help command_name
help on command_name, better to use the Help menu on
the PC
m = mean(Y);
a function call
save data A B C ascii double tab
saves the variables A, B, C in the file " data.mat"
in double,
ASCII form, tab delimited.
load data
loads the variables stored in the file "data.mat"
load data.txt
loads an array of numbers stores in ASCII for in
the file "data.txt"
filename
runs the "m" file named "filename.m" which
contains Matlab program commands Plotting and Graphing:
plot(x,y)
plot(y)
plot(t,[x1,x2,x3])
plot(x1,y1,'r',x2,y2,'g')
subplot(r,c,p)
plots plots the vector y versus the vector x
y is a complex vector, plots Im{y} versus Re{y}
plots x 1,x2,x3 versus t on the same graph
plot y1 versus x 1 in "red" and y2 versus x 2 in "green"
defines the current plot as plot, "p" in a multi plot graph of "r" by "c" plots: usually proceeds the plot function call.
title('text'), xlabel('text'), ylabel('text')
define a title and labels for the x and y axis
axis([xmin, xmax, ymin, ymax])
defines the axis scales
semilogx(x,y), semilogy(x,y)
same as plot(x,y) b ut with a semilog x or y axis
grid
draws grid lines on the current plot Edit menu in Figure: copy a figure to the clipboard and then paste it into another
application
such "Word", copy options; "Windows Metafile" and "Invert
Background" Transfer Function Models:
Transfer function models are represented by two polynomials; a numerator and
denominator polynomial. In the commands below , "g1" represents the system
define by "n1/d1".
n1 = [ 1 5] ;
d1 = [ 1 1 15 0];
printsys (n1,d1);
g1=tf(n1,d1);
g1
n = poly(V);
V = roots(n);
n3 = conv(n1,n2);
g3=g1*g2
(new)
g3= g1 + g2
(new)
g3= inv(g1)
g3=g1/g2
g3=g1\g2
g3 = feedback (g1,g2); define the polynomial s + 5
define the polynomial s3 + s2 + 15s
a function to print out the TF system as a rational polynomial
create a transfer function model (also see zpk and ss comands)
prints out the transfer funciton g 1 (new)
forms a polynomial "n" which has roots given in the vecto r "V"
calculates the roots of the polynomial "n"
forms the product of two polynomials
forms the product of two transfer functions, a series connection
forms the sum of two transfer functions, a parallel connection forms the inverse of the transfer funciton g 1 (new)
forms g1 * inv(g2) (new)
forms inv (g1) * g2 (new)
forms the closed loop TF with a forward TF , g1 and a feedback
of g2 with negative feedback
[r1,p1,k1] = residue(n1,d1); determines the residues, poles a nd direct term of the
partial fraction
expansions of the TF n 1/d1 Frequency Response Analysis:
bode(g1)
plot the Bode plot of the system g 1
bode(g1,w)
plot the Bod e plot for frequencies given in the vector w (in
rads/sec)
[mag,phase,w] = bode(g1); calculate the magnitude and phase of the frequency response at
frequencies "w"
F = freqs(g1,w);
calculate the complex fre quency response of system g 1 at
frequencies "w"
[Gm,Pm,Wcg,Wcp] = margin(g1)
calculate the gain and phase margin and their frequencies for g 1
nyquist (g1,w)
plot the Nyquist plot for system g 1 at frequen cies "w"
[r1,i1,w] = nyquist (g1,w)
calculate the real and imaginary form of the complex frequency
response How to plot your own Bode plots?
w = logspace(.01, 100, 100);
[mag,phas e] = bode(g1,w);
subplot (2,1,1)
mag=squeeze(mag);
phase=sqeeze(phase);
subplot (2,1,1)
semilogx (w, 20*log10(mag))
page; defines the frequency range
calculates the magnitude and phase characteristics
required to change 'mag' from a 3D array to a 1D array
required to change 'phase' f rom a 3D array to a 1D array plots the magnitude plot on the top half of the plot
add any title, xlabel, ylabel, grid, etc. subplot (2,1,2)
semilogx(w,phase)
page; plots the magnitude plot on the top half of the plot
add any title, xlabel, ylabel, grid, etc. Root Locus:
rlocus(g1)
rlocus(g1,k)
R = rlocus(g1,k)
"k"
[R,k] = rlocus(g1)
[p,k] = rlocfind(g1)
the mouse
pzmap(g1)
sgrid calculates and pl ots the root locus of g 1
calculates and plots the root locus of n 1/d1 at gains given in "k"
calculates the matrix of roots (dim(k) x order of d 1) for values in
automati c selection of "k" values
find the roots (poles) at a point on the root locus graph located by
calculate and plot the poles and zeros of g 1
plot a grid of c onstant damping ratio Time Response:
step(g1)
impulse(g1)
step(g1,t) or impulse (g1,t)
lsim(g1,u,t) calculates and plots the step response of g 1
calculates and plots the impulse response of g 1
as above for a specified time, t
linear simulation for input, u over time, t Reference:
Control System Design Using MATALB , Bahram Shahian, Michael Hassul, Prentice
Hall, 1993 Prof. Dr. Hamdy E lGoharey
October, 2007 ...
View
Full
Document
This note was uploaded on 03/03/2010 for the course AUTOMATIC 335 taught by Professor ? during the Winter '10 term at Ain Shams University.
 Winter '10
 ?

Click to edit the document details