Professional Documents
Culture Documents
MATLAB Tutorial
For reading
Reference Chapter Page
Chapra Appendix 1 933-940
http://www.mathworks.com/academia/student_center/tutorials/launchpad.
html
http://www.mathworks.com/help/techdoc/learn_matlab/bqr_2pl.html
1
Objectives
2
MATLAB Interface
3
MATLAB Interface
Demonstration:
4
To make your own layout
5
Introduction Documents
From WWW
http://www.mathworks.com/academia/student_center/tutorials/launchpad.
html
http://www.mathworks.com/help/techdoc/learn_matlab/bqr_2pl.html
6
Introduction documents for self-learning
>> g1(1,1)= 1
>> g1(1,2)= 2
g1= 1 2 3 >> g1(1,3) =3
>> g1(1)=1
>> g1(2)=2
>> g1(3)=3
9
Variables
1
>> g2(1,1)=1
2 = 2 >> g2(2,1)=2
3 >> g2(3,1)=3
>> g4(1,1)= 1
1 2 >> g4(1,2)= 2
3 =
3 4 >> g4(2,1) =3
>> g4(2,2) =4
10
Variables
1
>> g2(1,1)=1
2 = 2 >> g2(2,1)=2
3 >> g2(3,1)=3
>> g4(1,1)= 1
1 2 >> g4(1,2)= 2
3 =
3 4 >> g4(2,1) =3
>> g4(2,2) =4
11
Variables
>> g5 (4,4) = 4
12
Variables
>> g6 (1:10,1:10) = 4
13
Variables
>>f=[c;c]
>>g=[f,f]
14
Variables
+ Additional operator
- Subtraction operator
* Multiplication
/ Right division
\ Left division
^ Exponentiation
Scalar to scalar operations and scalar to matrix operations
are straightforward
>>2+3/4*(5/3)^(2/3)
>>a=[1 2 3]
>>3*a
>>a/3
17
Basic operations and commands
Addition and subtraction of matrix operations are similar to
scalars, however their sizes must match: a(1x3) - b(3x1)
gives an error;
>>a=[1,2,3]
>>b=[1;2;3]
>>c=a-b
18
Basic operations and commands
>>a=[1 2 3]
>>b=a
>>c=a.*b
>>d=a.^2
How about?
>>c=a*b
>>d=a^2
19
Basic operations and commands
Other useful operation functions:
log dot(A,B)
sqrt cross(A,B)
exp inv(A)
det(A) transpose(A)
eig(A) sum(A)
prod(A)
>> a = (1 2 3)
>> prod(a)
>> b = [ 1 2 3
0 2 5]
>> prod(b)
20
Basic operations and commands
Other useful operation functions:
rand(N) or rand(N,M)
zeros(N) or zeros(N,M)
ones(N) or ones(N,M)
nan(N) or nan(N,M)
>> rand(4)
>> zeros(4)
>> rand(4,4)
21
Editor for scripts and functions
23
Flow control
Relational operators:
eq - Equal ==
ne - Not equal ~=
lt - Less than <
gt - Greater than >
le - Less than or equal <=
ge - Greater than or equal >=
>> 1 ==1
>> eq(1,1)
24
Flow control: conditional statement
if (condition)
x=1
statements
to be executed if x>0
y=x*2;
end end
25
Flow control: conditional statement
end end
26
Flow control: loop statement
statements ii
to be executed end % If ii = the last number, then
get out of the loop
end
statements statements
to be executed to be executed
end end
x=0 x=0
for ii=1:10 while x < 10
x=x+1 x=x+1
end end
save add x
28
Flow control: loop statement
statements statements
to be executed to be executed
end end
x=0 x=0
for ii=1:10 while x < 10
x=x+1 x=x+1
end end
save add x
29
Flow control: double loop statement
for (condition)
for (condition) for ii =1:2
for jj = 1:2
statements ii
to be executed jj
end
end end
end
30
Flow control: create a matrix using a double loop statement
for ii =1:2
for jj = 1:2
z(ii,jj) = ii + jj
end
end
31
Functions
function [v,w,z]=myfunction(t,p)
32
Example on function
function v=myfunction(t,p)
%function test
%this calculates the specific volume of an ideal gas
R=8314;
v =R*t/p;
end
p=10; t=10;
Vol=myfunction(t,p)
pp=10; tt=10;
vol2=myfunction(tt,pp)
33