7/17/2019 Intro Matlab Aesde
1/35
MATLAB
MatLab
28 2014
7/17/2019 Intro Matlab Aesde
2/35
MATLAB
MATLAB
7/17/2019 Intro Matlab Aesde
3/35
MATLAB
MATLAB
>> a
>> b ; ;
>> x >> y
>>z= [x y]
7/17/2019 Intro Matlab Aesde
4/35
MATLAB
length(x) x
size(x)
x x >> x
l= 5
l= 5 >>t=la
>>t=a/l
7/17/2019 Intro Matlab Aesde
5/35
MATLAB
>>c=x+y >>c=xy
>>c=xy ;
>>c=xy
x
y
>>v= [1 4 5] >>u= [2;3;6]
>>vu
vi ui
>>uv ; ;
. ./ . >>x.y >>y./x >>y.x
7/17/2019 Intro Matlab Aesde
6/35
MATLAB
>>a = [3 4 5;6 7 8] >>b= [1 : 3, 2 : 3; 1 : 5; 1 : 2 : 10]
>>b(2, 3)
>>b(2, 1 : 4) >>b(1 :end, 3)
>>[r, c] =size(b) ;
7/17/2019 Intro Matlab Aesde
7/35
MATLAB
>>c= [2 5 7; 3 9 10] >>a+c >>ca
>>ac ; ; >>ac >>a c ;
A/B=A B1 A\B=A1 B
>>a\c
>>a/c
Ak =AA . . . A >>d3
. ./ .
7/17/2019 Intro Matlab Aesde
8/35
MATLAB
>>a
>>d= [5 8; 9 4] >>inv(d)
d >>det(d)
>>eye(n) >>zeros(n) >>ones(n) nn
>>rand(n, m) nm
diag( ) p A
Ai,i+p p Ai,ip
>>diag([5 7 8 9]) 44
>>diag([5 7 8 9],1) 55
7/17/2019 Intro Matlab Aesde
9/35
MATLAB
>>sum(a) a >>prod(a)
>>sort(a)
a= [2 3 5 1] >>sort(a) [5 3 1 2]
>>sort(a) ;
>>max(a) >>min(a) a
>>[value pos] =min(a) value=5 pos= 3
7/17/2019 Intro Matlab Aesde
10/35
MATLAB
max() min() sum() prod()
>>a = [1 : 3 : 9; 15 :5 : 1] >>min(a) [1 4 5] >>max(a) [15 10 7] >>prod(a) [15 40 35] >>sum(a) [16 14 12]
>>sort(a, descend)
a 15 10 71 4 5
>>sort(a, 2, descend)
a
7 4 115 10 5
7/17/2019 Intro Matlab Aesde
11/35
MATLAB
MATLAB
>> abs(x)
>> sqrt(x)
>> sign(x) +1 x> 0 1 x< 0
>> ceil(x)
>> floor(x)
>> round(x)
>> fix(x)
7/17/2019 Intro Matlab Aesde
12/35
MATLAB
MATLAB
>> sin(x)
>> cos(x)
>> tan(x)
>> asin(x)
>> acos(x)
>> atan(x)
>> sinh(x)
>> cosh(x)
>> tanh(x)
7/17/2019 Intro Matlab Aesde
13/35
MATLAB
MATLAB
>> asinh(x)
>> acosh(x)
>> asinh(x)
>> exp(x)
>> log(x)
e
>> log2(x)
>> log10(x)
7/17/2019 Intro Matlab Aesde
14/35
MATLAB
L2 norm
n Rn
x= [x1, x2, . . . , xn]
||x||2 =
x21 +
x22 +. . .+
x2n
MATLAB >>norm(x) >>norm(x, 2)
x= [x1, x2, . . . , xn] y= [y1, y2, . . . , yn] n Rn
||xy||2 =
(x1 y1)2 + (x2y2)2 +. . .+ (xn yn)2
MATLAB
>>norm(xy) >>norm(xy, 2)
7/17/2019 Intro Matlab Aesde
15/35
MATLAB
L1 norm Manhattan
n R
n
L1 norm Manhattan x = [x1, x2, . . . , xn]
||x||1 =
n
i=1 |x
i|
MATLAB >>norm(x, 1)
Manhattan (City-block)
x = [x1, x2, . . . , xn] y = [y1, y2, . . . , yn] n R
n
L1
||xy||1 =n
i=1
|xiyi|
MATLAB >>norm((xy), 1)
7/17/2019 Intro Matlab Aesde
16/35
MATLAB
L infinity
n Rn L norm x= [x1, x2, . . . , xn]
||x|| =max(|x1|, |x2|, . . . , |xn|)
MATLAB
>>norm(x, inf)
Minkowski
x= [x1, x2, . . . , xn] y= [y1, y2, . . . , yn]
n R
n
Minkowski
||xy||p= (n
i=1
|xiyi|p)
1p
MATLAB
>>norm((xy), p)
7/17/2019 Intro Matlab Aesde
17/35
MATLAB
1:
MATLAB x y n
L L1 L2 Lp p
2:
MATLAB
k
x1, x2, . . . , xk n
7/17/2019 Intro Matlab Aesde
18/35
MATLAB
MATLAB
7/17/2019 Intro Matlab Aesde
19/35
MATLAB
y=f(x)
x
x
x
y=f(x)
plot()
y =sin(x
23
)
[2 2]
x >>x=2pi : 0.1 : 2pi
y >>y=sin(x.2/3)
>>plot(x, y, r)
7/17/2019 Intro Matlab Aesde
20/35
MATLAB
3 D
z=f(x, y)
x
x
y
y
x y
X Y
3D
[X, Y] =meshgrid(x, y)
X,Y z=f(X, Y)
surface(z)
7/17/2019 Intro Matlab Aesde
21/35
MATLAB
3 D
y =sin( 13 x2) +cos( 13 y
2)
[2 2]
x >>x=2pi : 0.1 : 2pi
y >>y=2pi : 0.1 : 2pi >>y=x
X Y >>[X, Y] =meshgrid(x, y)
y >>z=sin(X.2/3) +cos(Y.2/3)
>>surface(z)
7/17/2019 Intro Matlab Aesde
22/35
MATLAB
3 D
>>surface(z)
7/17/2019 Intro Matlab Aesde
23/35
MATLAB
surfc
>>surfc(z) surface(z)
>>surfc(z)
7/17/2019 Intro Matlab Aesde
24/35
MATLAB
MATLAB
7/17/2019 Intro Matlab Aesde
25/35
MATLAB
MATLAB
input( )
input ;
>>a = input( n )
fprintf( )
fprintf( format, A,....)
format C
>> x = 0:.1:1; y = [x; exp(x)];>> fprintf( %6.2f, %12.8f \n,y );
7/17/2019 Intro Matlab Aesde
26/35
MATLAB
MATLAB
True
False
== =
> >=
<
7/17/2019 Intro Matlab Aesde
27/35
MATLAB
MATLAB
True
isequal(a,b) isempty(a)
isnan(a)
NaN Not a Number
isinf(a) Inf
7/17/2019 Intro Matlab Aesde
28/35
MATLAB
MATLAB
&
|
xor()
all
any
7/17/2019 Intro Matlab Aesde
29/35
MATLAB
MATLAB
>> A = [1, 2 ; 3, 4];>> B = 2*ones(2);
>>A == B
ans=
7/17/2019 Intro Matlab Aesde
30/35
MATLAB
MATLAB
if
if
elseif
else
end
:>> x = 1:3; y = [1, -2, 1];>> if (x > 0) | (y > 0), z=x+y, endz=2 0 4
7/17/2019 Intro Matlab Aesde
31/35
MATLAB
MATLAB
for
for
end
while
while
end
7/17/2019 Intro Matlab Aesde
32/35
MATLAB
MATLAB
forfork= 1 : 10z(k) = 2k
end
for
fork= [2, 5, 7]2k
end
while
>>num= 0; x= 10;while num
7/17/2019 Intro Matlab Aesde
33/35
MATLAB
MATLAB
Scripts
scripts MATLAB
.m myscript.m
editor
MATLAB Debug Run commandwindow myscript
enter
.m
script
scripts
MATLAB
7/17/2019 Intro Matlab Aesde
34/35
MATLAB
MATLAB
functions
MATLAB
m
function[y1, y2,...] = fname(x1, x2,...)
y1, y2,...
x1, x2,...
fname
fname.m
function
command window
>> [z1, z2,...] = fname(w1, w2,...)
fname
w1,w2,... x1,x2,... z1,z2,... y1, y2,...
>> [z1, z2,...] = feval(fname, w1, w2,...)
MATLAB
7/17/2019 Intro Matlab Aesde
35/35
MATLAB
MATLAB
functions - :
%
help fname
fname.m