This preview shows page 1. Sign up to view the full content.
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 = 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 plotT,y Residue computation: MATLAB command for residues is R,P,K = residuenum,den; If there is no repeated poles, then R2 Rn R1 + + + +K T s = s , P 1 s , P 2 s , P n where R,P,K are MATLAB variables: R= P= Rn P 1 P 2 P n
R1 R2 For multiple poles with P i = P i + 1 = P i + l , 1, Ri Ri + 1 Ri + l , 1 T s = + + + + rest: 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 2 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 99 = 100. Hence 99 log = log100 = 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; end
s1 = elog100=99 = 1:0476: 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 0, and = 1 , e,1 0; within an error no larger than 0:001. Answer: Because f 0x = 1 + e,x
f 0 = ,1 0; f 1 there is a unique root between 0 and 1. We use bisection method: Step 1: Set x = 0, and y = 1. Step 2: If jy , xj 0:001, then stop; Otherwise go to the next step. Step 3: Set z = x + y=2. If f z 0, set x = z ; If f z 0, set y = z . Go back to Step 2. 4 A MATLAB program to implement this algorithm can be: x=0; y=1; tol=0.001; while absy , x tol, z = x + y =2; f = z , exp,z ; if f 0, x = z; else y = z; end end root= 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 1 ; = 0:1 + 0:1k; Gs = 2 s + 2
s + 1 on the same picture for k = 0; 1; 2; ; 8: num=1; den= 1 2*0.1 1 ; stepnum,den hold on for k=1:8 den= 1 2*0.1+k 10 1 ; stepnum,den end 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 function: nums Hs = || dens 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 of s. See also TF2ZP. J.N. Little 7-17-85 Revised 6-27-88 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.'; end error'K must have as many elements as Z has columns.'; end for j=1:n 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 Option Simulation Style Sources Sinks 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 you will see many di erent signal sources which can be used as input or timing clock. Sources: A signal source library. If you click the block Sources, Sinks: A signal sink library which can be used to view or save
the signals anywhere in the simulink block diagram. summarized in the following table. Discrete Linear Nonlinear Connections Extras Table of Libraries: Other libraries are system blocks or links
Library of discrete-time systems Library of linear continuous-time systems Library of nonlinear continuous-time systems Connection library 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
P s 1 2 ; C s = ; s+2 s as plant and controller respectively, and observe step response at the output, and tracking error response. = 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 as well. 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 Step Input Sum 2 s
Transfer Fcn 1 s+ 2
Transfer Fcn Graph 13 ...
View Full Document
This note was uploaded on 02/03/2012 for the course EE 3530 taught by Professor Chen during the Fall '07 term at LSU.
- Fall '07