You are on page 1of 24

MATLAB in Control

Systems
Practical Session 1
Commonly used commands
Principles
MATLAB: Matrix environment i.e. matrices, vectors,
constants and labels as variables.
Different toolboxes access different environments
Poly-
nomial
Control
Systems
Symbolic
Math
Organising the workspace
All MATLAB commands are typed in lowercase .
Recommendation: Capitalise your variables names
Previous commands can be accessed by using the
UP arrow, so that
Commands issued incorrectly, can be accessed and
edited
Hitting <Enter> will execute the edited command.
Clear variables with clear Var or clear
Clear command window with clc no effect on stored
variables or command history
Polynomial Domain
Rows
Matrices and vectors
| | 3 2 1
(
(
(

k h g
f e d
c b a
Matrix row elements are separated by spaces or
commas (or both), e.g. [2 3 4] or [2, 3, 4]
Polynomial Domain
Columns
Columns are created by separating rows with semi-
colons, e.g. [1, 2, 3; 4, 5, 6].
Columns may also be ceated by entering a carriage
return after each row:
[2 4 6
8 5 3];
Polynomial Domain
Matrix Manipulations
Matrices and vectors may be transposed by
adding an apostrophe after the specification,
e.g. A or B=[1 2 4].
Normal rules regarding dimensions apply
when arithmetic operations are performed.
Polynomial Domain
Inverse and determinants
The inverse of a matrix may be obtained by the
commands inv(A) or A
-1
, i.e. A^-1
The determinant of a matrix may be found by the
command det(A)
Polynomial Domain
Vectors
Vectors are matrices that have either a single
row of elements, or a single column of
elements.
Row vectors are used in Control Systems to
represent polynomials, i.e.
[1 0 4 6 9]
represents
s
4
+ 0 + 4s
2
+ 6s + 9
Polynomial Domain
Variables
Variables can be created by setting the
variable name equal to the value e.g.
Mat = [2,6,8;2,4,6;9,7,8];
K = 5;
V = [4 6 8];
L = Label;
Depending on the type , various operations
may be performed on variables, e.g.
C=9;
D=sqrt(C) OR D=C^0.5
Polynomial Domain
Creating Labels
Labels are created by including them in single
quotes, e.g. Label
Programming
Commands are executed as they are issued
in the command window.
A set of commands may be programmed in
an m-file, and batch-executed from the m-file,
by selecting (high-lighting) the commands,
followed by pressing the F9 key.
M-files stored in the specified path may also
be executed from the command window, by
typing the name of the m-file.
Programming
Once the answer is obtained, it may be
copied from the command window back into
the m-file, and commented
Example: N=[1 4]; D=[2 4 6];
tf(N,D)
% s+4
% --------------
% 2s
2
+4s+6
Commonly Used Commands
Nise Chapter 2 (1)
conv(A,B) Used to multiply polynomials,
represented as row vectors, with each other.
(Also conv([1 3 5],[2 4 6])
poly(A) Where A=[-a, -b, -c]) Used to
create polynomials if roots are known, i.e. find
A as a polynomial if A=(s+a)(s+b)(s+c).
Commonly Used Commands
Nise Chapter 2 (2)
roots(A) Used to extract the value of the roots of a
polynomial.
residue(A,B) Used to find the residues
(coefficients) of the partial fractions of a function of
which the numerator is A (or [a b c ]) and the
denominator is B (or [f g h ]).
[c,r,d]=residue(A,B) Used to find the coefficients,
roots and direct quotient of the function A/B as above
in a single operation. The answer is given as column
vectors c (coefficients), r (roots) and row vector d
(direct quotient).
Commonly Used Commands
Nise Chapter 2 (3)
tf(A,B) Creates the transfer function in the
classical form
zpk(N,D,k) Creates the transfer function in
the zero-pole-gain form. N are the roots of the
numerator, i.e. if the numerator is (s+4)(s -2),
N = [-4, 2], and D the roots of the
denominator.
k is a coefficient, known as the direct gain.
Commonly Used Commands
Nise Chapter 2 (4)
Once the transfer function is found in one
form, in can be converted to the other form by
the same commands, e.g.
A=tf([1,4,4],[1 3 5 7]);
B=zpk(A) and
C=tf(B) (=A)
Transfer functions found in this way are
referred to as LTI objects (Linear Time
Invariant Objects)
Converting between domains
Control Systems to Polynomial
Transfer functions (LTI objects) may be
converted to the polynomial domain with the
instruction: [N,D]=tfdata(G,v)
Example:
8 14 5
12 2
2 3
2
+ + +
+ +
=
s s s
s s
G
[N,D]=tfdata(G,v) yields:
N=[0 1 2 12]
D=[1 5 14 8]
Converting between domains
Polynomial to Symbolic Math
Row and column vectors can be converted to
symbolic math with the poly2sym(A)
command
Column vectors will yield the same result as
the transposedrow vector.
The default variable is x
Example: If A=[1 3 5] and B=[1;3;5]
poly2sym(A) yields: poly2sym(B) yields:
x^2+3x+5 x^2+3x+5
Converting between domains
Symbolic Math to Polynomial (1)
Variables must be declared for symbolic
math e.g syms s
The numerator and denominator of a
symbolic math expression is first
separated with `[N,D] = numden(H)
The numerator and denominator are
then separately converted to the
polynomial domain with sym2poly(A)
Converting between domains
Symbolic Math to Polynomial (2)
Example: syms s
A=(s+4)/(s^2+2*s+24);
[Ns,Ds]= numden(A)
% Ns=s+4
% Ds= s^2+2*s+24
N=sym2poly(Ns)
% N=[1 4]
sym2poly(Ds)
% D=[1 2 24]
Laplace and Inverse Laplace
Laplace transforms may be converted to
symbolic math functions with ilaplace(B)
Example
T=(s+3)/(s^2+8*s+25)
Var=ilaplace(T)
% exp(-4*t)*cos(3*t)-1/3*exp(- 4*t)*sin(3*t)
Example 1
Find the output (in the time domain) if a
28-unit step is applied to a system with
the given transfer function, using the
residue() command in MATLAB
(For solution, see P1 Example 1.doc file)
) 41 10 )( 7 (
) 9 ( 205
) (
2
+ + +
+
=
s s s
s
s T
Assignment 1(a)
Find the output (in the time domain) if a
113-unit step is applied to a system with
the given transfer function. Verify your
answer, using the residue() command in
MATLAB
) 113 16 (
15
) 4 (
26
) (
2
+ +
+

+
=
s s
s
s
s T
Assignment 1(b)
Find the output (in the time domain) if a
109-unit impulse is applied to a system
with the given transfer function. Verify
your answer, using MATLAB
) 125 10 (
15
) 4 (
4
) 2 (
1
) (
2
+ +
+

+
=
s s
s
s s
s T

You might also like