You are on page 1of 0

CHNG1:MATLABCBN

1.KHINGMATLAB
1.KhingMATLAB:MATLAB(Matrixlaboratory)lphnmmdng
gii mt lot cc bi ton k thut, c bit l cc bi ton lin quan n ma
trn. MATLAB cung cp cc toolboxes, tc cc hm m rng mi trng
MATLABgiiquytccvncbitnhxltnhius,hthngiu
khin,mngneuron,fuzzylogic,mphngv.v.

khingMATLABtanhnpvoiconcantrnmnhnh.

2.nhlnhtrongcaslnh:Khitanhlnhvocaslnh,nsc
thihnhngayvktquhinlnmnhnh.Nutakhngmunchoktqu
hinlnmnhnhthsaulnhtatthmdu;.Nulnhqudi,khng
va mt dng dng c th nh lnh trn nhiu dng v cui mi dng t
thmdu...rixungdng.Khisontholnhtacthdngccphmtt:

CtrlP gililnhtrc

CtrlN gilnhsau
CtrlB lilimtkt
CtrlF tinlnmtkt
Ctrl CtrlR sangphimtt
Ctrl CrtlL sangphimtt
home CtrlA vudng
end CtrlE vcuidng
esc CtrlU xodng
del CtrlD xokttichconnhyng

backspace CtrlH xokttrcchconnhyng

3.Setpath:KhichyccchngtrnhMATLABccthmckhcthmc
hinhinhnhtaphiithmcbnglnhFile|SetPath...

4. Help v Demo: Phn nay gip chng ta hiu bit cc hm, cc lnh ca
MATLABvchythccchngtrnhdemo

2.CCMATRN
1. Cc ton t: MATLAB khng i hi phi khai bo bin trc khi dng.
MATLABphnbitchhoavchthng.
1

Ccphpton:
+,,*,/,\(chiatri),^(m),(chuynvhaysphclinhip).

x=2+3

a=5

b=2

a/b

a\b
Cctontquanh:

<,<=,>,>=,==,~=
Cctontlogic:
&,|(or),~(not)
Cchng:

pi 3.14159265

i so

j tngti

eps sais252

realmin sthcnhnht21022

realmax sthclnnht21023

inf vcngln

NaN
Notanumber

2.Ccmatrn:

a. Nhp ma trn: Ma trn l mt mng cc s liu c m hng v n ct.


Trnghpmatrnchcmtphnt(matrn11)tacmts.Matrnch
cmtctcgilmtvect.TacthnhpmatrnvoMATLABbng
nhiucch:
nhpmtdanhschccphnttbnphm
npmatrntfilesliu
tomatrnnhcchmcsntrongMATLAB
tomatrnnhhmtto
Khinhpmatrntbnphmtaphituntheoccquynhsau:

ngncchccphntcamatrnbngdu,haydutrng

dngdu;ktthcmthng

baoccphntcamatrnbngcpdungocvung[]
Vd:Tanhpmtmatrn
A=[163213;510118;96712;415141]
Bygitanhlnh:
2

sum(A)
ans=
34343434
nghalnlytngccctvMATLABcvitlvicviccct.Khi
takhngchbinchaktquthMATLABdngbinmcnhlans,vit
ttcaanswer.

Mun ly tng ca cc hng ta cn chuyn v ma trn bng cch nh


volnh:

A
ans=

16594

310615

211714

138121
vylchuynvcamatrnA.

Matrna=[]lmatrnrng

b.Chs:PhnthngictjcamatrnckhiulA(i,j).Tuynhin
ta cng c th tham chiu ti phn t ca mng nh mt ch s, v d A(k).
Cchnythngdngthamchiuvecthnghayct.Trongtrnghp
matrnythncxemlmatrnmtctditotccctcama
trnbanu.NhvyvitA(8)cnghalthamchiuphntA(4,2).

c.Tont::Tont:lmttontquantrngcaMATLAB.N
xuthinnhiudngkhcnhau.Biuthc

1:10
lmtvecthngcha10snguynt1n10
ans=

12345678910

100:7:50
tomtdyst100n51,gim7miln
ans=
10093867972655851

0:pi/4:pi
tomtdyst0npi,cchunhaupi/4
ans=

00.78541.57082.35623.1416
Ccbiuthcchsthamchiutimtphncamatrn.VitA(1:k,j)l
3

thamchiunkphntutincactj.
Ngoiratont:thamchiutittcccphntcamthnghaymt
ct.

A(:,3)

ans=
2
11
7
14
v
A(3,:)

ans=
96712
Vit B=A(:,[1324])
ta to c ma trn B t ma trn A bng cch i th t cc ct t [1234]
thnh[1324]
B=
162313
511108
97612
414151

d.Tomatrnbnghmcsn:MATLABcungcpmtshmto
ccmatrncbn:

zeros toramatrnmccphntulzeros
z=zeros(2,4)
z=
0000

0000

ones toramatrnmccphntul1
x=ones(2,3)
x=

111

111

y=5*ones(2,2)
y=
4

55
55

rand toramatrnmccphntngunhinphnbu
d=rand(4,4)
d=

0.95010.89130.82140.9218
0.23110.76210.44470.7382
0.60680.45650.61540.1763
0.48600.01850.79190.4057
randn toramatrnmccphntngunhinphnbtrcgiao
e=randn(3,3)
e=

0.43260.28771.1892

1.66561.14650.0376
0.12531.19090.3273
magic(n)toramatrncpngmccsnguynt1nn2vitngcc
hngbngtngccct.nphilnhnhaybng3.
pascal(n)toramatrnxcnhdngmccphntlyttamgic
Pascal.
pascal(4)
ans=
1111

1234

13610

141020

eye(n)tomatrnnv
eye(3)
ans=

100

010

001

eye(m,n)tomatrnnvmrng
eye(3,4)

ans=
5


1000

0100

0010

e.Lnhload:Lnhloaddngcmtfiledliu.Vvytacthto
mtfilechamatrnvnpvo.Vdcfilemtran.datchamtmatrnth
tanpmatrnnynhsau:

loadmtran.dat
Khidngmttrnhsonthovnbntomatrncnch:
- filechamatrnlmtbnghnhchnht
- mihngvittrnmtdng
- sphntcchngphibngnhau
- ccphntphicchnhaubngdutrng

f. Mfile: Mfile l mt file text cha cc m ca MATLAB. to mt


ma trn ta vit mt mfile v cho MATLAB c file ny. V d ta to file
ct1_1.mnhsau

A=[
1 23
2 34
3 45]
vnpvoMATLABbngcchnhlnh:

ct1_1

g.Lpghp:Tacthlpghp(concatenation)ccmatrncsnthnh
mtmatrnmi.Vd:

a=ones(3,3)
a=
111

111

111
b=5*ones(3,3)
b=
555

555

555
c=[a+2;b]
c=
333

333
6


333

555

555

555

h.Xohngvct:Tacthxohngvcttmatrnbngdngdu
[].
Vd:
b=
555

555

555
xoctth2tavit:

b(:,2)=[]
b=
55

55

55
Vitx(1:2:5)=[]nghaltaxoccphntbtutnphntth5v
cch2rispxplimatrn.

3.Cclnhxlmatrn:

Cng :X=A+B

Tr :X=AB

Nhn :X=A*B

:X.*Anhnccphnttngngvinhau

Chia :X=A/BlcX*B=A

:X=A\BlcA*X=B

:X=A./Bchiaccphnttngngvinhau

Lutha :X=A^2

:X=A.^2

Nghcho :X=inv(A)

nhthc :d=det(A)

3.LPTRNHTRONGMATLAB
1.Ccphtbiuiukinif,else,elseif:
Cphpcaif:

if<biuthciukin>
7

<phtbiu>

end
Nu <biu thc iu kin> cho kt qu ng th phn lnh trong thn ca if
cthchin.

Ccphtbiuelsevleseifcngtngt.
Vd:Taxtchngtrnhct1_2.montuinhsau:

disp(Xinchao!Hanhanhduoclamquen);

x=fix(30*rand);

disp(Tuoitoitrongkhoang030);

gu=input(Xinnhaptuoicuaban:);
ifgu<x

disp(Bantrehontoi);
elseifgu>x

disp(Banlonhontoi);
else

disp(Banbangtuoitoi);
end

2.switch:Cphpcaswitchnhsau:

switch<biuthc>

casen1:<lnh1>

casen2:<lnh2>

...............

casenn:<lnhn>

otherwise:<lnhn+1>

end

3.While:vnglpwhiledngkhikhngbittrcslnlp.Cphpcan
nhsau:

while<biuthc>

<phtbiu>

end
Vd:XtchngtrnhinrachuoiXinchaolnmhnhvislnnhpt
bnphm(ct1_3.m)nhsau:
disp(xinchao);
gu=input(Nhapsolanin:);
i=0;
8

whilei~=gu
disp([Xinchaoi]);
i=i+1
end

4.For:vnglpfordngkhibittrcslnlp.Cphpnhsau:

for<chs>=<gitru>:<mctng>:<gitrcui>
Vd:Xydngchngtrnhons(ct1_4.m)
x=fix(100*rand);
n=7;
t=1;
fork=1:7
num=int2str(n);
disp([Bancoquyendudoan,num,lan]);
disp(Socandoannamtrongkhoang0100);
gu=input(Nhapsomabandoan:);
ifgu<x
disp(Bandoannhohon);
elseifgu>x
disp(Sobandoanlonhon);
else
disp(Bandadoandung.Xinchucmung);
t=0;
break;
end
n=n1;
end
ift>0
disp(Bankhongdoanraroi);
numx=int2str(x);
disp([Dolaso:,numx]);
end

5.Break:phtbiubreakktthcvnglpforhaywhilemkhngquan
tmniukinktthcvnglpthomnhaycha.

4.CCFILEVHM
1.Scriptfile:KchbnlMfilenginnht,khngcis.Nrtcch
khithihnhmtlotlnhMATLABtheomttrnhtnhtnh.Taxtvd
hmfibnotoraccsFibonnaci.

f=[11];

i=1;

while(f(i)+f(i+1))<1000

f(i+2)=f(i)+f(i+1);

i=i+1;

end

plot(f)

Taluonmlnhnyvomtfiletnlct1_5.m.ychnhlmt
scriptfile.thchinccmchatrongfilect1_5.mtcaslnhtanhp
ct1_5vnhnenter.

2.Filehm:HmlMfilecchaccis.Tacmtvdvhm:

functiony=tb(x)

%Tinhtritrungbinhcuacacphantu

[m,n]=size(x);

ifm==1

m=n;

end

y=sum(x)/m;
TvdtrntathymthmMfilegmccphncbnsau:

Mt dng nh ngha hm gm: function y = tb(x) gm t kho


function,istrvy,tnhmtbvisvox.
Mtdngh1ldngtrgiputin.Vyldngvnbnnnn
phitsau%.Nxuthintanhplnhlookfor<tnhm>

Phnvnbntrgipgipngidnghiutcdngcahm.

ThnhmchamMATLAB

Ccligiithchdngchochngtrnhsngr.Nctsau
du%.
Cnchltnhmphibtubngktvcngtnvifilechahm.
TcasMATLABtanhlnh:

z=1:99;

tb(z)
Ghich:tnhmltbthtnfilecngltb.m
10

CcbinkhaibotrongmthmcaMATLABlbinaphng.Cc
hm khc khng nhn thy v s dng c bin ny. Mun cc hm khc
dngcbinnocahmtacnkhaibonlglobal.Vdtacngii
hphngtrnh:
y& 1 = y1 y1 y 2

y& 2 = y 2 + y1 y 2

TatoraMfiletnlct1_6.m

functionyp=lotka(t,y)

globalalphabeta

yp=[y(1)alpha*y(1)*y(2);y(2)+beta*y(1)*y(2)];
vsautdnglnhtanhpcclnhsau:

globalalphabeta

alpha=0.01;

beta=0.02;

[t,y]=ode23(ct1_6,[010],[11]);

plot(t,y)
tindngtacthluonlnhtrnvoMfilect1_7.m.

Mtbincthnhnghalpersistentgitrcankhngthayi
tlnginysanglngikhc.Ccbinpersistentchcthkhaibotrong
hm.Chngtntitrongbnhchonkhihmbxohaythayi.

3.iukhinvovra:Cclnhsaudngsliuavovra

disp(a)

hinthnidungcamngahayvnbn
a=[123];

disp(a)

t=Xinchao;

disp(t)

format

iukhinkhundngs

Lnh
Ktqu
Vd
format
Default.Sameasshort.
formatshort 5digitscaledfixedpoint
3.1416
formatlong
15digitscaledfixedpoint
3.14159265358979
formatshorte 5digitfloatingpoint
3.1416e+00
formatlonge 15digitfloatingpoint
3.141592653589793e+00
formatshortg Bestof5digitfixedorfloating
3.1416
11

formatlongg Bestof15digitfixedorfloating
formathex
Hexadecimal
formatbank Fixeddollarsandcents
formatrat
Ratioofsmallintegers
format+
+,,blank
format
Suppressesexcesslinefeeds
compact
formatloose Addslinefeeds

input
nhpdliu

x=input(Chotricuabienx:)

Chotricuabienx:4

x=

4.Cchmtonhccbn:

exp(x) hm e x

sqrt(x) cnbchaicax

log(x) logarittnhin

log10(x) logaritcs10

abs(x) moduncasphcx

angle(x) argumentcasphca

conj(x) sphclinhpcax

imag(x) phnocax

real(x) phnthccax

sign(x) ducax

cos(x)

sin(x)

tan(x)

acos(x)

asin(x)

atan(x)

cosh(x)

coth(x)

sinh(x)

tanh(x)

3.14159265358979
400921fb54442d18
3.14
355/113
+

12


acosh(x)

acoth(x)

asinh(x)

atanh(x)

5.Ccphptontrnhmtonhc:
a. Biu din hm ton hc: MATLAB biu din cc hm ton hc bng

cchdngccbiuthcttrongMfile.Vdkhosthm:
1
1

+
6
f ( x) =
2
( x 0.3) + 0.01 ( x 0.9)2 + 0.04

tatoramtfile,ttnlhumps.mcnidung:

functiony=humps(x)

y=1./((x0.3).^2+0.01)+1./((x0.9).^2+0.04)6;
Cchthhaibiudinmthmtonhctrndnglnhltoramti
tng inline t mt biu thc chui. V d ta c th nhptdnglnhhm
nhsau:

f=inline(1./((x0.3).^2+0.01)+1./((x0.9).^2+0.04)6);
tacthtnhtrcahmtix=2nhsau:f(2)vcktqul4.8552

b.Vthcahm:Hmfplotvthhmtonhcgiaccgitr
cho.
Vd:

fplot(humps,[55])

gridon

c.Tmcctiucahm:Chomthmtonhcmtbin,tacthdng
hmfminbndcaMATLABtmcctiuaphngcahmtrongkhong
cho.
Vd:
f=inline(1./((x0.3).^2+0.01)+1./((x0.9).^2+0.04)6);

x=fminbnd(f,0.3,1)

x=

0.6370
Hm fminsearch tng t hm fminbnd dng tm cc tiu a
phngcahmnhiubin.
Vd:Tachmthree_var.m:

functionb=three_var(v)

x=v(1);
y=v(2);
13

z=v(3);
b=x.^2+2.5*sin(y)z^2*x^2*y^2;
vbygitmcctiuivihmnybtutx=0.6,y=1.2vz=0.135
v=[0.61.20.135];
a=fminsearch(three_var,v)
a=

0.00001.57080.1803

d.Tmimzero:Hmfzerodngtmimzerocahmmtbin.
Vdtmgitrkhngcahmlncngitr0.2tavit:
f=inline(1./((x0.3).^2+0.01)+1./((x0.9).^2+0.04)6);

a=fzero(f,0.2)

Zerofoundintheinterval:[0.10949,0.264].
a=
0.1316

5.HO
1.Cclnhv:MATLABcungcpmtlothmvbiudinccvects
liucngnhgiithchvinccngcongny.

plot ha2Dvisliu2trcvhngvtuyntnh

plot3
ha3Dvisliu2trcvhngvtuyntnh

loglog
hovicctrclogarit

semilogx hovitrcxlogaritvtrcytuyntnh

semilogy hovitrcylogaritvtrcxtuyntnh

plotyy
hovitrcycnhnbntrivbnphi

2.Tohnhv:Hmplotcccdngkhcnhauphthucvoccisa
vo.Vdnuylmtvectthplot(y)toramtngthngquanhgia
ccgitrcayvchscan.Nutac2vectxvythplot(x,y)tora
thquanhgiaxvy.
Vd:
t=[0:pi/100:2*pi]

y=sin(t);

plot(t,y)
gridon

3.ctkiungv:Tacthdngcckiungvkhcnhaukhiv
hnh.Munthtachuynkiungvchohmplot.
14


t=[0:pi/100:2*pi];

y=sin(t);

plot(t,y,.)%vbngngchmchm
gridon
(lutrongfilect1_8.m)

4.ctmuvkchthcngv:ctmuvkchthcngv
tadngccthamssau:

LineWidth rngngthng,tnhbngsim

MarkerEdgeColor

mucacccnhcakhinhdu

MarkerFaceColor

mucakhinhdu

MarkerSize

kchthccakhinhdu
Mucxcnhbngccthams:
M
Mu
M
Mu
r
red
m
magenta
g green
y
yellow
b blue
k
black
c
cyan
w
white

Ccdngngthngxcnhbng:
M Kiung

nglin

ngtnt
:
ngchmchm
.
ngchmgch

Ccdngimnhduxcnhbng:
M
Kiunhdu
M
Kiunhdu
+ ducng
. im
o vngtrn
x chthp
* dusao
s hnhvung
d htkimcng
v imtamgichngxung
^ imtamgichngln
< tamgicsangtri
> tamgicsangphi
h lcgic
p nggic

15

Vd(lutrongct1_9.m):

x=pi:pi/10:pi;

y=tan(sin(x))sin(tan(x));
plot(x,y,rs,LineWidth,2,MarkerEdgeColor,k,...
MarkerFaceColor,g,MarkerSize,10)

svngcongy=f(x)cccctsau:

ngvlngtnt()

khinhduhnhvung(s),ngvmu(r)

ngvrng2point

cccnhcakhinhmuen

khinhdumugreen

kchthckhinhdu10point

5.Thmngvvothc:lmiunytadnglnhhold.Khita
nhlnhholdonthMATLABkhngxothangc.Nthmsliuvo
thminy.Nuphmvigitrcathmivtquccgitrcatrc
tocthnsnhlitlxch.

6.Chvccimsliu:vccimnhdumkhngnichngli
vinhautadngctnirngkhngcccngnigiaccimtagi
hmplotchvictmuvimnhdu.
Vd:

x=pi:pi/10:pi;

y=tan(sin(x))sin(tan(x));

plot(x,y,s,MarkerEdgeColor,k)
(lutrongct1_10.m)

7. V cc im v ng: v c cc im nh du v ng ni gia
chngtacnmtkiungvkiuim.
Vd(lutrongct1_11.m):
x=0:pi/15:4*pi;

y=exp(2*sin(x));

plot(x,y,r,x,y,ok)
vngcongy=f(x).ngnilin,mu.imnhduchocmu
en.

8.Vvihaitrcy:Lnhplotyychophptomtthchaitrcy.Tacng
16

cthdngplotyychogitrtrnhaitrcyckiukhcnhaunhmtinso
snh.
Vd:
t=0:900;

A=1000;

b=0.005;

a=0.005;

z2=sin(b*t);

z1=A*exp(a*t);

[haxes,hline1,hline2]=plotyy(t,z1,t,z2,semilogy,plot);
(lutrongct1_12.m)

9.Vngcongvisliu3D:Nux,y,zl3vectccngdithplot3
svngcong3D.
Vd:
t=0:pi/50:10*pi;

plot3(sin(t),cos(t),t)

axissquare;

gridon
(lutrongct1_13.m)

10.tccthngschotrc:Khitatomthnhv,MATLABtngchn
ccgiihntrntrctovkhongcchnhdudatrnsliudng
v.Tuynhintacthmtliphmvigitrtrntrcvkhongcch
nhdutheoring.Tacthdngcclnhsau:

axis
tliccgitrtrntrcto

axes
tomttrctomiviccctnhcmt

getvset chophpxcnhvtccthuctnhcatrctoang
c

gca trvtrctoc

a.Giihncatrcvchiavchtrntrc:MATLABchnccgiihn
trntrctovkhongcchnhdudatrnsliudngv.Dng
lnhaxiscthtligiihnny.Cphpcalnh:

axis[xmin,xmax,ymin,ymax]
Vd:
x=0:0.025:pi/2;

plot(x,tan(x),ro)
17


axis([0pi/205])
(lutrongct1_14.m)
MATLABchiavchtrntrcdatrnphmvidliuvchiau.Tacth
mtcchchianhthngsxtickvytickbngmtvecttngdn.
Vd:

x=pi:.1:pi;

y=sin(x);

plot(x,y)

set(gca,xtick,pi:pi/2:p);

set(gca,xticklabel,{pi,pi/2,0,pi/2,pi})
(lutrongct1_15.m)

8.Ghinhnlncctrcto:MATLABcungcpcclnhghinhnln
hogm:

title
thmnhnvoho

xlabel
thmnhnvotrcx

ylabel thmnhnvotrcy

zlabel
thmnhnvotrcz

legend
thmchgiivoth

text
hinthchuivnbnvtrnhtnh

gtext
tvnbnlnhonhchut

\bf
boldfont

\it
italicsfont

\sl
obliquefont(chnghing)

\rm
normalfont
CcktcbitxemtrongStringproperties.
Tadngcclnhxlabel,ylabel,zlabelthmnhnvocctrcto.
Vd:

x=pi:.1:pi;

y=sin(x);

plot(x,y)

xlabel(t=0to2\pi,Fontsize,16)

ylabel(sin(t),Fontsize,16)

title(\it{Giatricuasintuzeron2pi},Fontsize,16)
(lutrongct1_16.m)

9.Thmvnbnvoho:Tacththmvnbnvobtkchnotrn
18

hnhvnhhmtext.
Vd:

text(3*pi/4,sin(3*pi/4),\leftarrowsin(t)=0.707,FontSize,12)

10.nhvvnbntrnhnhv:Tacthsdngitngvnbnghi
chcctrcvtrbtk.MATLABnhvvnbntheonvdliutrn
trc.Vdvhmy=AetviA=0.25,t=0n900v=0.005tavit:
Vd(lutrongct1_17.m):
t=0:900;

plot(t,0.25*exp(0.005*t))
thmghichtiimt=300tavit:
text(300,.25*exp(.005*300),...
\bullet\leftarrow\fontname{times}0.25{\ite}^(0.005{\itt}}at,...
{\itt}=300,FontSize,14)
Tham s HorizontalAlignment v VerticalAlignment nh v vn bn so vi
cctox,y,zcho.

11.hocbit:

a.Khivvng:hokhivvngbiudinsliulvecthayma
trn.MATLABcungcpcchmhokhivvng:

bar
hinthccctcamatrnm*nnhlmnhm,minhm
cnbar

barh
hinthccctcamatrnm*nnhlmnhm,minhm
cnbarnmngang

bar3
hinthccctcamatrnm*nnhlmnhm,minhm
cnbardng3D

bar3h
hinthccctcamatrnm*nnhlmnhm,minhm
cnbardng3Dnmngang
Mcnh,miphntcamatrncbiudinbngmtbar.
Vd:

y=[521

673

863

555
158];

bar(y)
(lutrongct_18.m).Saunhplnhbar3(y)tacth3D.
19


b.Mtdliutrntrc:Tadngcchm xlabelv ylabelmtcc
dliutrntrc.
Vd:
nhdo=[2923272520232327];
ngay=0:5:35;
bar(ngay,nhdo)

xlabel(ngay)

ylabel(Nhietdo(^{o}C))
(lutrongct1_19.m)
Mc nh,phm vi gi tr ca trc y l t 0 n 30. xem nhit trong
khongt15n30tathayiphmvigitrcatrcy

set(gca,YLim,[1530],Layer,top)
vtrnth,phmvigitrcatrcythayi.

c.Xpchngth:Tacthxpchngsliutrnththanhbng
cchtoramttrckhctrncngmtvtrvnhvytacmttrcyc
lpvibsliukhc.
V d: Kho st nhp sinh hc lin quan n mt trichloetylene(TCE)
chosliu:

TCE=[5154203702501351206020];

nhdo=[2923272520232327];

ngay=0:5:35;

bar(ngay,nhdo)

xlabel(Ngay)

ylabel(Nhietdo(^{o}C))

xp chng mt s liu ln mt th thanh trn, c trc th 2


cngvtrnhtrcthnhttavit:

h1=gca;
vtotrcth2vtrtrcthnhttrcnhtvbsliuth2

h2=axes(Position,get(h1,Position));

plot(days,TCE,LineWidth,3)
trcth2khnggytrngichotrcthnhttavit:

set(h2,YAxisLocation,right,Color,none,XTickLabel,[])
set(h2,XLim,get(h1,XLim),Layer,top)
ghichlnthtavit:

text(11,380,Matdo,Rotation,55,FontSize,16)

ylabel(TCEMatdo(PPM))

title(Xepchongdothi,FontSize,16)
20

(lutrongct1_20.m)

d.hovng:Hm areahinthngcongtotmtvecthayt
mt ct ca ma trn. N v cc gi tr ca mt ct ca ma trn thnh mt
ngcongringvtyvngkhnggiangiaccngcongvtrcx.
Vd(lutrongct1_21.m):
Y=[512

837

968

555

423];

area(Y)
hin th th c 3 vng, mi vng mt ct. cao ca mi th vng l
tngccphnttrongmthng.Mingcongsausdngngcong
trclmcs.hinthngchialitadnglnh:

set(gca,Layer,top)

set(gca,XTick,1:5)

gridon

f.thpie:thpiehinththeotlphntrmcamtphntca
mtvecthaymtmatrnsovitngccphnt. piev pie3torath
2Dv3D.
Vd(lutrongct1_22.m):

X=[19.3 22.1 51.6;

34.2 70.3 82.4;

61.4 82.9 90.8;

50.5 54.9 59.1;

29.4 36.3 47.0];

x=sum(X);

explode=zeros(size(x));

[c,offset]=max(x);

explode(offset)=1;

h=pie(x,explode)
Khi tng cc phn t trong i s th nht bng hay ln hn 1, pie v pie3
chunhoccgitr.Nhvychovectx,miphncdintch xi / sum( xi )
vixilmtphntcax.Gitrcchunhomtphnnguynca
mivng.Khitngccphnttrongisthnhtnhhn1,pievpie3
khngchunhoccphntcavectx.Chngvmtphnpie.
Vd:

21

x=[.19.22.41];
pie(x)
g.Lmhnhchuynng:Tacthtorahnhchuynngbng2cch
tovlunhiuhnhkhcnhauvlnlthinthchng
vvxolintcmtitngtrnmnhnh,milnvlicsthay

i.
Vicchthnhttathchinhnhchuynngqua3bc:

dnghm movieindnhbnhchomtmatrnlnnhmlu
cckhunghnh.

dnghmgetframestocckhunghnh.

dnghmmoviehinthcckhunghnh.
Sau y l v d s dng movie quan st hm fft(eye(n)).Ta to hm
ct1_23.mnhsau:

axisequal

M=moviein(16,gcf);

set(gca,NextPlot,replacechildren)

h=uicontrol(style,slider,position,[1001050020],Min,1,Max,16)

forj=1:16

plot(fft(eye(j+16)))

set(h,Value,j)

M(:,j)=getframe(gcf);

end

clf;

axes(Position,[0011]);

movie(M,30)
Bc u tin to hnh nh chuyn ng l khi gn ma trn. Tuy nhin
trckhigihmmoviein,tacntoracctrctoccngkchthcvi
kchthcmtamunhinthhnh.Dotrongvdnytahinthccsliu
cchutrnvngtrnnvnntadnglnhaxisequalxcnhtlcc
trc.Hmmovieintoramatrnlncha16khunghnh.Phtbiu:

set(gca,NextPlot,replacechildren)
ngn hm plot a t l cc trc v axis normal mi khi n c gi. Hm
getframekhngistrliccimnhcatrchinhnhhnhhinc.
Mikhunghnhgmccsliutrongmtvectct.Hm getframe(gcf)chp
tonbphntrongcamtcashinhnh.Saukhitorahnhnhtacth
chychngmtslnnhtnhvd30lnnhhmmovie(M,30).
22

Mtphngphpnatohnhchuynnglvvxo,nghalvmt
itnghorithayivtrcanbngcchthayitox,yvz
mtlngnhnhmtvnglp.Tacthtoracchiungkhcnhaunh
cccchxohnhkhcnhau.Chnggm:

none

MATLABkhngxoitngkhindichuyn

background
MATLABxoitngbngcchvncmu
nn

xor

MATLABchxoitng
Vd:TatoraMfilectnlct1_24.mnhsau:

A=[8/300;01010;0281];

y=[35107];

h=0.01;

p=plot3(y(1),y(2),y(3),.,...

EraseMode,none,MarkerSize,5);%datEraseModevenone

axis([05025252525])

holdon

fori=1:4000

A(1,3)=y(2);

A(3,1)=y(2);

ydot=A*y;

y=y+h*ydot;

set(p,XData,y(1),YData,y(2),ZData,y(3))%thaydoitoado

drawnow

i=i+1;

end

12.ho3D:

a.Cclnhcbn:Lnhmeshvsurftoramt3Dtmatrnsliu.Gi
matrnsliulzmmiphntcanz(i,j)xcnhtungcamtth
mesh(z)toramtlicmuthhinmtzcnsurf(z)toramtmtcmu
z.

b.thcchmhaibin:Bcthnhtthhinhm2binz=f(x,y)
l to ma trn x v y cha cc to trong min xc nh ca hm. Hm
meshgridsbinivngxcnhbi2vectxvythnhmatrnxvy.
Sautadngmatrnnynhgihm.
Vd:Takhosthmsin(r)/r.tnhhmtrongkhong8v8theoxvy
tachcnchuynmtvectischomeshgrid:
23


[x,y]=meshgrid(8:.5:8);

r=sqrt(x.^2+y.^2)+0.005;
matrnrchakhongcchttmcamatrn.Tiptheotadnghmmesh
vhm.

z=sin(r)./r;

mesh(z)
(lutrongct1_25.m)

c.thngngmc:Cchmcontourto,hinthvghichcc
ngngmccamthaynhiumatrn.Chnggm:

clabel
toccnhnsdngmatrncontourvhinthnhn

contour hin th cc ng ng mc to bi mt gi tr cho trc


camatrnZ.

contour3 hinthccmtngmctobimtgitrchotrcca
matrnZ.

contourf hinththcontour2Dvtmuvnggia2ccng

contourc hmcpthptnhmatrncontour
Hmmeshchinthcontourvlivsurfchinthmtcontour.
Vd:

[X,Y,Z]=peaks;
contour(X,Y,Z,20)
Micontourcmtgitrgnvin.Hm clabeldnggitrnyhinth
nhn ng ng mc 2D. Ma trn contour cha gi tr clabel dng cho cc
ngcontour2D.Matrnnycxcnhbicontour,contour3vcontourf.
Vd:hinth10ngngmccahmpeaktavit:

Z=peaks;

[C,h]=contour(Z,10);

clabel(C,h)

title({Caccontourconhan,clabel(C,h)})
(lutrongct1_26.m)
Hm contourfhinththngngmctrnmtmtphngvtmu
vng cn li gia cc ng ng mc. kim sot mu t ta dng hm
caxis.
Vd(lutrongct1_27.m):
Z=peaks;

[C,h]=contourf(Z,10);

caxis([2020])

title({Contourcotomau,contourf(Z,10)})
24

Cc hm contour(z,n) v contour(z,v) cho php ta ch r s lng mc


contour hay mt mc contour cn v novizlmatrnsliu,nls
ngcontourvvlvectccmccontour.MATLABkhngphnbitgia
ilngvectmtphnthayilngvhng.Nhvynuvlvect
mt phn t m t mt contour n mt mc hm contour s coi n l s
lng ng contour ch khng phi l mc contour. Nh vy,contour(z,v)
cng nh contour(z,n). hin th mt ng ng mc ta cn cho v l mt
vectc2phntvichaiphntbngmcmongmun.Vdtora
mtngngmc3Dcahmpeaks
Vd(lutrongct1_28.m):

xrange=3:.125:3;

yrange=xrange;

[X,Y]=meshgrid(xrange,yrange);

Z=peaks(X,Y);

contour3(X,Y,Z)
hinthmtmcZ=1,tachovl[11]

v=[11]

contour3(X,Y,Z,v)
Hm ginputchophptadngchuthayccphmmitnchnccim
v. N tr v to ca v tr con tr. V d sau s minh ho cc dng hm
ginputvhmsplinetorangcongnisuyhaibin.
Vd:TatomtMfilectnct1_29.mnhsau:

disp(Chuotphaitrocacdiemtrenduongve)

disp(Chuottraitrodiemcuoicuaduongve)

axis([010010])

holdon

x=[];

y=[];

n=0;

but=1;

whilebut==1

[xi,yi,but]=ginput(1);

plot(xi,yi,go)

n=n+1;

x(n,1)=xi;

y(n,1)=yi;

end
25


t=1:n;

ts=1:0.1:n;

xs=spline(t,x,ts);

ys=spline(t,y,ts);

plot(xs,ys,c);

holdoff

13.Vccvect:CnhiuhmMATLABdnghinthccvectchngv
vectvntc.Tanhnghamtvectbngcchdngmthay2is.Cc
ismtthnhphnxvthnhphnycavect.Nutadng2isth
isthnhtsmtthnhphnxvisthhamtthnhphny.
NutachdngmtisthMATLABxlnnhmtsphc,phnthc
lthnhphnxvphnolthnhphny.

Cchmvvectgm:

compass vccvctbtutgctocahtocc

feather
vccvectbtutmtngthng

quiver
vccvect2Dcccthnhphn(u,v)

quiver3 vccvect3Dcccthnhphn(u,v,w)

a. Hm compass: Ta xt v d v hng v tc gi. Cc vec t xc


nhhng(gctnhbng)vtcgi(km/h)l:
hg=[45909045360335360270335270335335];
td=[668639689101412];
Tabinihnggithnhradiantrckhibininthnhto
vunggc.
hg1=hg*pi/180;
[x,y]=pol2cart(hg1,td);
compass(x,y)
vtoraghichtrnth:
gc={HuonggiovsucgiotaisanbayDaNang)
text(28,15,gc)
(lutrongct1_30.m)
b. Hm feather: Hm feather hin th cc vec t bt u t mt ng
thngsongsongvitrcx.Vdtoraccvectcgct900n00v
cngditac:
theta=90:10:0;
r=ones(size(theta));
26

trckhiv,chuynccsliusangtovunggcvtnglnthnhr
dnhn(lutrongc1_31.m):
[u,v]=pol2cart(theta*pi/180,r*10);
feather(u,v)
axisequal
Nuislsphczthfeathercoiphnthclxvphnoly(lutrong
ct1_32.m):
t=0:0.3:10;
s=0.05+i;
Z=exp(s*t);
feather(Z)
c.Hmquiver:Hmquiverhinthccvectccimchotrong
mtphng.Ccvectnycxcnhbngccthnhphnxvy.Vd
tora10contourcahmpeakstavit(lutrongct1_33.m):
n=2.0:.2:2.0;
[X,Y,Z]=peaks(n);
contour(X,Y,Z,10)
Bygidnghmgradienttoccthnhphncavectdnglmis
choquiver:
[U,V]=gradient(Z,.2);
tholdonthmngcontour:
holdon
quiver(X,Y,U,V)
holdoff
d. Hm quiver3: Hm quiver3 hin th cc vec t c cc thnh phn
(u,v,w)tiim(x,y,z).Vdtabiudinquocamtvtcnmi
theot.Phngtrnhcachuynngl:
at 2

z( t) = v 0 t +
2
Trchttagnvntcbanuvgiatca:
v0=10;%Vantocbandau
a=32;%giatoc
Tiptheotnhzticcthiim:
t=0:.1:1;
z=vz*t+1/2*a*t.^2;
Tnhvtrtheohngxvy:
vx=2;
27

x=vx*t;
vy=3;
y=vy*t;
Tnhccthnhphncavectvntcvhinthbngccdngquiver3:
u=gradient(x);
v=gradient(y);
w=gradient(z);
scale=0;
quiver3(x,y,z,u,v,w,scale)
axissquare
(lutrongct1_34.m)

28

You might also like