Unformatted text preview: MATLAB Tutorial I
Representation of transfer function:
b0 sm + b1sm,1 + bm
i T s = n
a0 s + a1 sn,1 + an
MATLAB command is
num = b0 b1 bm ;
den = a0 a1 an ;
s , z1s , z2 s , zm
ii T s = K
s , p1s , p2 s , pn
MATLAB command is
K = ;
Z = z1 z2 zm ;
P = p1 p2 pn ;
Conversion between i and ii:
MATLAB command is
Z,P,K = tf2zpnum,den;
num,den = zp2tfZ,P,K;
Output response for transfer function of num, den :
MATLAB command for impulse response is
y,T = impulsenum,den; or
y = impulsenum,den,T;
1 The MATLAB command for step response is similar: simply
replace impulse by step in the previous commands.
Plot of output response:
MATLAB command for plotting signals is
MATLAB command for residues is
R,P,K = residuenum,den;
If there is no repeated poles, then
T s =
s , P 1
s , P 2
s , P n
where R,P,K are MATLAB variables:
P 1 P 2 P n
R1 R2 For multiple poles with P i = P i + 1 = P i + l , 1,
Ri + 1
Ri + l , 1
T s =
s , P i
s , P i2
s , P il
Attention should be paid to multiple poles, or poles very close
to each other which may result in numerical problems. 2 MATLAB Tutorial II
for-end" command: if some commands are repeatedly used
which can be indexed each time they are used, this command
will be useful.
Example: Compute values of
s2 + 2s + 2
at s = j! with 100 samples of ! 2 0:1; 10 in logrithm scale.
F s = Answer: Take !k +1 = 0:1 k for k = 0; 1; ; 99. Then
= 100. Hence
99 log = log100 = = elog100=99 = 1:0476: Our MATLAB commands are:
= 0:1 sqrt,1;
for k = 1 : 100,
sk + 1 = sk 1:0476;
tf k = 2=sk 2 + 2 sk + 1;
s1 3 while-end" command: if some commands are repeatedly
used, but termination is dependent on some computed results, rather on the index, then this command will be useful.
Example: Compute roots of
f x = x , e,x within an error no larger than 0:001.
Answer: Because f 0x = 1 + e,x
f 0 = ,1 0; f 1 0, and
= 1 , e,1 0; there is a unique root between 0 and 1. We use bisection
Step 1: Set x = 0, and y = 1.
Step 2: If jy , xj 0:001, then stop; Otherwise go to the
Step 3: Set z = x + y=2. If f z 0, set x = z ; If
0, set y = z . Go back to Step 2. 4 A MATLAB program to implement this algorithm can be:
while absy , x tol,
z = x + y =2;
f = z , exp,z ;
if f 0,
x = z;
y = z;
After running the above program gives the root: 0:5674 with
a guaranteed error smaller than 0:001.
if-elseif-end" command: this is a commonly used command
which is already used in the previous example. 5 MATLAB Tutorial III
m-File for MATLAB:
m- le: a set of MATLAB commands can be written together
to form an m- le with name lename.m. The command
lename" excutes each of the MATLAB command in
lename.m one by one.
function m- le: an m- le can be in the form of function:
y1, y2, , yn =funcx1, x2, , xm
computes output variable y1yn with input variable x1xm. Comment: If it is not in function form, then no input output variables are required. For example, we may save any set of MATLAB commands into a le called lename.m". This is so called
m- le. Then in MATLAB environment, we may type lename"
to execute this program. If it is in function form, then it becomes
a subroutine which can be used as existing MATLAB command
such as zp2tf" whose m- le is listed on next page: 6 Examples:
1. Write an m- le to plot step response for
; = 0:1 + 0:1k;
Gs = 2
s + 2
s + 1
on the same picture for k = 0; 1; 2; ; 8:
den= 1 2*0.1 1 ;
den= 1 2*0.1+k 10 1 ;
2. MATLAB command zp2tf": 7 function num,den = zp2tfz,p,k
ZP2TF Zero-pole to transfer function conversion.
NUM,DEN = ZP2TFZ,P,K forms the transfer
Hs = ||
given a set of zero locations in vector Z, a set of
pole locations in vector P, and a gain in scalar K.
Vectors num and den are returned with numerator
and denominator coe cients in descending powers
See also TF2ZP.
J.N. Little 7-17-85
Copyright c 1984-98 by The MathWorks, Inc.
$Revision: 1.12 $ $Date: 1997 11 21 23:41:24 $ 8 Note: the following will not work if p or z
have elements not in complex pairs.
den = realpolyp:;
md,nd = sizeden;
k = k:;
mk,nk = sizek;
if isemptyz, num = zerosmk,nd-1,k ; return; end
m,n = sizez;
if mk = n
if m == 1
error'Z and P must be column vectors.';
error'K must have as many elements as Z has
zj = z:,j;
pj = realpolyzj*kj;
numj,: = zeros1,nd-lengthpj pj ;
end 9 How to Start Simulink
Simulink toolbox can be used to build system block diagrams
for simulation of dynamic systems. In MATLAB environment,
type simulink" will pop up a window as follows:
File Edit Sources Option Sinks Simulation Style Discrete Linear Nonlinear Connection Extras SIMULINK Block Library (version 1.3c) If you want to program a new simulink le, go to File and
highlight new". Otherwise highlight open" in File to open an
existing Simulink le which you wrote before. 10 Sources: A signal source library. If you click the block Sources, you will see many di erent signal sources which can be used as
input or timing clock. Sinks: A signal sink library which can be used to view or save
the signals anywhere in the simulink block diagram. Table of Libraries: Other libraries are system blocks or links summarized in the following table.
Extras Library of discrete-time systems
Library of linear continuous-time systems
Library of nonlinear continuous-time systems
Additional blocks using mask utility The best way to learn Simulink is through exercise. We will illustrate it using one example. 11 Example: Construct a feedback system 1
; C s = ;
as plant and controller respectively, and observe step response at
the output, and tracking error response.
P s = Solution:
We open a new simulink window in File.
We open linear library to drag the block Transfer function"
to the new window.
Click twice the block Transfer function" to enter coe cients
of numerator and denominator, and then close it.
Drag another block Transfer function" to enter controller in
the same way. This can also be done using copy" and paste
in Editor of the new window.
Drag block Sum" from linear library, and set its sign.
Drag block Step Input" from Sources library, and two blocks
of Graph" to the new window. 12 Draw arrows to connect these blocks, and set blocks Step
Input" step time, values of step, and Graph" maximum,
minimum, time range of the screen, and etc.
In the window, highlight parameter" in Simulation to set the
duration of the response and, if necessary, other parameters
To run the simulation, highlight Start" in Simulation. You
may also stop it by highlight Stop" in Simulation.
The le can be saved by highlight of save as", if it is a new
le, or save" if it is an old le in File. Graph 2
s Step Input
s+ 2 Transfer Fcn Transfer Fcn Graph 13 ...
View Full Document
- Fall '10
- matlab, The MathWorks, MATLAB command, coe cients