Download as pdf or txt
Download as pdf or txt
You are on page 1of 86

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 very


helpful in solving the kinds of mathematical problems
you 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, and


Linux platforms.
Introduction Other Scientific Computing Softwares 1–2

Software Windows Linux Free Software


Mathematica
http://www.wolfram.com/products/mathematica/index.html 4 4 7
SciLab
http://scilabsoft.inria.fr/ 4 4 4
Octave
http://www.octave.org 7 4 4
Maple
http://www.maplesoft.com 4 4 7

Matlab tutorial, June 15, 2005


Lecture 2
Variables

• Double array
• Char array
Variables Double array 2–1

Generate at the command line.


>> x = 1
x = A scalar, a vector , or a matrix is stored as
1 a double array with different sizes.
>> y = [1 2]
y =
1 2 A polynomial is also stored as a row vector.
>> z = [1 -1 0; 0 1 -1; 0 0 1]
q(s) = s2 + 5s + 2
z =
1 -1 0 >> q = [1 5 2]
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
Matlab tutorial, June 15, 2005
Variables Char array 2–2

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 The help command 3–1

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 The help command (cont.) 3–2

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 The help command (cont.) 3–3

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 The lookfor command 3–4

• 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 The lookfor command 3–5

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 The help desk 3–6

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 The workspace 3–7

• The area of memory.


• Accessible from the command line.
• The commands who and whos show the current contents.
who −→ a short list
whos −→ 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 The workspace (cont.) 3–8

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 The workspace (cont.) 3–9

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 The save command 3 – 10

• 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 The search path 3 – 11

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 The format command 4–1

• 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 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

Note For proper spacing, use Fixedsys or Courier.


Matlab tutorial, June 15, 2005
The command window Suppressing output 4–3

• 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 Long command lines 4–4

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 Command line editing 4–5

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 Command line editing (con.) 4–6

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 Command line editing (con.) 4–7

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 Example Usages 5–1

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 Example Usages (con.) 5–2

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 Generating matrices 6–1

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 Generating matrices (cont.) 6–2

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 Loading matrices 6–3

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, create
outside 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 Concatenation 6–4

Concatenate matrices to a bigger matrix:

 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

Matlab tutorial, June 15, 2005


Matrix operations Manipulating rows and columns 6–5

Access a row. Access a column.

 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

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 Matrix relating functions 6–7

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 Matrix relating functions 6–8

 
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

Matlab tutorial, June 15, 2005


Matrix operations Matrix relating functions (cont.) 6–9

size and length:


• size of matrices; length of vectors;
 
1 −1 0
• outputs of size: Suppose B = .
0 1 −1

 X = size(B)  [Y, Z] = size(B)


X = Y =
2 3 2
Z =
3
To get # of rows or columns, specify 1 or 2 as the second argument, respectively.
 X = size(B,1)  Y = size(B,2)
X = Y =
2 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 Plotting 7–1

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 Plotting: Axis title and label 7–2

 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)

Matlab tutorial, June 15, 2005


Graphics in Matlab Plotting: Multiple data sets in single graph 7–3

 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 Plotting (cont.) 7–4

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

Matlab tutorial, June 15, 2005


Graphics in Matlab Plotting: Line styles, markers, and colors 7–5

 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 Plotting: Line styles and marker types 7–6

• 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

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 Plotting examples (cont.) 7–8

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

Matlab tutorial, June 15, 2005


Graphics in Matlab Figure windows 7–9

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 Add plots to an existing figure 7 – 10

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 Adding plots to an existing graph (cont.) 7 – 11

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

Matlab tutorial, June 15, 2005


Graphics in Matlab Subplots 7 – 12

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 Subplots (cont.) 7 – 13

 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

Matlab tutorial, June 15, 2005


Graphics in Matlab Saving and Exporting 7 – 14

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 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.

% 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 My function (cont.) 8–3

First, type the information of this function and comment it. This message will be displayed
when 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 My function (cont.) 8–4

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 TF Object 9–1

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 numerator
and denominator, respectively.

Create a continuous-time transfer function. Create a discrete-time transfer function and


specify the sampling time (1 sec).
>> systf = tf(num,den) >> systfz = tf(num,den,1)
Transfer function: Transfer function:
s + 2 z + 2
------------- -------------
s^2 + 2 s + 1 z^2 + 2 z + 1

Sampling time: 1
Matlab tutorial, June 15, 2005
Control Systems Toolbox SS Object 9–2

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 SS Object (cont.) 9–3

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 ZPK Object 9–4

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. 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

• 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 Conversion among TF, SS, ZPK 9–6

• 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

Matlab tutorial, June 15, 2005


Control Systems Toolbox Conversion between 9–7
Continuous-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) = (s2(s+1)


+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) >> sysd = c2d(systf,0.1,’zoh’)


Transfer function: Transfer function:
s + 1 0.08611 z - 0.07791
------------- ----------------------
s^2 + 4 s + 5 z^2 - 1.629 z + 0.6703
Sampling time: 0.1
Create a TF object
Convert to the discrete-time model.

Matlab tutorial, June 15, 2005


Control Systems Toolbox Poles, Zeros, and Eigenvalues 9–8

According to systf created in the previous slide, we calculate poles, zeros and eigenvalues of
the system by the following command.

>> pole(systf) >> zero(systf) >> eig(systf)


ans = ans = ans =
-2.0000 + 1.0000i -1 -2.0000 + 1.0000i
-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 Classical Design 9–9

• 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

Matlab tutorial, June 15, 2005


Control Systems Toolbox Root Loci 9 – 10

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)

>> num=[1 1];den = [1 4 5]; Create LTI models in various types


>> [a,b,c,d]=tf2ss(num,den);
>> systf = tf(num,den);
>> sysss=ss(a,b,c,d);
The command can be used with all model
>> rlocus(num,den)
types.
>> rlocus(systf)
>> rlocus(a,b,c,d)
>> rlocus(sysss)

Matlab tutorial, June 15, 2005


Control Systems Toolbox Root Loci (cont.) 9 – 11

Root Locus Root Locus


1.5 1.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.

Matlab tutorial, June 15, 2005


Control Systems Toolbox SISOTOOL 9 – 12

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.

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 SISOTOOL (cont.) 9 – 13

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

• step Step response


• impulse Impulse response
• initial Response of state-space system with given initial conditions.

Matlab tutorial, June 15, 2005


Control Systems Toolbox Step response 9 – 15

The step response of G(s) from t = 0 to t = 5 can be obtained by:


Step Response
0.35

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)

Matlab tutorial, June 15, 2005


Control Systems Toolbox Impulse response 9 – 16

The impulse response of G(s) from t = 0 to t = 5 can be obtained by:


Impulse Response
1.2

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)

Matlab tutorial, June 15, 2005


Control Systems Toolbox Initial response 9 – 17

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)

>> 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 Frequency-domain analysis 9 – 18

• 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 Bode 9 – 19

Draw a Bode plot of open-loop system G(s).


Bode Diagram
>> bode(systf)
−10

−15 The following command return the response


−20
magnitudes and phases in degrees along with
Magnitude (dB)

−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.

Matlab tutorial, June 15, 2005


Control Systems Toolbox Nyquist 9 – 20

The command nyquist draws the Nyquist plot of G(s).


>> nyquist(systf)
Nyquist Diagram
1

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

Note: The argument of nyquist is any kind of LTI models.


Matlab tutorial, June 15, 2005
Control Systems Toolbox Frequency Response 9 – 21

The frequency response of G(s)


s+1 s=jw (jw + 1)
G(s) = 2 ⇐⇒ G(jw) =
s + 4s + 5 (−w2 + 4jw + 5)
can be computed by :
1. Specifying a vector of frequency grid • The commands mag(Gjw) and
>> w = logspace(-2,3,500) phase(Gjw) give the same results
0.0100 as that obtained from bode command.
0.0102
...
2. Generating the command
>> Gjw = freqresp(systf,w)
Gjw(:,:,1) =
0.2000 + 0.0004i
Gjw(:,:,2) =
0.2000 + 0.0004i
...
Matlab tutorial, June 15, 2005
Control Systems Toolbox Response analysis GUI: LTI Viewer 9 – 22

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

Matlab tutorial, June 15, 2005


Control Systems Toolbox Response analysis GUI: LTI Viewer (cont.) 9 – 23

1. Import the data to the viewer

Choose the variable systf available in the workspace.


Matlab tutorial, June 15, 2005
Control Systems Toolbox Response analysis GUI: LTI Viewer (cont.) 9 – 24

2. Select the option of graphs to be displayed. For example, step and bode response will be
shown in the same window.

Matlab tutorial, June 15, 2005


Control Systems Toolbox Simulink 9 – 25

Simulink is a tool for creating a diagram


of a particular system and simulating its
time response.

See an example in the class.

Matlab tutorial, June 15, 2005


Control Systems Toolbox References 9 – 26

[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

You might also like