Professional Documents
Culture Documents
02212471
Semester I - 2011
1
Matlab
[24may2011 rev.1.2]
1.1 Matlab ?
Matlab Visualization
Matlab MATrix LABoratory MathWorks
Matlab (Matrix Manipulation and
Computation) Matlab (interactive) Basic
QBasic compiled mode C Pascal Matlab
Matlab
(strings) array
matrix (scalar) 1x1
1 (Row vector)
1 (Column vector) Matlab
vector matrix
[A]{x} = {b}
( x) Matlab
- Matlab
2 3
- Matlab (command line)
(script file)
- Matlab
C, Pascal, Fortran
- Matlab (built-in function)
02212471
Semester I - 2011
1.2 Matlab
Matlab desktop Matlab Desktop
(window) GUI (Graphic User Interface) Matlab
Matlab desktop 1.1
Command window prompt, >>
prompt cursor Matlab
02212471
Semester I - 2011
1.3 Matlab
Matlab
Enter
>> 10+13
ans =
23
Matlab (ans =)
variables
>> john = 10
john =
10
>>sam = 13;
>> john + sam
ans =
23
Sine 90
>> sin(pi/2)
ans =
1
02212471
Semester I - 2011
= 1 2 Row Vector (
B) Column Vector ( C)
>> B = [1 2 3]
B =
1
2
>> C = [1;2;3]
C =
1
2
3
(Properties) transpose B
C C=B Matlab single quote () transpose
>> B
ans =
1
2
3
1
Matlab
>> T = [42.1 43.6 39.5 38.2 40.4 41.7 41.9]
T =
42.1000
43.6000
39.5000
38.2000
40.4000
41.7000
41.9000
02212471
Semester I - 2011
1.4.1
1. syntax
matrixname(row#,column#)
% Matlab comment
Matlab code
colon Matlab
: row A(:,2) 2
A 2
2. syntax
>> A(2:3,1:2)
ans =
3
4
5
6
% 2 3 2
: 2:3
2 3
3. (major diagonal main diagonal) diag( )
square matrix
>> Adiag = diag(A)
Adiag =
1
4
02212471
Semester I - 2011
5. (Concatenation)
>>D=[3 7 4;5 9 2;4 6 1];
>>E=[A A+12; A*3 A/2]
B =
3.0000
7.0000
4.0000
5.0000
9.0000
2.0000
4.0000
6.0000
1.0000
9.0000 21.0000
12.0000
15.0000 27.0000
6.0000
12.0000 18.0000
3.0000
15.0000
17.0000
16.0000
1.5000
2.5000
2.0000
19.0000
21.0000
18.0000
3.5000
4.5000
3.0000
16.0000
14.0000
13.0000
2.0000
1.0000
0.5000
1.4.2
3 zero matrix,
the identity matrix matrixes/vectors of ones
>> F=zeros(3,3)
>> G=eye(4,4)
>> H=ones(3,1)
1.5
Matlab built-in functions (trigonometry
functions) 1 Sine 1
365 t 365
>> t=1:1:365
>> temp=15*sin(2*pi*t/365)+12
02212471
Semester I - 2011
02212471
Semester I - 2011
>> temp=15*sin(2*pi*t/365)+12;
>> plot(t,temp,+);
>> xlabel(Time, days);
>> ylabel(Temperature, degree C);
>> title(Annual Temperature Distribution);
1.3 sine
built-in function help
>>help plot
02212471
Semester I - 2011
>>lookfor interpolation
1.6 Matlab
Matlab
Matlab Windows
ASCI text file Matlab
0.0700
0.1400
0.2100
0.2700
0.3400
0.4100
0.4800
0.5500
0.6200
0.6800
0.7500
0.8200
0.8900
0.9600
1.0300
1.0900
1.1600
1.2300
1.3000
1.3700
1.4400
1.5000
0.02
0.02
0.02
0.02
0.02
0.02
0.02
0.02
0.02
0.000
0.0000
0.0000
0.02
0.1
0.44
0.78
0.66
0.3
0.14
0.06
0.02
0.02
load bt.q
bt.xxx
9
02212471
Semester I - 2011
>>load bt
1.4 bt.dat
10
02212471
Semester I - 2011
(addition)
(subtraction)
(multiplication)
(division)
inv( )
det( )
= inv(C)
1.7.1
>> A=[1 2 3; 4 5 6]
A =
1 2 3
4 5 6
>> B=[7 8 9; 10 11 12]
B =
7 8 9
10 11 12
>> A+B
ans =
8 10 12
14 16 18
>> A-B
ans =
-6 -6 -6
-6 -6 -6
>> 2*A
ans =
2 4 6
8 10 12
11
02212471
Semester I - 2011
>> sqrt(A)
ans =
1.0000 1.4142 1.7321
2.0000 2.2361 2.4495
-
1.1
1.1
Properties of Matrix Addition and Scalar Multiplication
1. A + B = B + A
2. A + (B + C) = (A + B) + C
3. A + 0 = 0 + A = A
0 is the zero matrix
4. c(A + B) = cA + cB
5. (a + b)C = aC + bC
6. a(bC) = (ab)C
1.7.2
(inner dimensions) CD = Cm x n Di x j
n = i m j ( outer dimensions)
A2 x 3 B3 x 2 Matlab
b1 b4
a1 a 2 a3
A=
, B = b2 b5
a
a
a
5
6
4
b3 b6
(a1b1 + a 2 b2 + a3b3 ) (a1b4 + a 2 b5 + a3b6 )
A B =
(a 4 b1 + a5 b2 + a6 b3 ) (a 4 b4 + a5 b5 + a6 b6 )
>> A*B
??? Error using ==> *
Inner matrix dimensions must agree.
A2 x 3 B2 x 3 Matlab Error
B transpose B A
12
02212471
Semester I - 2011
>> BT=B'
BT =
7 10
8 11
9 12
>> A*BT
ans =
50 68
122 167
1.2
1.2
Properties of Matrix Multiplication
1. A(BC) = (AB)C
2. A(B + C) = AB + AC
3. (A + B)C = AC + BC
4. AIn = InA = A
5. c(AB) = (cA)B = A(cB)
AB BA (not
communicative)
1.7.3 (Multiplication on an element-by-element basis)
A A A A^2
conform Matlab dot
operation ( dot product )
>> A^2
>> A.^2
>> A.*B
ans =
7
40
16
55
27
72
1.7.4 transpose
transpose A, AT A
1.3 transpose
13
02212471
Semester I - 2011
1.3
Properties of Transpose
1. (A + B) = A + B
2. (cA) = cA
3. (AB) = BA
***
4. (A) = A
transpose (square matrix)
Symmetric matrix symmetric matrix
mirror main diagonal line
2
5
5
-4
0
1
-4
1
7
8
-4
8
3
(eqn. 1.1)
02212471
Semester I - 2011
4 10
A=
10 30
10 30 0 1
1
2
.
5
0
.
25
0
10 30 0
1
3.
1 2.5 0.25 0
0 5 2.5 1
4.
1 2.5 0.25 0
0 1 0.5 0.2
5.
1 0 1.5 0.5
0 1 0.5 0.2
- Row1 1
1 4 (Row1)/4
- Row1 Column1
0 Row2 (10 x Row1)
- Row 1
(Row2)/5
- upper triangular 0
Row1 (2.5 x Row2) [In][A]-1
[A] [A]-1 In
4 10 1.5 0.5 6 + (5) 2 + 2 1 0
10 30 0.5 0.2 = 15 15 5 + 6 = 0 1
inverse matrix 3 x 3
row reduction inverse matrix
Matlab inv()
singular matrix inverse Matlab
.
15
02212471
Semester I - 2011
1.7.5
matrix
X/Y = X * 1/Y = X * Y-1 / Y
* Y-1 Matlab function 2
- / ( slash) MATLAB right matrix division
A / B = A * B-1
- Matlab
A scaling
A
-identity matrix, I
(eqn. 1.2)
a 21 a 22
16
02212471
Semester I - 2011
| A | = a11a22 a21a12
3 x 3 determinant (1.4)
b11 b12 b13
B = b21 b22 b23
b31 b32 b33
b22 b23
b21 b23
| B | = b11
b32
b33
b12
b31 b33
+b13
b21 b22
b31 b32
(eqn. 1.3)
(eqn. 1.4)
4 x 4 determinant
...
determinant square matrix a Matlab det() determinant
singular matrix determinant Matlab
>> a=[1 2 3; 2 7 6; 5 4 8];
>> det(a)
ans =
-21
1.8 Matlab
Matlab Numerical Method
Matlab Command window
Matlab
Command
window Matlab Matlab 2
1.8.1 (Script M-files)
Matlab
(*.m) M-file ASCII text file M-file
(text) editor
data bt.dat
text editor Notepad M-file editor Matlab plotbt.m
17
02212471
Semester I - 2011
1.4
code
command window
Pythagoras
% filename : pytha.m
% my Pythagoras
a = input(Please input the first side );
b = input(Please input the second side );
c = sqrt(a^2 + b^2);
disp(The third side = );
disp(c);
M-file
Control Flow
For Loops, While Loops If-Else-End control flow
control flow
18
02212471
Semester I - 2011
1. For Loops
execute
file
M-
clear;
%this clears all variables from the workspace
n=10;
beta=zeros(n,1); %Create an nx1 vector to hold Beta vector
for i=1:n
%set # of times to execute the following commands
beta(i,1)=i+1; %formula for the ith element of beta
end
%end the For loop
beta
%print beta in the command window
2. While Loops
execute (Control expression)
clear;
b=0;t=0;
while 2^b<200
b=b+1;
t=t+2^b;
end
b
t
3. If-Else-End
logical expression If (true) execute
logical expression (false) else
clear;
b=randn;
if b>0
count=1;
else
count=0;
end
count
19
02212471
Semester I - 2011
a
c, d
function[a,b,c] = yourfunc(c,d,e,f)
command prompt
>> sinedeg(30)
ans =
0.5000
20
02212471
Semester I - 2011
1.9 (Closures)
Matlab
Matlab Command window
Matlab
quit exit
clc
clf
clear
save
edit
Ctrl + c
Matlab
Command Window
Graphic Window
harddisk
M-file (.m)
Matlab
(help)
- help Matlab function help
- help function Matlab function
- helpwin Help Windows
- demo Matlab
*****
21