Professional Documents
Culture Documents
Matlab Tutorial June15 2005
Matlab Tutorial June15 2005
Jitkomut Songsiri
• Double array
• Char array
Variables Double array 2–1
str is stored in a char array whose size is equal to the number of characters
help inv
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 The help command (cont.) 3–2
what matfun
help
HELP topics:
For example,
help inverse
inverse.m not found
lookfor inverse
Results:
searches the entire help text (not just the first line).
>> 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
...
To delete all the existing variables from the workspace, enter the command clear
clear
clear A B C D
• Type path.
• Choose Set Path from the File menu (on PCs).
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 The format command (con.) 4–2
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
a = 1; b = a+1;
Statement does not fit in one line. −→ Use three periods, then Enter.
Note
• Blank spaces around =, +, and - are optional.
• Frequently used inside scripts and functions
x = (1 + sqt(5))/2
Undefined function or variable ’sqt’
Solutions:
>> z = 3+j*4 ;
>> abs(z)
ans =
5
>> sqrt(real(z)^2+imag(z)^2)
ans =
5
• Generating matrices
• Loading matrices
• Concatenation
• Manipulating rows and columns
• Matrix relating function
Matrix operations Generating matrices 6–1
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:
A = [ 1 -1 0
0 1 -1
0 0 1 ]
mat gen.m
A =
1 -1 0
0 1 -1
0 0 1
Text files must be a rectangular table of numbers, separated by blanks. For example, create
outside Matlab :
16 3 2
5 10 11
8 4 7
load matrix.txt
Matlab tutorial, June 15, 2005
Matrix operations Concatenation 6–4
A = [B C; D]
• # of rows of B = C,
• # of columns of D = B + C.
1 −1 0
For example, we can get A = 0 1 −1 from
0 0 1
1 −1 0
B = , C = , D = 0 0 1 .
0 1 −1
X = A(3,:) Y = A(:,2)
X = Y =
0 0 1 -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 Manipulating rows and columns (cont.) 6–6
Delete a row.
A(2,:) = [ ]
A =
1 -1 0
0 0 1
1 −1 0
Examples for A = 0 1 −1.
0 0 1
det(A) trace(A)
ans = ans =
1 3
A’ inv(A)
ans = ans =
1 0 0 1 1 1
-1 1 0 0 1 1
0 -1 1 0 0 1
• Plotting
• Figure windows
• Add plots to an existing figure
• Subplots
• Print graphics
Graphics in Matlab Plotting 7–1
• plot(x,y) −→ y vs x.
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
xlabel(’x (rad)’)
ylabel(’Sine of x’)
title(’Sine Function’,’FontSize’,12)
Sine Function
1
0.8
0.6
0.4
0.2
Sine of x
−0.2
−0.4
−0.6
−0.8
−1
0 1 2 3 4 5 6 7
x (rad)
y1= sin(x);
y2 = sin(x-.25);
y3 = sin(x-.5);
plot(x,y1,x,y2,x,y3)
legend(’sin(x)’,’sin(x-.25)’,’sin(x-.5)’)
1
sin(x)
sin(x−.25)
0.8 sin(x−.5)
0.6
0.4
0.2
−0.2
−0.4
−0.6
−0.8
−1
0 1 2 3 4 5 6 7
• Colors:
’c’ cyan, ’g’ green
’m’ magenta, ’b’ blue
’y’ yellow, ’w’ white
’r’ red, ’k’ black
• 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 Plotting examples 7–7
plot(x,y,’r:+’)
plot(x,y,’ks’)
x1 = 0:pi/100:2*pi;
x2 = 0:pi/10:2*pi;
plot(x1,sin(x1),’r:’,x2,sin(x2),’r+’)
0.8
0.6
0.4
0.2
−0.2
−0.4
−0.6
−0.8
−1
0 1 2 3 4 5 6 7
figure(n)
figure
The command hold prepares to add a new graph to the existing axis.
0.4
0.35
0.3
0.25
0.2
0.15
0.1
0.05
−0.05
0 1 2 3 4 5 6
Type subplot(m,n,p):
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)
5 5
0 0
−5 −5
40 40
30 30
20 20 20 20
10 10
0 0 0 0
1 1
0.5 0.5
0 0
40 5
30 5
20 20 0
10 0
0 0 −5 −5
Saving a figure
Exporting figure
An M-File is used to
• Store the set of commands to be executed at once
• Create your own function
M-Files Script M-file 8–1
For example, save the commands from ”Graphics in Matlab” Lecture to a file named
plotsine.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 My function 8–2
For example, to calculate age from a given birthdate, we develop a function named
cal age.m in Matlab editor.
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
First, type the information of this function and comment it. This message will be displayed
when we run help command.
Second, define input arguments and returned values. Terminate the function by using end
.
.
end
Third, insert your codes and save this file with the extension .m
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.
num and den which are stored as double arrays represent the coefficients of the numerator
and denominator, respectively.
Sampling time: 1
Matlab tutorial, June 15, 2005
Control Systems Toolbox SS Object 9–2
c = d =
x1 x2 u1
y1 1 0 y1 0
Continuous-time model.
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.
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. Create a discrete-time ZPK model with the
>> syszpk = zpk(Z,P,K) sampling time = 1 sec.
Zero/pole/gain: >> syszpkz = zpk(Z,P,K,1)
(s+0.5) Zero/pole/gain:
------------- (z+0.5)
(s+0.7) (s+1) -------------
(z+0.7) (z+1)
Sampling time: 1
Matlab tutorial, June 15, 2005
Control Systems Toolbox Frequently Used Commands 9–5
• ss2tf • tf2zp
TF : Transfer Function • ss2zp • zp2tf
SS : State Space Model
ZPK : Zeros, Poles, and Gain • tf2ss • zp2ss
(s + 1)
For example, convert G(s) =
2
to the state-space model.
(s + 4s + 5)
>> 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
According to systf created in the previous slide, we calculate poles, zeros and eigenvalues of
the system by the following command.
• 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
• 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.
Root Locus
2
1.5
0.5
Imaginary Axis
−0.5
−1
−1.5
−2
−10 −8 −6 −4 −2 0 2 4
Real Axis
rlocus sisotool
In plotting root loci with MATLAB, we deal with the system equation in the form of
1 + KG(s) = 0
(s + 1)
For example, let G(s) = 2
.
(s + 4s + 5)
1 1
0.5 0.5
System: systf2
Gain: 1.66
Pole: −3.99
Damping: 1
Imaginary Axis
Imaginary Axis
Overshoot (%): 0
Frequency (rad/sec): 3.99
0 0
−0.5 −0.5
−1 −1
−1.5 −1.5
−4.5 −4 −3.5 −3 −2.5 −2 −1.5 −1 −0.5 0 −4.5 −4 −3.5 −3 −2.5 −2 −1.5 −1 −0.5 0
Real Axis Real Axis
• Clicking at a point on root locus returns the closed-loop poles, feedback gain (K), and
other parameters.
• The user is able to design the gain (K) by dragging mouse to the appropriate point
corresponding to the desired closed-loop pole and time response specification.
This GUI lets you design single-input/single-output (SISO) compensators by interacting with
the root locus, Bode, and Nichols plots of the open-loop system.
The structure of the compensator can be Step response, Closed-Loop Bode plot,
customized. and other loop responses are provided in
analysis menu.
Matlab tutorial, June 15, 2005
Control Systems Toolbox Time-domain analysis 9 – 14
0.3
0.25
0.2
Amplitude
0.15
0.1
0.05
0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
Time (sec)
>> step(systf,5)
0.8
0.6
Amplitude
0.4
0.2
−0.2
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
Time (sec)
>> impulse(systf,5)
The inital response of G(s) from t = 0 to t = 5 with a given initial condition (x0 = [−1 1])
can be obtained by:
Response to Initial Conditions
1
0.8
0.6
0.4
0.2
Amplitude
−0.2
−0.4
−0.6
−0.8
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
Time (sec)
−25
the frequency data.
−30
−35
>> [MAG,PHASE,W] = bode(systf);
−40
45
>> MAG
0
MAG(:,:,1) =
Phase (deg)
0.2008
−45
−90
−1 0 1 2
>> PHASE
10 10 10 10
Frequency (rad/sec)
PHASE(:,:,1) =
1.1275
Note: The argument of bode is any kind of LTI models.
0.8
0.6
0.4
0.2
Imaginary Axis
−0.2
−0.4
−0.6
−0.8
−1
−1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4
Real Axis
The LTI Viewer is an interactive graphical user interface (GUI) for analyzing the time and
frequency responses of linear systems and comparing many systems in the same time.
A blank viewer is run by
>> ltiview
2. Select the option of graphs to be displayed. For example, step and bode response will be
shown in the same window.
[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.