Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
MATLAB Tutorial
For reading
Reference
Chapra
Chapter
Appendix 1
Page
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
MATLAB Interface
MATLAB Interface
To make your own layout:
Introduction Documents
From the reference books
Reference
Alkis
Chapter
Appendix 1
Page
531-544
From WWW
http://www.mathworks.com/academia/student_center/tutorials/launchpad.
html
http://www.mathworks.com/help/techdoc/learn_matlab/bqr_2pl.html
>> a=3
>> a=5
For complex, use i or j
>> c=[1 2 3]
>> c=[1,2,3]
>> comp=2+3i
>> d=[1;2;3]
For matrixes
or >>e=[1 2 3
1 2 3]
8
Variables
For vectors and matrixes:
>> variable name (ii,jj)= value
ii indicates row elements and jj = column elements
g1= 1 2 3
>> g1(1,1)= 1
>> g1(1,2)= 2
>> g1(1,3) =3
>> g1(1)=1
>> g1(2)=2
>> g1(3)=3
Variables
For vectors and matrixes:
>> variable name (ii,jj)= value
ii indicates row elements and jj = column elements
2 =
1
2
3
1
3 =
3
>> g2(1,1)=1
>> g2(2,1)=2
>> g2(3,1)=3
2
4
>> g4(1,1)= 1
>> g4(1,2)= 2
>> g4(2,1) =3
>> g4(2,2) =4
10
Variables
For vectors and matrixes:
>> variable name (ii,jj)= value
ii indicates row elements and jj = column elements
2 =
1
2
3
1
3 =
3
>> g2(1,1)=1
>> g2(2,1)=2
>> g2(3,1)=3
2
4
>> g4(1,1)= 1
>> g4(1,2)= 2
>> g4(2,1) =3
>> g4(2,2) =4
11
Variables
For vectors and matrixes:
>> variable name (ii,jj)= value
ii indicates row elements and jj = column elements
What happens if you write
>> g5 (4,4) = 4
12
Variables
For vectors and matrixes:
>> variable name (ii,jj)= value
ii indicates row elements and jj = column elements
What happens if you write
>> g6 (1:10,1:10) = 4
13
Variables
Matrixes can be combined to form a new matrix
>>f=[c;c]
>>g=[f,f]
Use the colon to generate a row vector;
>> h=1:10
You can control the increment by adding a value
between
>>h=1:0.5:10
14
Variables
To define character variables: use single quotes and equal
sign.
>> k=rilla
Variables
To clear variables from the Workspace:
>> clear variable; >> clear a b c or >>clear all
To clear the Command Window:
>>clc
Built-in variables:
i and j
pi
ans
Inf and Inf
NaN
Additional operator
Subtraction operator
Multiplication
Right division
Left division
Exponentiation
>> d=[ 1 2; 1 2]
>> dd=d*d
>>d2=d^2
18
19
dot(A,B)
cross(A,B)
inverse(A)
transpose(A)
sum(A)
prod(A)
>> a = (1 2 3)
>> prod(a)
>> b = [ 1 2 3
0 2 5]
>> prod(b)
20
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
%
;
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
if (condition)
x=1
statements
to be executed
if x>0
end
end
y=x*2;
25
x=1;
statements
to be
executed
if x<0
if (condition)
statements
to be executed
y=x*2;
else
else
x = 4;
elseif
statements
to be executed
end
statements
to be
executed
else
statements
to be executed
end
end
26
for (condition)
for ii =1:2
statements
to be executed
ii
end % If ii = the last number, then
get out of the loop
end
for ii =1:10
for ii=1:2:10
ii
end
ii
end
for
ii=1:2.5:10
ii
end
27
while (condition)
statements
to be executed
statements
to be executed
end
end
x=0
for ii=1:10
x=x+1
end
x=0
while x < 10
x=x+1
end
save add x
28
while (condition)
statements
to be executed
statements
to be executed
end
end
x=0
for ii=1:10
x=x+1
end
x=0
while x < 10
x=x+1
end
save add x
29
for (condition)
for (condition)
statements
to be executed
end
end
for ii =1:2
for jj = 1:2
ii
jj
end
end
30
for ii =1:2
for jj = 1:2
z(ii,jj) = ii + jj
end
end
31
Functions
There are many built-in functions in MATLAB, however you
can still build your own functions for your programs to run;
function [output]=function name[input]
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;
p=10; t=10;
Vol=myfunction(t,p)
pp=10; tt=10;
vol2=myfunction(tt,pp)
33
Summary
MATLAB Interface
34