You are on page 1of 59

MATLAB

(
)
Introduction to MATLAB
MATLABMathworks1984

MATrix LABobratory

7.116.17.x
http://www.mathworks.com
Why MATLAB ?
MATLAB
C/C++JAVAVB
MATLAB

MATLAB


MATLAB
MATLAB

:
1.

2.
3.
MATLAB
4.

5.

6. 40(ToolBox)








MATLAB , :

, 2004 9 ,
,
:
MATLAB ,

2005 10 ,
MATLAB
step 1 Windows
step 2 Matlab


MATLAB Command Window
>>
Enter
>> (5*2+3.5)/5
ans =
2.7000

MATLAB
;
>> (5*2+3.5)/5;


Ex 1:
>>chicken=5;
>>rabbit=2;
>>legs=chicken*2+rabbit*4

legs =
18
3 1
Ex 2: e cos(3) / sin (0.5)
>>exp(-3)*cos(3)/asin(0.5)

ans =
-0.0941

Ex 3: 23=?

>>2^3
ans =
8
NOTE: ^ / + -

19
19 19


Ex1: c123 o

4c123X

c_123o

X
c-123

MATLAB
Variable Declaration
double




Getting workspace information

who %
whos %
dir %
what %.m.mat
clc %
clear %

>>clear all %
>>clear a b % a b

1. abs(x) %
1
2. acos(x) % cos ( x )
1
3. acosh(x) % cosh ( x )
4. angle(x) %
1
5. asin(x) % sin ( x )
1
6. atan(x) % tan ( x )
1
7. atanh(x) % tanh ( x )

8. ceil(x) %
()
9. floor(x) %
10. round(x) %
11. fix(x) %
12. conj(x) %
13. cosh(x) % cosine hyperbolic function
x
14. exp(x) % exponential : e
15. real(x) % imag(x)

16. log(x) % loge x = ln x
17. log10(x) % log 10 x
18. rem(x,y) % x/y
19. sign(x) %
20. sin(x)
21. sinh(x)
22. sqrt(x) % x
23. tan(x)
24. tanh(x)
Ex1: ax + bx + c = 0
2

>> x , x = b b 2
4ac
1 2
2a

>> a=1; b=2; c=3;


>>x1=(-b+sqrt(b^2-4*a*c))/(2*a)

x1 =
-1.0000 + 1.4142i

>>x2=(-b-sqrt(b^2-4*a*c))/(2*a)

x2 =
-1.0000 - 1.4142i

(1) i j
>>x=1+cos(2)*i

x=
1.0000 - 0.4161i
NOTE: i i
*

(2) abs(x) , angle(x)


(3) real(x) , imag(x)
(4) conj(x)

help
lookfor
help
helpwin helpdesk
MATLAB

doc

(1)
Ex1:
>>x=[1 2 3]

x=
1 2 3

>>x=[pi/2 sqrt(2) 2]

x=
1.5708 1.4142 2.0000
(2)
1. x=[1 2 3] %13
2. x=1:1:3 or x=1:3 %1
x=::

Ex1: 0~

>>x=0:pi/9:pi

x=
Columns 1 through 7
0 0.3491 0.6981 1.0472 1.3963 1.7453 2.0944
Columns 8 through 10
2.4435 2.7925 3.1416

Ex2: 0~
>>x=linspace(0,pi,10)

x=
Columns 1 through 7
0 0.3491 0.6981 1.0472 1.3963 1.7453 2.0944
Columns 8 through 10
2.4435 2.7925 3.1416

NOTE: x=linspace(, , )

2 3 4 5 6 7 8
i.e., x = 0
9 9 9 9 9 9 9 9
Ex3: 100~102 11

>>x=logspace(0,2,11)
x=
Columns 1 through 7
1.0000 1.5849 2.5119 3.9811 6.3096 10.0000 15.8489
Columns 8 through 11

25.1189 39.8107 63.0957 100.0000

i.e., x=[ 100 100.2 100.4 100.6 100.8 101.0 102 ]


(3)
>>x=[1 2 sqrt(2)];
>>x(3)

ans =
1.4142
(4)
>>x=[1 2 3];

>> y=cos(x) % note: y=
y=
0.5403 -0.4161 -0.9900
Ex: t
tp
pn
n






>> t=[100 200 300 400 500]; % temperature in K
>> p=[2 3 4 5 6]; % atm
>> n=1; % mole
>> r=8.314; % joule/K mol
>> v=n*r*t./p

v=
415.7000 554.2667 623.5500 665.1200 692.8333

NOTE: /
.

(5) roots (
)

a n x n + a n 1 x n 1 ++a1 x 1 + a 0 = 0
Usage: roots([ a n a n 1 a n 2 a1 a 0 ])

Ex: 2x 5
+ 3x 4
+ x 2
+1 = 0
>>roots([2 3 0 1 0 1])
ans =
-1.7246
-0.3632 + 0.6597i
-0.3632 - 0.6597i
0.4755 + 0.5339i
0.4755 - 0.5339i
(6)
>>x=[1 2 3 3 2 1];
>>y=[4 5];
>>a=[x y]

a=
1 2 3 3 2 1 4 5

>>b=[a(1:2:5) 1 0 1]

b=
1 3 2 1 0 1

(1)
Ex:
>>x=[1 2 3]

>>a=x+1 %
a=
2 3 4
>>1-x
ans =
0 -1 -2
>>x=[1 2 3];
>>y=[4 5 6];

>>x+y %

ans =
5 7 9

>>z=[-1 -2];
>>x+z

??? Error using ==> +


Matrix dimensions must agree.
,

NOTE: x+z %
(2)
>>x=[1 2 3];
>>pi*x %

ans =
3.1416 6.2832 9.4248

>>x=[1 2 3];
>>y=[4 5 6];
>>x.*y %

ans =
4 10 18
(3)
x y
,x./y ,x.\y
y x
>>x=[1 2 3];
>>y=[4 5 6];
>>x./y %

ans =
0.2500 0.4000 0.5000

Ex:

>>x=[1 2 3]; %

>>y=x' %

y=
1
2
3
Ex: (1 2 3) (4 5 6) =?
>>x=[1 2 3];
>>y=[4 5 6];
>>x*y' %

ans =
32

>>x.*y %

ans =
4 10 18
Ex:
>>x=[1+i 2 3-i];

>>x' %
ans =
1.0000 - 1.0000i
2.0000
3.0000 + 1.0000i

>>x.' %
ans =
1.0000 + 1.0000i
2.0000
3.0000 - 1.0000i

(1)
Ex:
.
>>A=[ 1 2 3 1 2 3
4 5 6 4 5 6
7 8 9] ;
7 8 9
.
>>A=[1 2 3 ; 4 5 6 ; 7 8 9];
(2)
>>A=[1 2 3 ; 4 5 6 ; 7 8 9]
>>R=[10 11 12]

A=
1 2 3
4 5 6
7 8 9

R=
10 11 12
>>B=[A;R]
>>R=[10 11 12]

B=
1 2 3
4 5 6
7 8 9
10 11 12

>>C=[A;R']
??? All rows in the bracketed expression must
have the same number of columns.
(3)

A i j
A(i, j)
 i j Subscript
Index
MATLAB

 A(i, j) A(i+(j-1)*m) ~mA


(3)
Ex:
>>a=[1 2 ;3 4];
>>a(3,3)=1; % a 1
(3,3)

>>a

a=
1 2 0
3 4 0
0 0 1
(3)
>>A=[1 2 3;4 5 6;7 8 9]
>>X=A(3,2) %X=A(row , column)
>>B=A(1:2,1:2)
>>C=A( : ,1:2) %C=A ,

>>D=A(2:3, : )
>>E=A(1:2,[1 3])

A=
1 2 3
4 5 6
7 8 9
X= D=
8 4 5 6
7 8 9
B=
1 2 E=
4 5 1 3
4 6
C=
1 2
4 5
7 8
(4)
flipud(a) %
fliplr(a) %
rot90(a) %90()
reshape(a,m,n) %
diag(v) %
(5)
1 2 1 3
A= A' =
3 4 2 4

1 1+ i 1 1+ i
B= B' =
1 i 3i 1 i 3i

1 1 i
B.' =
i + i 3i

B' , B.'

NOTE:
[r,c]=size(A)
c=
r=
n=length(v)


NOTE:
>>A=[1 2 ; 3 4];
>>B=A(:)
B=
1
3
2
4
NOTE:

1 2
1 2
Ex: A = 3 4 A=
5 6 5 6

>>A=[1 2; 3 4; 5 6];
>>A(2,:)=[ ]

A=
1 2
5 6

(
)


1. det(A) %
2. inv(A) %
3. eig(A) %
4. rank(A) %
5. cond(A) %2-norm
6. poly(A) %
7. polyvalm(v,A) %
8. expm(A) % eA
9. [r,c]=size(A)
10. n=length(v)

1. eye(n,m) %nxm
2. eye(n) %nxn
3. ones(n,m) %nxm1
4. ones(n) %nxn1
5. zeros(n,m) %nxm0
6. zeros(n) %nxn0
7. rand(n,m) %nxm
8. randn(n) %nxn
?
save
savematlab.mat
save fnamefname.mat
save fname X Y X Y fname.mat
save fname X -ascii 8 X
fname
save fname X -ascii -double 16
X fname
?
load
loadmatlab.mat
load fnamefname.mat
load fname X Yfname.mat X Y
load fname -asciifname
load fname.extfname.ext


binary (.mat)
ASCII
X

>>[m,index]=max(data)
column

Note: m ,
index

data =
12 8 18
m=15 9 23
15 9 22
index=2 2 4
13 5 19
14 7 23
10 3 20

>>[n,index]=min(data)

n=10 3 18
index=5 5 1
SUM
>>avg=mean(data)

avg= 12.8 6.4 20.4

>>s=sum(data)

s= 64 32 102

NOTE:

corrcoef(x)
cov(x)
cumprod(x)
diff(x)
hist(x)
median(x)
prod(x) %
sort(x) %
std(x) %


t=national taipei university';
u=t(17:26)
v=t(26:-1:17)
u=
university
v=
ytisrevinu

<
<=
>
>=
== equal to
~= not equal to
1 0
Ex: Ex:
>>a=[1 2 3 4]; >>2>3
>>b=[5 6 7 2]; ans =
>>a>b 0
ans = >>2<=3
0 0 0 1 ans =
1
>>tf=b-(a>2) >>2==3
tf = ans =
5 6 6 1 0
sin( x )
Ex: Find out , x=-1:1/3:1
x
>>x=-1:1/3:1;
>>sin(x)./x
Warning: Divide by zero
ans =
0.8415 0.9276 0.9816 NaN 0.9816 0.9276 0.8415

>>x==0
ans =
0 0 0 1 0 0 0
>>x=x+(x==0)*eps; NOTE: eps 2.2204e-16
>>sin(x)./x
ans =
0.8415 0.9276 0.9816 1.0000 0.9816 0.9276 0.8415

Ex:
and
>>a=1:9;
or >>tf=a>4
tf =
~ not 0 0 0 0 1 1 1 1 1
>>tf=~(a>4)
tf =
1 1 1 1 0 0 0 0 0
>>tf=(a>2)&(a<6)
tf =
0 0 1 1 1 0 0 0 0

You might also like