matlab basics ecen 605 linear control systems instructor: s.p. bhattacharyya
TRANSCRIPT
![Page 1: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/1.jpg)
MATLAB BASICS
ECEN 605
Linear Control Systems
Instructor:
S.P. Bhattacharyya
![Page 2: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/2.jpg)
STARTING MATLAB
You can start MATLAB by double-clicking on the MATLAB icon or invoking the application from the Start menu of Windows. The main MATLAB window will then pop-up.
![Page 3: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/3.jpg)
![Page 4: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/4.jpg)
BASIC OPERATION
MATLAB is based on matrix and vector algebra; even scalars are treated as 1x1 matrices.
e.g. v = [1 3 5 7];
t = 0:.1:10;
k = 0:10;
M = [1 2 4; 3 6 8];
![Page 5: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/5.jpg)
APPROACH
TRY EXPLORING ALL THE COMMANDS INITIALLY TO GET FAMILIARIZED.
TAKE UP EXAMPLES AND SOLVE THEM TRY THE ASSIGNMENT QUESTIONS
![Page 6: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/6.jpg)
SPECIAL MATRICES
There are a number of special matrices that can be defined:
null matrix: M = [ ];
nxm matrix of zeros: M = zeros(n,m);
nxm matrix of ones: M = ones(n,m);
nxn identity matrix: M = eye(n);
![Page 7: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/7.jpg)
GENERAL INFORMATION
Matlab is case sensitive so "a" and "A" are two different names.
Comment statements are preceded by a "%". On-line help for MATLAB can be reached by typing
help for the full menu or typing help followed by a particular function name.
The commands who and whos give the names of the variables that have been defined in the workspace.
The command length(x) returns the length of a vector x and size(x) returns the dimension of the matrix x.
![Page 8: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/8.jpg)
CREATING FUNCTIONS
As example of an M-file that defines a function, create a file in your working directory named yplusx.m that contains the following commands:
function z = yplusx(y,x) z = y + x;
![Page 9: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/9.jpg)
USING THE FUNCTIONS
The following commands typed from within MATLAB demonstrate how this M-file is used:
x = 2; y = 3; z = yplusx(y,x)
![Page 10: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/10.jpg)
PLOTTING
plot xlabel ylabel title grid axis stem subplot
![Page 11: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/11.jpg)
EXAMPLE OF A PLOT
Z = [0 : pi/50 : 10*pi]; X = exp(-.2.*Z).*cos(Z); Y = exp(-.2.*Z).*sin(Z); plot3(X,Y,Z); grid on; xlabel('x-axis'); ylabel('y-axis'); zlabel('z-
axis');
![Page 12: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/12.jpg)
Continuous Time System Analysis
Transfer Function Representation Time Simulations Frequency Response Plots Control Design State Space Representation
![Page 13: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/13.jpg)
Transfer Function Representation
Tf2zp Zp2tf Cloop Feedback Parallel series
![Page 14: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/14.jpg)
Cont…
Transfer functions are defined in MATLAB by storing the coefficients of the numerator and the denominator in vectors. Given a continuous-time transfer function
B(s)
H(s) = ---------
A(s)
![Page 15: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/15.jpg)
Cont…
Where B(s) = bMsM+bM-1sM-1+…+b0
and A(s) = aNsN+aN-1sN-1+…+a0
Store the coefficients of B(s) and A(s) in the vectors
num = [bM bM-1 … b0]
den = [aN aN-1 … a0]
![Page 16: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/16.jpg)
Example
5s+6
H(s) = ----------------
s3+10s2+5 num = [5 6]; den = [1 10 0 5];
all coefficients must be included in the vector, even zero coefficients
![Page 17: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/17.jpg)
Cont…
To find the zeros, poles and gain of a transfer function from the vectors num and den which contain the coefficients of the numerator and denominator polynomials:
[z,p,k] = tf2zp(num,den)
![Page 18: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/18.jpg)
Example
num = [5 6]; den = [1 10 0 5]; [z,p,k] = tf2zp(num,den)
z = -1.2000 p = -10.0495 0.0248+0.7049i 0.0248-0.7049i k = 5
![Page 19: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/19.jpg)
Example Cont…
(s-z1)(s-z2)...(s-zn)
H(s) = K --------------------------
(s-p1)(s-p2)...(s-pn)
5*(s+1.2)
----------------------------------------------------------
(s+10.0495)(s-{0.0248+0.7049i})(s-{0.0248-0.7049i})
![Page 20: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/20.jpg)
Cont…
To find the numerator and denominator
polynomials from z, p, and k:
[num,den] = zp2tf(z,p,k) To reduce the general feedback system
to a single transfer function:
T(s) = G(s)/(1+G(s)H(s))
[numT,denT]=feedback(numG,denG,numH,denH);
![Page 21: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/21.jpg)
Cont…
For a unity feedback system:
[numT,denT] = cloop(numG,denG,-1); To reduce the series system to a single transfer
function, T(s) = G(s)H(s)
[numT,denT] = series(numG,denG,numH,denH); To reduce the parallel system to a single transfer
function, T(s) = G(s) + H(s)
[numT,denT] = parallel(numG,denG,numH,denH);
![Page 22: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/22.jpg)
Example
[num,den] = zp2tf(z,p,k)
num = 0 0 5 6 den = 1.0000 10.0000 0.0000 5.0000
![Page 23: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/23.jpg)
Time Simulations
residue Step Impulse lsim
![Page 24: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/24.jpg)
Cont…
[R,P,K] = residue (B,A) finds the residues, poles and direct term of a partial fraction expansion of the ratio of two polynomials B(s)/A(s)
The residues are stored in r, the corresponding poles are stored in p, and the gain is stored in k.
![Page 25: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/25.jpg)
Example
If the ratio of two polynomials is expressed as b(s) 5s3+3s2-2s+7
------- = -------------------
a(s) -4s3+8s+3
b = [ 5 3 -2 7] a = [-4 0 8 3]
![Page 26: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/26.jpg)
Example Cont…
r = -1.4167 -0.6653 1.3320 p = 1.5737 -1.1644 -0.4093 k = -1.2500
B(s) R(1) R(2) R(n) ------ = -------- + --------- + ... + --------- + K(s) A(s) s - P(1) s - P(2) s - P(n)
![Page 27: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/27.jpg)
Find the response of a system to a particular input
First store the numerator and denominator of the transfer function in num and den, respectively.
To plot the step response:
step(num,den) To plot the impulse response:
impulse(num,den)
![Page 28: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/28.jpg)
Cont…
For the response to an arbitrary input, use the command lsim (linear simulation)
Create a vector t which contains the time values in seconds
t = a:b:c; Define the input x as a function of time, for
example, a ramp is defined as x = t
lsim(num,den,x,t);
![Page 29: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/29.jpg)
Frequency Response Plots
Freqs Bode Logspace Log10 Semilogx
![Page 30: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/30.jpg)
Cont…
To compute the frequency response H of a transfer function, store the numerator and denominator of the transfer function in the vectors num and den.
Define a vector w that contains the frequencies for which H) is to be computed, for example w = a:b:c where a is the lowest frequency, c is the highest frequency and b is the increment in frequency.
H = freqs(num,den,w)
![Page 31: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/31.jpg)
Cont…
To draw a Bode plot of a transfer function which has been stored in the vectors num and den:
bode(num,den)
![Page 32: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/32.jpg)
Cont…
To customize the plot, first define the vector w which contains the frequencies at which the Bode plot will be calculated.
Since w should be defined on a log scale, the command logspace is used.
For example, to make a Bode plot ranging in frequencies from 0.1 to 100, define w by
w = logspace(-1,2); The magnitude and phase information for the Bode plot can
then be found by:
[mag,phase] = bode(num,den,w);
![Page 33: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/33.jpg)
Cont…
To plot the magnitude in decibels, convert mag using the following command:
magdb = 20*log10(mag); To plot the results on a semilog scale where the y-
axis is linear and the x-axis is logarithmic:
semilogx(w,magdb) For the log-magnitude plot :
semilogx(w,phase)
![Page 34: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/34.jpg)
Control Design
Rlocus
Consider a feedback loop where G(s)H(s) = KP(s) and K is a gain and P(s) contains the poles and zeros of the controller and of the plant.
Suppose that the numerator and denominator coefficients of P(s) are stored in the vectors num and den.
rlocus(num,den)
![Page 35: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/35.jpg)
State Space Representation
Ss Step Lsim Ss2tf Tf2ss ss2ss
![Page 36: MATLAB BASICS ECEN 605 Linear Control Systems Instructor: S.P. Bhattacharyya](https://reader035.vdocuments.us/reader035/viewer/2022062404/551a8e12550346b52d8b5c93/html5/thumbnails/36.jpg)