Control Systems
Lab # 1
Introduction To MATLAB
Engr. Muhammad Arsalan Jalees Abro
[email protected]
1
Introduction
• MATLAB is short for MATrix LABoratory.
• It is a commercial software package.
• Basic function is to perform calculations on:
I. Matrices
II. Row Vectors
III.Column Vectors
• MATLAB consists of built-in functions for performing various
operations.
• We will write the commands one at a time known as “interpretive”
2
Entering and Quitting MATLAB
In order to open MATLAB, simply double click on the MATLAB
icon.
There are two ways of quitting MATLAB
• i) the usual way of closing an application
• ii) type ‘quit’ and press enter.
3
Creating & Manipulating Matrices &
Vectors
To create a row vector ‘a’ simply type in:
a = [2 4 7 5]
After pressing “enter” the value of ‘a’ will be shown on the screen.
If you don’t want the value to be echoed back to you
use a semicolon after the command b = [1 3 8 9];
4
Operation on Matrices
Exercise#1: Investigate the following commands:
(a) a(2) (b) sum = a + b
(c) diff = a – b (d) ab = [a b]
(e) ab(2: 6) (f) a’
5
Creating a Column Vector
[]
1
z= 1
0
0
This column matrix can be created by two ways:
z = [1; 1; 0; 0];
or
z = [1
1
0 6
0];
Exercise#2: Investigate the following commands
(a) z’ (b) z*a
(c) [a; b] (d) a*z
(e) [z; a’] (f) z + a’
7
To enter the matrix
M
[ ]
1 2
3 4
By combing row and column vectors
M = [1 2; 3 4];
or
M = [1 2
3 4]; 8
Exercise#3: Investigate the following commands:
(a) N = inv(M) (b) M*N (c) I = eye(2)
(d) M + I (e) M*z(1:2) (f) v(3:4)*M
(g) M(1,1) (h) M(1:2,1:2) (i) M(:,1)
(j) M(2,:) (k) M(2,end) (l) M(2,:)=[]
9
Help Command
• The help command provides the description of a particular
function or a command in MATLAB.
• Type help to see the list of variables stored on your
machine.
• If you wish to get help on a particular function example
“ínv”
Type ‘help inv’
10
Exercise#4: Use the help command to find out about the
following built-in functions:
1. ones 2. zeros
3. det 4. linspace
5. logspace
11
Graphs
We will plot the graph of y = sin(t) for t going from 0 to 20 seconds
Type:
t=linspace(0,20,100);
y = sin(t);
plot(t,y), xlabel('Time in seconds' ),ylabel('sin(t)’),
title('your Roll No.'), grid
12
Exercise#5
1. Generate a vector yc containing the values of cos(t)
for the same time range 0 to 20.
2. From the “help” entry for “plot” find out how to
get both ys and yc plotted against t on the same
axis.
3. From the “help” entry for “subplot” find out how
to plot ys and yc plotted on two separate graphs,
one above the other.
13
End of Lab 1
14