You are on page 1of 50

MATLAB Premier

Middle East Technical University


Department of Mechanical Engineering

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

„ MATrix LABoratory “MATLAB” is an interactive software


for numerical / symbolic computations and graphics.
„ Originally designed for matrix computations, solving linear
equation sets, data visualization, etc.
„ The capabilities of MATLAB can be extended through
programs written in its own programming language called
“M-script”.
„ Over the years, the capabilities of MATLAB in every aspect
have widened rapidly.
„ It has now become an “industry-standard” computing
environment and an outstanding tool for engineering
education.

15-
15-Apr-
Apr-04 ME 304 3/50
MATLAB Prompt

To get started, type one of these: helpwin, helpdesk, or demo. For


product information, type tour or visit www.mathworks.com.
»
» (45 + 7)/3 Arithmetic operations: +, -, *, /, ^
ans = ans (ANSwer) is a temporary “variable”
17.3333
storing the result of an instant calculation.
» 100^2 – 3.5*ans Previous answer can be recalled.
ans =
1.0061e+004 Scientific notation: 1.0061×104
» sqrt(ans) MATLAB offers a large number of predefined
ans = functions like sqrt, sin, cos, tan, exp,
100.3029 log, atan, abs, tanh, and more...

15-
15-Apr-
Apr-04 ME 304 4/50
MATLAB Objects

» a = 2.12 Result of various operations can be assigned to


a = the variables you want to define. Variable
2.1200 names are case sensitive.
» b = -9.75; Note that semicolon (;) suppresses the display.

» x = [1; 2; 3] Column vector (3×1) is defined:


x = 1
1
2 x = 2
3
3

» y = [1 2 3] y is a row vector (1×3):


y =
1 2 3
y = [1 2 3]

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

» whos whos displays the names of all


Name Size Bytes Class variables defined in the MATLAB
“workspace” (memory).
A 3x3 72 double array
a 1x1 8 double array
ans 1x1 8 double array
b 1x1 8 double array
x 3x1 24 double array
y 1x3 24 double array

Grand total is 18 elements using 144 bytes


15-
15-Apr-
Apr-04 ME 304 6/50
Special Variables
» i By default, i and j are defined as imaginary
ans =
unit (√-1).
0 + 1.0000i

» -5 + a*i MATLAB allows storage of complex numbers


ans = and relevant operations on them.
-5.0000 + 2.1200i

» 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

» help ans An extensive online help is available.


ANS Most recent answer.
ANS is the variable created automatically when expressions
are not assigned to anything else. ANSwer.

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

Vector x Unity Matrix A


vector
15-
15-Apr-
Apr-04 ME 304 11/50
11/50
Matrix Operations
» A = [1 2 3; 4 5 6; 7 8 9]; Let us define these matrices.
» B = [1 2 0; 4 0 6; 0 8 9];
» C = [1 2; 3 4; 5 6];
» C' Matrix transpose using (') operator.
ans = The command transpose(C)
1
2
3
4
5
6
serves for the same purpose.
» det(A) DETerminant of matrix A.
ans =
0

» inv(B) INVerse of matrix B.


ans =
0.4000 0.1500 -0.1000
0.3000 -0.0750 0.0500
-0.2667 0.0667 0.0667

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

» A+C Matrices must be conformable to


??? Error using ==> + carry out a certain operation.
Matrix dimensions must agree.
» A*C Matrix multiplication.
ans =
22 28
49 64
76 100
» C*A Invalid operation due to a
??? Error using ==> * mismatch in matrix inner
Inner matrix dimensions must agree.
dimensions.
15-
15-Apr-
Apr-04 ME 304 13/50
13/50
Matrix Operations
(Cont’d)
» A-3 Scalars (1×1) are exceptions in
ans = matrix operations.
-2 -1 0
1 2 3
4 5 6

» 3*A Multiplies every element of the


ans = matrix A by 3.
3 6 9
12 15 18
21 24 27

» A.*B Multiplies the corresponding


ans = elements of matrix A and B.
1 4 0
16 0 36 The operator (.*) is called array
0 64 81 multiplication.

15-
15-Apr-
Apr-04 ME 304 14/50
14/50
Solving Equation Sets

Consider this equation set: To implement this in MATLAB,


x1 + x 2 − 4 x 3 = 8 one defines the following:
2 x1 − 3x 2 + 2 x 3 = 1 » A = [1 1 -4; 2 -3 2; 5 -8 10];
5x1 − 8x 2 + 10 x 3 = 2 » b = [8; 1; 2];
» x = A\b
Alternatively, we have x =
5.5000
⎡1 1 − 4⎤ ⎡ x1 ⎤ ⎡8 ⎤ 3.5000
⎢ 2 − 3 2 ⎥ ⎢ x ⎥ = ⎢1 ⎥ ⇒ A⋅x = b 0.2500
⎢ ⎥⎢ 2 ⎥ ⎢ ⎥
⎢⎣5 − 8 10 ⎥⎦ ⎢⎣ x 3 ⎥⎦ ⎢⎣2⎥⎦ The operator (\) is called left matrix
divide and it essentially solves the
Therefore, the solution becomes linear system using Gauss-Seidel
method. Note that x = inv(A)*b
(A −1A ) x = A −1b ⇒ x = A −1b
yields the same result.

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

» [V,D] = eig(A) „ qr computes the QR


factorization of a matrix.
V = Eigenvectors
„ svd obtains singular value
-0.7071 0.8058 -0.4082 decomposition of a matrix.
-0.0000 0.0620 -0.8165
0.7071 -0.5889 -0.4082 „ norm computes various matrix
or vector norms.
D = Eigenvalues „ cond, condest, rcond
5.0000 0 0 estimates various condition
0 4.0000 0 numbers.
0 0 -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

» t = linspace(0,1,11) Generates a vector with 11 equally spaced


t = elements that lie between 0 and 1.
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

„ MATLAB offers a wide variety of


built-in data visualization functions.
„ Function plot is used to generate
2D graphs.
„ To obtain a graph of y=f(x), one
needs to create the domain x and
then form the range y with the
corresponding function values.

» 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)

As the second example, let


us plot the following function:
x
y = f (x) =
1+ x2

The corresponding MATLAB


commands are as follows:
» x = linspace(-5,5,100);
» y = x./(1+x.*x);
» plot(x,y);
» xlabel(‘x’);
» ylabel(‘y’);
» grid on;

15-
15-Apr-
Apr-04 ME 304 19/50
19/50
Programming

„ Entering all the commands at the MATLAB prompt


sequentially is a tedious and slow process.
„ One can type in all the commands in a text file having an
extension of “m”. The file containing these commands is
called M-script.
„ When the name of this “m” file is keyed in just like a
command at the prompt, MATLAB executes all the
commands sequentially.
„ Similar to a high-level language, MATLAB provides a
number of standard programming constructs such as
loops and conditionals.

15-
15-Apr-
Apr-04 ME 304 20/50
20/50
Conditionals

IF Statement „ The use of conditionals in


MATLAB programs are very
if <boolean expression 1>
MATLAB statements similar to those of common
elseif <boolean expression 2> high-level languages.
MATLAB statements
else „ The logical operators in MATLAB
MATLAB statements are <, >, <=, >=, == (logical
end
equal), ~= (not equal).
Example „ Boolean expressions take the
if x == 0 values 1 (true) or 0 (false).
y = 0;
elseif x == 1
y = 1;
else
y = 1 – 2*(x – .25);
end
15-
15-Apr-
Apr-04 ME 304 21/50
21/50
For Loop

FOR Statement „ for loop repeats the


for variable = expression statements as the loop index
MATLAB statements takes on the values in a given
end
vector.
„ Like an if construct, the loop
Example 1 Example 2
must be terminated by end
for k = 1:4 for k = [1 3 7 11] statement.
disp(k); disp(k);
end end „ disp command simply displays
its argument without showing
1
2
1
3
the variable name.
3 7
4 11

15-
15-Apr-
Apr-04 ME 304 22/50
22/50
While Loop

WHILE Statement „ while loop repeats the statements


while <boolean expression>
MATLAB statements
as long as the given boolean
end expression is true.
„ Another important command is
Example break which terminates the current
x = 1;
while 1+x > 1
for or while loop.
x = x/2; „ The sample program is used to test
end
disp(2*x) the floating point accuracy (“machine
epsilon”) of a particular computer
Output system.
2.2204e-016

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

Poles of a System Characteristic Polynomial


D(s) = s5 + 3s 4 + 7s3 + 5s 2 + 2s + 1 Let the poles of a system be
p1 = -3; p2 = -2;
» den = [1 3 7 5 2 1]; p3 = -1; p3,4= -5 ± 5i;
» roots(den)
ans =
-1.0777 + 1.9230i » poles = [-3 -2 -1 –5+5*i –5-5*i];
-1.0777 - 1.9230i » poly(poles)
-0.7742 ans =
-0.0352 + 0.5144i 1 16 121 416 610 300
-0.0352 - 0.5144i
The resulting characteristic polynomial is

D(s) = s5 + 16s 4 + 121s3 + 416s 2 + 610s + 300

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

The resulting fractions are p =


-3.0000
N -2.0000 Poles
r
G (s) = ∑ i + k -1.0000
i =1
s − pi
k =
−6 −4 3 2 Remainder
G (s) = + + +2
s + 3 s + 2 s +1

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

The resulting polynomial becomes -0.5

-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

Transfer Functions in Series


» num1 = [1 5]; den1 = [1 3 5];
X(s) Y(s) » num2 = [1]; den2 = [1 1];
G1(s) G2(s) » [num,den] = series(num1,den1,num2,den2)
num =
0 0 1 5
Y (s)
= G (s) = G1 (s)G 2 (s)
X (s) den =
1 4 8 5

As an illustration, let The resulting TF becomes


s+5 1 s+5 NUMerator
G1 (s) = ; G 2 (s) = G (s) =
s 2 + 3s + 5 s +1 s3 + 4s 2 + 8s + 5 DENominator

15-
15-Apr-
Apr-04 ME 304 29/50
29/50
BD Reduction (Cont’d)

Transfer Functions in Parallel


» num1 = [1 5]; den1 = [1 3 5];
G1(s) » num2 = [1]; den2 = [1 1];
» [num,den]=parallel(num1,den1,num2,den2)
X(s) + Y(s) num =
G2(s) +
0 2 9 10

den =
Y (s)
= G (s) = G1 (s) + G 2 (s) 1 4 8 5
X (s)

As an illustration, let The resulting TF becomes

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)

Closed Loop Transfer Functions


Y(s) » num1 = [1 5]; den1 = [1 3 5];
X(s) +
G1(s) » num2 = [1]; den2 = [1 1];
_ » [num,den]=feedback(num1,den1,num2,den2)
num =
G2(s) 0 1 6 5

den =
Y (s) G1 (s)
= G (s) = 1 4 9 10
X (s) 1 + G1 (s)G 2 (s)

As an illustration, let The resulting TF becomes

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

MATLAB commands are


» num = [2 4]; den = [1 2 4];
» impulse(num,den,10)

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

MATLAB commands are


» num = [2 4]; den = [1 2 4];
» step(num,den,10)

Duration of simulation
(10 sec.)
15-
15-Apr-
Apr-04 ME 304 33/50
33/50
Simulink®

Run MATLAB® (Latest version 6.5 Rev13)


Welcome screen of Matlab®
Select Simulink
Simulink Library Browser

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

You might also like