Professional Documents
Culture Documents
15-
15-Apr-
Apr-04 ME 304 1/50
Outline
Introduction
Basic Features of MATLAB
– Prompt Level and Basic Arithmetic Operations
– Scalars, Vectors, and Matrices
– Matrix Operations and MATLAB Functions
– Graphics and Data Visualization
– Programming in MATLAB
MATLAB in Control Engineering
– Operations on Transfer Functions
– Simulation of LTI Systems
Simulink
15-
15-Apr-
Apr-04 ME 304 2/50
Introduction
15-
15-Apr-
Apr-04 ME 304 3/50
MATLAB Prompt
15-
15-Apr-
Apr-04 ME 304 4/50
MATLAB Objects
15-
15-Apr-
Apr-04 ME 304 5/50
MATLAB Objects (Cont’d)
» A = [1 2 3; 4 5 6; 7 8 9] A is a matrix (3×3):
A =
1 2 3 1 2 3
4 5 6
A = 4 5 6
7 8 9
7 8 9
» cos(pi) cos(π)
ans =
-1
» a = inf; a = ∞
» 1/a
ans =
0
15-
15-Apr-
Apr-04 ME 304 7/50
Important Notes
» sin(x) Most arithmetic operators and certain functions
ans = work for matrices and vectors as well.
0.8415
0.9093
0.1411
15-
15-Apr-
Apr-04 ME 304 8/50
Array Manipulations
» A(2,3) Returns the element at 2nd row, 3rd column.
ans =
6
» A(:,2) Returns all the elements at 2nd column.
ans =
2
5
8 Column 1 2 3
» A(3,:) Returns all the
ans = elements at 3rd row.
7 8 9
1 2 3 Row 1
» A(1:2,2:3) Returns submatrix.
ans = A = 4 5 6 2
2 3
5 6 7 8 9 3
15-
15-Apr-
Apr-04 ME 304 9/50
Array Manipulations
(Cont’d)
» B = [A; zeros(1,3); y] Forms a new matrix B using the variables
previously defined. Here, the command
B =
1 2 3
4 5 6 zeros(m,n) creates an (m by n) matrix
7 8 9 consisting of 0 elements.
0 0 0
1 2 3
1 2 3
[A]3x3 4 5 6 Matrix A
B = [0]1x3 B = 7 8 9
[y]1x3
[ 0 0 0] Zero vector
[ 1 2 3] Vector y
15-
15-Apr-
Apr-04 ME 304 10/50
10/50
Array Manipulations
(Cont’d)
» B = [x ones(3,1) A] Redefines matrix B using the variables
B = previously defined. Here, the command
ones(m,n) creates an (m by n) matrix
1 1 1 2 3
2 1 4 5 6
3 1 7 8 9 consisting of 1s.
1 1 1 2 3
B = [x]3x1 [1]3x1 [A]3x3 B = 2 1 4 5 6
3 1 7 8 9
15-
15-Apr-
Apr-04 ME 304 12/50
12/50
Matrix Operations
(Cont’d)
» A+B MATLAB enables you to perform
ans = arithmetic operations (+,-,*) on
2
8
4
5
3
12
matrices.
7 16 18
15-
15-Apr-
Apr-04 ME 304 14/50
14/50
Solving Equation Sets
15-
15-Apr-
Apr-04 ME 304 15/50
15/50
Advanced Matrix
Functions
lu (command) computes LU
» eig(A) Returns all the
factorization of a matrix.
eigenvalues of
ans =
5.0000 chol computes the cholesky
matrix A.
4.0000 factorization of a symmetric
positive definite matrix.
-1.0000
15-
15-Apr-
Apr-04 ME 304 16/50
16/50
Vectors
» t = 1:5 Creates a row vector whose components
t = increase arithmetically.
1 2 3 4 5
» t = 0:.1:1 Components can change by non-unit steps.
t =
Columns 1 through 7
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000
Columns 8 through 11
0.7000 0.8000 0.9000 1.0000
15-
15-Apr-
Apr-04 ME 304 17/50
17/50
2D Graphics
» x = linspace(0,2*pi,100);
» y = sin(x);
» plot(x,y);
» xlabel(‘x [rad]’);
» ylabel(‘y’);
» grid on;
15-
15-Apr-
Apr-04 ME 304 18/50
18/50
2D Graphics (Cont’d)
15-
15-Apr-
Apr-04 ME 304 19/50
19/50
Programming
15-
15-Apr-
Apr-04 ME 304 20/50
20/50
Conditionals
15-
15-Apr-
Apr-04 ME 304 22/50
22/50
While Loop
15-
15-Apr-
Apr-04 ME 304 23/50
23/50
MATLAB Functions
While programming, it is often times necessary to define one’s
own subroutines (procedures/functions).
Unlike high-level languages, these functions cannot be included
in the main program.
Each function has to be an individual “m” file.
This “m” file that begins with a line of the following form:
function [out1,out2,...] =
cmd_name(in1,in2,...)
When a function is invoked, MATLAB creates a temporary
workspace. The statements inside the function have no access
to the variables used in the main workspace unless they are
passed as inputs.
When execution ends, all local variables are erased.
15-
15-Apr-
Apr-04 ME 304 24/50
24/50
MATLAB Functions
(Cont’d)
function [y,z] = fcn(x) M-file named “fcn.m”.
z = 1 + x.*x;
y = x./z;
plot(x,y)
» x = linspace(-5,5,100);
» fcn(x) Calls M-file named “fcn.m” and
passes the vector x.
» y1 = fcn(x); Different output arguments were
» [y2,z2] = fcn(x);
passed from the MATLAB
function fcn.
15-
15-Apr-
Apr-04 ME 304 25/50
25/50
Useful MATLAB Functions
15-
15-Apr-
Apr-04 ME 304 26/50
26/50
Useful Commands
(Cont’d)
Partial Fractions Expansion » num = [2 5 3 6];
» den = [1 6 11 6];
Consider the following TF: » [r,p,k] = residue(num,den)
r =
2s3 + 5s 2 + 3s + 6 -6.0000
Residues
G (s) = -4.0000
s3 + 6s 2 + 11s + 6 3.0000
15-
15-Apr-
Apr-04 ME 304 27/50
27/50
Useful Commands
(Cont’d)
Polynomial Multiplication Zero/Pole Locations
P(s) = s3 + 2s 2 + 3s + 1⎫⎪ » num = [2 5 3 6];
⎬ D(s) = P(s)Q(s) = ? » den = [1 6 11 6];
Q(s) = s + s + 5 ⎪⎭
2
» pzmap(num,den)
» P = [1 2 3 1]; Pole-Zero M ap
1.5
» Q = [1 1 5];
» D = conv(P,Q) 1
D =
0.5
1 3 10 14 16 5
Imag Axis 0
-1
5 4 3 2
D(s) = s + 3s + 10s + 14s + 16s + 5
-1.5
-3.5 -3 -2.5 -2 -1.5 -1 -0.5 0
Real Axis
15-
15-Apr-
Apr-04 ME 304 28/50
28/50
Block Diagram Reduction
15-
15-Apr-
Apr-04 ME 304 29/50
29/50
BD Reduction (Cont’d)
den =
Y (s)
= G (s) = G1 (s) + G 2 (s) 1 4 8 5
X (s)
s+5 1 2s 2 + 9s + 10
G1 (s) = ; G 2 (s) = G (s) =
s 2 + 3s + 5 s +1 s3 + 4s 2 + 8s + 5
15-
15-Apr-
Apr-04 ME 304 30/50
30/50
BD Reduction (Cont’d)
den =
Y (s) G1 (s)
= G (s) = 1 4 9 10
X (s) 1 + G1 (s)G 2 (s)
s+5 1 s 2 + 6s + 5
G1 (s) = ; G 2 (s) = G (s) =
s 2 + 3s + 5 s +1 s 3 + 4s 2 + 9s + 10
15-
15-Apr-
Apr-04 ME 304 31/50
31/50
Unit Impulse Response
As an illustration, let
2s + 4
G (s) =
s 2 + 2s + 4
Duration of simulation
(10 sec.)
15-
15-Apr-
Apr-04 ME 304 32/50
32/50
Unit Step Response
As an illustration, let
2s + 4
G (s) =
s 2 + 2s + 4
Duration of simulation
(10 sec.)
15-
15-Apr-
Apr-04 ME 304 33/50
33/50
Simulink®
15-
15-Apr-
Apr-04 ME 304 34/50
34/50
Select
NEW
or
OPEN
(a previous
model file)
15-
15-Apr-
Apr-04 ME 304 35/50
35/50
An Untitled model is opened and
using Simulink Library you can
create your model.
15-
15-Apr-
Apr-04 ME 304 36/50
36/50
Under
Sources
Select
Signal
Generator
and drag
into model
15-
15-Apr-
Apr-04 ME 304 37/50
37/50
Under
Sinks
Select
Scope
and drag
into
model
15-
15-Apr-
Apr-04 ME 304 38/50
38/50
Under
Signals and
Systems
Select
Mux
and drag into
model
15-
15-Apr-
Apr-04 ME 304 39/50
39/50
Select
PID Controller
Under
Simulink
Extras
Additional
Linear
and drag into
model
15-
15-Apr-
Apr-04 ME 304 40/50
40/50
Under
Discontinuities
Select
Saturation
and drag into
model
15-
15-Apr-
Apr-04 ME 304 41/50
41/50
Under
Math Operations
Select
Sum
and drag into
model
15-
15-Apr-
Apr-04 ME 304 42/50
42/50
Complete Model
15-
15-Apr-
Apr-04 ME 304 43/50
43/50
Select Simulation Parameters
under Simulation Menu
15-
15-Apr-
Apr-04 ME 304 44/50
44/50
Set Solver Parameters
15-
15-Apr-
Apr-04 ME 304 45/50
45/50
Set Scope Parameters
15-
15-Apr-
Apr-04 ME 304 46/50
46/50
Set Format to Array, remove the tick
on Limit data points to last, tick Save
data to workspace, and give a
variable name.
15-
15-Apr-
Apr-04 ME 304 47/50
47/50
You may plot the response by
using plot command on the
command window by using
the data saved in the
workspace.
15-
15-Apr-
Apr-04 ME 304 48/50
48/50
Examples from System Responses
(PD-control)
15-
15-Apr-
Apr-04 ME 304 49/50
49/50
END
15-
15-Apr-
Apr-04 ME 304 50/50
50/50