Matlab Tutorial June15 2005
-
Upload
javier-toscano -
Category
Documents
-
view
10 -
download
0
Transcript of Matlab Tutorial June15 2005
MATLAB Tutorial
Jitkomut Songsiri
Department of Electrical Engineering,
Faculty of Engineering,
Chulalongkorn University
Room 404, EE Bldg.
June 15, 2005
Lecture 1
Introduction
• MATLAB is a computer program that can be veryhelpful in solving the kinds of mathematical problemsyou will frequently encounter.
• MATLAB can solve a wide variety of numerical prob-lems, from the very basic to complex problems.
• MATLAB can be used to plot several kinds of graphs
• MATLAB is supported to Windows, Mac OS X, andLinux platforms.
Introduction 1 – 2Other Scientific Computing Softwares
Software Windows Linux Free SoftwareMathematicahttp://www.wolfram.com/products/mathematica/index.html 4 4 7
SciLabhttp://scilabsoft.inria.fr/ 4 4 4
Octavehttp://www.octave.org 7 4 4
Maplehttp://www.maplesoft.com 4 4 7
Matlab tutorial, June 15, 2005
Lecture 2
Variables
• Double array
• Char array
Variables 2 – 1Double array
Generate at the command line.
>> x = 1
x =
1
>> y = [1 2]
y =
1 2
>> z = [1 -1 0; 0 1 -1; 0 0 1]
z =1 -1 0
0 1 -1
0 0 1>> whos x y z
Name Size Bytes Class
x 1x1 8 double array
y 1x2 16 double array
z 3x3 72 double array
A scalar, a vector , or a matrix is stored asa double array with different sizes.
A polynomial is also stored as a row vector.
q(s) = s2 + 5s + 2
>> q = [1 5 2]
Matlab tutorial, June 15, 2005
Variables 2 – 2Char array
Generate at the command line.
>> str = ’hello’
str =
hello
>> whos str
Name Size Bytes Class
str 1x5 10 char array
Grand total is 5 elements using 10 bytes
str is stored in a char array whose size is equal to the number of characters
Matlab tutorial, June 15, 2005
Lecture 3
Help and Matlab environment
• The help command
• The lookfor command
• The help desk and link to the mathworks
• The workspace
• The save command
• The search path
• Disk file manipulation
Help and Matlab environment 3 – 1The help command
To get syntax and instructions for inv,
� help inv
INV Matrix inverse.
INV(X) is the inverse of the square
matrix X. A warning message is printed
if X is badly scaled or nearly singular.
See also SLASH, PINV, COND, CONDEST,
LSQNONNEG, LSCOV.
All functions are organized into directories, e.g., all linear algebra functions are in matfun.To list the name and description of functions in matfun,
� help matfun
Matlab tutorial, June 15, 2005
Help and Matlab environment 3 – 2The help command (cont.)
Alternatively, to list the name only,
� what matfun
The command help alone lists all the directories,
� help
HELP topics:
matlab\general - General purpose commands
matlab\ops - Operators and special . . .matlab\lang - Programming language . . .. . .
Matlab tutorial, June 15, 2005
Help and Matlab environment 3 – 3The help command (cont.)
The help window
• Select Help Window under the Help menu (on PCs)
• Click the question mark on the menu bar (on PCs)
• Type helpwin
Help window gives the same information as help,
Matlab tutorial, June 15, 2005
Help and Matlab environment 3 – 4The lookfor command
• Search for functions based on a keyword.
• Search through the first line of help text.
• Return the first line containing a keyword.
For example,
� help inverse
inverse.m not found
One should type
� lookfor inverse
Matlab tutorial, June 15, 2005
Help and Matlab environment 3 – 5The lookfor command
Results:
INVHILB Inverse Hilbert matrix.
ACOS Inverse cosine.
ASIN Inverse sine.
ATAN Inverse tangent.
ERFINV Inverse error function.
INV Matrix inverse.
PINV Pseudoinverse.
. . .
Adding the option -all,
� lookfor -all inverse
searches the entire help text (not just the first line).
Matlab tutorial, June 15, 2005
Help and Matlab environment 3 – 6The help desk
The Matlab Help Desk
• Provide access to helps and references stored in your system.
• Many documents in HTML format.
• Access via internet web browser.
• Select the Help Desk under the Help menu (on PCs).
• Type helpdesk on the workspace.
Note This gives more details than help.
Matlab tutorial, June 15, 2005
Help and Matlab environment 3 – 7The workspace
• The area of memory.
• Accessible from the command line.
• The commands who and whos show the current contents.
who −→ a short listwhos −→ size and storage information
For example, (variables from the Variables Lecture)
>> who
Your variables are:
A P sysss syszpkz
B Z sysssz x
C den systf y
D num systfz z
K str syszpk
Matlab tutorial, June 15, 2005
Help and Matlab environment 3 – 8The workspace (cont.)
Results from whos
>> whos
Name Size Bytes Class
A 2x2 32 double array
B 2x1 16 double array
C 1x2 16 double array
D 1x1 8 double array
K 1x1 8 double array
P 1x2 16 double array
Z 1x1 8 double array
den 1x3 24 double array
num 1x2 16 double array
str 1x5 10 char array
sysss 1x1 2824 ss object
sysssz 1x1 2824 ss object
...
Matlab tutorial, June 15, 2005
Help and Matlab environment 3 – 9The workspace (cont.)
To delete all the existing variables from the workspace, enter the command clear
� clear
To delete specific variables use the commend clear followed by variables
� clear A B C D
Matlab tutorial, June 15, 2005
Help and Matlab environment 3 – 10The save command
• Save the contents of the workspace as a MAT-file
• Read the MAT-file with command load
Save all contents to var_data.mat:� save var data
Save specific contents:� save var data systf sysss syszpk
Append the data to var_data.mat:� save -append var data systf sysss syszpk
Restore the saved data to workspace:� load var data
Matlab tutorial, June 15, 2005
Help and Matlab environment 3 – 11The search path
To view the search path,
• Type path.
• Choose Set Path from the File menu (on PCs).
Disk file manipulation
Generic system commands: dir, type, delete, and cd.
MATLAB MS-DOS UNIX
dir dir ls
type type cat
delete del rm
cd chdir cd
Matlab tutorial, June 15, 2005
Lecture 4
The command window
• The format command
• Suppressing output
• Long command lines
• Command line editing
The command window 4 – 1The format command
• specify the displayed numeric format
• does not affect computation
Example. Display x in different formats
� x = [4/3 1.2345e-6]
format short
1.3333 0.0000
format short e
1.3333e+000 1.2345e-006
format short g
1.3333 1.2345e-006
format long
1.33333333333333 0.00000123450000
Matlab tutorial, June 15, 2005
The command window 4 – 2The format command (con.)
format long e
1.333333333333333e+000 1.234500000000000e-006
format long g
1.33333333333333 1.2345e-006
format bank
1.33 0.00
format rat
4/3 1/810045
format hex
3ff5555555555555 3eb4b6231abfd271
Note For proper spacing, use Fixedsys or Courier.
Matlab tutorial, June 15, 2005
The command window 4 – 3Suppressing output
•Matlab automatically displays results.
• Need no display. −→ End the line with “;”.
Useful for:
– Large outputs,
– Scripts and functions.
• Multiple expressions in one line. −→ Separate each with “;”.
� a = 1; b = a+1;
• Force to display results. −→ Use “,” instead.
� a = 1, b = a+1,
a =
1
b =
2
Matlab tutorial, June 15, 2005
The command window 4 – 4Long command lines
Statement does not fit in one line. −→ Use three periods, then Enter.
� s = 1 -1/2 + 1/3 ...
-1/4 + 1/5 - 1/6 ...
+ 1/7 - 1/8 + 1/9 ...
- 1/10 + 1/11 - 1/12
s =
0.6532
Note
• Blank spaces around =, +, and - are optional.
• Frequently used inside scripts and functions
Matlab tutorial, June 15, 2005
The command window 4 – 5Command line editing
Suppose you mistakenly enter
� x = (1 + sqt(5))/2
Undefined function or variable ’sqt’
The error is due to the misspell of sqrt.
Solutions:
• Press the ↑ key to correct;
• Guide Matlab with a few characters, and press ↑;
• Use command history window.
Matlab tutorial, June 15, 2005
The command window 4 – 6Command line editing (con.)
Command line editing keys.
↑ Ctrl + p Recall previous line
↓ Ctrl + n Recall next line
← Ctrl + b Move back one character
→ Ctrl + f Move forward one character
Ctrl + ← Ctrl + r Move right one word
Ctrl + → Ctrl + l Move left one word
Matlab tutorial, June 15, 2005
The command window 4 – 7Command line editing (con.)
Home Ctrl + a Move to beginning of line
End Ctrl + e Move to end of line
Esc Ctrl + u Clear line
Del Ctrl + d Delete character at cursor
Backspace Ctrl + h Delete character before cursor
Ctrl + k Delete to end of line
Matlab tutorial, June 15, 2005
Lecture 5
Elementary Mathematical Functions
The commands are listed in elfun category
>> help elfun
Elementary math functions.
Trigonometric.
sin - Sine.
sinh - Hyperbolic sine.
asin - Inverse sine.
asinh - Inverse hyperbolic sine.
cos - Cosine.
cosh - Hyperbolic cosine.
acos - Inverse cosine.
acosh - Inverse hyperbolic cosine.
tan - Tangent.
tanh - Hyperbolic tangent
...
Elementary Mathematical Functions 5 – 1Example Usages
Example 1 Check the validation of Euler formula
ejθ = cos θ + j sin θ
by using the command
exp - Exponential
sin - Sine
cos - Cosine
>> theta= pi/3;
>> exp(j*theta)
ans =
0.5000 + 0.8660i
>> cos(theta)+j*sin(theta)
ans =
0.5000 + 0.8660i
Matlab tutorial, June 15, 2005
Elementary Mathematical Functions 5 – 2Example Usages (con.)
Example 2 Find the absolute value of a complex number, z = a + jb
|z| =√
a2 + b2
by using the command
abs - Absolute value
real - Complex real part
imag - Complex imaginary part
sqrt - Square root
>> z = 3+j*4 ;
>> abs(z)
ans =
5
>> sqrt(real(z)^2+imag(z)^2)
ans =
5
Matlab tutorial, June 15, 2005
Lecture 6
Matrix operations
• Generating matrices
• Loading matrices
• Concatenation
• Manipulating rows and columns
• Matrix relating function
Matrix operations 6 – 1Generating matrices
Generate at the command line.
� A = [1 -1 0; 0 1 -1; 0 0 1]
A =1 -1 0
0 1 -1
0 0 1
The numeric format of the command window is specified by the format command:
Several interesting functions,
• ones(n,m),• zeros(n,m),• eye(n).
Matlab tutorial, June 15, 2005
Matrix operations 6 – 2Generating matrices (cont.)
Generate via M-file editor.
A = [ 1 -1 0
0 1 -1
0 0 1 ]
Save as an M-file, say mat_gen.m, and run at the command line,
� mat gen.m
A =1 -1 0
0 1 -1
0 0 1
The extension .m can be omitted.
Matlab tutorial, June 15, 2005
Matrix operations 6 – 3Loading matrices
The load command
• Read binary files (MAT files)
• Read text files
Text files must be a rectangular table of numbers, separated by blanks. For example, createoutside Matlab :
16 3 2
5 10 11
8 4 7
Save as matrix.txt, and run
� load matrix.txt
Matlab tutorial, June 15, 2005
Matrix operations 6 – 4Concatenation
Concatenate matrices to a bigger matrix:
� A = [B C; D]
• # of rows of B = C,
• # of columns of D = B + C.
For example, we can get A =
1 −1 00 1 −10 0 1
from
B =
[1 −10 1
], C =
[0−1
], D =
[0 0 1
].
Matlab tutorial, June 15, 2005
Matrix operations 6 – 5Manipulating rows and columns
Access a row.
� X = A(3,:)
X =
0 0 1
Access a column.
� Y = A(:,2)
Y =-1
1
0
Replace a row
� A(2,:) = [1 2 3]
A =1 -1 0
1 2 3
0 0 1
Matlab tutorial, June 15, 2005
Matrix operations 6 – 6Manipulating rows and columns (cont.)
Delete a row.
� A(2,:) = [ ]
A =1 -1 0
0 0 1
Obtain a part of matrix. −→ Apply vector subscript.
� X = A([1 2],[2 3])
X =-1 0
1 -1
Matlab tutorial, June 15, 2005
Matrix operations 6 – 7Matrix relating functions
Basic arithmetic functions:
• plus(A,B) or A + B,
• minus(A,B) or A - B,
• mtimes(A,B) or A*B,
• A^b.
A scalar is applied to any matrix elementwisely.
Other linear algebraic functions:
• det(A),• trace(A),• A’,• inv(A).
Matlab tutorial, June 15, 2005
Matrix operations 6 – 8Matrix relating functions
Examples for A =
1 −1 00 1 −10 0 1
.
� det(A)
ans =
1
� trace(A)
ans =
3
� A’
ans =1 0 0
-1 1 0
0 -1 1
� inv(A)
ans =1 1 1
0 1 1
0 0 1
Matlab tutorial, June 15, 2005
Matrix operations 6 – 9Matrix relating functions (cont.)
size and length:
• size of matrices; length of vectors;
• outputs of size: Suppose B =
[1 −1 00 1 −1
].
� X = size(B)
X =
2 3
� [Y, Z] = size(B)
Y =
2
Z =
3
To get # of rows or columns, specify 1 or 2 as the second argument, respectively.
� X = size(B,1)
X =
2
� Y = size(B,2)
Y =
3
Matlab tutorial, June 15, 2005
Lecture 7
Graphics in Matlab
• Plotting
• Figure windows
• Add plots to an existing figure
• Subplots
• Print graphics
Graphics in Matlab 7 – 1Plotting
Different forms of plot.
• plot(y) −→ y vs indices of elements.
• plot(x,y) −→ y vs x.
For example, to plot sin(x) from 0 to 2π:
� x = 0:pi/100:2*pi;
� y = sin(x);
� plot(x,y)
Note The ”;” is used to suppressed the output.
Matlab tutorial, June 15, 2005
Graphics in Matlab 7 – 2Plotting: Axis title and label
� xlabel(’x (rad)’)
� ylabel(’Sine of x’)
� title(’Sine Function’,’FontSize’,12)
0 1 2 3 4 5 6 7−1
−0.8
−0.6
−0.4
−0.2
0
0.2
0.4
0.6
0.8
1
x (rad)
Sin
e of
x
Sine Function
Matlab tutorial, June 15, 2005
Graphics in Matlab 7 – 3Plotting: Multiple data sets in single graph
� y1= sin(x);
� y2 = sin(x-.25);
� y3 = sin(x-.5);
� plot(x,y1,x,y2,x,y3)
The legend command help identify each plot.
� legend(’sin(x)’,’sin(x-.25)’,’sin(x-.5)’)
Matlab tutorial, June 15, 2005
Graphics in Matlab 7 – 4Plotting (cont.)
0 1 2 3 4 5 6 7−1
−0.8
−0.6
−0.4
−0.2
0
0.2
0.4
0.6
0.8
1sin(x)sin(x−.25)sin(x−.5)
Matlab tutorial, June 15, 2005
Graphics in Matlab 7 – 5Plotting: Line styles, markers, and colors
� plot(x,y,’color style marker’)
color_style_marker is a string defining a color, a line style, and a marker type:
• Colors:
’c’ cyan, ’g’ green’m’ magenta, ’b’ blue’y’ yellow, ’w’ white’r’ red, ’k’ black
Matlab tutorial, June 15, 2005
Graphics in Matlab 7 – 6Plotting: Line styles and marker types
• Line styles:
’-’ solid ’:’ dotted’--’ dashed ’-.’ dash-dot
• Marker types:
’+’ plus sign, ’s’ square ’p’ pentagram’o’ o sign, ’d’ diamond ’h’ hexagram’*’ star sign, ’^’ up triangle ’>’ right triangle’x’ x sign, ’v’ down triangle ’<’ left triangle
Note again that you can edit colors, line styles, and markers interactively.
Matlab tutorial, June 15, 2005
Graphics in Matlab 7 – 7Plotting examples
Example 1: Red dotted line, plus sign.
� plot(x,y,’r:+’)
Example 2: Black squares, no line.
� plot(x,y,’ks’)
Example 3: # of markers < # plotting points.
� x1 = 0:pi/100:2*pi;
� x2 = 0:pi/10:2*pi;
� plot(x1,sin(x1),’r:’,x2,sin(x2),’r+’)
Matlab tutorial, June 15, 2005
Graphics in Matlab 7 – 8Plotting examples (cont.)
0 1 2 3 4 5 6 7−1
−0.8
−0.6
−0.4
−0.2
0
0.2
0.4
0.6
0.8
1
Matlab tutorial, June 15, 2005
Graphics in Matlab 7 – 9Figure windows
For any graphing functions,
• No figure window. −→ open a new one.
• One figure window. −→ use the existing one.
• Many figure windows. −→ chooses the current figure
Make figure #n a current figure.
� figure(n)
Open blank figure.
� figure
Matlab tutorial, June 15, 2005
Graphics in Matlab 7 – 10Add plots to an existing figure
The command hold prepares to add a new graph to the existing axis.
� sys = tf(1,[1 2 3])
� [y1,t1] = step(sys); plot(t1,y1,’--’)
� hold on
� [y2,t2] = impulse(sys); plot(t2,y2,’:’)
� hold off
Toggle holding states:
� sys = tf(1,[1 2 3])
� [y1,t1] = step(sys); plot(t1,y1,’--’)
� hold
Current plot held
� [y2,t2] = impulse(sys); plot(t2,y2,’:’)
� hold
Current plot released
Matlab tutorial, June 15, 2005
Graphics in Matlab 7 – 11Adding plots to an existing graph (cont.)
0 1 2 3 4 5 6−0.05
0
0.05
0.1
0.15
0.2
0.25
0.3
0.35
0.4
Matlab tutorial, June 15, 2005
Graphics in Matlab 7 – 12Subplots
The subplot command:
• Display multiple plots in one window.
• Print multiple plots on one piece of paper.
Type subplot(m,n,p):
• Partition a window into an m× n table of subplots.
• Select the pth subplot.
Figure # runs from −→ and from ↓.
Matlab tutorial, June 15, 2005
Graphics in Matlab 7 – 13Subplots (cont.)
� t = 0:pi/10:2*pi;
� [X,Y,Z] = cylinder(4*cos(t));
� subplot(2,2,1); mesh(X)
� subplot(2,2,2); mesh(Y)
� subplot(2,2,3); mesh(Z)
� subplot(2,2,4); mesh(X,Y,Z)
010
2030
0
20
40−5
0
5
010
2030
0
20
40−5
0
5
010
2030
0
20
400
0.5
1
−50
5
−5
0
50
0.5
1
Matlab tutorial, June 15, 2005
Graphics in Matlab 7 – 14Saving and Exporting
Saving a figure
• Select Save from the File menu.
• The standard format is FIG.
Exporting figure
• Select Export from the File menu.
• EPS format −→ LATEX
• EMF format −→ MS Powerpoint
Matlab tutorial, June 15, 2005
Lecture 8
M-Files
An M-File is used to
• Store the set of commands to be executed at once
• Create your own function
M-Files 8 – 1Script M-file
For example, save the commands from ”Graphics in Matlab” Lecture to a file namedplotsine.m
The command
>> plotsine
gives the same results as we did and all variables are shown in the workspace.
Matlab tutorial, June 15, 2005
M-Files 8 – 2My function
For example, to calculate age from a given birthdate, we develop a function namedcal age.m in Matlab editor.
% CAL_AGE(MONTH,YEAR) give the age of a person by assigning month
% and year of the birthdate. MONTH and YEAR are specified in
% numeric format and your age will be returned.
%
% [yr,mo] = CAL_AGE(7,1978);
function[yr,mo] = cal_age(month,year)
today = clock; % get the current date from clock command
yr = today(1)-year- (month > today(2)) ; % year difference
mo = today(2)-month + 12*(today(2) < month); % month difference
fprintf(’Your age is %d years %d months’,yr,mo) % print the result
end
Matlab tutorial, June 15, 2005
M-Files 8 – 3My function (cont.)
First, type the information of this function and comment it. This message will be displayedwhen we run help command.
>> help cal age
CAL_AGE(MONTH,YEAR) give the age of a person by assigning month
and year of the birthdate. MONTH and YEAR are specified in numeric
format and your age will be returned.
[yr,mo] = CAL_AGE(7,1978);
Second, define input arguments and returned values. Terminate the function by using end
function[yr,mo] = cal age(month,year)
...
end
Matlab tutorial, June 15, 2005
M-Files 8 – 4My function (cont.)
Third, insert your codes and save this file with the extension .m
Your function will be executed by using the filename as a command
>> [yr,mo] = CAL_AGE(7,1978)
Your age is 26 years 10 months
yr =
26
mo =
10
Note:
• Variables defined in a function are local. They will not be shown in the workspace.
• The function can be run only if you are in the same path as the file is kept.
Matlab tutorial, June 15, 2005
Lecture 9
Control Systems Toolbox
• TF, SS, ZPK object
• Frequently Used Commands
• Classical Design
• Time-domain analysis
• Frequency-domain analysis
• Simulink
Control Systems Toolbox 9 – 1TF Object
A tf object is a continuous-time or a discrete-time transfer function created by tf command.
Define the numerator and denominator of a transfer function.>> num = [1 2]; den = [1 2 1] ;
num and den which are stored as double arrays represent the coefficients of the numeratorand denominator, respectively.
Create a continuous-time transfer function.
>> systf = tf(num,den)
Transfer function:
s + 2
-------------
s^2 + 2 s + 1
Create a discrete-time transfer function andspecify the sampling time (1 sec).
>> systfz = tf(num,den,1)
Transfer function:
z + 2
-------------
z^2 + 2 z + 1
Sampling time: 1
Matlab tutorial, June 15, 2005
Control Systems Toolbox 9 – 2SS Object
An SS Object is a continuous-time or a discrete-time state-space model.
Define state-space system matrices.>> A = [-0.5 0; 0 -0.2] ; B = [1; 0] ; C =[1 0 ] ; D = 0 ;
Create a continuous-time state-space model.
>> sysss = ss(A,B,C,D)
a = b =
x1 x2 u1
x1 -0.5 0 x1 1
x2 0 -0.2 x2 0
c = d =
x1 x2 u1
y1 1 0 y1 0
Continuous-time model.
Matlab tutorial, June 15, 2005
Control Systems Toolbox 9 – 3SS Object (cont.)
Create a discrete-time state-space model and specify the sampling time (1 sec).
>> sysssz = ss(A,B,C,D,1)
a = b =
x1 x2 u1
x1 -0.5 0 x1 1
x2 0 -0.2 x2 0
c = d =
x1 x2 u1
y1 1 0 y1 0
Sampling time: 1
Discrete-time model.
Matlab tutorial, June 15, 2005
Control Systems Toolbox 9 – 4ZPK Object
A ZPK Object is a zero-pole-gain model.
Define zeros (Z), poles (P), and gain (K) of a transfer function.>> Z = -0.5 ; P = [-0.7 -1] ; K = 1 ;
Create a continuous-time ZPK model.
>> syszpk = zpk(Z,P,K)
Zero/pole/gain:
(s+0.5)
-------------
(s+0.7) (s+1)
Create a discrete-time ZPK model with thesampling time = 1 sec.
>> syszpkz = zpk(Z,P,K,1)
Zero/pole/gain:
(z+0.5)
-------------
(z+0.7) (z+1)
Sampling time: 1
Matlab tutorial, June 15, 2005
Control Systems Toolbox 9 – 5Frequently Used Commands
• Three combinations of conversions among TF, SS, ZPK.
• Conversion between continuous and discrete models.
• System poles, zeros, and eigenvalues.
Matlab tutorial, June 15, 2005
Control Systems Toolbox 9 – 6Conversion among TF, SS, ZPK
TF : Transfer FunctionSS : State Space ModelZPK : Zeros, Poles, and Gain
• ss2tf• ss2zp• tf2ss
• tf2zp• zp2tf• zp2ss
For example, convert G(s) =(s + 1)
(s2 + 4s + 5)to the state-space model.
>> num = [1 1]; den = [1 4 5];
>> [a,b,c,d] = tf2ss(num,den)
a = b =
-4 -5 1
1 0 0
c = d =
1 1 0
Matlab tutorial, June 15, 2005
Control Systems Toolbox 9 – 7Conversion betweenContinuous-Time and Discrete-Time Models
• c2d : Conversion of continuous-time models to discrete time.
• d2c : Conversion of discrete-time models to continuous time.
For example, convert G(s) = (s+1)(s2+4s+5)
to a discrete model by assuming Zero-Order hold on
the inputs with the sampling time = 0.1 sec.
>> systf = tf(num,den)
Transfer function:
s + 1
-------------
s^2 + 4 s + 5
Create a TF object
>> sysd = c2d(systf,0.1,’zoh’)
Transfer function:
0.08611 z - 0.07791
----------------------
z^2 - 1.629 z + 0.6703
Sampling time: 0.1
Convert to the discrete-time model.
Matlab tutorial, June 15, 2005
Control Systems Toolbox 9 – 8Poles, Zeros, and Eigenvalues
According to systf created in the previous slide, we calculate poles, zeros and eigenvalues ofthe system by the following command.
>> pole(systf)
ans =
-2.0000 + 1.0000i
-2.0000 - 1.0000i
>> zero(systf)
ans =
-1
>> eig(systf)
ans =
-2.0000 + 1.0000i
-2.0000 - 1.0000i
• The input arguments of pole and zero are LTI models (SS or TF object)
• The input argument of eig could be:
1. An LTI model (SS or TF object)
2. A square matrix
Matlab tutorial, June 15, 2005
Control Systems Toolbox 9 – 9Classical Design
• rlocus computes and plots the root locus of a single-input, single-output LTI model
• sisotool A Graphical User Interface that allows you to design single-input/single-output(SISO) compensators.
−10 −8 −6 −4 −2 0 2 4−2
−1.5
−1
−0.5
0
0.5
1
1.5
2
Root Locus
Real Axis
Imag
inar
y A
xis
rlocus sisotool
Matlab tutorial, June 15, 2005
Control Systems Toolbox 9 – 10Root Loci
In plotting root loci with MATLAB, we deal with the system equation in the form of
1 + KG(s) = 0
For example, let G(s) =(s + 1)
(s2 + 4s + 5).
>> num=[1 1];den = [1 4 5];
>> [a,b,c,d]=tf2ss(num,den);
>> systf = tf(num,den);
>> sysss=ss(a,b,c,d);
Create LTI models in various types
>> rlocus(num,den)
>> rlocus(systf)
>> rlocus(a,b,c,d)
>> rlocus(sysss)
The command can be used with all modeltypes.
Matlab tutorial, June 15, 2005
Control Systems Toolbox 9 – 11Root Loci (cont.)
−4.5 −4 −3.5 −3 −2.5 −2 −1.5 −1 −0.5 0−1.5
−1
−0.5
0
0.5
1
1.5
Root Locus
Real Axis
Imag
inar
y A
xis
−4.5 −4 −3.5 −3 −2.5 −2 −1.5 −1 −0.5 0−1.5
−1
−0.5
0
0.5
1
1.5
System: systf2 Gain: 1.66 Pole: −3.99 Damping: 1 Overshoot (%): 0 Frequency (rad/sec): 3.99
Root Locus
Real Axis
Imag
inar
y A
xis
• Clicking at a point on root locus returns the closed-loop poles, feedback gain (K), andother parameters.
• The user is able to design the gain (K) by dragging mouse to the appropriate pointcorresponding to the desired closed-loop pole and time response specification.
Matlab tutorial, June 15, 2005
Control Systems Toolbox 9 – 12SISOTOOL
This GUI lets you design single-input/single-output (SISO) compensators by interacting withthe root locus, Bode, and Nichols plots of the open-loop system.
1. Current parameters of the Compensator
2. Configuration of Feedback System
3. Root Locus
4. Open-Loop Bode Plot
Matlab tutorial, June 15, 2005
Control Systems Toolbox 9 – 13SISOTOOL (cont.)
The structure of the compensator can becustomized.
Step response, Closed-Loop Bode plot,and other loop responses are provided inanalysis menu.
Matlab tutorial, June 15, 2005
Control Systems Toolbox 9 – 14Time-domain analysis
• step Step response
• impulse Impulse response
• initial Response of state-space system with given initial conditions.
Matlab tutorial, June 15, 2005
Control Systems Toolbox 9 – 15Step response
The step response of G(s) from t = 0 to t = 5 can be obtained by:
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 50
0.05
0.1
0.15
0.2
0.25
0.3
0.35
Step Response
Time (sec)
Am
plitu
de
>> step(systf,5)
Matlab tutorial, June 15, 2005
Control Systems Toolbox 9 – 16Impulse response
The impulse response of G(s) from t = 0 to t = 5 can be obtained by:
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5−0.2
0
0.2
0.4
0.6
0.8
1
1.2
Impulse Response
Time (sec)
Am
plitu
de
>> impulse(systf,5)
Matlab tutorial, June 15, 2005
Control Systems Toolbox 9 – 17Initial response
The inital response of G(s) from t = 0 to t = 5 with a given initial condition (x0 = [−1 1])can be obtained by:
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5−0.8
−0.6
−0.4
−0.2
0
0.2
0.4
0.6
0.8
1
Response to Initial Conditions
Time (sec)
Am
plitu
de
>> inital(sysss,[-1 1],5)
Note: The first argument of command initial must be an SS object or state-space model.
Matlab tutorial, June 15, 2005
Control Systems Toolbox 9 – 18Frequency-domain analysis
• bode Bode diagrams of the frequency response
• nyquist Nyquist plot
• freqresp Frequency response over a frequency grid
• ltiview Response analysis GUI (LTI Viewer)
Matlab tutorial, June 15, 2005
Control Systems Toolbox 9 – 19Bode
Draw a Bode plot of open-loop system G(s).
−40
−35
−30
−25
−20
−15
−10
Mag
nitu
de (
dB)
10−1
100
101
102
−90
−45
0
45
Pha
se (
deg)
Bode Diagram
Frequency (rad/sec)
>> bode(systf)
The following command return the responsemagnitudes and phases in degrees along withthe frequency data.
>> [MAG,PHASE,W] = bode(systf);
>> MAG
MAG(:,:,1) =
0.2008
>> PHASE
PHASE(:,:,1) =
1.1275
Note: The argument of bode is any kind of LTI models.
Matlab tutorial, June 15, 2005
Control Systems Toolbox 9 – 20Nyquist
The command nyquist draws the Nyquist plot of G(s).
>> nyquist(systf)
−1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4−1
−0.8
−0.6
−0.4
−0.2
0
0.2
0.4
0.6
0.8
1
Nyquist Diagram
Real Axis
Imag
inar
y A
xis
Note: The argument of nyquist is any kind of LTI models.
Matlab tutorial, June 15, 2005
Control Systems Toolbox 9 – 21Frequency Response
The frequency response of G(s)
G(s) =s + 1
s2 + 4s + 5
s=jw⇐⇒ G(jw) =(jw + 1)
(−w2 + 4jw + 5)
can be computed by :
1. Specifying a vector of frequency grid
>> w = logspace(-2,3,500)
0.0100
0.0102
...
2. Generating the command
>> Gjw = freqresp(systf,w)
Gjw(:,:,1) =
0.2000 + 0.0004i
Gjw(:,:,2) =
0.2000 + 0.0004i
...
• The commands mag(Gjw) andphase(Gjw) give the same resultsas that obtained from bode command.
Matlab tutorial, June 15, 2005
Control Systems Toolbox 9 – 22Response analysis GUI: LTI Viewer
The LTI Viewer is an interactive graphical user interface (GUI) for analyzing the time andfrequency responses of linear systems and comparing many systems in the same time.A blank viewer is run by
>> ltiview
Matlab tutorial, June 15, 2005
Control Systems Toolbox 9 – 23Response analysis GUI: LTI Viewer (cont.)
1. Import the data to the viewer
Choose the variable systf available in the workspace.
Matlab tutorial, June 15, 2005
Control Systems Toolbox 9 – 24Response analysis GUI: LTI Viewer (cont.)
2. Select the option of graphs to be displayed. For example, step and bode response will beshown in the same window.
Matlab tutorial, June 15, 2005
Control Systems Toolbox 9 – 25Simulink
Simulink is a tool for creating a diagramof a particular system and simulating itstime response.
See an example in the class.
Matlab tutorial, June 15, 2005
Control Systems Toolbox 9 – 26References
[1] Matlab Help [Computer Software], The Mathworks Inc., version 6.5.0.180913a,Release 13, 2002.
[2] W. Khaisongkram, MATLAB Tutorial, CSRL, Dept. of Electrical Engineering,Chulalongkorn University, July 2004.
Matlab tutorial, June 15, 2005