Beruflich Dokumente
Kultur Dokumente
Introduction to MATLAB
by
Dr. Ahmed Balamesh
1427/2006
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
What is MATLAB?
programming language
Advanced visualization
Extensive set of special-purpose toolboxes.
1427/2006
Getting
1427/2006
MATLAB Environment
Command Window
Enter commands
See results
Directory
Prompt
Previous
commands
1427/2006
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
1427/2006
>> A-B
ans =
2 0
0 5
13 9
6 0
4 6
11 17
>> B*A
??? Error using ==> mtimes
Inner matrix dimensions must
agree.
1427/2006
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
>> A'
ans =
1
2
3
4
5
6
7
8
9
10
A. = AT
A = (AT)*
For real matrices, A = A.
1427/2006
10
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
1427/2006
11
>> flipud(A)
ans =
11 12 13 14 15
6 7 8 9 10
1 2 3 4 5
1427/2006
12
>> sum(A,2)
ans =
15
40
65
>> sum(A)
ans =
18 21 24
27
30
1427/2006
13
>> A=[5;6;7;8;9]
A=
5
6
7
8
9
>> sum(A)
ans =
35
1427/2006
14
>> sum(sum(A))
ans =
36
>> sum(A(:))
ans =
36
1427/2006
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
1427/2006
16
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
1427/2006
17
Exercise
Write MATLAB code to:
1427/2006
18
Answer
(1)
>> sum(1:100)
ans =
5050
(2)
>> sum(1:2:100)
ans =
2500
1427/2006
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
1427/2006
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
1427/2006
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
1427/2006
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
1427/2006
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'.
1427/2006
24
Expressions Numbers
>> x=3.5e-5
x=
3.5000e-005
>> x^2
ans =
1.2250e-009
1427/2006
25
>> 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
1427/2006
26
1427/2006
27
Expressions
Operators
+
1427/2006
28
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
1427/2006
29
>> 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
1427/2006
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
1427/2006
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
1427/2006
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
1427/2006
33
>> 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
34
directory = folder
pwd: shows the path of current working directory
dir: shows the contents of the current directory
1427/2006
35
1427/2006
36
>> 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
37
M-Files
M-files
Function
1427/2006
M-files
38
MATLAB Programming
MATLAB editor
Relational operators (==,~=,>,<,<=,>=)
Logical operators (&,|,~,&&,||)
If statement
for loops
while loops
break statement
1427/2006
39
MATLAB Editor
Command
Window
1427/2006
MATLAB Editor
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
>> 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
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
43
MATLAB Graphics
Line plots
3D surface plots (mesh)
Contour plots
Image plots
Other visualization functions (demo)
1427/2006
44
1427/2006
45
Advanced Topics
mex files
MATLAB compiler
1427/2006
46
Overview of Toolboxes
Signal processing
Control
Optimization
Symbolic math
Data acquisition
1427/2006
47