You are on page 1of 47

Workshop

Introduction to MATLAB
by
Dr. Ahmed Balamesh
1427/2006

KAU: Dr. Ahmed Balamesh

Contents

What is MATLAB?
MATLAB Environment
Matrices
Expressions
Special Matrices
Help and Information
Element-by-Element Operations
Storing Values into a File (MAT Files)
Storing Commands in a File (M-Files)
MATLAB Programming
MATLAB Toolboxes
MATLAB GUI

1427/2006

KAU: Dr. Ahmed Balamesh

What is MATLAB?

A powerful interactive environment for technical


computing
MATLAB = MATrix LABoratory
Originally written to implement numerical linear
algebra algorithms (LINPACK, EISPACK)
Advantages:
Interactive
Fast
Powerful

programming language
Advanced visualization
Extensive set of special-purpose toolboxes.
1427/2006

KAU: Dr. Ahmed Balamesh

For More Info


On-line help from matlab menus
pdf files in c:\matlab\help\pdf_doc\matlab
or in documentation cd.

Getting

started, reference book, etc.

Run demo from matlab prompt >


Mathworks website: www.mathworks.com
Tutorials allover the internet.

1427/2006

KAU: Dr. Ahmed Balamesh

MATLAB Environment

Command Window
Enter commands
See results

Directory

Prompt

Previous
commands

1427/2006

KAU: Dr. Ahmed Balamesh

Entering Matrices
>> A=[1 2, 3 4;5 6,7 8;9 10 11 12]
A=
1 2 3 4
5 6 7 8
9 10 11 12

A 3x4 matrix
A: Matrix name = variable name (start with a letter, limit on length)
MATLAB is case sensitive
Matrix between [ ]
Separate elements in a row by space or comma (,)
Separate rows by (;)

1427/2006

KAU: Dr. Ahmed Balamesh

Matrix Operations Addition


>> A=[1 2 3 4;5 6 7 8;9 10 11 12]
A=
1 2 3 4
5 6 7 8
9 10 11 12
>> B=[-1 2 -3 4;5,1 3 2;-4 1 0 -5]
B=
-1 2 -3 4
5 1 3 2
-4 1 0 -5
>> C=A+B
C=
0 4 0 8
10 7 10 10
5 11 11 7

1427/2006

>> A-B
ans =
2 0
0 5
13 9

KAU: Dr. Ahmed Balamesh

6 0
4 6
11 17

Matrix Operations - Multiplication


>> A=[1 2 3;4 5 6]
A=
1 2 3
4 5 6
>> B=[1 -1 2 3;2 1 4 1;3 2 5 1]
B=
1 -1 2 3
2 1 4 1
3 2 5 1
>> A*B
ans =
14 7 25 8
32 13 58 23

>> B*A
??? Error using ==> mtimes
Inner matrix dimensions must
agree.

Sizes must be compatible mxn times nxk

1427/2006

KAU: Dr. Ahmed Balamesh

Matrix Operations
Inverse and determinant
>> A=rand(4,4)
A=
0.9355 0.0579
0.9169 0.3529
0.4103 0.8132
0.8936 0.0099
>> B=inv(A);
>> det(A)
ans =
-0.0154

0.1389
0.2028
0.1987
0.6038

0.2722
0.1988
0.0153
0.7468

A must be square
B=A-1
AB=BA=I
rand(m,n) generates an mxn random matrix

1427/2006

>> A*B
ans =
1.0000
-0.0000
-0.0000
-0.0000
>> B*A
ans =
1.0000
0.0000
-0.0000
-0.0000

0
1.0000
0.0000
0

0 -0.0000
0 0.0000
1.0000 0.0000
0 1.0000

0.0000 0.0000 -0.0000


1.0000 -0.0000 0.0000
0.0000 1.0000 -0.0000
0.0000 -0.0000 1.0000

-ve zeros appear due to numerical


errors
A ; at the end of a command,
suppresses output.
det(A) = |A|

KAU: Dr. Ahmed Balamesh

Matrix Operations Transpose


>> A=[1 2 3 4 5;6 7 8 9 10]
A=
1 2 3 4 5
6 7 8 9 10
>> A.'
ans =
1 6
2 7
3 8
4 9
5 10

>> A'
ans =
1
2
3
4
5

6
7
8
9
10

A. = AT
A = (AT)*
For real matrices, A = A.

1427/2006

KAU: Dr. Ahmed Balamesh

10

Matrix Operations Diagonal


>> A
A=
0.9355
0.9169
0.4103
0.8936
>> diag(A)
ans =
0.9355
0.3529
0.1987
0.7468

0.0579
0.3529
0.8132
0.0099

0.1389
0.2028
0.1987
0.6038

0.2722
0.1988
0.0153
0.7468

>> B=rand(3,4)
B=
0.4451 0.4186
0.9318 0.8462
0.4660 0.5252
>> diag(B)
ans =
0.4451
0.8462
0.8381

0.2026
0.6721
0.8381

0.0196
0.6813
0.3795

Type A + [ENTER] displays the value of A


diag(A) produces a column containing main diagonal elements of A

1427/2006

KAU: Dr. Ahmed Balamesh

11

Matrix Operations fliplr & flipud


>> A = [1 2 3 4 5;6 7 8 9 10;11 12 13
14 15]
A=
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
>> fliplr(A)
ans =
5 4 3 2 1
10 9 8 7 6
15 14 13 12 11

>> flipud(A)
ans =
11 12 13 14 15
6 7 8 9 10
1 2 3 4 5

fliplr : produces a horizontal mirror image


flipud : produces a vertical mirror image

1427/2006

KAU: Dr. Ahmed Balamesh

12

Matrix Operations sum


>> A=[1 2 3 4 5;6 7 8 9 10;11 12 13 14
15]
A=
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
>> sum(A,1)
ans =
18 21 24 27 30

>> sum(A,2)
ans =
15
40
65
>> sum(A)
ans =
18 21 24

27

30

sum(A,1) : sums rows


sum(A,2) : sums columns
sum(A) : sum rows if A is not a single-row matrix (see next slide)

1427/2006

KAU: Dr. Ahmed Balamesh

13

Matrix Operations sum


>> A=[1 2 3 4 5]
A=
1 2 3 4
>> sum(A,1)
ans =
1 2 3 4
>> sum(A)
ans =
15

>> A=[5;6;7;8;9]
A=
5
6
7
8
9
>> sum(A)
ans =
35

sum(A) = sum of all elements if A is a row or column

1427/2006

KAU: Dr. Ahmed Balamesh

14

Matrix Operations sum


>> A=[1 2 3 4;5 6 7 8]
A=
1 2 3 4
5 6 7 8
>> A(:)
ans =
1
5
2
6
3
7
4
8

>> sum(sum(A))
ans =
36
>> sum(A(:))
ans =
36

sum(sum(A)) = sum(A(:)) = sum of all elements of A


A(:) converts any matrix to column matrix

1427/2006

KAU: Dr. Ahmed Balamesh

15

Matrices - Subscripts
>> A=[1 2 3 4;5 6 7 8;9 10 11 12]
A=
1 2 3 4
5 6 7 8
9 10 11 12
>> A(3,2)
ans =
10
>> A(3,2)=0
A=
1 2 3 4
5 6 7 8
9 0 11 12

>> A(2,4)=-A(2,4)
A=
1 2 3 4
5 6 7 -8
9 10 11 12

A(i,j) is the element in the ith row and jth column


You can assign values to A(i,j)

1427/2006

KAU: Dr. Ahmed Balamesh

16

Matrices Colon Operator


>> 4:8:20
ans =
4 12

20

>> 1:2:10
ans =
1 3 5
>> x=10:-2:0
x=
10 8 6

>> x=20:-1:40
x=
Empty matrix: 1-by-0

>> 1:5
ans =
1 2

i:j:n = row starting at i and changing in step of j and ending at or


before n
i:n = row starting at I and changing in step of 1 and ending at or
before n

1427/2006

KAU: Dr. Ahmed Balamesh

17

Exercise
Write MATLAB code to:

Find the sum of the numbers from 1 to 100

Find the sum of the odd numbers from 1 to


100

1427/2006

KAU: Dr. Ahmed Balamesh

18

Answer
(1)
>> sum(1:100)
ans =
5050
(2)
>> sum(1:2:100)
ans =
2500

1427/2006

KAU: Dr. Ahmed Balamesh

19

Matrices - Submatrices
>> A=[1 2 3 4 5;6 7 8 9 10;11 12 13 14 15;16
17 18 19 20]
A=
1 2
3
4
5
6 7
8
9 10
11 12 13 14 15
16 17 18 19 20
>> A(:,4)
ans =
4
9
14
19

>> A(3,:)
ans =
11 12

13

14

15

>> A(3,:)=[ ]
A=
1 2 3 4 5
6 7 8 9 10
16 17 18 19 20

A(:,j) = jth column of A


A(i,:) = ith row of A
A(:,j)=[ ] deletes the jth column of A
A(i,:) = [ ] deletes the ith row of A

1427/2006

KAU: Dr. Ahmed Balamesh

20

Matrices - Submatrices
>> A=[1 2 3 4 5;6 7 8 9 10;11 12 13 14 15;16
17 18 19 20]
A=
1
2
3
4
5
6
7
8
9 10
11 12 13 14 15
16 17 18 19 20
>> A([1 3],[2 4 5])
ans =
2 4 5
12 14 15
>> A([3 1],[2 4 5])
ans =
12 14 15
2 4 5

>> A=[1 2 3 4 5;6 7 8 9 10;11 12 13 14


15;16 17 18 19 20]
A=
1
2
3
4
5
6
7
8
9 10
11 12 13 14 15
16 17 18 19 20
>> A(2:4,1:2:4)
ans =
6 8
11 13
16 18

A(x1,x2) = rows of A includes in x1 and columns of A included in x2


You can assign value to any submatrix.

1427/2006

KAU: Dr. Ahmed Balamesh

21

Matrices - Submatrices
>> A=[1 2 3 4 5;6 7 8 9 10;11 12 13 14 15;16
17 18 19 20]
A=
1
2
3
4
5
6
7
8
9 10
11 12 13 14 15
16 17 18 19 20
>> A(3,:)=[ ]
A=
1 2 3 4 5
6 7 8 9 10
16 17 18 19 20

>> A=[1 2 3 4 5;6 7 8 9 10;11 12 13 14


15;16 17 18 19 20]
A=
1
2
3
4
5
6
7
8
9 10
11 12 13 14 15
16 17 18 19 20
>> A(:,[2 4])=[ ]
A=
1 3 5
6 8 10
11 13 15
16 18 20

We can delete a row or column from A by assigning [ ] to it.

1427/2006

KAU: Dr. Ahmed Balamesh

22

Matrices - Concatenation
>> A=[1 2 3;4 5 6]
A=
1 2 3
4 5 6
>> b=[7;8]
b=
7
8
>> c=[9 10 11]
c=
9 10 11

>> D=[A b;c 12]


D=
1 2 3 7
A
b
4 5 6 8
9 10 11 12

>> E=[D [1;1;1]; [0 0 0 0 0]]


E=
1 2 3
7
1
4 5 6
8
1
9 10 11 12 1
0 0 0
0
0

You can form a compound matrix from blocks of other matrices,


provided that they form a proper matrix

1427/2006

KAU: Dr. Ahmed Balamesh

23

Expressions - Variables
>> _abc = 10
??? _abc = 10
|
Error: Missing variable or function.
>> z=[1 2 3 4 5]
z=
1 2 3 4

>> Z
??? Undefined function or variable 'Z'.

A variable name must start with a letter


Variable in MATLAB are case-sensitive (a A)

1427/2006

KAU: Dr. Ahmed Balamesh

24

Expressions Numbers
>> x=3.5e-5
x=
3.5000e-005
>> x^2
ans =
1.2250e-009

Same as most programming languages


3, -99, 0.0001, 1.06455e-10

1427/2006

KAU: Dr. Ahmed Balamesh

25

Expressions Complex Numbers


>> i
ans =
0 + 1.0000i
>> j
ans =
0 + 1.0000i
>> i=5;
>> i
i=
5
>> 1i
ans =
0 + 1.0000i

>> 1i=5
??? 1i=5
|
Error: The expression to the left of the
equals sign is not a valid target for
an assignment.
>> 1i^2
ans =
-1
>> 1j^2
ans =
-1

i=sqrt(-1), j=sqrt(-1) unless used as variables


1i=sqrt(-1), 1j=sqrt(-1)
23.0i, -2.4e-5i, 3+2.3i
Advice: Never use i or j for sqrt(-1)

1427/2006

KAU: Dr. Ahmed Balamesh

26

Expressions Complex Numbers


>> z=2-3i
z=
2.0000 - 3.0000i
>> z^2
ans =
-5.0000 -12.0000i

1427/2006

KAU: Dr. Ahmed Balamesh

27

Expressions

Operators
+

Functions: sin, cos, log, log10, exp, etc


>> help elfun (shows all elementary functions)
>> help specfun (shows all special functions)

1427/2006

KAU: Dr. Ahmed Balamesh

28

Special Matrices rand and randn


>> rand(2,4)
ans =
0.3704 0.5466
0.7027 0.4449

0.6946
0.6213

0.7948
0.9568

>> z=randn(10000,1);
>> hist(z,50);

>> randn(2,4)
ans =
-0.4326 0.1253 -1.1465 1.1892
-1.6656 0.2877 1.1909 -0.0376

rand(m,n) produces an mxn random matrix with uniform distribution


in (0,1)
randn(m,n) produces an mxn normally/gaussian-distributed matrix
hist : produces histogram (probability density)

1427/2006

KAU: Dr. Ahmed Balamesh

29

Special Matrices zeros, ones, eye


>> zeros(3,4)
ans =
0 0 0
0 0 0
0 0 0

>> ones(4,2)
ans =
1 1
1 1
1 1
1 1

0
0
0

>> eye(6)
ans =
1 0
0 1
0 0
0 0
0 0
0 0

0
0
1
0
0
0

0
0
0
1
0
0

0
0
0
0
1
0

0
0
0
0
0
1

zeros(m,n) = mxn matrix with all elements equal to 0


ones(m,n) = mxn matrix with all elements equal to 1
eye(n) = identity matrix of size n

1427/2006

KAU: Dr. Ahmed Balamesh

30

Element-by-Element Operators
>> A=[10 100 1000;20 40 60]
A=
10
100
1000
20
40
60
>> B=[1 2 3; 4 5 6]
B=
1 2 3
4 5 6

>> A.*B
ans =
10
200
3000
80
200
360
>> A./B
ans =
10.0000 50.0000 333.3333
5.0000 8.0000 10.0000

A .* B multiples every element in A with the corresponding element


in B (A and B must have the same size)
A ./ B divides every element in A with the corresponding element in
B (A and B must have the same size)

1427/2006

KAU: Dr. Ahmed Balamesh

31

Element-by-Element Operators
>> A=[4 3 5;1 4 2]
A=
4 3 5
1 4 2
>> B=[1 2 3;4 5 6]
B=
1 2 3
4 5 6

>> A.^B
ans =
4
1

9
1024

125
64

A .^ B raises every element in A to the power of the corresponding


element in B (A and B must have the same size)

1427/2006

KAU: Dr. Ahmed Balamesh

32

Element-by-Element Operators
>> A=[1 2 3;4 5 6]
A=
1 2 3
4 5 6
>> 1./A
ans =
1.0000 0.5000
0.2500 0.2000

0.3333
0.1667

>> A=[1 2 3;4 5 6]


A=
1 2 3
4 5 6
>> 2.^A
ans =
2 4 8
16 32 64

scalar ./ A = a matrix having same size as A with every element


equal scalar / corresponding element in A
scalar .^ A = a matrix having the same size as A with every element
equal scalar ^ corresponding element in A

1427/2006

KAU: Dr. Ahmed Balamesh

33

Some Useful Commands


>> who
Your variables are:
A B ans

>> clear

>> whos
Name
Size
Bytes Class
A
2x3
48 double array
B
2x3
48 double array
ans
2x3
48 double array
Grand total is 18 elements using 144
bytes

>> A=5;

>> who

>> who
Your variables are:
A

who : show all the variables that you have defined in the current
session.
whos : like who, but also show size and type
clear : erases all current variables

1427/2006

KAU: Dr. Ahmed Balamesh

34

Working with Directories


>> pwd
ans =
C:\Documents and Settings\user\my documents\matlab\sc32006
>> dir
.
getpts.m
honor4.asv mydata.mat plots3d.asv sndord.asv
..
honor.m
honor4.m
newton1.m
plots3d.m
sndord1.m
add1.m
honor2.m
hw5.txt
notebook1.doc prll.m
sndord2.m
butter.m
honor3.asv matlab.mat plots.asv
rho1.m
sndord3.m
getcount.m honor3.m
matlab.ppt plots.m
rlcde.m

directory = folder
pwd: shows the path of current working directory
dir: shows the contents of the current directory

1427/2006

KAU: Dr. Ahmed Balamesh

35

Working with Directories


>> cd('c:\documents and settings\user\my documents\matlab')
>> pwd
ans =
c:\documents and settings\user\my documents\matlab
>> !dir
Volume in drive C has no label.
Volume Serial Number is 9060-7DDB
Directory of C:\Documents and Settings\user\my documents\matlab\sc32006
03/09/2006 10:45 AM <DIR>
.
03/09/2006 10:45 AM <DIR>
..
03/02/2006 01:47 PM
181 add1.m
......................................................................
03/02/2006 01:48 PM
32 butter.m
03/02/2006 03:15 PM
187 getcount.m
27 File(s)
279,539 bytes
2 Dir(s) 7,868,497,920 bytes free

cd: changes the current directory to the specified directory


!: runs DOS commands (e.g. dir, copy, del, move, mkdir, rmdir, etc)

1427/2006

KAU: Dr. Ahmed Balamesh

36

Storing Your Data in a File


>> clear
>> who
>> A=5;
>> b=[1 2 3];
>> c = 10;
>> save 'mydatafile' A b
>> clear
>> A
??? Undefined function or variable 'A'.

>> A=6
A=
6
>> load 'mydatafile'
>> who
Your variables are:
A b
>> A
A=
5

save : saves variables in a file that has extension .mat (called mat
file). It saves variables with their names.
load: loads the data saved by save. It loads variables with their
names.

1427/2006

KAU: Dr. Ahmed Balamesh

37

M-Files

An M-file is a file that stores MATLAB commands.


An M-file can be executed from the command
window.
There are two types of M-files
Open

M-files

Commands are executed as if they were typed in the


command window
You can see values of all variables from command window

Function

1427/2006

M-files

Have their own internal isolated memory space


Variables are local to the m-file and cannot be seen from
command window

KAU: Dr. Ahmed Balamesh

38

MATLAB Programming
MATLAB editor
Relational operators (==,~=,>,<,<=,>=)
Logical operators (&,|,~,&&,||)
If statement
for loops
while loops
break statement

1427/2006

KAU: Dr. Ahmed Balamesh

39

MATLAB Editor

Command
Window

1427/2006

MATLAB Editor

KAU: Dr. Ahmed Balamesh

40

Relational Operators
>> 5==5
ans =
1
>> 1==5
ans =
0

>> 5>=1
ans =
1
>> 5>=5
ans =
1
>> 1>=5
ans =
0

1 means true
0 means false

1427/2006

KAU: Dr. Ahmed Balamesh

>> 5>1
ans =
1
>> 5>5
ans =
0
>> 1>5
ans =
0
>> 5<1
ans =
0
>> 5<5
ans =
0
>> 1<5
ans =
1

41

Logical Operators
>> 1 & 5

MATLAB treats:

>> 5>=1
ans =
1
>> 5>=5
ans =
1
>> 1>=5
ans =
0

Zero as false
Any nonzero number (positive or negative) as true

>> 5>1
ans =
1
>> 5>5
ans =
0
>> 1>5
ans =
0
>> 5<1
ans =
0
>> 5<5
ans =
0
>> 1<5
ans =
1
is false,

& : and
| : or
&& : short-circuited and (if one of the operands
the result is false
even if any of the remaining operands was not defined, i.e. no error is given
|| : short-circuited or

1427/2006

KAU: Dr. Ahmed Balamesh

42

MATLAB Programming
What about goto statement?
switch .. case
input function
Timing functions (tic, toc)
Speed issues (loops vs. vectorized)
function M-files
Vectorizing your function

1427/2006

KAU: Dr. Ahmed Balamesh

43

MATLAB Graphics
Line plots
3D surface plots (mesh)
Contour plots
Image plots
Other visualization functions (demo)

1427/2006

KAU: Dr. Ahmed Balamesh

44

More Useful Functions


prod : like sum but does product.
find : finds elements of a matrix that satisfy
a certain condition.
min, max : minimum and maximum
sort
Kronecker product (kron)
ode45
bit-oriented functions (bitor, bitand, bitxor)

1427/2006

KAU: Dr. Ahmed Balamesh

45

Advanced Topics

cell data type


structure data type
A brief introduction to MATLAB gui
Reading and writing binary files
More on linear algebra

eigenvalues and eigenvectors


LU decomposition
QR decomposition

mex files
MATLAB compiler

1427/2006

KAU: Dr. Ahmed Balamesh

46

Overview of Toolboxes
Signal processing
Control
Optimization
Symbolic math
Data acquisition

1427/2006

KAU: Dr. Ahmed Balamesh

47

You might also like