chuong 1 matlab co ban

Upload: my-nguyen-duc

Post on 17-Oct-2015

27 views

Category:

Documents


0 download

DESCRIPTION

matlab

TRANSCRIPT

  • CHNG1:MATLABCBN

    1.KHINGMATLAB1.KhingMATLAB:MATLAB(Matrixlaboratory)lphnmmdnggiimt lotccbi tonk thut,cbit lccbi ton linquannmatrn.MATLAB cung cp cc toolboxes, tc cc hmm rngmi trngMATLABgiiquytccvncbitnhxltnhius,hthngiukhin,mngneuron,fuzzylogic,mphngv.v. khingMATLABtanhnpvoiconcantrnmnhnh.2.nhlnhtrongcaslnh:Khitanhlnhvocaslnh,nscthihnhngayvktquhinlnmnhnh.Nutakhngmunchoktquhinlnmnhnhthsaulnhtatthmdu;.Nulnhqudi,khngvamtdngdngc thnh lnh trnnhiudngvcuimidngtthmdu...rixungdng.Khisontholnhtacthdngccphmtt: CtrlP gililnhtrc CtrlN gilnhsau

    CtrlB lilimtkt CtrlF tinlnmtktCtrl CtrlR sangphimttCtrl CrtlL sangphimtthome CtrlA vudngend CtrlE vcuidngesc CtrlU xodngdel CtrlD xokttichconnhyng

    backspace CtrlH xokttrcchconnhyng3.Setpath:KhichyccchngtrnhMATLABccthmckhcthmchinhinhnhtaphiithmcbnglnhFile|SetPath...4.Help vDemo:Phnnay gip chng tahiu bit cchm, cc lnh caMATLABvchythccchngtrnhdemo

    2.CCMATRN1.Cc ton t:MATLABkhng ihiphikhaibobin trckhidng.MATLABphnbitchhoavchthng.

    1

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

    x=2+3 a=5 b=2 a/b a\bCctontquanh: =,==,~=Cctontlogic:

    &,|(or),~(not)Cchng: pi 3.14159265 i so j tngti eps sais252 realmin sthcnhnht21022 realmax sthclnnht21023 inf vcngln NaN Notanumber2.Ccmatrn: a.Nhpmatrn:Ma trn lmtmngccs liucmhngvnct.Trnghpmatrnchcmtphnt(matrn11)tacmts.Matrnchcmtctcgilmtvect.TacthnhpmatrnvoMATLABbngnhiucch:

    nhpmtdanhschccphnttbnphmnpmatrntfilesliutomatrnnhcchmcsntrongMATLABtomatrnnhhmtto

    Khinhpmatrntbnphmtaphituntheoccquynhsau: ngncchccphntcamatrnbngdu,haydutrng dngdu;ktthcmthng baoccphntcamatrnbngcpdungocvung[]Vd:Tanhpmtmatrn

    A=[163213;510118;96712;415141]Bygitanhlnh:

    2

  • sum(A) ans=

    34343434nghalnlytngccctvMATLABcvitlvicviccct.KhitakhngchbinchaktquthMATLABdngbinmcnhlans,vitttcaanswer. Mun ly tngcacchng tacnchuynvma trnbngcchnhvolnh: A

    ans= 16594 310615 211714 138121vylchuynvcamatrnA. Matrna=[]lmatrnrng b.Chs:PhnthngictjcamatrnckhiulA(i,j).Tuynhintacngc th thamchiu tiphn tcamngnhmtchs,vdA(k).Cchnythngdngthamchiuvecthnghayct.Trongtrnghpmatrnythncxemlmatrnmtctditotccctcamatrnbanu.NhvyvitA(8)cnghalthamchiuphntA(4,2). c.Tont::Tont:lmttontquantrngcaMATLAB.Nxuthinnhiudngkhcnhau.Biuthc 1:10lmtvecthngcha10snguynt1n10

    ans= 12345678910 100:7:50tomtdyst100n51,gim7miln

    ans=10093867972655851

    0:pi/4:pitomtdyst0npi,cchunhaupi/4

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

    3

  • thamchiunkphntutincactj.Ngoiratont:thamchiutittcccphntcamthnghaymtct. A(:,3) ans=

    211714

    v A(3,:) ans=

    96712Vit B=A(:,[1324])ta tocma trnB tma trnAbngcchi th tccct t [1234]thnh[1324]

    B=16231351110897612414151

    d.Tomatrnbnghmcsn:MATLABcungcpmtshmtoccmatrncbn: 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.73820.60680.45650.61540.17630.48600.01850.79190.4057randn toramatrnmccphntngunhinphnbtrcgiaoe=randn(3,3)e= 0.43260.28771.1892 1.66561.14650.0376 0.12531.19090.3273magic(n)toramatrncpngmccsnguynt1nn2vitngcc

    hngbngtngccct.nphilnhnhaybng3.pascal(n)toramatrnxcnhdngmccphntlyttamgic

    Pascal.pascal(4)ans=

    1111 1234 13610 141020

    eye(n)tomatrnnveye(3)ans=

    100 010 001 eye(m,n)tomatrnnvmrng

    eye(3,4) ans=

    5

  • 1000 0100 0010 e.Lnhload:Lnhloaddngcmtfiledliu.Vvytacthtomtfilechamatrnvnpvo.Vdcfilemtran.datchamtmatrnthtanpmatrnnynhsau: loadmtran.datKhidngmttrnhsonthovnbntomatrncnch:

    - filechamatrnlmtbnghnhchnht- mihngvittrnmtdng- sphntcchngphibngnhau- ccphntphicchnhaubngdutrng

    f.Mfile:Mfile lmt file textchaccmcaMATLAB. tomtma trn ta vitmtmfile v choMATLAB c file ny.V d ta to filect1_1.mnhsau A=[

    1 232 343 45]

    vnpvoMATLABbngcchnhlnh: ct1_1 g.Lpghp:Tacthlpghp(concatenation)ccmatrncsnthnhmtmatrnmi.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 555xoctth2tavit: b(:,2)=[]

    b= 55

    55 55Vitx(1:2:5)=[]ngha ltaxoccphntbtutnphntth5vcch2rispxplimatrn.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.LPTRNHTRONGMATLAB1.Ccphtbiuiukinif,else,elseif:Cphpcaif: if

    7

  • endNuchoktqung thphn lnh trong thnca ifcthchin. Ccphtbiuelsevleseifcngtngt.Vd:Taxtchngtrnhct1_2.montuinhsau: disp(Xinchao!Hanhanhduoclamquen); x=fix(30*rand); disp(Tuoitoitrongkhoang030); gu=input(Xinnhaptuoicuaban:);

    ifgux disp(Banlonhontoi);else disp(Banbangtuoitoi);end2.switch:Cphpcaswitchnhsau: switch casen1: casen2: ............... casenn: otherwise: end3.While:vnglpwhiledngkhikhngbittrcslnlp.Cphpcannhsau: while endVd:XtchngtrnhinrachuoiXinchaolnmhnhvislnnhptbnphm(ct1_3.m)nhsau:

    disp(xinchao);gu=input(Nhapsolanin:);i=0;

    8

  • whilei~=gudisp([Xinchaoi]);i=i+1end

    4.For:vnglpfordngkhibittrcslnlp.Cphpnhsau: for=::Vd:Xydngchngtrnhons(ct1_4.m)

    x=fix(100*rand);n=7;t=1;fork=1:7num=int2str(n);disp([Bancoquyendudoan,num,lan]);disp(Socandoannamtrongkhoang0100);gu=input(Nhapsomabandoan:);ifguxdisp(Sobandoanlonhon);elsedisp(Bandadoandung.Xinchucmung);t=0;break;endn=n1;endift>0disp(Bankhongdoanraroi);numx=int2str(x);disp([Dolaso:,numx]);end

    5.Break:phtbiubreakktthcvnglpforhaywhilemkhngquantmniukinktthcvnglpthomnhaycha.

    9

  • 4.CCFILEVHM1.Scriptfile:KchbnlMfilenginnht,khngcis.NrtcchkhithihnhmtlotlnhMATLABtheomttrnhtnhtnh.TaxtvdhmfibnotoraccsFibonnaci. f=[11]; i=1; while(f(i)+f(i+1))
  • CcbinkhaibotrongmthmcaMATLABlbinaphng.Cchmkhckhngnhn thyv sdng cbinny.Mun cchmkhcdngcbinnocahmtacnkhaibonlglobal.Vdtacngiihphngtrnh:

    2122

    2111

    yyyyyyyy

    +==

    &&

    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. Mtbincthnhnghalpersistentgitrcankhngthayitlnginysanglngikhc.Ccbinpersistentchcthkhaibotronghm.Chngtntitrongbnhchonkhihmbxohaythayi.3.iukhinvovra:Cclnhsaudngsliuavovra disp(a) hinthnidungcamngahayvnbn a=[123]; disp(a) t=Xinchao; disp(t) format iukhinkhundngs

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

    11

  • formatlongg Bestof15digitfixedorfloating 3.14159265358979formathex Hexadecimal 400921fb54442d18formatbank Fixeddollarsandcents 3.14formatrat Ratioofsmallintegers 355/113format+ +,,blank +formatcompact Suppressesexcesslinefeeds

    formatloose Addslinefeeds

    input nhpdliu x=input(Chotricuabienx:) Chotricuabienx:4 x= 44.Cchmtonhccbn: exp(x) hm xe 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)

    12

  • acosh(x) acoth(x) asinh(x) atanh(x)5.Ccphptontrnhmtonhc: a.Biudinhmtonhc:MATLABbiudincchm tonhcbngcchdngccbiuthcttrongMfile.Vdkhosthm:

    604.0)9.0x(

    101.0)3.0x(

    1)x(f 22 +++= tatoramtfile,ttnlhumps.mcnidung: functiony=humps(x) y=1./((x0.3).^2+0.01)+1./((x0.9).^2+0.04)6;Cchthhaibiudinmthmtonhctrndnglnhltoramtitng inline tmtbiu thcchui.Vd tac thnhptdng lnhhmnhsau: f=inline(1./((x0.3).^2+0.01)+1./((x0.9).^2+0.04)6);tacthtnhtrcahmtix=2nhsau:f(2)vcktqul4.8552 b.Vthcahm:Hmfplotvthhmtonhcgiaccgitrcho.Vd:

    fplot(humps,[55]) gridon c.Tmcctiucahm:Chomthmtonhcmtbin,tacthdnghmfminbndcaMATLABtmcctiuaphngcahmtrongkhongcho.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 aphngcahmnhiubin.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.135v=[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.HO1.Cclnhv:MATLABcungcpmtlothmvbiudinccvectsliucngnhgiithchvinccngcongny. plot ha2Dvisliu2trcvhngvtuyntnh plot3 ha3Dvisliu2trcvhngvtuyntnh loglog hovicctrclogarit semilogx hovitrcxlogaritvtrcytuyntnh semilogy hovitrcylogaritvtrcxtuyntnh plotyy hovitrcycnhnbntrivbnphi2.Tohnhv:Hmplotcccdngkhcnhauphthucvoccisavo.Vdnuylmtvectthplot(y)toramtngthngquanhgiaccgitrcayvchscan.Nutac2vectxvythplot(x,y)torathquanhgiaxvy.Vd:

    t=[0:pi/100:2*pi] y=sin(t); plot(t,y) gridon3.ctkiungv:Tacthdngcckiungvkhcnhaukhivhnh.Munthtachuynkiungvchohmplot.

    14

  • t=[0:pi/100:2*pi]; y=sin(t); plot(t,y,.)%vbngngchmchm gridon(lutrongfilect1_8.m)4.ctmuvkchthcngv:ctmuvkchthcngvtadngccthamssau: LineWidth rngngthng,tnhbngsim MarkerEdgeColor mucacccnhcakhinhdu MarkerFaceColor mucakhinhdu MarkerSize kchthccakhinhduMucxcnhbngccthams:

    M Mu M Mur red m magentag green y yellowb blue k blackc cyan w white

    Ccdngngthngxcnhbng:

    M Kiung nglin ngtnt: ngchmchm. ngchmgch

    Ccdngimnhduxcnhbng:

    M Kiunhdu M Kiunhdu+ ducng . imo vngtrn x chthp* dusao s hnhvungd htkimcng v imtamgichngxung^ imtamgichngln tamgicsangphi h lcgicp 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 kchthckhinhdu10point5.Thmngvvothc:lmiunytadnglnhhold.KhitanhlnhholdonthMATLABkhngxothangc.Nthmsliuvothminy.Nuphmvigitrcathmivtquccgitrcatrctocthnsnhlitlxch.6.Chvccimsliu:vccimnhdumkhngnichnglivinhautadngctnirngkhngcccngnigiaccimtagihmplotchvictmuvimnhdu.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 imv ng: v c cc im nhduv ngnigiachngtacnmtkiungvkiuim.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.imnhduchocmuen.8.Vvihaitrcy:Lnhplotyychophptomtthchaitrcy.Tacng

    16

  • cthdngplotyychogitrtrnhaitrcyckiukhcnhaunhmtinsosnh.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,zl3vectccngdithplot3svngcong3D.Vd:

    t=0:pi/50:10*pi; plot3(sin(t),cos(t),t) axissquare; gridon(lutrongct1_13.m)10.tccthngschotrc:Khitatomthnhv,MATLABtngchnccgiihntrntrctovkhongcchnhdudatrnsliudngv.Tuynhintacthmt liphmvigitrtrntrcvkhongcchnhdutheoring.Tacthdngcclnhsau: axis tliccgitrtrntrcto axes tomttrctomiviccctnhcmt getvset chophpxcnhvtccthuctnhcatrctoangc gca trvtrctoc a.Giihncatrcvchiavchtrntrc:MATLABchnccgiihntrntrctovkhongcchnhdudatrnsliudngv.Dnglnhaxiscthtligiihnny.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.Tacthmtcchchianhthngsxtickvytickbngmtvecttngdn.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:MATLABcungcpcclnhghinhnlnhogm: title thmnhnvoho xlabel thmnhnvotrcx ylabel thmnhnvotrcy zlabel thmnhnvotrcz legend thmchgiivoth text hinthchuivnbnvtrnhtnh gtext tvnbnlnhonhchut \bf boldfont \it italicsfont \sl obliquefont(chnghing) \rm normalfontCcktcbitxemtrongStringproperties.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:Tacthsdngitngvnbnghichcctrcvtrbtk.MATLABnhvvnbntheonvdliutrntrc.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 sHorizontalAlignmentvVerticalAlignment nhvvnbn sovicctox,y,zcho.11.hocbit: a.Khivvng:hokhivvngbiudinsliulvecthaymatrn.MATLABcungcpcchmhokhivvng: bar hinthccctcamatrnm*nnhlmnhm,minhmcnbar barh hinthccctcamatrnm*nnhlmnhm,minhmcnbarnmngang bar3 hinthccctcamatrnm*nnhlmnhm,minhmcnbardng3D bar3h hinthccctcamatrnm*nnhlmnhm,minhmcnbardng3DnmngangMcnh,miphntcamatrncbiudinbngmtbar.Vd:

    y=[521 673 863 555 158]; bar(y)(lutrongct_18.m).Saunhplnhbar3(y)tacth3D.

    19

  • b.Mtdliutrntrc:Tadngcchmxlabelvylabelmtccdliutrntrc.Vd:

    nhdo=[2923272520232327];ngay=0:5:35;bar(ngay,nhdo) xlabel(ngay) ylabel(Nhietdo(^{o}C))(lutrongct1_19.m)Mc nh,phmvigi tr ca trcy l t 0 n 30. xemnhit trongkhongt15n30tathayiphmvigitrcatrcy set(gca,YLim,[1530],Layer,top)vtrnth,phmvigitrcatrcythayi. c.Xpchngth:Tacthxpchngs liutrnththanhbngcchtoramttrckhctrncngmtvtrvnhvytacmttrcyclpvibsliukhc.Vd:Kho stnhp sinhhc linquan nmt trichloetylene(TCE)chosliu: TCE=[5154203702501351206020]; nhdo=[2923272520232327]; ngay=0:5:35; bar(ngay,nhdo) xlabel(Ngay) ylabel(Nhietdo(^{o}C)) xpchngmt s liu lnmt th thanh trn,c trc th2 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:Hmareahinthngcongtotmtvecthaytmt ct cama trn.N v cc gi tr camt ct cama trn thnhmtngcongringvtyvngkhnggiangiaccngcongvtrcx.Vd(lutrongct1_21.m):

    Y=[512 837 968 555 423]; area(Y)hin th thc3vng,mivngmtct.caocami thvng ltngccphnt trongmthng.Mingcongsausdngngcongtrclmcs.hinthngchialitadnglnh: set(gca,Layer,top) set(gca,XTick,1:5) gridon f.thpie:thpiehinththeotlphntrmcamtphntcamtvecthaymtmatrnsovitngccphnt.pievpie3torath2Dv3D.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 tngccphn t trongis thnhtbnghay lnhn1,pievpie3chunhoccgitr.Nhvychovectx,miphncdintch )x(sum/x ii vixi l mtphntcax.Gitrcchunhomtphnnguyncamivng.Khitngccphnttrongisthnhtnhhn1,pievpie3khngchunhoccphntcavectx.Chngvmtphnpie.Vd:

    21

  • x=[.19.22.41]; pie(x) g.Lmhnhchuynng:Tacthtorahnhchuynngbng2cch tovlunhiuhnhkhcnhauvlnlthinthchng vvxolintcmtitngtrnmnhnh,milnvlicsthayi.Vicchthnhttathchinhnhchuynngqua3bc: dnghmmovieindnhbnhchomtmatrnlnnhmlucckhunghnh. dnghmgetframestocckhunghnh. dnghmmoviehinthcckhunghnh.Sau y l v d s dngmovie quan st hm fft(eye(n)).Ta to hmct1_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)Bcu tin tohnh nhchuynng lkhignma trn.Tuynhintrckhigihmmoviein,tacntoracctrctoccngkchthcvikchthcmtamunhinthhnh.Dotrongvdnytahinthccsliucchutrnvngtrnnvnntadnglnhaxisequalxcnhtlcctrc.Hmmovieintoramatrnlncha16khunghnh.Phtbiu: set(gca,NextPlot,replacechildren)ngn hm plot a t l cc trc v axis normalmi khi n c gi.Hmgetframekhngistrliccimnhcatrchinhnhhnhhinc.Mikhunghnhgmccsliutrongmtvectct.Hmgetframe(gcf)chptonbphntrongcamtcashinhnh.Saukhitorahnhnhtacthchychngmtslnnhtnhvd30lnnhhmmovie(M,30).

    22

  • Mtphngphpnatohnhchuynnglvvxo,nghalvmtitnghorithayivtrcanbngcchthayitox,yvzmtlngnhnhmtvnglp.Tacthtoracchiungkhcnhaunhcccchxohnhkhcnhau.Chnggm: none MATLABkhngxoitngkhindichuyn background MATLABxoitngbngcchvncmunn xor MATLABchxoitngVd: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; end12.ho3D: a.Cclnhcbn:Lnhmeshvsurftoramt3Dtmatrnsliu.Gimatrnsliulzmmiphntcanz(i,j)xcnhtungcamtthmesh(z)toramtlicmuthhinmtzcnsurf(z)toramtmtcmuz. b.thcchmhaibin:Bcthnhtthhinhm2binz=f(x,y)l toma trn x v y cha cc to trongmin xc nh ca hm.Hmmeshgridsbinivngxcnhbi2vectxvythnhmatrnxvy.Sautadngmatrnnynhgihm.Vd:Takhosthmsin(r)/r.tnhhmtrongkhong8v8theoxvytachcnchuynmtvectischomeshgrid:

    23

  • [x,y]=meshgrid(8:.5:8); r=sqrt(x.^2+y.^2)+0.005;matrnrchakhongcchttmcamatrn.Tiptheotadnghmmeshvhm. z=sin(r)./r; mesh(z)(lutrongct1_25.m) c.thngngmc:Cchmcontourto,hinthvghichccngngmccamthaynhiumatrn.Chnggm: clabel toccnhnsdngmatrncontourvhinthnhn contour hin th cc ng ngmc tobimtgi tr cho trccamatrnZ. contour3 hinthccmtngmctobimtgitrchotrccamatrnZ. contourf hinththcontour2Dvtmuvnggia2ccng contourc hmcpthptnhmatrncontourHmmeshchinthcontourvlivsurfchinthmtcontour.Vd: [X,Y,Z]=peaks;

    contour(X,Y,Z,20)Micontourcmtgitrgnvin.Hmclabeldnggitrnyhinthnhnngngmc2D.Ma trncontourchagi trclabeldngchoccngcontour2D.Matrnnycxcnhbicontour,contour3vcontourf.Vd:hinth10ngngmccahmpeaktavit: Z=peaks; [C,h]=contour(Z,10); clabel(C,h) title({Caccontourconhan,clabel(C,h)})(lutrongct1_26.m)Hmcontourfhinththngngmctrnmtmtphngvtmuvng cn ligia cc ng ngmc.kim sotmu t tadnghmcaxis.Vd(lutrongct1_27.m):

    Z=peaks; [C,h]=contourf(Z,10); caxis([2020]) title({Contourcotomau,contourf(Z,10)})

    24

  • Cchmcontour(z,n)vcontour(z,v)chophp tachrs lngmccontourhaymtmccontourcnvnoviz lmatrns liu,n lsngcontourvvlvectccmccontour.MATLABkhngphnbitgiailngvectmtphnthayilngvhng.Nhvynuvlvectmtphn tm tmtcontournmtmchm contourscoin lslng ng contour chkhngphi lmc contour.Nhvy,contour(z,v)cngnhcontour(z,n).hin thmtngngmc tacnchov lmtvectc2phntvichaiphntbngmcmongmun.Vdtoramtngngmc3DcahmpeaksVd(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)Hmginputchophptadngchuthayccphmmitnchnccimv.N trv tocav trcon tr.Vdsausminhhoccdnghmginputvhmsplinetorangcongnisuyhaibin.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); holdoff13.Vccvect:CnhiuhmMATLABdnghinthccvectchngvvectvntc.Tanhnghamtvectbngcchdngmthay2is.Ccismtthnhphnxvthnhphnycavect.Nutadng2isthis thnhtsmtthnhphnxvisthhamtthnhphny.NutachdngmtisthMATLABxlnnhmtsphc,phnthclthnhphnxvphnolthnhphny. Cchmvvectgm: compass vccvctbtutgctocahtocc feather vccvectbtutmtngthng quiver vccvect2Dcccthnhphn(u,v) quiver3 vccvect3Dcccthnhphn(u,v,w) a.Hm compass:Taxtvdvhngv tc gi.Ccvec txcnhhng(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 featherhin thccvec tbtu tmtng

    thngsongsongvitrcx.Vdtoraccvectcgct900n00vcngditac:

    theta=90:10:0;r=ones(size(theta));

    26

  • trckhiv,chuynccsliusangtovunggcvtnglnthnhrdnhn(lutrongc1_31.m):

    [u,v]=pol2cart(theta*pi/180,r*10);feather(u,v)axisequal

    Nuislsphczthfeathercoiphnthclxvphnoly(lutrongct1_32.m):

    t=0:0.3:10;s=0.05+i;Z=exp(s*t);feather(Z)c.Hmquiver:Hmquiverhinthccvectccimchotrong

    mtphng.Ccvectnycxcnhbngccthnhphnxvy.Vdtora10contourcahmpeakstavit(lutrongct1_33.m):

    n=2.0:.2:2.0;[X,Y,Z]=peaks(n);contour(X,Y,Z,10)

    Bygidnghmgradienttoccthnhphncavectdnglmischoquiver:

    [U,V]=gradient(Z,.2);tholdonthmngcontour:

    holdonquiver(X,Y,U,V)holdoffd.Hm quiver3:Hm quiver3 hin th cc vec t c cc thnh phn

    (u,v,w)tiim(x,y,z).Vdtabiudinquocamtvtcnmitheot.Phngtrnhcachuynngl:

    2attv)t(z

    2

    0 += Trchttagnvntcbanuvgiatca:

    v0=10;%Vantocbandaua=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

    MV d (lu trong ct1_22.m):