You are on page 1of 222

ci t

1. MATLAB for WIN Yu cu h thng H thng IBM hoc tng thch 100% vi b vi x l 486 Intel cng vi b ng x l ton hc 487 ( ngoi tr 486 DX c b x l bn trong ), Pentium hoc Pentium Pro Processor. Microsoft Window 95 hoc Window NT.

a) CD ROM - B iu phi ho 8 bit v card mn hnh ( 256 mu ng thi ) - Khong trng a ci t v chy cc tu chn. S yu cu a cng thay i tu theo kch c cc partition v cc tp tr gip help c ci t trc tip theo tu chn. Qu trnh ci t s thng bo cho bn bit t m v dung lng a yu cu. V d: Partition vi mt lin cung mt 0 cn 25 MB cho ring MATLAB v 50 MB cho c MATLAB v HELP. Partition vi lin cung 64 KB cn 115 MB cho ring MATLAB v 250 MB cho c MATLAB v HELP. b ) B nh. Microsoft WIndow 95: 8 MB ti thiu v 16 MB khuyn ngh. Microsoft WIN NT 3.51 hoc 4.0: 12 MB ti thiu v 16 MB khuyn ngh. Cc khuyn ngh B nh ph vo (B nh b sung: additional Memory). V mch tng tc ho b tr cho Microsoft Window. My in tr gip cho Microsoft Window. V mch m thanh tr gip cho Microsoft Window. Microsoft Word 7.0 hoc hn ( nu bn c nh s dng MATLAB NoteBook ). Trnh bin dch Watcom C, Borland, Microsoft (xy dng file MEX). Netscape Navigator 2.0 hoc version cao hn hoc Microsoft Internet Explorer 3.0 chy MATLAB Help Desk. Qu trnh ci t 1. t a vo CD. Trn WIN 95 chng trnh SETUP bt u chy t ng nu nh MATLAB cha c ci t trc. Cn khng, nhn p vo biu tng setup.exe bt u qu trnh ci t. 2. Chp nhn hay b i nhng khuyn co v cp ng k phn mm trn mn hnh. Nu chp nhn bn mi c th bt u qu trnh ci t.

3. Trn Custumer Information, nhp vo tn bn, a ch ca bn. Tn khng c qu 30 k t. Nhn nt NEXT. 4. Nhn vo cc hp trng thnh phn du v nu nh bn mun tu chn v nhn tip nu bn c nh khng mun tu chn ( c th thm vo sau ny nu mun ). Trn mn hnh hin th C:\MATLAB l th mc ch mc nh ca qu trnh ci t. Nu bn mun ci t vo th mc khc hoc i tn th mc th bn la chn Browse. MATLAB cho Macintosh. MATLAB cho my Macintosh chy c trn: Mi my Macintosh c cu hnh mnh ( power Macintosh ). Mi Macintosh c trang b b vi x l 68040 ( b ng x l ton hc bn trong ). Mi my Macintosh c trang b b vi x l 68020 hoc 68030 v b ng x l ton hc 68881 hoc 68882. Yu cu ti thiu chy MATLAB. a cng trng ti thiu 26 MB, cn thm 60 MB cho h thng tu chon HELP trc tuyn. 16 MB cho phn vng b nh. CD ROM. Color Quick Draw.

Chng1

GII THIU chung

B
ygi bn ci t xong, chng ta hy xem MATLAB c th lm c nhng g. Trong phn ny chng ta s trnh by mt s nhng ng dng ca n; v trnh by tt c nhng ng dng ca MATLAB s rt di v tn thi gian. Nu bn c quyn hng dn ny, bn s thy MATLAB l ngn ng rt mnh gii quyt nhng vn quan trng v kh khn ca bn. N s rt hu ch khi bn c phn hng n c bn v n s cung cp cho bn nhng kin thc c bn bn hiu r MATLAB v pht trin c nhng kh nng ca mnh sau ny. C l cch d nht hng dung v MATLAB l n c y cc c im ca my tnh c nhn: ging nh cc my tnh c bn, n lm tt c cc php tnh ton hc c bn nh cng, tr, nhn, chia; ging nh my tnh k thut, n bao gm: s phc, cn thc, s m, logarithm, cc php ton lng gic nh sine, cosine, tang; n cng ging nh my tnh c kh nng lp trnh, c th lu tr, tm kim li d liu, cng c th to, bo v v ghi trnh t cc lnh t ng php ton khi gii quyt cc vn , bn c th so snh logic, iu khin thc hin lnh m bo tnh ng n ca php ton. Ging nh cc my tnh hin i nht, n cho php bn biu din d liu di nhiu dng nh: biu din thng thng, ma trn i s, cc hm t hp v c th thao tc vi d liu thng cng nh i vi ma trn. Trong thc t MATLAB cn ng dng rt rng ri trong nhiu lnh vc v n cng s dng rt nhiu cc php tnh ton hc. Vi nhng c im v kh nng thn thin vi ngi s dng nn n d dng s dng hn cc ngn ng khc nh Basic, Pascal, C. N cung cp mt mi trng phong ph cho biu din d liu, v c kh nng mnh m v ho, bn c th to cc giao din ring cho ngi s dng(GUIs) gi quyt nhng vn ring cho mnh. Thm vo MATLAB a ra nhng cng c gii quyt nhng vn c bit, gi l Toolbox (hp cng c). V d Student Edition ca

MATLAB bao gm c Toolbox iu khin h thng, Toolbox x l tn hiu, Toolbox biu tng ton hc. Ngoi ra bn c th to Toolbox cho ring mnh. Vi nhng kh nng mnh m, rng ln ca MATLAB nn n rt cn thit cho bn bt u t phn c bn. Sau y chng ta s nghin cu tng phn, v cun sch ny s gip bn hiu c chng. Trc tin, mt cch n gin nht l chng ta quan nim nh l mt my tnh c bn, tip theo l nh my tnh k thut v nh my tnh c th lp trnh c, cui cng l nh my tnh hin i nht. Bng cch quan nim ny bn s d dng hiu c nhng cch m MATLAB gii quyt nhng vn thng thng v xem MATLAB gii quyt nhng vn v s phc mm do nh th no. Tu thuc vo kin thc ca bn, bn c th tm thy nhng phn trong cun sch hng dn ny hng th hay bun t... Khi bn chy chng trnh MATLAB, n s to mt hoc nhiu ca s trn mn hnh ca bn, v ca s lnh (command) l ca s chnh bn giao tip vi MATLAB, ca s ny xut hin nh hnh di y. Cc k t EDU>> l du nhc ca MATLAB trong student MATLAB. Trong cc version khc ca MATLAB, du nhc n gin ch l >>. Khi ca s lnh xut hin, l ca s hot ng, con tr xut hin bn phi du nhc nh hnh di. Con tr v du nhc ny ca MATLAB bo rng MATLAB ang i thc hin lnh.

Hnh 1.1

Ca s lnh ca Student MATLAB

1.1 Cc php ton n gin Ging nh my tnh n gin thng thng, MATLAB c th thc hin cc php ton n gin, nh v d di y: Mary n mt ca hng vn phng phm v mua 4 cc ty, 25 xu mt cc, 6 tp v, 52 xu mt tp, hai cun bng i, 99 xu mt cun. Hy tnh xem Mary mua bao nhiu vt, v tng s tin l bao nhiu? Nu dng my tnh thng thng, ta vo cc s: 4 + 6 + 2 = 12 ( vt) 4x25 + 6x52 + 2x99 = 610 (xu)

Hnh 1.2

Ca s lnh ca MATLAB version 5.2

Trong MATLAB chng ta c th gii quyt vn ny theo nhiu cch. Trc tin ging nh my tnh trn, chng ta c th tnh: >> 4 + 6 + 2 ans=

12 >> 4*25 + 6*52 + 2*99 ans= 610 Ch rng MATLAB khng ch n nhng khong trng, cho tt c cc phn, v php nhn c mc u tin cao hn php cng. V mt ch khc l MATLAB gi kt qu ans (vit tt ca answer) cho c hai php tnh. Nh ni trn, vn trn c th gii quyt bng cch cha cc thng tin vo bin ca MATLAB: >> erasers = 4 erasers= 4 >> pads = 6 pads= 6 >> tape = 2; >> iterms = erases + pads + tape iterms= 12 >> cost = erases*25 + pads*52 + tape*99 cost= 610 y chng ta to 3 bin MATLAB: erases, pads, tape cha s lng mi loi vt. Sau khi vo cc gi tr cho cc bin ny, MATLAB hin th kt qu ra mn hnh, tr trng hp bin tape. Du hai chm ng sau cu lnh >> tape = 2; thng bo cho MATLAB nhn gi tr gn nhng khng hin th ra mn hnh. Cui cng khc vi gi kt qu ans, chng ta yu cu MATLAB gi kt qu tng s cc vt l iterms, v tng s tin l cost. Ti mi bc MATLAB u a ra cc thng tin. V c lu gi cc bin nn chng ta c th yu cu MATLAB tnh gi tr trung bnh cho mi vt: >> everage_cost = cost/iterms everage_cost= 50.8333 Bi v everage cost c hai t, m MATLAB yu cu bin ch c mt t, nn chng ta dng du gch di ni hai t ny thnh mt t. Ngoi cc php tnh trn, MATLAB cn c mt s php tnh c bn khc nh bng di y: Php tnh Php cng, a + b Php tr, a - b Php nhn, a.b Php chia, ab Biu tng + * / hoc \ V d 5+3 7-4 18*24 56/ 8 = 8\ 56

Php lu tha, ab

5^2

Trong cc php ton trn c mc u tin khc nhau, khi tnh t tri sang phi ca mt dng gm nhiu lnh th php ton lu tha c mc u tin cao nht, tip theo l php nhn v php chia c mc u tin bng nhau cui cng l php cng v php tr cng c mc u tin bng nhau. 1.2 Khng gian lm vic ca MATLAB Cng nh bn lm vic vi ca s Lnh, MATLAB nh cc lnh bn g vo cng nh cc gi tr bn gn cho n hoc n c to ln. Nhng lnh v bin ny c gi l lu gi trong khng gian lm vic ca MATLAB, v c th c gi li khi bn mun. V d, kim tra gi tr ca bin tape, tt c nhng g bn phi lm l yu cu MATLAB cho bit bng cch nh vo tn bin ti du nhc: >> tape tape= 2 Nu bn khng nh tn bin, bn c th yu cu MATLAB cho danh sch cc bin bng cch dnh lnh who t du nhc lnh: >> who Your variables are: ans cost average_cost erasers

iterms pads

tape

Ch rng MATLAB khng a ra gi tr ca tt c cc bin, nu bn mun bit gi tr, bn nh vo tn bin ti du nhc lnh ca MATLAB. gi li cc lnh bn dng, MATLAB dng cc phm mi tn () trn bn phm ca bn. V d gi li lnh bn g vo lc gn hin ti nht, bn nhn phm mi tn , tip tc nhn phm ny, n s li gi tip lnh trc , Nu bn dng phm mi tn n s gi li lnh t lnh u tin cho n lnh gn hin ti nht. Cc phm mi tn v c th dng thay i v tr con tr trong dng lnh ti du nhc ca MATLAB, nh vy chng ta c th sa dng lnh, thm na, chng ta c th dng chut cng vi b nh m ct, copy, dn, v sa vn bn ti du nhc ca dng lnh. 1.3 Bin Ging nh nhng ngn ng lp trnh khc, MATLAB c nhng quy nh ring v tn bin. Trc tin tn bin phi l mt t, khng cha du cch, v tn bin phi c nhng quy tun th nhng quy tc sau:

Quy nh v tn bin Tn bin c phn bit ch hoa ch thng. l cc bin khc nhau

Ch dn/ V d Iterms, iterms, itErms, v ITERMS

Tn bin c th cha nhiu nht 31 k t, cn cc k t sau k t th 31 b l i. Tn bin bt u phi l ch ci, tip theo c th l ch s, s gch di K t chm cu khng c php dng v n c nhng ngha c bit Cng vi nhng quy nh trn, MATLAB c nhng bin c bit trong bng sau:

howaboutt how_abou

Cc bin c bit ans pi eps nht ln hn 1 flops inf NaN hoc nan i (v) j nargin narout realmin realmax

Gi tr Tn bin mc nh dng tr v kt qu = 3.1415.. S nh nht, nh vy dng cng vi 1 c s nh S ca php ton s thc ch s v cng nh kt qu ca 1/0 Dng ch s khng xc nh nh kt qu ca 0/0 i=j= S cc i s a vo hm c s dng S cc i s hm a ra S nh nht c th c ca s thc S ln nht c th c ca s thc

Nh bn c th to mt bin ca MATLAB, v bn cng c th gn li gi tr cho mt hoc nhiu bin. V d: >> erases >> pads = >> tape = >> iterms iterms= 12 >> erases erases= 6 >> iterms iterms= 12 = 4; 6; 2; = eases + pads + tape

= 6

y chng ta s dng li v d trn, chng ta tm c s vt m Mary mua sau chng ta thay i s cc ty ln 6, gi tr ny s ln gi tr trc ca n l 4. Khi bn lm nh vy, gi tr ca iterms vn khng thay i, v MATLAB khng tnh li iterms vi gi tr mi ca erases. Khi MATLAB thc hin mt php tnh, n ly gi tr ca cc bin hin thi, nn nu bn mun tnh gi tr mi ca iterms, cost, average_cost, bn gi li cc lnh tnh cc gi tr . i vi cc bin c bit trn, n c sn gi tr, nh vy khi bn khi ng MATLAB; nu bn thay i gi tr ca n th nhng gi tr c bit ban u s b mt cho n khi bn xo bin i hoc khi ng li MATLAB. Do bn khng nn thay i gi tr ca bin c bit, tr khi n thc s cn thit. Cc bin trong khng gian lm vic ca MATLAB c th b xo khng iu kin bng cch dng lnh clear. V d: >> clear erases ch xo mt bin erases >> clear cost iterms xo c hai bin cost v iterms >> clear cl*

du * ch rng xo tt c cc bin bt u bng hai k t cl. >> clear xo tt c cc bin trong khng gian lm vic!. Bn s khng c hi xc nhn cu lnh ny v tt c cc bin b xo khng th khi phc li. C th ni rng dng lnh clear rt nguy him, v vy khi dng lnh ny bn nn dng ng v tr. 1.4 Cu gii thch (comment) v s chm cu Tt c cc vn bn ng sau k hiu phn trm (%) u l cu gii thch. V d: >> erases = 4 erases= 4 % S cc ty.

Bin erases c gn gi tr l 4, cn tt c k hiu phn trm v vn bn ng sau n u b l i. c im ny gip cho chng ta d theo di cng vic chng ta ang lm. Nhiu lnh c th t trn cng mt hng, chng cch nhau bi du phy hoc du chm phy, nh: >> erases = 4, pads = 6; tape = 2

erases= 4 tape= 2 du phy yu cu MATLAB hin th kt qu trn mn hnh; cn du chm phy l khng hin th kt qu trn mn hnh. >> average_cost = cost/ ... iterms average_cost= 50.83333 Nh v d trn, ta c th dng du ba chm (...) ch cu lnh c tip tc hng di, php tnh thc hin c khi du ba chm ngn cch gia ton t v bin, ngha l tn bin khng b ngn cch gia hai hng: >> average_cost = cost/ it... erms ??? age_cost = cost/iterms Missing operator, coma, or semicolon.

ging nh vy, trng thi ca li gii thch khng th tip tc: >> % Comments cannot be continued ... >> either ??? Undefined function or variable either. Bn c th dng chng trnh bng cch nhn ng thi Ctrl v C. 1.5 S phc Mt trong nhng c im mnh m nht ca MATLAB l lm vic vi s phc. S phc trong MATLAB c c nh ngha theo nhiu cch, v d nh sau: >> c1 = 1 - 2i % Chn thm k t i vo phn o. c1= 1.0000 - 2.0000i >> c1 = 1 - 2j % j y tng t nh i trn. c1= 1.0000 - 2.0000i >> c2 = 3*(2-sqrt(-1)*3) c2= 6.0000 - 9.0000i >> c3 = sqrt(-2) c3=

0 + 1.4142i >> c4 = 6 + sin(.5)*i c4= 6.0000 + 0.4794i >> c5 = 6 + sin(.5)*j c5= 6.0000 + 0.4794i Trong hai v d cui, MATLAB mc nh gi tr ca i = j = dng cho phn o. Nhn vi i hoc j c yu cu trong trng hp ny, sin(.5)i v sin(.5)j khng c ngha i vi MATLAB. Cui cng vi cc k t i v j, nh trong hai v d u trn ch lm vic vi s c dnh, khng lm vic c vi biu thc. Mt s ngn ng yu cu s iu khin c bit cho s phc khi n xut hin, trong MATLAB th khn cu nh vy. Tt c cc php tnh ton hc u thao tc c nh i vi s thc thng thng: >> c6 = (c1 + c2)/c3 % T cc d liu trn c6= -7.7782 - 4.9497i >> check_it_out = i^2 % Bnh phng ca i phi l -1 check_it_out= -1.0000 + 0.0000i trong v d ny ch cn li phn thc, phn o bng khng. Chng ta c th dng hm real v imag kim tra tng phn thc v o. Chng ta c th biu din s phc dng ln v gc (dng cc): M M.ej = a+bi trn s phc c biu din bng ln M v gc , quan h gia cc i lng ny v phn thc, phn o ca s phc biu din di dng i s l: M= = tan-1(b/ a) a = Mcos b = Msin Trong MATLAB, chuyn t dng cc sang dng i s, dng cc hm real, imag, v angle: >> c1 c1= 1.0000 - 2.0000i >> M_c1 = abs(c1) M_c1= 2.2361 >> angle_c1 = angle(c1) angle_c1= % Gi li c1 % Tnh argument ca s phc % Tnh gc ca s phc theo radian

-1.1071 >> deg_c1 = angle_c1*180/ pi -63.4349 >> real_c1 = real(c1) real_c1= 1 >> imag_c1 = imag(c1) imag_c1= -2

% Chuyn t radian sang % Tnh phn thc % Tnh phn o

Chng2

CC C tnh K THUT

ing nh hu ht cc my tnh k thut, MATLAB a ra rt nhiu cc hm ton hc, k thut thng dng, ngoi ra MATLAB cn cung cp hng trm cc hm c bit v thut ton, n rt hu ch gii quyt cc vn khoa hc. Tt c cc hm ny c lit k trong online help, cn y ch cp n nhng hm thng dng nht. 2.1 Cc hm ton hc thng thng Cc hm ton hc ca MATLAB c lit k trong bng di y, chng u c chung mt cch gi hm nh v d di y: >> x = sqrt(2)/2 x= 0.7071 >> y = sin(x) y= 0.7854 >> y_deg = y*180/pi y_deg= 45.0000 Nhng lnh ny tm mt gc (tnh bng ) khi bit gi tr hm sin ca n l / 2. Tt c cc hm lin quan n gc ca MATLAB u lm vic vi radian. Bng cc hm:

Cc hm thng thng abs(x) acos(x) acosh(x) angle(x) asin(x) asinh(x) atan(x) atan2(x, y) atanh(x) ceil(x) conj(x) cos(x) cosh(x) exp(x) fix(x) floor(x) gdc(x, y) imag(x) lcm(x, y) log(x) log10(x) real(x) rem(x, y) round(x) sign(x) sign(-23.4)=-1; sign(0)=0 sin(x) sinh(x) sqrt(x) tan(x) tanh(x)

Tnh argument ca s phc x Hm ngc ca cosine Hm ngc ca hyperbolic cosine Tnh gc ca s phc x Hm ngc ca sine Hm ngc ca hyperbolic sine Hm ngc ca tangent L hm arctangent ca phn thc ca x v y Hm ngc ca hyperbolic tangent Xp x dng v cng S phc lin hp Hm cosine ca x Hm hyperbolic cosine ca x Hm ex Xp x khng Xp x m v cng c s chung ln nht ca hai s nguyn x v y Hm tr v phn o ca s phc Bi s chung nh nht ca hai s nguyn x v y Logarithm t nhin Logarithm c s 10 Hm tr v phn thc ca x Phn d ca php chia x/ y Hm lm trn v s nguyn t Hm du: tr v du ca argument nh: sign(1.2)=1; Hm tnh sine ca x Hm tnh hyperbolic sine ca x Hm khai cn bc hai Tangent Hyperbolic tangent

>> 4*atan(1) ans= 3.1416 >> help atant2 ATAN2

% Mt cch tnh xp x gi tr ca pi % Yu cu gip i vi hm atan2

four quadrant inverse tangent ATAN2(Y, X) is the four quadrant arctangent of the real parts of the elements of X and Y. -pi <= ATAN2(Y, X) <= pi

see also ATAN. >> 180/pi*atan(-2/ 3) ans= -33.69 >> 180/pi*atan2(2, -3) ans= 146.31 >> 180/pi*atan2(-2, 3) ans= -33.69 >> 180/pi*atan2(2, 3) ans= 33.69 >> 180/pi*atan2(-2, -3) ans= -146.31 Mt s v d khc: >> y = sqrt(3^2 + 4^2) % Tnh cnh huyn ca tam gic pitago 3-4-5 y= 5 >> y = rem(23,4) % 23/4 c phn d l 3 y= 3 >> x = 2.6,y1 = fix(x),y2 = floor(x),y3 = ceil(x),y4 = round(x) x= 2.6000 y1= 2 y2= 2 y3= 3 y4= 3 >> gcd(18,81) % 9 l c s chung ln nht ca 18 v 81 ans= 9 >> lcm(18,81) % 162 l bi s chung ln nht ca 18 v 81 ans= 162 V d: c lng chiu cao ca ngi nh

Vn : Gi thit bit khong cch t ngi quan st n ngi nh l D, gc t ngi quan st n ngi nh l ; chiu cao ca ngi quan st l h. Hi ngi nh cao bao nhiu? Gii php: Ta biu din kch thc nh hnh 2.1:

Hnh 2.1 Ngi nh c chiu cao l H + h, H l chiu di ca mt cnh ca tam gic, chiu di ny c th tnh c bng cng thc quan h gia gc v cnh ca tam gic: tan() = T ta c chiu cao ca ngi nh l h + H = h + D.tan() Nu h =2meters, D =50meters, v l 60o, MATLAB s a ra kt qu l: >> h = 2 h = 2 >> theta = 60 theta = 60 >> D = 50 D = 50 >> buiding_height = h+D*atan(theta*pi/180) buiding_height = 54.3599 V d s suy gim do phn r Vn : S phn r phn t polonium c chu k phn r l 140 ngy, tc l sau 140 ngy th lng poloniun cn li l 1/2 lng ban u. Hi nu ban u c 10 grams polonium, n s cn li bao nhiu sau 250 ngy? Gii quyt: Sau 1 chu k phn r hoc 140 ngy, cn li 10x0.5 = 5 grams; sau 2 chu k phn r hoc 280 ngy, cn li 5x0.5 = 10x(0.5)2 = 2.5grams, t ta c kt qu nm trong khong 5 v 2.5 grams, v ta c cng thc tnh phn cn li sau khong thi gian bt k: khi lng cn li = khi lng ban u x(0.5)thi gian/ chu k v d thi gian l 250 ngy, v kt qu MATLAB a ra l:

>> initial_amount = 10; % Khi lng ban u >> half_life = 140; % Chu k phn r >> time = 250; % Thi gian tnh khi lng >> amount_left = initial_*0.5^(time/half_life) amount_left= 2.9003 V d tnh ton v li xut Vn : Bn ng mua t mi vi gi 18,500 dollars. Ngi bn t a ra hai gii php v ti chnh l: th nht, tr 2.9% li xut ca s tin trn trong vng 4 nm. Th hai l tr 8.9% li xut ca s tin trn trong vng 4 nm v gi bn c gim i mt khon l 1500 dollars. Hi vi gii php no th bn mua c t vi gi r hn? Gii php: S tin tr hng thng l P, trn tng s tin l A dollars, t s li xut hng thng l R, tr trong M thng: P=A Tng s tin phi tr s l: T = PxM Gii php MATLAB a ra l: >> format bank % Dng dng hin th ngn hng >> A = 18500; % Tng s tin >> M = 12*4; % S thng phi tr li >> FR = 1500; % Tin gim gi ca nh my >> % Gii php th nht >> R = (2.9/100)/12; % T l li xut hng thng >> P = A*(R*(1+R)^M/((1+R)^M - 1)) % Khon tin phi tr hng thng P= 408.67 >> T1 = P*M % Tng gi tr ca t T1= 19616.06 >> % Gii php th hai >> R = (8.9/100)/12; % T l li xut hng thng >> P = (A-FR)*(R*(1 + R)^M/((1+R)^M - 1)) % Tin phi tr hng thng P= 422.24 >> T2 = P*M % Tng gi tr ca t T2= 20267.47 >> Diff = T2 - T1 Diff= 651.41 Nh vy ta c gii php th nht gi r hn gii php th hai. V d: Vn nng acid

Vn : Nh mt phn ca qu trnh sn xut b phn ca vt c ti mt nh my t ng, b phn c nhng trong nc lm ngui, sau nhng trong bn ng dung dch acid lm sch. Trong ton b ca qu trnh nng acid gim i khi cc b phn c ly ra khi bn acid v khi nhng b phn ca vt c vo bn th mt lng nc cn bm trn vt c khi nhng b trc cng vo theo v khi nhc ra khi bn mt lng acid bm theo vt. m bo cht lng th nng acid phi khng c nh hn mt lng ti thiu. Bn hy bt u vi nng dung dch l 90% th nng ti thiu phi l 50%. Lng cht lng thm vo v ly i sau mi ln nhng dao ng trong khong t 1% n 10%. Hi bao nhiu b phn c th nhng vo b dung dch acid trc khi nng ca n gim xung di mc cho php? Gii php: Ban u nng acid l initial_con = 90% = acid/ (acid + water) sau ln nhng th nht nng acid cn: con = = = = acid l lng acid ban u trong dung dch, water l lng nc ban u trong dung dch, lost l lng phn trm nc thm vo. S acid cn li trong dung dch sau ln nhng th nht l: acid_left = Ngha l, khi nhng ln th hai nng dung dch s l: con = = = Tip tc qu trnh ny, sau n ln nhng, nng acid l: con = Nu nng acid cn li l mc ti thiu chp nhn c, s ln nhng cc i s l mt s nguyn bng hoc nh hn n: n= Trong MATLAB gii php s l: >> initial_con = 90

initial_con= 90 >> min_con = 50 min_con= 50 >> lost = 0.01; >> n = floor(log( initial_con/min_con)/log(1+lost)) n= 59 Nh vy c th nhng 59 ln trc khi nng acid gim xung di 50%. Ch hm floor dng lm trn s n xung s nguyn gn nht, v dy ta cng c th dng hm logarithm c s 10 v logarithm c s 2 thay cho hm logarithm t nhin trn.

chng 3

NHNG C IM CA CA S LNH

a s lnh (comand) ca MATLAB c rt nhiu nhng c im cn ch , mt s chng c gii thiu chng trc, v sau y chng ta tm hiu r hn v chng. 3.1 Qun l khng gian lm vic ca MATLAB Cc d liu v bin c to ln trong ca s lnh, c lu trong mt phn gi l khng gian lm vic ca MATLAB. Mun xem tn bin trong khng gian lm vic ca MATLAB ta dng lnh who: >> who Your variables are: D buiding_height h theta

Cc bin ny c dng trong v d c lng chiu cao ngi nh. xem chi tit hn v cc bin ta dng lnh whos: >> whos

Name D buiding_height h theta

Size 1x1 1x1 1x1 1x1

Bytes 8 8 8 8

Class array array array array

double double double double

Grand total is 4 elements using 32 bytes Mi bin c lit k vi kch c ca n, s bytes s dng, v cc lp ca chng (class), trong v d c bit ny, cc bin u l s n, c chnh xc hai s sau du phy. Lnh whos c bit c ch khi nghin cu n phn mng v cc kiu d liu khc. Ngoi cc hm ny, trong mc Show Workspace trong bng chn file to ra ca s GUI gi l Workspace Browser, n cha cc thng tin tng t nh lnh whos. Thm na n to cho bn kh nng xo, lm sch cc bin m bn chn. Ca s ny cng c th to bng cch nhn nt Workspace Browser, trn thanh cng c ca ca s lnh. Nh trnh by trn, lnh clear c th xo bin t khng gian lm vic ca MATLAB. V d: >> clear >> who h D % Xo cc bin h v D

Your variables are: buiding_height theta

Cc tu chn khc ca hm clear chng ta c th tm hiu thm bng lnh help: >> help clear CLEAR Clear variables and functions from memory. CLEAR removes all variables from the workspace. CLEAR VARIABLES does the same thing. CLEAR GLOBAL removes all global variables. CLEAR FUNCTIONS removes all compiled M-functions. CLEAR MEX removes all links to MEX-files. CLEAR ALL removes all variables, globals, functions and MEX links. CLEAR VAR1 VAR2 ... clears the variables specified. The wildcard character '*' can be used to clear variables that match a pattern. For instance, CLEAR X* clears all the variables in the current workspace that start with X.

If X is global, CLEAR X removes X from the current workspace, but leaves it accessible to any functions declaring it global. CLEAR GLOBAL X completely removes the global variable X. CLEAR FUN clears the function specified. been locked by MLOCK it will remain in memory. CLEAR ALL also has the side effect of debugging breakpoints since the breakpoints for cleared whenever the m-file changes or is cleared. If FUN has

removing a file

all are

Use the functional form of CLEAR, such as CLEAR('name'), when the variable name or function name is stored in a xu. See also WHO, WHOS, MLOCK, MUNLOCK. Cui cng, khi lm vic trong khng gian lm vic ca MATLAB, n thng thun tin ghi hoc in mt bn sao cng vic ca bn, lnh diary ghi d liu ngi dng a vo v ca s lnh v a ra file vn bn dng m ASCII c tn l diary trong th mc hin ti. >> diary frame >> diary off % ghi d liu vao file frame % kt thc lnh diary v ng file

Khi ca s lnh c chn, chn print... t bng chn file in mt bn ca ca s lnh, bn c th dng chut la chn phn mnh mun ghi, chon Pint Selection... t bng chn file, in mt phn vn bn la chn. 3.2 Ghi v phc hi d liu nh cc bin MATLAB c th ghi v gi li d liu t file trong my tnh ca bn. Mc Workspace as... trong bng chn file m hp chun hi thoi ghi tt c cc bin hin ti. Ging nh vy, trong mc Load Workspace trong bng chn file m hp hi thoi gi li tt c cc bin m ta ghi li t khng gian lm vic trc, n khng lm mt cc bin ny trong khng gian lm vic hin ti. Khi ta gi li cc bin, m cc bin ny trng tn vi cc bin trong khng gian lm vic ca MATLAB, n s thay i gi tr ca cc bin theo gi tr ca cc bin gi ra t file. Nu bng chn file khng thun tin hoc khng p ng c nhng yu cu ca bn, MATLAB cung cp hai lnh save v load, n thc hin mt cch mm do hn,

trong trng hp c bit, lnh save cho php bn ghi mt hoc nhiu hn mt bin tuy theo s la chon ca bn. V d: >> save Cha tt c cc bin trong MATLAB theo kiu nh phn trong file MATLAB.mat >> save data

cha tt c cc bin trong MATLAB theo kiu nh phn trong fle data.mat. >> save data erasers pads tape -ascii

Ghi cc bin erasers, pads, tape trong dng m ASCII 8 s trong file data. File dng m ASCII c th sa i bng bt c chng trnh son tho vn bn no, ch rng file ASCII khng c phn m rng .mat. >> save data erasers pads tape -ascii -double

Ghi cc bin erasers, pads, tape dng ASCII 16 s trong file data. Lnh load cng dng vi c php tng t. 3.3 Khun dng hin th s Khi MATLAB hin th kt qu dng s, n tun theo mt s quy nh sau: Mc nh, nu kt qu l s nguyn th MATLAB hin th n l mt s nguyn, khi kt qu l mt s thc th MATLAB hin th s xp x vi bn ch s sau du phy, cn cc s dng khoa hc th MATLAB hin th cng ging nh trong cc my tnh khoa hc. Bn c th khng dng dng mc nh, m to mt khun dng ring t mc Preferences, trong bng chn file, c th mc nh hoc nh dng xp x ti du nhc. Chng ta dng bin average_cost ( trong v d trc) lm v d, dng s ny l: Lnh ca MATLAB format short format long format short e format long e format short g hoc format short e format long g hoc format long e format hex format bank format + format rat average_cost 50.833 50.83333333333334 5.0833e+01 5.083333333333334e+01 50.833 50.83333333333333 40496aaaaaaaaaab 50.83 + 305/ 6 ch thch 5 s 16 s 5 s vi s m 16 s vi s m chnh xc hn format short chnh xc hn format long h c s 16 hai s h 10 dng, m hoc bng khng dng phn s

Mt ch quan trng l MATLAB khng thay i s khi nh li khun dng hin th c chn, m ch thay i mn hnh thay i.

Chng 4

Script M_files

M
t vn n gin l, yu cu ca bn ti du nhc ca MATLAB trong ca s lnh l nhanh v hiu qu. Tuy nhin v s lnh tng ln, hoc khi bn mun thay i gi tr ca mt hoc nhiu bin v thc hin li mt s lnh vi gi tr mi, nu c nh lp li ti du nhc ca MATLAB th s tr ln bun t, do vy MATLAB cung cp mt gii php cho vn ny l: n cho php bn thay th cc lnh ca MATLAB bng mt file vn bn n gin, v yu cu MATLAB m file v thc hin lnh chnh xc nh l nh ti du nhc ca MATLAB ti ca s lnh, nhng file ny gi l script file, hoc n gin l M_file. Danh t "script" ch rng thc t MATLAB c t file kch bn tm thy trong file. Danh t "M_file" ch rng tn script file phi kt thc bng phn m rng l '.m' nh v d example1.m. to mt script M_file, chn New trong bng chn file v chn M_file. Th tc ny s to ra mn hnh son tho, v bn c th nh c cc lnh ca MATLAB trong . V d di y l cch lnh trong v d c lng chiu cao ngi nh trc: function example1 % example1.m V d c lng chiu cao ngi nh h = 2 theta = 60 D = 50; building_height = h + D*tan(theta*pi/180) Bn c th ghi v lu gi file nybng cch chn Save t bng chn file. Khi bn ghi ln file ch phi nh tn file trng vi tn hm (example) khng cn nh vo phn m rng, MATLAB t gn vo cho n. Khi t du nhc ta c th nh:

>> example1 h= 2 theta= 60 building_height= 54.3599 Khi MATLAB din gii cc trng thi ca example1 trn, n s c ni k hn chng sau, nhng mt cch ngn gn, MATLAB dng cc trng thi ca bin MATLAB hin ti v to ln cc lnh ca n, bt u bng tn M_file. Ngha l, nu example1 khng phi l bin hin ti, hoc mt lnh MATLAB xy dng ln, MATLAB m file example1.m (nu n tm thy) v tnh gi tr cc lnh tm thy ch khi chng ta vo cc thng s chnh xc ti du nhc ca ca s lnh. Nh thy lnh trong M_file truy cp n tt c cc bin trong khng gian lm vic ca MATLAB, v tt c cc bin trong M_file tr thnh mt phn ca khng gian lm vic. Bnh thng cc lnh c trong M_file khng c hin th nh l n c tnh trong ca s lnh, nhng lnh echo on yu cu MATLAB hin th hoc lp li lnh i vi ca s lnh nh chng ta c v tnh. Tip theo bn c th on c lnh echo off lm g. Ging nh vy, lnh echo lp li bi chnh n lm thay i chnh trng thi ca n. Vi c im ny ca M_file bn c th thay i li ni dung ca file, v d bn c th m M_file example1.m thay i li cc gi tr ca h, D, hoc theta, ghi li file v yu cu MATLAB tnh li lnh trong file. Thm na, bng cch to M_file, cc lnh ca bn c lu trn a v c th ng dng v sau khi bn cn. Nhng ng dng ca ch dn ca MATLAB gip chng ta hiu c khi dng script file nh trong example1.m, ch dn cho php bn lu gi cng cc lnh trong script file, v vy bn nh c nhng lnh lm g khi bn nhn li file sau y. Thm na, du chm phy ng sau cu lnh khng cho hin th kt qu, t bn c th iu chnh script file a ra nhng kt qu cn thit. V nhng ng dng ca script file, MATLAB cung cp mt s hm c bit c ch khi bn s dng trong M_file: Cc hm M_file disp(ans) echo input keyboard pause pause(n) waitforbuttonpress

Hin th cc kt qu m khng hin tn bin iu khin ca s lnh lp li cc lnh ca script file S dng du nhc a d liu vo Trao iu khin tm thi cho bn phm Dng li cho n khi ngi dng nhn mt phm bt k Dng li n giy Dng li cho n khi ngi dng nhn chut hoc phm.

Khi lnh ca MATLAB khng kt thc bng du chm phy, kt qu ca lnh c hin th trn ca s lnh cng vi tn bin. i lc n thun tin khi khng cho hin tn bin, trong MATLAB ta dng lnh disp thc hin vic ny:

>> h h= 2 >> disp(h) 2

% Cch truyn thng hin kt qu % Hin kt qu khng c tn bin

gip bn son tho script file khi tnh ton cho nhiu trng hp, lnh input cho php bn to cu nhc vo d liu c an ton. V d example1.m vi nhng phn c sa: function example1 % example1.m V d c lng chiu cao ngi nh h = 2 theta = 60 D = input( Vo khong cch gia ngi v ngi nh: ) building_height = h + D*tan(theta*pi/180) chy file ny: >> example1 h= 2 theta= 60 Vo khong cch gia ngi v ngi nh: 60 D= 60 building_height= 64.8319 v d trn ta g vo s 60 v n Enter. Nhng lnh sau s tnh vi gi tr ca D l 60. Ch rng hm input c th dng vi cc php ton khc ging nh i vi cc hm thng thng khc, hm input cng chp nhn i vi bt c kiu biu din s no, v d ta vo mt s l: +5. >> example1 h= 2 theta= 60 Vo khong cch gia ngi v ngi nh: sqrt(1908)+5 D= 48.6807 building_height= 52.9783 xem nhng tc ng ca lnh echo, ta dng chng trong script file:

echo on function example1 % example1.m V d c lng chiu cao ngi nh h = 2 theta = 60 D = input( Vo khong cch gia ngi v ngi nh: ) building_height = h + D*tan(theta*pi/180) echo off chy chng trnh ta c: >> example1 % example1.m V d c lng chiu cao ngi nh h = 2 h= 2 theta = 60 theta= 60 D = input( Vo khong cch gia ngi v ngi nh: ) Vo khong cch gia ngi v ngi nh: 60 building_height = h + D*tan(theta*pi/180) building_height= 64.8319 echo off Nh bn thy trong trng hp ny, lnh echo lm cho kt qu kh c hn, nhng ngc li lnh n c th rt c ch khi g ri nhiu script file ng dng.

Chng 5

QUN L Tp

M
ATLAB cung cp mt s cc hm file h thng v cc lnh cho php bn lit k tn file, xem, v xo M_file, hin th v thay i th mc cha n. Mt s tng kt cc lnh c a ra trong bng di y. Thm vo bn c th xem v sa ng dn ca MATLAB (matlabpath). Nhng ng dn ny ch cho MATLAB ni cha script file v hm M_file trong my tnh ca bn. C rt nhiu trng hp cc hm trong MATLAB l cc M_file n gin c cha trong a, nhng MATLAB thng bo khng bit hm ny, nh vy do n khng tm c ng dn ca MATLAB, bn cn phi thay i li ng dn:

Cc hm h thng file addpath dir1 cd p = cd cd path delete test.m dir d = dir edit test file exist(cow,file) exist(d,dir) filesep Macintosh fullfile inmem ls MATLABrc.m startup.m MATLABroot path pathdef.m pathsep pwd rmpath dir1 startup.m tempdir tempname type test what hin thi which test

Thm th mc dir1 vo bt u ca ng dn Hin th th mc hin thi Gn th mc lm vic hin thi cho bin p Thay i th mc a ra bng ng dn Xo M_file test.m Danh sch tt c cc file trong th mc hin thi Tr li file trong th mc hin thi trong cu trc bin d M test.m son tho, ging nh Open trong bng chn Kim tra s tn ti ca file cow.m trong ng dn Kim tra s tn ti ca th mc d trong ng dn Tch file nh \ trong Windows95 v NT, : trn To tn file vi ng dn y Danh sch hm M_file, gi ra t b nh Ging nh dir MATLAB ch khi ng script M_file, thc hin trc khi Tr ng dn th mc cho chng trnh thc hin MATLAB Hin th hoc sa ng dn ca MATLAB (MATLABpath) Hm M_file, ni m mmatlabpath l ng Chia ng dn cho matlabpath Ging nh cd B i th mc dir1 t ng dn matlabpath script M_file thc hin khi MATLAB khi ng Tn ca th mc tm thi Tn ca file tm thi Hin ra M_file test.m trong ca s lnh Tr li danh sch tt c M_file v MAT_file trong th mc Hin th ng dn th mc n test.m

ng n ca MATLAB l danh sch ca tt c cc th mc lu tr cc file ca MATLAB. Hn na, nu bn to mt th mc ca M_file th ng dn ca n phi c thm vo matlabpath, nu khng th MATLAB khng th truy cp n cc file ca bn c, tr khi file t trong th mc hin thi. xem MATLAB s dng matlabpath nh th no, hy xem trng hp c m t trong bng sau:

ng dn ca MATLAB Khi bn g >> cow, MATLAB s lm nh sau: (1) Kim tra nu cow l mt bin trong khng gian lm vic ca MATLAB, nu khng th... (2) N kim tra nu cow l mt hm c xy dng, nu khng th... (3) N kim tra nu mt tn M_file cow.m tn ti trong th mc hin thi, nu khng th... (4) N kim tra nu cow.m tn ti bt c ni no trn ng dn ca MATLAB bng cch tm kim ng dn. Khi no s ph hp c tm thy th MATLAB chp nhn n. V d nh cow tn ti nh mt bin trong khng gin lm vic ca MATLAB, th MATLAB khng dng hm hoc bin c tn l cow. V vy bn trnh khng nn to bin c tn trng vi tn hm nh: >> sqrt = 1.2; >> sqrt(2); Nhng lnh trn s to ra li, bi v sqrt y khng phi l hm tnh cn bc hai, n l bin c gi tr l 1.2. Th tc ng dn cn c dng khi lnh load c dng. u tin MATLAB tm kim trong th mc hin ti, sau n tm theo ng dn ca MATLAB n file d liu. Thc t th tc tm kim ca MATLAB phc tp hn l trnh by trn rt nhiu v MATLAB dng rt nhiu file c phn m rng l .m . Hm M_file c th cha nhiu hn mt bin, th mc trong matlabpath c th c th mc con gi l private, v MATLAB cung cp chng trnh hng i tng vi cc ton t nh ngha li M_file trong th mc con, bt u bng k t @. Nu tt c nhng c im ny c cng thm vo bng trn th n s y hn, nhng s rt kh hiu. Nu bn mun nghin cu thm v phn ny th xem cc ti liu cung cp trong a CD. Nu bn c M_file hoc MAT_file cha trong th mc khng phi trong ng n ca MATLAB v khng trong th mc hin ti, MATLAB khng th tm thy chng. C hai gii php cho vn ny l: (1)_To th mc thit k thnh th mc hin ti, dng lnh cd hoc pwd t trong bng trc. (2)_Cng thm th mc thit k trong ng dn ca MATLAB . Cui cng n rt d dng khi ta s dng phng php duyt qua cc ng dn (path browser) hoc cc lnh trong ca s lnh path v addpath. dng path browser, ta chn set path t bng chn file hoc nhn chut trn nt path browser trn thanh cng c ca ca s lnh. Lm nh vy ta s c mn hnh ging nh hnh 5.1: Ging nh thit k cc GUI, n lin quan trc tip khi ta s dng. ng dn matlabpath c hin th bn tri, th mc con nm trong ng dn c chn nm bnn tri, cn cc nt thay i ng dn nh thm ng dn mi (add to path), loi b ng dn (remove from path) pha trn. ghi li s thay i ta chn save path t bng chn file ca ca s path browser trc khi ng GUI.

Hnh 5.1 path browser trong MATLAB 5.2 Ca s path browser trong MATLAB 5.0 khng khc lm so vi MATLAB 5.2, ch yu l cc nt thay i ng dn trong MATLAB 5.2 th n t trn nh cn MATLAB 5.0 n c t bn phi. ghi li s thay i ng dn trong MATLAB 5.0 trc khi ng GUI ta nhn nt save settings.

Hnh 5.2 path browser trong MATLAB to Student 5.1 MATLAB khi khi ng Khi khi ng MATLAB, n to ra hai script M_file l matlabrc.m v startup.m, trong atlabrc.m i cng MATLAB, v nhn chung l khng c sa n. Cc lnh trong M_file to mt cu hnh mc nh v kch c ca ca s v v tr ca n, cng nh cc c im mc nh khc trong Windows95, WindowNT. ng dn mc nh c to bng cch gi script file pathdef.m t matlabrc.m. Trong cc phn, cc lnh trong matlabrc.m kim tra s tn ti ca script M_file startup.m trong ng dn ca MATLAB nu n tn ti, cc lnh trong n c thc hin. S la chon M_file startup.m cha cc lnh c nhng c im ring i vi MATLAB. V d n rt thng thng nu ta thm mt hoc hn cc lnh path hoc addpath trong startup.m chn thm cc th mc vo trong ng dn ca MATLAB. Ging nh vy, mc nh hin th khun dng s c th thay i c nh format compact. Nu bn c mn hnh cn bng xm, lnh graymon s c ch khi to mc nh ho cho ch ny. Hn na, nu bn v th c cc kiu mc nh ring th mt s gi ti colordef c th xut hin trong startup.m. Khi startup.m l mt file chun trong script M_file, th khng mt lnh no c th thay th c trong n. Tuy nhin ta c th thay th lnh quit trong startup.m.

chng 6

cc php ton vi MNg

T
t c mi s tnh ton u duy tr mt im l c s dng n cc s n, gi l scalars. Php ton c lin quan n scalars l cc php ton c bn, nhng mt lc no , php ton phi lp li nhiu ln khi tnh trn nhiu s. gii quyt vn ny, MATLAB nh ngha thao tc trn mng d liu. 6.1 Mng n Gi s ta xt hm y=sin(x) trong mt na chu k ( x 0 ) trong khong ny s im gi tr ca x l v tn, nhng ta ch xt nhng im cch nhau mt khong gi tr l 0.1 nh vy s cc gi tr ca x l m c. T ta c mng cc gi tr ca x l x= 0, 0.1, 0.2,..., Nu ta dng my tnh k thut tnh th ta c tng ng cc gi tr ca y, t ta c mng ca y x y 0 0 0.1 0.31 0.2 0.59 0.3 0.81 0.4 0.95 0.5 1.0 0.6 0.95 0.7 0.81 0.8 0.59 0.9 0.31 0

trong mng x cha cc phn t x1, x2, ..., x11 trong mng y cha cc phn t y1, y2, ..., y11 Trong MATLAB to nhng mng ny rt n gin; v d to hai mng trn ta nh cc lnh sau vo du nhc ca MATLAB:

>> x=[0 .1*pi .2*pi .3*pi .4*pi .5*pi .6*pi .7*pi .8*pi .9*pi pi] x= Columns 1 through 7 0 0.3142 0.6283 0.9425 1.2566 1.5708 1.8850 Columns 8 through 11 2.1991 2.5133 2.8274 3.1416 >> y = sin(x) y= Columns 1 through 7 0 0.3090 0.5878 0.8090 0.9511 1.0000 0.9511 Columns 8 through 11 0.8090 0.5878 0.3090 0.0000 Kt qu trn ta c mng ca y gm cc phn t tng ng l sine ca cc phn t ca x, y MATLAB ngm hiu l ta tnh sine ca tng phn t ca x. to mng, ta t cc phn t ca mng vo gia hai du ngc vung "[...]"; gia hai phn t ca mng c th l du cch hoc du phy "," 6.2 a ch ca mng trn mng x c 1 hng, 11 ct hay c th gi l vector hng, mng c di 11 +) truy nhp n cc phn t ca mng ta dng cc ch s th t ca phn t trong mng v d x(1) l phn t th nht ca mng, x(2) l phn t th hai ca mng... >> x(2) % phn t th nht ca mng ans= 0.3142 >> y(5) % phn tu th 5 ca mng ans= 0.9511 +) truy nhp n nhiu phn t ca mng, v d ta truy nhp t phn t th nht n phn t th nm ca mng x: >> x(1:5) ans= 0 0.3142

0.6283

0.9425

1.2566

Truy nhp t phn t th 7 n phn t cui ca mng y: >> y(7:end) ans= 0.9511

0.8090

0.5878

0.3090

0.0000

Truy nhp t phn t th ba n phn t th nht ca mng y: >> y(3:-1:1) ans= 0.5878

0.3090

v d trn 3 l phn t th 3, 1 l ch phn t u tin, cn -1 l gi tr cng (v tr phn t sau bng v tr phn t trc cng vi -1) Truy nhp n cc phn t trong khong t phn t th 2, n phn t th 7, v tr ca phn t sau bng v tr ca phn t trc cng vi 2, ca mng x: >> x(2:2:7) ans= 0.3142

0.9425

1.5708

To mng gm cc phn t th 1, 2, 8, 9 ca mng y: >> y([8 2 9 1]) ans= 0.8090

0.3090

0.5878

Nu ta truy nhp vo cc phn t ca mng m th t cc phn t tng u vi 1, ta c th nh lnh: >> x(1:3) ans= 0

0.3142

0.6283

6.3 Cu trc ca mng Vi mng c s lng phn t t th ta c th nhp vo trc tip, nhng vi mng c s lng ln cc phn t th ta dng mt trong hai cch sau: +) To mt mng bt u l phn t 0, sau bng phn t trc cng vi 0.1, phn t cui l 1, tt c cc phn t ca mng c nhn vi : >> x= (0:0.1:1)*pi x= Columns 1 through 7 0 0.3142 0.6283 1.8850 Columns 8 through 11 2.1991 2.5133

0.9425

1.2566

1.5708

2.8274

3.1416

+) To mng gm cc phn t ca x bng hm linspace. C php ca hm ny nh sau: linspace(gi tr phn t u, gi tr phn t cui, s cc phn t)

v d >> x = linspace(0,pi,11) x= Columns 1 through 7 0 0.3142 0.6283 1.8850 Columns 8 through 11 2.1991 2.5133

0.9425

1.2566

1.5708

2.8274

3.1416

Cch th nht gip ta to mng m ch cn vo khong cch gi tr gia cc phn t (khng cn bit s phn t), cn cch th hai ta ch cn vo s phn t ca mng (khng cn bit khong cch gi tr gia cc phn t). Ngoi cc mng trn, MATLAB cn cung cp mng khng gian theo logarithm bng hm logspace. C php ca hm logspace nh sau: logspace(s m u, s m cui, s phn t) v d >> logspace(0,2,11) ans= Columns 1 through 7 1.0000 1.5849 2.5119 3.9811 6.3096 15.8489 Columns 8 though 11 25.1189 39.8107 63.0957 100.0000

10.0000

To mng, gi tr bt u ti 100, gi tr cui l 100, cha 11 gi tr Cc mng trn l cc mng m cc phn t ca n c to ln theo mt quy lut nht nh. Nhng i khi mng c yu cu, n khng thun tin to cc phn t bng cc phng php trn, khng c mt mu chun no to cc mng ny. Tuy nhin ta c th to mng bng cch vo nhiu phn t cng mt lc V d >> a = 1:5,b = 1:2:9 a= 1 2 3 4 b= 1 3 5 7 >> c = [a b] 1 2 3 4 5

5 9 1 3 5 7 9

v d trn ta to hai mng thnh phn l a v b sau to mng c bng cch ghp hai mng a v b. Ta cng c th to mng nh sau: >> d=[a(1:2:5) 1 0 1]

d= 1 3 5 1 0 1 a l mng gm cc phn t [1 3 5], mng d l mng gm cc phn t ca a v ghp thm cc phn t [1 0 1] Tm li ta c bng cu trc cc mng c bn:

x=[ 2 2*pi sqrt(2) 2-3j ] To vector hng x cha cc phn t c bit. x= first : last To vector hng x bt u ti first, phn t sau bng phn t trc cng vi 1, kt thc l phn t c gi tr bng hoc nh hn last . x= first : increment : last To vector hng x bt u ti fist, gi tr cng l increment, kt thc l phn t c gi tr bng hoc nh hn last. x= linspace(fist, last, n) To vector hng x bt u ti first, kt thc l last, c n phn t. x= logspace(first, last, n) To vector hng khng gian logarithm x bt u ti first last 10 , kt thc ti 10 , c n phn t. 6.4 Vector hng v vector ct Trong cc v d trc, mng cha mt hng v nhiu ct, ngi ta thng gi l vector hng. Ngoi ra ta cn c mng l vector ct, tc l mng c mt ct v nhiu hng, trong trng hp ny tt c mi thao tc v tnh ton i vi mng nh trn l khng thay i. T cc hm to mng minh ho phn trc (tt c u to vector hng), c nhiu cch to vector ct. Mt cch trc tip to vector ct l vo tng phn t ca mng nh v d sau: >> c = [1;2;3;4;5] c= 1 2 3 4 5 Khc vi trc l ta dmg du cch hay du phy phn cch gia hai ct ca vector hng. Cn v d ny ta dng du chm phy phn cch gia hai hng ca vector ct.

Mt cch khc to cc vector ct l dng cc hm linspace, logspace, hay t cc vector hng, sau dng phng php chuyn v. MATLAB dng ton t chuyn v l ( ' ) chuyn t vector hng thnh vector ct v ngc li. V d to mt vector a v vector b l chuyn v ca vector a, vector c l chuyn v ca vector b: >> a= 1:5 a= 1 2 >> b= a' b= 1 2 3 4 5 >> c= b' c= 1 2

Ngoi ra MATLAB cn s dng ton t chuyn vi du chm ng trc ( .' ) ( ton t chuyn v chm). Ton t ny ch khc vi ton t chuyn v ( ' ) khi cc phn t ca mng l s phc, tc l t mt vector ngun vi cc phn t l s phc, ton t ( ' ) to ra vector phc lin hp chuyn v, cn ton t ( .' ) ch to ra vector chuyn v. V d sau y s lm r iu trn: >> c = a.' % To vector c t vector a trn bng ton t chuyn v chm c= 1 2 3 4 5 >> d = a + i*a % To vector s phc d t vector a d= Columns 1 though 4 1.0000+1.0000i 2.0000+2.0000i 3.0000+3.0000i 4.0000+4.0000i Columns 5 5.0000+5.0000i >> e = d.' % To vector e t vector d bng ton t chuyn v chm ( .' ) e= 1.0000 + 1.0000i 2.0000 + 2.0000i 3.0000 + 3.0000i 4.0000 + 4.0000i 5.0000 + 5.0000i

>> f = d' f= 1.0000 2.0000 3.0000 4.0000 5.0000

% To ra vector f t vector d bng ton t chuyn v ( ' ) 1.0000i 2.0000i 3.0000i 4.0000i 5.0000i

trn ta ch xt n mng c mt hng hay mt ct by gi ta xt trng hp c nhiu hng v nhiu ct, n cn c gi l ma trn. V d sau y l ma trn g c hai hng v bn ct: >> g = [1 2 3 4;5 6 7 8] g= 1 2 3 4 5 6 7 8 Trong v d ny ta dng du cch vo cc phn t trong hng v du chm phy ( ; ) to hai hng; ngoi ra ta cng c th to ma trn nh sau: >> g = [1 2 3 4 5 6 7 8 9 10 11 12] g= 1 2 3 5 6 7 9 10 11

4 8 12

Ch : Khi nhp vo ma trn th gia cc hng s phn t phi bng nhau nu khng chng trnh s b bo li nh v d sau: >> h = [1 2 3;4 5 6 7] Numbers of elements in each row must be the same +) Php ton gia mng vi s n. Trong v d trc chng ta to mng x bng cch nhn cc phn t ca mt mng vi . Cc php ton n gin khc gia mng vi s n l php cng, php tr, php nhn, v php chia ca mng cho s bng cch thc hin php ton i vi tng phn t ca mng. V d: >> g = [1 2 3 4; 5 6 7 8; 9 10 11 12]; >> -2 % Tr cc phn t ca mng g i 2 ans= -1 0 1 2 3 4 5 6 7 8 9 10

>> 2*g - 1 ans= 1 3 9 11 17 19

% Nhn tt c cc phn t ca mng g vi 2 sau tr i 1 5 13 21 7 15 23

+) Php ton gia mng vi mng Thut ton thc hin php ton gia cc mng khng phi n gin nh trn m n cn b rng buc bi cc iu kin khc nh i vi hai mng kch c nh nhau th ta c cc php ton sau: php cng, php tr, php nhn, chia tng ng gia cc phn t ca ca hai mng. V d >> g % Gi li mng g g= 1 2 3 4 5 6 7 8 9 10 11 12 >> h = [1 1 1 1; 2 2 2 2; 3 3 3 3] % To mt mng mi h. h= 1 1 1 1 2 2 2 2 3 3 3 3 >> h + g % Cng hai ma trn g v h ( cng tng ng tng phn t ca h vi g) ans= 2 3 4 5 7 8 9 10 12 13 14 15 >> ans - h % Ly kt qu trc tr i mng h, ta c li mng g. ans= 1 2 3 4 5 6 7 8 9 10 11 12 >> 2*g - h % Nhn ma trn g vi 2 sau ly kt qu tr i ma trn h. ans= 1 3 5 7 8 10 12 14 15 17 19 21 >> g.*h % Nhn tng ng cc phn t ca mng g vi cc phn t ca mng h ans= 1 2 3 4 10 12 14 16 27 30 33 36 v d trn ta dng ton t chm_nhn ( .* ), ngoi ra MATLAB cn dng ton t chm_chia ( ./ hoc .\ ) chia tng ng cc phn t ca hai mng nh v d di y:

>> g./h % Chia phi tng ng cc phn t ca mng g vi cc phn t ca mng h ans= 1.0000 2.0000 3.0000 4.0000 2.5000 3.0000 3.5000 4.0000 3.0000 3.3333 3.6667 4.0000 >> h.\g % Chia tri tng ng cc phn t ca mng g vi cc phn t ca mng h ans= 1.0000 2.0000 3.0000 4.0000 2.5000 3.0000 3.5000 4.0000 3.0000 3.3333 3.6667 4.0000 Ch ta ch c th dng php nhn_chm hay php chia_chm i vi cc mng g v h m khng th dng php nhn ( * ) hay php chia ( / hoc \ ) v i vi cc php ton ny yu cu s ct v s hng ca hai ma trn phi tng thch. v d: >> g*h ??? Error using ==> * Inner matrix dimensions must agree. >> g/h Warning: Rank deficient, rank = 1 tol = 503291e-15. ans= 0 0 0.8333 0 0 2.1667 0 0 3.5000 >> h/g Warning: Rank dificient, rank = 2 tol = 1.8757e-14. ans= - 0.1250 0 0.1250 - 0.2500 0 0.2500 - 0.3750 0 0.3750 Php chia ma trn a ra kt qu m khng cn thit phi cng kch c nh ma trn g v ma trn h. V cc php ton i vi ma trn chng ta s ni n sau +) Mng vi lu tha. MATLAB dng ton t ( .^ ) nh ngha lu tha ca mng. V d ta c hai mng g v h nh trn, ta c th to cc mng mi bng ton t ( .^ ) nh sau: >> g.^2 % Cc phn t ca g c lu tha v s m l 2. ans= 1 4 9 16 25 36 49 64 81 100 121 144 >> g.^-1 % Cc phn t ca g c lu tha vi s m l -1. ans= 1 0.5 0.33333 0.25

0.2 0.16667 0.14286 0.125 0.11111 0.1 0.090909 0.083333 >> 2.^g % Cc phn t ca g l s m ca 2. ans= 2 4 8 16 25 36 49 64 729 1000 1331 1728 >> g.^(h - 1) % Cc phn t ca g c lu tha vi s m l tng ng ... cc phn t ca h tr i 1. ans= 1 1 1 1 5 6 7 8 81 100 121 144 Sau y l bng mt s php ton c bn ca mng:

Cc php ton i vi cc phn t ca mng D liu minh ho: a = [a1 a2 ... an] , b = [b1 b2 ... bn] , c l s v hng Cng vi s n a+c = [a1 +c a2 +c ... an+c] Nhn vi s n a*c = [a1 *c a2 *c ... an*c] Cng mng a+b = [ a1+b1 a2+b2 ... an+bn ] Nhn mng a.*b = [ a1*b1 a2*b2 ... an*bn ] Chia phi mng a./ b = [ a1/ b1 a2/ b2 ... an/ bn ] Chia tri mng a.\ b = [ a1\ b1 a2\ b2 ... an\ bn ] Lu tha mng a.^c = [ a1^c a2^c ... an^c ] c.^a = [ c^a1 c^a2 ... c^an ] a.^b = [ a1^b1 a2^b2 ... an^bn ]

6.5 Mng c cc phn t l 0 hoc 1. Bi v c nhng ng dng chung ca chng m MATLAB cung cp nhng hm to nhng mng m cc phn t ca chng l 0 hoc 1. V d: >> ones(3) % To mng 3 hng, 3 ct vi cc phn t l 1. ans= 1 1 1 1 1 1 1 1 1 >> zeros(2,5) ans= 0 0 0 % To mng 2 hng, 5 ct vi cc phn t l 0. 0 0

To mng c cc phn t l 1, kch c bng mng g bit. >> size(g) % Hm tr v kch c ca mng g. ans= 3 4 >> ones(size(g)) ans= 1 1 1 1 1 1 1 1 1 1 1 1 Khi gi hm ones(n), zeros(n) vi mt thng s n th MATLAB s to mng vung vi s hng v s ct l n. Khi gi hm vi hai thng s ones(r,c), zeos(r,c) th r l ch s hng, c l ch s ct. 6.6 Thao tc i vi mng T cc mng v cc ma trn c bn ca MATLAB, c nhiu cch thao tc i vi chng. MATLAB cung cp nhng cch tin ch chn vo, ly ra, sp sp li nhng b phn t con ca chng bng cc ch s ca cc phn t. V d di y s minh ho nhng c im thao tc i vi mng v ma trn trn: >> A = [1 A= 1 4 7 2 2 5 8 3; 4 3 6 9 % Gn phn t hng th 3, ct th 3 bng 0. 3 6 0 % Gn phn t hng th 2, ct th 6 bng 1. 3 6 0 0 0 0 0 0 0 0 1 0 5 6; 7 8 9]

>> A(3,3) = 0 1 2 4 5 7 8 >> A(2,6) = 1 A= 1 2 4 5 7 8

y ma trn A khng c 6 ct, kch c ca ma trn A phi tng ln cho ph hp, cc phn t tng thm c in bng cc con s khng. >> A(:,4) = 4 % Gn tt c cc phn t thuc ct th 4 bng 4. A= 1 2 3 4 0 0 4 5 6 4 0 1

trn ta dng du hai chm ( : ) ch tt c cc hng. >> A = [1 2 3; 4 5 6; 7 8 9]; % Gn li cc gi tr ca ma trn A. >> B = A(3:-1:1,1:3) % To ma trn B bng cch o ngc cc hng ca ma trn A. B= 7 8 9 4 5 6 1 2 3 >> B = A(3:-1:1,:) % Cng to ma trn B nh trn % nhng y ta dng ( : ) ch tt c cc ct. B= 7 8 9 4 5 6 1 2 3 >> C = [ A B(:,[1 3])] % To ma trn C bng cch ghp ma trn A v % ct th nht, th ba ca ma trn B vo bn phi ma trn A. C= 1 2 3 7 9 4 5 6 4 6 7 8 9 1 3 >> C = [1 3] C= 1 3 >> B = A(C,C) % Dng ma trn C lm ch s to ma trn B T ma trn A. B= 1 3 7 9 >> B= A(:) % To ma trn ct B t ma trn A. B= 1 4 7 2 5 8 3 6 9 >> B = B.' % Chuyn ma trn B thnh ma trn hng bng ton t chuyn v chm. B= 1 4 7 2 5 8 3 6 9 >> B = A;

>> B(:,2) = [] B= 1 3 4 6 7 9

% Loi b ct th hai ca ma trn B.

Khi ta gn ct th hai ca ma trn B cho ma trn rng ([]) th n s b xo, ma trn cn li s rt b i hng th hai. >> B = B.' B= 1 4 7 3 6 9 >> B(2,:) = [] B= 1 4 7 >> A(2,:) = B % Thay hng th hai ca ma trn A bng ma trn B. A= 1 2 3 1 4 7 7 8 9 >> B = A(:,[2 2 2 2]) B= 2 2 2 2 4 4 4 4 8 8 8 8 To ma trn B bng cch to bn ct ging ct th hai ca ma trn A, s hng vn gi nguyn bng s hng ca ma trn A. >> A(2,2) = [] ??? Indexed empty matrix assignment is not allowed. y MATLAB khng cho php xo i mt phn t ca ma trn m phi xo i mt ct hoc mt hng. >> B = A(4,:) ??? Index exeeds matrix dimension. V d trn ma trn A khng c bn hng, nn MATLAB thng bo nh trn. >> B(1:2,:) = A ??? In an assignment A(matrix, :) = B, the number of columns in A and B must be the same. MATLAB ch ra rng bn khng th gn mt ma trn vo trong mt ma trn khc m khc nhau v kch c.

>> B = [1 4 7]; >> B(3:4,:) = A(2:3,:) B= 1 4 7 0 0 0 1 4 7 7 8 9 Nhng ta c th gn hai hng ca ma trn A cho hai hng ca ma trn B, khi ma trn A v ma trn B c cng s ct. Ma trn B ch c mt hng nn khi thm hng th ba v hng th t th hng th hai ca ma trn B dc mc nh cho thm cc phn t 0 vo. >> G(1:6) = A(:,2:3) G= 2 4 8

T phn t th nht n phn t th su ca ma trn G c gn bng ct th hai v ct th ba ca ma trn A. i khi tin li hn ta ch dng ch s n truy nhp n cc phn t ca mng. Khi ch s n c dng trong MATLAB th th t cc phn t ca mng c tnh bt u t phn t u tin ca ct, tnh ht ct th tnh n ct tip theo.. V d: >> D = [1 D= 1 5 9 >> D(2) ans= 5 >> D(5) ans= 6 >> D(end) ans= 12 >> D(4:7) ans= 2 2 3 4; 5 6 7 8; 9 10 11 12]

2 3 4 6 7 8 10 11 12 % Phn t th hai ca mng. % Phn t th nm ca mng ( ct 2, hng 2 ). % Phn t cui cng ca mng. % T phn t th t n phn t th by ca ma trn. 6 10 3

Ngoi trng hp dng a ch da trn bng ch s, chng ta cn c th dng a ch da trn mng logic_l kt qu t cc php ton logic. Nu kch c ca mng logic cn bng vi mng to ra n th chnh l a ch ca mng. Trong trng hp ny th phn t True (1) dc gia li v phn t False (0) b b i V d:

>> x = -3:3 % To mng d liu. x= -3 -2 -1 0 >> abs(x)>1 ans= 1 1 0 0

Tr v mt mng logic v gi tr mt ti nhng phn t c tr tuyt i ln hn mt. >> y= y = x( abs(x)>1) -3 -2 2 3

To mng y bng cch ly nhng phn t ca x m c tr tuyt i ln hn mt. >> y = x([1 1 0 0 0 1 1]) ??? Index into matrix is negative notes on changes to logical indices or zero. See release

Cu lnh b li mc d abs(x)>1 v [1 1 0 0 0 1 1] cng l vector nh nhau. Trong trng hp ny, [1 1 0 0 0 1 1] l mt mng s, khng phi l mng logic. V vy MATLAB c nh a ch cc phn t c s ch s trong mng [1 1 0 0 0 1 1] v cu lnh b li v khng c phn t 0. Tuy nhin MATLAB cung cp hm logical chuyn i t mng s sang mng logic >> y = x(logical([1 1 y= -3 -2 2 0 3 0 0 1 1]))

mng logic lm vic vi ma trn cng nh l i v vector: >> B = [5 -3; 2 B= 5 -3 2 -4 >> x = abs(B)>2 x= 1 1 0 0 >> y = B(x) 5 -3 4 -4]

Tuy nhin kt qu c chuyn thnh vector ct v khng cch no nh ngha ma trn ch c ba phn t. a ch ca mng A( r, c ) a ch mt mng con trong mng A, nh ngha bng cc ch s vector ca hng thit k trong r, ch s vector ca ct thit k trong c. A( r, : ) a ch mt mng con trong mng A, nh ngha bnh cc ch s vector ca hng thit k trong r, v tt c cc ct ca A. A( : , c) a ch mt mng con trong mng A, nh ngha bng tt c cc hng ca A, ch s vector ca ct c thit k trong c. A( : ) a ch tt c cc phn t ca A nh mt vector ct, bng cch ghp th t cc ct ca vector A. A( i ) a ch mt mng con trong mng A, nh ngha bng cc ch s vector n c thit k trong i, vi gi s A l vector ct. A( x ) a ch mt mng con trong mng A, nh ngha bi mng logic x. x phi cng kch c vi A. 6.7 Tm kim mng con Nhiu khi chng ta mun bit cc ch s hay danh sch cc ch s ca nhng phn t ca mt mng m n tho mn mt biu thc quan h, trong MATLAB thc hin vic ta s dng hm find, hm ny tr v danh sch con ch s ti nhng phn t m biu thc quan h ca chng l ng: >> x = -3:3 x= -3 -2 -1 >> k = find(abs(x)>1) k= 1 2 6

tm nhng ch s ti nhng v tr m ti abs(x)>1 y = x(k) y= -3

-2

To mng y, dng cc ch s trong mng k. Hm find cng c th s dng trong ma trn: >> A = [1 A= 1 4 7 >> [i,j] = i= 3 3 2 2 3; 4 5 6; 7 8 9]

2 3 5 6 8 9 find(A>5)

3 j= 1 2 3 3 y i l ch s hng, cn j l ch s ct; gia i v j c mi quan h tng ng ch nhng v tr m ti biu thc quan h l ng. Ch : khi MATLAB tr li hai hoc nhiu bin, chng c t trong du nguc vung, v c t bn tri du bng. C php ny khc vi c php thao tc i vi mng trn, khi m [i,j] c t bn phi du bng, v n xy dng ln mt mng m j c kt ni vo bn phi du bng. Bng di y tm tt dng lnh ca phn tm kim mng: Tm kim mng i = find(x) Tr li cc ch s ca mng x ni m cc phn t ca n khc khng [ r, c ] = find(x) Tr li ch s hng v ch s ct ca mng x ni m cc phn t ca n khc khng. 6.8 So snh mng Chng ta c th dng hm isequal so snh hai mng. Th d: >> A = [1 2 3; 4 5 6; 7 8 9] A= 1 4 7 2 5 8 3 6 9 >> B = A.*(-1).^A B= -1 4 -7 2 -5 8 -3 6 -9 >> C = 1:9 % To mng c cng gi tr vi A nhng c khun dng khc. 1 2 3 4 5 6 7 8 9 >> isequal(A,C) ans= 0 >> isequal(A,B) ans= 0 >> isequal(A,A) ans=

1 >> isequal(C,C) ans= 0 Hm isequal tr li gi tr logic l ng (1) khi hai mng c cng kch c, cc phn t ging nhau. Ngoi ra n tr li gi tr l sai (0). Thm vo , hm ismember ch ra cc phn t ging nhau gia hai mng: >> ismember(A,B) ans= 0 1 0 1 0 1 0 1 0 >> ismember(A,B) ans= 1 1 1 1 1 1 1 1 1 % Kt qu tr v l vector ct.

ismember tr li gi tr ng cho nhng ch s trong A m phn t ny cng c trong i s th hai. Hai i s khng cn c cng kch c. >> x = 0:2:20 % mng vi 11 phn t. x= 0 2 4 6 8 10 20 >> ismember(x,A) ans= 0 1 1 1 1 0 0

12

14

16

18

y l mng c cng kch c vi x, vi 1 ti cc phn t chung. >> ismember(x,A) ans=

0 1 0 1 0 1 0 1 0 y l mng c s phn t bng s phn t ca A, vi 1 ti cc phn t chung. V vy ismember so snh i s th nht ca n vi i s th hai v tr li mt vector c cng s phn t vi i s th nht. Nhng hm to khc trong th vin MATLAB: >> union(A,B) % Tt c cc phn t c trong hai mng. ans= -9 -7 -5 -3 -1 1 2 3 4 5 6 7 8 9 >> intersect(A,B) % Phn t chung ca hai mng. ans= 2 4 6 8 >> setdiff(A,B) % Cc phn t c trong A nhng khng c trong B. ans= 1 3 5 7 9 >> setxor(A,B) % Cc phn t khng thuc phn chung gia A v B. ans= -9

-7 -5 -3 -1 1 3 5 7 9 Nhng hm ny c tng kt li trong bng di y: So snh mng isequal(A, B) ismember(A, B) intersect(A, B) setdiff(A, B) setxor(A, B) union(A, B) 6.9 Kch c ca mng phn trc chng ta bit lnh who cung cp tn bin do ngi dng nh ngha. Trong trng hp ca mng, n cn rt quan trng khi bit kch c ca mng. Trong MATLAB, lnh whos cung cp nhng thng tin ny: >> whos Name size Bytes Class A 3x3 72 double array B 1x3 24 double array ans 1x4 32 double array Grand total is 16 elements using 128 bytes ng nu A v B ging nhau. ng khi phn t ca A cng l phn t ca B. Cc phn t chung gia A v B. Cc phn t c trong A m khng c trong B. Cc phn t khng thuc phn chung gia A v B. Tt c cc phn t c trong A v B.

(logical)

Thm vo nh s v kch c ca bin, whos hin th tng s bytes chim, v class ca cc bin. V d, thng tin cp trn, ans l mng logic Trong nhng trng hp m kch c ca ma trn hoc ca vector khng c bit nhng n cn thit cho mt s cc thao tc, MATLAB cung cp hai hm ng dng l size v length : >> A = [1 2 3 >> s = size(A) s= 2 4 4; 5 6 7 8];

Vi mt thng s ra, hm size tr li mt vector hng trong c hai phn t, phn t th nht l ch s hng, cn phn t th hai ch s ct.

>> [r,c] = size(A) r= 2 c= 4 Vi hai thng s a ra, hm size tr li s hng trong bin th nht, v s ct trong bin th hai. >> r = size(A,1) r= 2 >> c = size(A,2) Gi hai thng s, hm size ch tr v s ct hoc s hng. >> length(A) ans= 4 Tr v gi tr s hng hoc s ct, gi tr no ln hn c tr v. >> B = pi:0.01:2*pi; >> size(B) ans= 1 315 Cho bit rng B l vector hng, v >> length(B) ans= 315 tr li di ca vector. >> size([ ]) ch ra rng ma trn rng khng c kch c. Nhng khi nim ny c tng kt trong bng di y: Kch c ca mng whos Hin th cc bin, m tn ti trong khng gian lm vic v kch c ca chng. s = size(A) Tr li vector hng s, m phn t th nht l s hng ca A, phn t th hai l s ct ca A. [ r, c ] = size(A) Tr li hai s v hng r, c cha s hng v s ct ca A.

r = size(A, 1) c = size(A, 2) n = length(A) 6.10 Mng nhiu chiu

Tr li s hng ca A trong bin r. Tr li s ct ca A trong bin c. Tr li max(size(A)) trong bin n khi A khng rng.

i vi cc MATLAB versions trc 5.0, mng ch c th c mt hoc hai chiu. T MATLAB 5.0 tr ln th s chiu ca mng tng ln. V d: >> a = [1 0; 0 1] a= 1 0 0 1 >> b = [2 2; 2 2] b= 2 2 2 2 >> c = [0 3; 3 0] c= 0 3 3 0 >> d = cat(3,a,b,c) d(:,:,1)= 1 0 0 1 d(:,:,2)= 2 2 2 2 d(:,:,3)= 0 3 3 0 >> size(d) ans= 2 2

To cc mng hai chiu a, b, c, sau ghp chng lai vi nhau thnh mng ba chiu bng cch s dng hm cat. Nh vy mng d l mng c hai hng, hai ct, v ba trang. Mng a to trang th nht, b l trang th hai, v c l trang th ba. Thng s trang din t chiu th ba ca mng, cung cp mt cch hnh dung v mng ba chiu nh mng hai chiu, cc trang xp th t t mt cho n cui nh trong mt quyn sch. i vi cc mng c s chiu cao hn, khng c tn chung, v n cng rt kh tng tng! Thao tc vi mng nhiu chiu cng ging nh cc th tc a ra trn i vi mng mt chiu v hai chiu. Ngoi ra MATLAB cn cung cp mt s hm thao tc trc tip i vi mng nhiu chiu: Cc hm vi mng nhiu chiu

s = size(A) Cho n_s chiu ca A, tr v vector hng s vi n phn t, phn t th i l kch c chiu th i ca mng A ndims(A) S chiu ca A, tng t nh hm length(size(A)) permute(A, order) n_s chiu, tng ng vi ton t chuyn v chm. ipermute(A, order) Ngc vi hm permute(A, order) shiftdim(A, n) Thay i s chiu ca mng A bng s nguyn n. squeeze(A) Tr li s chiu duy nht ca mng, tng ng vi tr li s chiu ln hn ba. V d: S suy gim do phn r dng mng Vn : Phn t polonium c chu k phn r l 140 ngy, c ngha l do s phn r m khi lng ca poloniun ch cn li 1/ 2 so vi khi lng ban u sau 140 ngy. Gi s ban u ta c 10 grams polonium, n s cn li bao nhiu sau mi tun trong vng mi tun? Gii php: Ta s dng phng php gii trong chng 2, khi lng cn li sau sau mt khong thi gian l: khi lng cn li = khi lng ban u . (0.5)thi gian/ chu k gii bi ton ny, gi php ca MATLAB l: >> initial_amount = 10; % Khi lng cht polonium ban u >> half_life = 140; % Chu k phn r >> time = 7:7:70 % Kt thc ca cc tun time= 7 14 21 28 35 42 49 56 63 70 >> amount_left = initial_amount*0.5.^(time/ half_life) amount_left= Columns 1 through 7 9.6594 9.3303 9.0125 8.7055 8.4090 8.1225 7.8458 Columns 8 through 10 7.5786 7.3204 7.0711 Dng ton t mng lm cho n tnh cc gi tr mt cch n gin hn khi nhn nhiu gi tr ca mt bin.Ch rng nhn chm (.^) c s dng v chng ta mun lu tha 0.5 ln i vi mi phn t ca mng. Nhng d liu ny c th d dng v chng trong MATLAB nh hnh di: >> plot(time/7,amount_left) >> xlabel(Week number), ylabel(Amount of Polonium left)
10 9. 5 Am ount of Pol ni um t o Lef 9 8. 5 8 7. 5 70 2 4 6 Wek N ber e um 8 10

Hnh 6.1 V d: Tm kim gii php s dng vectors Vn : Vn ca tun trong trng cp hai l tm mt s nh hn 100 m chia ht cho 7, nhng cn d li 1 khi chia cho 2, 3, 4, 5, v 6.

Gii php: Khng c mt gii php phn tch no cho vn ny c, v vy chng ta phi gii bng phng php tm kim. Nu bn bt u vi tt c cc s l bi s ca 7 v nh hn 1000, cn cc s khc th khng xt n, bn s xy dng c mt gii php. Trong MATLAB gii php c a ra trong script file l: function pow % pow.m script file to solve problem of the week n=7:7:1000 % all multiples of 7 less than 1000 number=length(n) % number of potential solutions n(rem(n,2)~=1)=[]; % throw out non solutions by number=length(n) n(rem(n,3)~=1)=[]; %setting them equal to an empty array, number=length(n) n(rem(n,4)~=1)=[]; % the function rem computes remainders number=length(n) n(rem(n,5)~=1)=[]; number=length(n) n(rem(n,6)~=1)=[]; Chy script file ny ta c gii php nh di y: >> pow number 142 number 71 number 24 number 12 number 2 n= = = = = =

301

721

V d: Tnh ton nng acid dng cc php ton vi mng Vn : Nh mt phn ca qu trnh sn xut b phn ca vt c ti mt nh my t ng, b phn c nhng trong nc lm ngui, sau nhng trong bn ng dung dch acid lm sch. Trong ton b ca qu trnh nng acid gim i khi cc b phn c ly ra khi bn acid v khi nhng b phn ca vt c vo bn th mt lng nc cn bm trn vt c khi nhng b trc cng vo theo v khi nhc ra khi bn mt lng acid bm theo vt. m bo cht lng th nng acid phi khng c nh hn mt lng ti thiu. Bn hy bt u vi nng dung dch l 90% th nng ti thiu phi l 50%. Lng cht lng thm vo v ly i sau mi ln nhng dao ng trong khong t 1% n 10%. Hi bao nhiu b phn c th nhng vo b nc acid trc khi nng ca n gim xung di mc cho php?

Gii php: Ta s dng phng php gii a ra chng 2: n= Trong MATLAB, gii php vit trong script M_file l: function example6_2 % script M_file example6_2 initial_con=90; min_con=50; lost=1:10 % consider 1% to 10% in increments of 1% n=floor(log(initial_con/min_con)./log(1+lost/100)) stem(lost,n) xlabel('Percent Lost with Each Dip') ylabel('Number of Dips') title('Acid-Water Bath Dipping Example') Chy chng trnh trn ta c kt qu nh sau: lost = 1 n = 59
60 50 N ber of D ps um i 40 Aci - W er Bat D pi g Exa pl d at h i n p m e 30 20 10 01 2 3 4 5 6 7 Per cnt Lost w hEachD i e t i p 8 9 10

2 29

3 19

4 14

5 12

6 10

7 8

8 7

9 6

10 6

Hnh 6.2 Ch y yu cu phng php chia chm v log(1 + lost/ 100) l mt vector

chng 7

cc php tnh vi mng

7.1 To phng trnh tuyn tnh. V c bn, MATLAB c vit i vi nhng ma trn v thc hin php ton s hc tuyn tnh n gin m xut hin trong nhiu ng dng. Mt vn chung nht ca s hc tuyn tnh l vic gii phng trnh. V d to phng trnh: . = A.x = b Biu tng php nhn ton hc (.) c nh ngha trong php ton trn, khc vi k hiu ta dng i vi mng trc kia. Trong MATLAB php nhn ma trn ny c nh ngha bng du sao (*). Tip theo nh ngha du bng, ma trn to ra t ma trn A v vector x bng vi vector b. Gii php tn ti cho s cn bng cp trn l nhng vn c bn ca s hc tuyn tnh. Thm na, khi li gii khng tn ti, c rt nhiu cch gn ng tm kim gii php, nh php loi tr Gaussian, s tm tha s LU, hoc tnh trc tip A-1 .b. Di y chng ta s cp n mt s cch gii quyt nh trn: Trc tin nhp vo ma trn A v b: >> A = [1 2 A= 1 4 7 >> b = [366; b= 366 804 351 3; 4 5 6; 7 8 0]

2 3 5 6 8 0 804; 315]

Nu bn c kin thc v s hc tuyn tnh, n rt d bn kim tra xem nh thc ca ma trn trn c khc khng hay khng: >> det(A) ans= 27

Nu n ng, MATLAB c th gii phng trnh theo hai cch, mt cch hay c dng hn, mt cch t s dng, nhng trc tip hn, phng php ny l chuyn thnh dng x=A1 .b. >> x = inv(A)*b x= 25.0000 22.0000 99.0000 y inv(A) l hm ca MAYLAB dng tnh A-1; v ton t nhn ( * ), khng c du chm pha trc, y l php nhn ma trn. Phng php c dng nhiu hn l dng ton t chia ma trn tri: >> x = A\b x= 25.0000 22.0000 99.0000 Phng trnh ny s dng phng php tm tha s LU gn ng v a ra cu tr li nh l php chia tri A cho b. Ton t chia tri ( \ ) khng c du chm pha trc l mt php ton ca ma trn, n khng phi l cc php ton gia cc phn t ca mng. Phng php th hai ny c s dng nhiu hn do nhiu nguyn nhn, mt trong nhng nguyn n gin nht l phng php ny dng t php ton hn v tc nhanh hn. Thm vo , nhn chung phng php ny chnh xc hn cho nhng bi ton ln. Trong trng hp khc, nu MATLAB khng tm thy phng php gii hoc khng tm thy phng php chinh xc, n s hin thng bo li. Nu bn nghin cu s hc tuyn tnh, bn bit rng khi s phng trnh v s bin khc nhau, th khng th c mt phng php duy nht gii. Trong MATLAB khi gp nhng h phng trnh c s phng trnh ln hn s bin n dng ton t chia tri hoc chia phi, t ng gim thp nht nhng phn t tha A.x - b. Cch ny gi l phng php vung nh nht. V d: >> A = [1 2 3; 4 5 6; 7 8 0; 2 5 8] A= 1 2 3 4 5 6 7 8 0 2 5 8 >> b = [366 804 351 514] b= 366 804 351 514 >> x = A\b % Phng php vung nh nht. % Bn phng trnh, ba bin.

x= 247.9818 -173.1091 114.9273 >> res = A*x - b res= -119.4545 11.9455 0.0000 35.8364 Mt khc khi s phng trnh t hn s bin tng t nh trng hp khng xc nh, th s nghim phng trnh l v tn. i vi nhng nghim ny MAYLAB tnh theo hai cch. Dng ton t chia a ra phng php m c s phn t 0 ca x l cc i. Nh mt s la chn, tnh x=pinv(A)*b a ra phng php chiu di hoc tiu chun ca x nh hn cc phng php khc. Phng php ny gi l phng php tiu chun cc tiu. V d: >> A = A % To ba phng trnh, bn bin. A= 1 4 7 2 2 5 8 5 3 6 0 8 >> b = b(1:3) b= 366 804 351 >> x = A\b % phng php vi s phn t 0 cc i. x= 0 -165.9000 99.0000 168.3000 >> xn = pinv(A)*b % Tm kim gii php tiu chun nh nht. xn= 30.8182 -168.9818 99.0000 159.0545 >> norm(x) % Tiu chun O_clit vi cc phn t 0. ans= 256.2200 >> norm(xn) % Gii php tiu chun nh nht ans= 254.1731

7.2 Cc hm ma trn . gii phng trnh tuyn tnh, MATLAB cung cp cc hm tr gip sau:

Cc hm ma trn balance(A) cdf2rdf(A) chol(A) cholinc(A, droptol) cond(A)

Cn bng tng chnh xc Chuyn t dng s phc cho sang dng s thc cho Tm tha s Cholesky Tha s Cholesky khng y S iu kin ma trn

condest(A) det(A) expm(A) expm1(A) expm2(A) funm(A, fun) hess(A) inv(A) logm(A) lu(A) luinc(A, droptol) norm(A) norm(A,1) norm(A, 2) norm(A, inf) norm(A, p) norm(A, fro) normest(A) null(A) orth(A) poly(A) polyvalm(A) qr(A) qrdelet(Q, R, j) qrinsert(Q, R, j, x) rank(A) rcond(A) sqrtm(A) subspace(A, B) svd(A) svds(A, K) trace(A) 7.3 Ma trn c bit

c lng s iu kin ma trn theo tiu chn 1 nh thc ma trn Ma trn theo lut m B sung M_file ca expm Ma trn theo lut hm m, dng th t Taylor Tnh ton hm ma trn chung Mu Hessenberg Ma trn chuyn v Ma trn logarithm Tm tha s vi php kh Gaussian Tha s LU khng y Ma trn v vector tiu chun Tiu chun 1 Tiu chun 2 V cng Tiu chun P (ch i vi vector) Tiu chun F Tiu chun 2 c lng cho ma trn ln Khong rng Tnh trc giao a thc c trng Tnh gi tr ca ma trn Xc nh trc giao tam gic Xo ct t tha s QR Chn ct trong tha s QR S ca hng hoc ct c lp c lng iu kin thun nghch Ma trn gc bnh phng Gc gia hai im Phn tch gi tr n Mt s cc gi tr n Tng cc phn t cho

MATLAB a ra mt s cc ma trn c bit, trong mt s chng c nhng ng dng rng ri trong cc php ton. Nhn chung nhng mtn l: >> a = [1 2 3; 4 >> b = find(a>10) b= [ ] 5 6];

y b l ma trn rng. MATLAB tr li ma trn rng khi php ton khng c kt qu. Trong v d trn khng c phn t no ca a ln hn 10. Ma trn rng khng c kch c, nhng tn bin ca chng vn tn ti trong khng gian lm vic.

>> zeros(3) % Ma trn khng 3 hng, 3 ct (3x3). ans= 0 0 0 0 0 0 0 0 0 >> ones(2,4) % Ma trn mt 2 hng, 4 ct (2x4). ans= 1 1 1 1 1 1 1 1 >> zeros(3) + pi ans= 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 v d trn v to ma trn 3x3 vi cc phn t u l . >> rand(3,1) ans= 0.2190 0.0470 0.6789 ma trn 3x1 gm cc phn t l s cung cp bi hm random gia 0 v 1. >> randn(2) ans= 1.1650 0.6268

0.0751 0.3516

ma trn 2x2 ca cc s cung cp bi hm random vi gi tr trung bnh l 0. Thut ton cho hm rand v randn c th tm thy trong S.K>Park and K.W.Miller,Random Number Generator: Good Ones Are Hard to Find, Comm. ACM, 32, 10, Oct. 19881201. >> eye(3) ans= 1 0 0

0 1 0

0 0 1

Ma trn ng nht 3x3 >> eye(3,2) ans= 1

0 0

1 0

Ma trn ng nht 3x2 Ngoi ra ch kch c ca mt ma trn, bn c th dng hm size to mt ma trn c kch c ging nh ma trn khc: >> A = [1 2 3; 4 >> ones(size(A)) ans= 1 1 1 1 5 6];

1 1

ma trn mt c cng kch c vi ma trn A. Cc ma trn trn v cc ma trn c bit khc c gii thiu trong bng sau: Cc ma trn c bit [ ] compan eye gallery hadamard hankel hilb invhilb magic t ones pascal rand randn rosser toeplitz vander wilkinson zeros

Ma trn rng To ma trn rng Ma trn ng nht Ma trn kim tra nh vi phn t Ma trn Hadamard Ma trn Hankel Ma trn Hilbert Chuyn thnh ma trn Hilbert Ma trn vung, gi tr cc phn t bng t 1 n gi tr s phn Ma trn 1 Ma trn tam gic Pascal Ma trn vi cc phn t ngu nhin t 0 n 1. Ma trn ngu nhin thng thng vi gi tr trung bnh bng 0 Ma trn kim tra i xng trc chnh Ma trn Toeplitz Ma trn Vandermond Ma trn kim tra Wilkinson Ma trn khng

V d Vn : Ta c mch in nh trong hnh 7.1 c m t bng phng trnh in p nt khi ngun a vo l sng hnh sin. =

Hnh 7.1 E = 10o; R1 = 2; L = 10j; C = ; R2 = 10. y vi l in p gia nt th i v t. Hi in p ti mi nt l bao nhiu? Gii php: y l vn v phn tch pha. Phng php gii bi ny l gii phng trnh trn, v chuyn cc kt qu v dng thi gian. Trong MATLAB gii php s l: function circuit % circuit.m script file to solve circuit proplem A(1,1)=1/2; % poke in nonzero values as needed A(1,2)=-1/2; A(2,1)=-1/2; A(2,2)=1/2 + 0.2j + 1/10j; A(2,3)= -1/10j; A(3,2)=-1/10j; A(3,3)=1/10 + 1/10j; y=[-1 0 0]'; % right hand side vector v=A\y % complex solution vmag=abs(v) % solution magnitudes vphase=angle(v)*180/pi % solution phase in degrees theta=linspace(0,2*pi); % plot results in time v1=vmag(1)*cos(theta-vphase(1)); v2=vmag(2)*cos(theta-vphase(2)); v3=vmag(3)*cos(theta-vphase(3)); thd=theta*180/pi; plot(thd,v1,thd,v2,thd,v3) Sau khi chy chng trnh trn, kt qu s l: v = -4.0000 + 6.0000i -2.0000 + 6.0000i 2.0000 + 4.0000i vmag = 7.2111 6.3246 4.4721

vphase = 123.6901 108.4349 63.4349

8 6 4 2 0 -2 -4 -6 - 80 50 100 150 200 250 300 350 400

Hnh 7.2

chng 8

cc php tnh LOGIC V QUAN H

T
hm vo nhng ton t truyn thng, MATLAB cung cp ton t logic v quan h. Bn c th quen thuc vi nhng php ton ny, nu bn lm quen vi cc ngn ng lp trnh khc. Mc ch ca nhng ton t v hm ny l tr li cu hi True_False (ng_sai). i vi cc s th trong ton t logic v quan h quy nh cc s khc khng l True cn s khng l False. Kt qu ca php ton logic v quan h a ra l 1 cho True, 0 cho False. 8.1 Ton t quan h Ton t quan h MATLAB bao gm tt c cc php so snh: Ton t quan h < <= > >= == ~= ngha nh hn nh hn hoc bng ln hn ln hn hoc bng bng khng bng

Ton t quan h MATLAB c th dng so snh hai mng c cng kch c hoc so snh mt mng vi mt s n. Trong trng hp th hai, s n so snh vi tt c cc phn t ca mng, kt qu tr v ging nh kch c ca mng. V d: >> A = 1:9, B = 9 - A A= 1 2 3 9 B= 8 7 6 0 >> tf = A>4 tf= 0 0 0 0

tm kim cc phn t ca A m ln hn 4. Kt qu bng 0 khi A 4, bng 1 khi A>4. >> tf = (A==B) tf= 0 0 0

Tm kim cc phn t ca A m bng vi B. Ch s khc nhau gia = v == dng so snh hai bin v tr v 1 khi chng bng nhau, 0 khi chng khc nhau; = dng gn kt qu a ra ca ton t cho mt bin. >> tf = B - (A>2) tf= 8 7 5

-1

Tm cc phn t A>2 v b tr bi vector B. V d ny ch ra rng kt qu a ra ca ton t logic l mt mng s bao gm cc s khng v mt, chng cng c th dng trong cc php ton s hc. >> B = B + (B==0)*eps B= Columns 1 through 7 8.0000 7.0000 6.0000 Columns 8 through 9 1.0000 0.0000

5.0000

4.0000

3.0000

2.0000

V d trn a ra cch thay th cc phn t ca B m trng vi khng bng s c bit ca MATLAB l eps, c gi tr xp x 2.2e-16. Cch thay th ny i khi c ch l trnh trng hp chia cho s khng nh v d sau: >> x = (-3:3)/3 -1.0000 -0.6667 1.0000 -0.3333 0 0.3333 0.6667

>> sin(x)./x Warning: Divide by zero ans= 0.8415 0.9276 0.9816 0.8415

NaN

0.9816

0.9276

Tnh ton hm sin(x)/ x a ra mt cnh bo v phn t th t bng khng, sin(0)/ 0 khng c nh ngha, MATLAB tr li NaN ( ngha l khng phi l mt s) ti v tr trong kt qu. Th li v d trn, sau khi thay th phn t c gi tr bng khng bng s eps: >> x = x + (x==0)*eps; >> sin(x)/x ans= 0.8415 0.9276 0.9816 0.8415

1.0000

0.9816

0.9276

By gi sin(x)/ x ti x = 0 a ra kt qu gii hn chnh xc. 8.2 Ton t Logic Ton t logic cung cp mt cch din t mi quan h ph nh hay t hp. Ton t logic MATLAB bao gm: Ton t logic & ~ ngha AND NOT

OR

Mt vi v d v dng ton t logic: >> A = 1:9; B = 9 - A; >> tf = A>4 tf= 0 0 0 0 1 1 Tm kim cc phn t ca A m ln hn 4. >> tf = ~(A>4) 1 1 1 0 0

ph nh ca kt qu, tng ng vi v tr no bng khng thay bng mt v ngc li. >> tf = (A>2)&(A<6) tf= 0 0 1 1

Tr li mt ti nhng v tr m phn t ca A ln hn 2 v nh hn 6. 8.3 Cc hm logic v hm quan h

Thm vo nhng ton t logic v ton t quan h cp n trn, MATLAB cung cp cc hm logic v quan h khc di y: Cc hm logic v hm quan h khc xor(x,y) Ton t hoc. Tr li gi tr 1 khi x hoc y khc khng (True), gi tr 0 khi c x v y cng bng khng (False) hoc cng khc khng (True) any(x) Tr li 1 nu bt c phn t no trong vector x khc khng. Tr li 1 cho mi ct trong ma trn x m c cc phn t khc khng. all(x) Tr li 1 nu tt c cc phn t ca vector x khc khng. Tr li 1 cho mi ct trong ma trn x m tt c cc phn t khc khng. MATLAB cn cung cp rt nhiu cc hm kim tra cho s tn ti ca cc gi tr c bit hoc iu kin v tr li nhng kt qu l gi tr logic. Cc hm kim tra isa(X, name) iscell(X) iscellstr(X) ischar(S) isempty(X) isequal(A, B) isfield(S, name) isfinite(X) isglobal(X) ishandle(h) ishold isiee isinf(X) isletter(S) islogical(X) ismember(A, B) isnan(X) isnumeric(X) isppc isprime(X) isreal(X) isspace(S) issparse(A) isstruct(S) isstudent isunix isvms

True nu X c lp i tng l name True nu i s l mng phn t. True nu i s l mng phn t ca cc xu. True nu i s l xu k t. True nu i s l rng. True nu A v B ging nhau. True nu namel mt trng ca cu trc S. True khi cc phn t c hn. True khi i s l bin ton cc. True khi i s l s iu khin i tng hp l. True nu th hin ti gi trng thi ON. True nu my tnh thc hin php s hc IEEE True ti nhng phn t v cng True khi cc phn t thuc bng ch ci. True khi i s l mng logic True ti nhng v tr m phn t ca A v B trng nhau True khi cc phn t l khng xc nh (NaN) True khi i s l mng s True cho Macintosh vi b x l PowerPC True khi cc phn t l s nguyn t True khi i s khng c phn o True khi cc phn t l k t trng True nu i s l ma trn Sparse True nu i s l mt cu trc True nu Student Edition ca MATLAB True nu my tnh l UNIX True nu my tnh l VMS

chng 9

VN Bn

tin ch ca MATLAB l x l vi cc con s. Tuy nhin chng ta nhiu ln cp n thao tc vi vn bn (text), nh khi a nhn v tiu vo trong th. Trong MATLAB bin text c dng n nh l xu k t, hoc n gin l cc xu. 9.1 Xu k t Xu k t trong MATLAB l mng ca cc gi tr ASCII m quy c ca n l cc k t. V d: >> t = 'How about this character t= How about this character string? >> size(t) ans= 1 32 >> whos Name Size t 1x32 Grand total is 32 elements using string?'

Bytes 64 char 64 bytes

Class array

Mt xu k t, n gin l dng vn bn, c t gia hai du nhy n. Mi k t trong xu l mt phn t ca mng, vi mi phn t chim hai bytes.

Mun xem cc m ASCII ca mt xu k t, bn phi dng cc php ton s hc i vi xu, hoc chuyn n sang dng s, dng hm double . V d: >> double(t) ans= Columns 1 through 12 72 111 119 32 97 98 111 117 116 32 116 104 Columns 12 through 24 105 115 32 99 104 97 114 97 99 116 101 114 Columns 25 through 32 32 115 116 114 105 110 103 63 >> abs(t) ans= Columns 1 through 12 72 111 119 32 97 98 111 117 116 32 116 104 Columns 13 through 24 105 115 32 99 104 97 114 97 99 116 101 114 Columns 25 through 32 32 115 116 114 105 110 103 63 Hm char chuyn li thnh xu: >> char(t) ans= How about this character string? Vi mng xu l mt mng s vi thuc tnh c bit, chng ta c th thao tc bng tt c cc cng c thao tc vi mng sn c trong MATLAB. V d: >> u = t(16:24) u= character a ch ca xu cng ging nh mng. y phn t t 16 n 24 cha t character >> u = t(24:-1:16) retcarahc y l t character c ngc li >> u = t(16:24) u= c h a

r a c t e r Dng ton t chuyn v chuyn t character sang dng ma trn ct >> v = 'I cant't find the manual!' v= I can't find the manual! Du nhy n vi xu k t l biu tng trong hai du nhy n. Chng ta c th ni hai xu nh i vi hai mng: >> w = [u,v] w= character I can t find the manual! Hm disp cho php bn hin th xu k t m khng c tn bin >> disp(v) I can't find the manual Ch l trng thi v= b b i, iu ny rt c ch cho chng ta hin th nhng li tr gip trong script file. Cng ging nh i vi ma trn, xu k t c th c nhiu hng, nhng mi mt hng phi c s ct bng nhau, cho s ct ca chng bng nhau chng ta c th dng k t trng. >> v = ['However, this' 'does work! '] v= However, this does work! >> w = ['this'; ' does not'] ??? All rows in the bracketed expression must have the same number of columns. >> size(v) ans= 2 13 Ta cng c th dng hm char to mt mng xu t cc xu, v n t thm cc k t trng to ra mt mng y . >> w = char('this', 'does not')

w= this does not >> size(w) ans= 2 9.2 Chuyn i xu

b xung thm v s chuyn i gia xu v m ASCII ca n nh trnh by trn, MATLAB a ra mt s cc hm chuyn i hu ch khc, chng bao gm di y:

Cc hm chuyn i xu base2dec bin2dec char dec2base dec2bin dec2hex double fprintf hex2dec hex2num IEEE int2str mat2str num2str sprintf sscanf str2num

Da trn xu x chuyn sang h mi. T xu nh phn sang h mi T xu sang ASCII T h mi sang xu x T s h mi sang xu nh phn T s h mi sang xu ca cc s h mi su. Chuyn t m ASCII sang xu Vit dng vn bn ra file hoc ra mn hnh Chuyn t xu gm cc s h 16 sang cc s h mi Chuyn t xu cc s h 16 sang s du phy ng Chuyn t s nguyn sang xu Chuyn t ma trn s sang xu gm cc s Chuyn t s sang xu Chuyn t m ASCII sang xu Chuyn t s sang xu c iu chnh kch thc Chuyn t xu sang s khng c iu chnh kch thc

Trong trng hp chng ta to mt thng bo c cha cc s khng phi l xu, nhng hm chuyn i s gip chng ta lm vic . >> rad = 2.5; area = pi*rad^2; >> t = ['A circle of radius ' num2str(rad)... 'has an area of ' num2str(area) '.']; >> disp(t) A circle of radius 2.5 has an area of 19.63. y hm num2str c dng chuyn t s sang xu. Ging nh vy int2str chuyn t s nguyn sang xu, c hai hm ny gi hm sprintf, n ging nh c php trong C dng chuyn s sang xu.

9.3 Cc hm v xu MATLAB a ra mt s cc hm ca xu, bao gm cc hm trong danh sch di y:

Cc hm xu blanks(n) cch deblank(s) eval(xu) eval(try, catch) feval(f, x, y, ...) findstr(s1, s2) ischar(s) isletter(s) isspace(s) lasterr lower(s) strcat(s1, s2, ...) strcmp(s1, s2) strmatch(s1, s2) strncmp(s1, s2, n) strrep(s1, s2) strtok(s) strvcat(s1, s2, ...) upper(s)

Tr li mt xu gm cc k t trng hay du Tr li cc vt trng t mt xu c lng xu nh l mt lnh ca MATLAB c lng xu v bt li Hm evaluate a ra bng xu Tm kim mt xu trong mt xu khc True nu a vo l mt xu True ti nhng v tr k t Alphabet tn ti True ti nhng v tr l k t trng Xu ca li cui cng MATLAB a ra Xu vi snhng ch ci thng Ni cc xu thnh hng True nu cc xu ging nhau Tm kim kh nng ging nhau ca xu True nu n k t u ging nhau Thay th mt xu bng mt xu khc Tm kim du hiu cho xu Ni cc xu thnh ct Chuyn thnh ch in

Mt s cc hm trn cung cp kh nng x l cc xu c bn. V d nh, findstr tr li ch s bt u ca mt xu trong mt xu khc: >> b = 'Peter Piper picked a peck of pickled peppers'; >> findstr(b, ' ') % Tm kim khong trng 6 12 19 21 26 29 37 >> findstr(b, 'p') 9 13 22 30 38 40 41 >> find(b=='p')

9 13 22 >> findstr(b, 'cow') ans= [ ] >> findstr(b,'pick') ans= 13 30

30 38 % Tm kim t cow

40

41

Hm ny tr li ma trn rng khi khng c nhng phn cn tm. >> strrep(b,'Peter','Pamela') ans= Pamela Piper picked a peck of pickled peppers Nh trnh by trn, strrep n gin ch l s thay th mt xu. strrep khng lm vic vi ma trn xu, v vy trc tin bn cn phi chuyn t ma trn thnh vector. 9.4 Ma trn t bo ca xu Ma trn t bo l mt kiu d liu cho php bn gi tn v thao tc vi mt nhm d liu c nhiu kch c v nhiu kiu. >> C = {'How';'about';'this strings?'} C= 'How' 'about' 'this for a' 'cell array of strings?' >> size(C) 4 1 for a';'cell array of

Ma trn trn c 4 hng v mt ct nhng mi ct li c di khc nhau. Tt c cc phn t c t trong du ngoc nhn, mi phn t c t trong du nhy n, gia hai hng l du chm phy. Mng t bo c nh a ch cng ging nh mng thng thng: >> C(2:) ans= 'about' 'this for a' >> C([4 3 2 1]) ans= 'cell array of strings?' 'this for a' 'about'

'How' >> C(1) ans= How y vn l mng t bo. thay i du nhy ca t bo, ta s dng nguc nhn: >> s = c{4} ans= cell array of strings? >> size(s) ans= 1 22 truy nhp vo nhiu hn mt t bo, ta dng hm deal: >> [a, b, c, d] = deal(C{:}) a= How b= about c= this for a d= cell array of trings? y C{:} ch truy nhp n tt c cc t bo, n ging nh: >> [a, b, c, d] = deal(C{1}, C{2}, C{3}, C{4}) a= How b= about c= this for a d= cell array of strings? Hm char c th dng chuyn t mng t bo sang mng xu: >> s = char(C) How about this for a cell array of strings? >> size(s) % Kt qu l cc xu vi cc khong trng. ans=

4 22 >> ss = char(C(1:2)) ss= How about >> size(ss) ans= 2 5 chuyn ngc li mng t bo, ta dng hm cellstr: >> cellstr(s) ans= 'How' 'about' 'this for a' 'cell array of strings?' Hu ht cc hm xu trong MATLAB lm vic vi c mng xu hoc mng t bo. V mng t bo s c trnh by r hn Chng 19.

chng 10

thi gian

M
ATLAB a ra mt s hm thao tc v thi gian t bn c th tnh ton vi ngy, gi, in lch v tm kin nhng ngy c th. MATLAB cha ngy v thi gian nh mt s c chnh xc hai s sau du phy tng trng cho s ngy, bt u bng nm khng. V d, mng 1 thng 1 nm 1997 ti lc na m, n c tng trng bi s 729391, v cng mt ngy nhng lc bui cha l 729391.5. Cu trc ny c th d dng cho my tnh x l, nhng n rt kh din gii. Do vy MATLAB cung cp cc hm tr gip chuyn i gia s v xu k t v thao tc vi ngy v thi gian. 10.1 Ngy v gi hin ti Hm clock tr v ngy v gi hin ti cha trong mt mng. V d: >> T = clock T= 1997 1

21

16

33

39.934708

Hm now tr v ngy v thi gian hin ti nh s ngy quy c ca my hoc n gin l s ngy. >> t = now t= 729411.690045541 C hai kt kt qu trn c cng mt thng tin. Hm date tr li ngy hin ti nh mt xu theo mu: dd-mmm-yyyy

>> date ans = 21-Jan-1997 10.2 S chuyn i gia cc kiu Bn c th chuyn s ngy ra xu, s dng hm datestr. Cu trc ca hm ny c dng nh sau: datestr(date_number,format_spec). Sau y l tr gip ca help cho hm datestr: >> help datestr DATESTR string representation of date. DATESTR(D,DATEFORM) converts a serial data number D (as returned by DATENUM) into a date string. The string is formatted according to the format number or string DATEFORM (see table below). By default, DATEFORM is 1, 16, or 0 depending on whether D contains dates, times or both. DATEFORM number DATEFORM string 0 'dd-mmm-yyyy HH:MM:SS' 1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 'dd-mmm-yyyy' 'mm/dd/yy' 'mmm' 'm' 'mm' 'mm/dd' 'dd' 'ddd' 'd' 'yyyy' 'yy' 'mmmyy' 'HH:MM:SS' 'HH:MM:SS PM' 'HH:MM' 'HH:MM PM' Example 01-Mar-1995 15:45:17 01-Mar-1995 03/01/95 Mar M 3 03/01 1 Wed W 1995 95 Mar95 15:45:17 3:45:17 PM 15:45 3:45 PM

17 18 v d vi hm datestr:

'QQ-YY' 'QQ'

Q1-96 Q1

>> datestr(t) ans= 21-Jan-1997 16: 33: 40 >> datestr(t,14) ans= 4: 33: 40 PM Hm datenum l hm ngc ca datestr. Hm ny chuyn mt xu k t dng ngy dng mu datenum(str), hoc mt s c lp hoc mt vector sang s dng ngy, dng mu datenum(year, month, day) hoc datenum(year, month, day, hour, minute, second). >> datenum('21-Jan-1997 16: 33: 40') ans= 729411.690045541 >> datenum(1997, 01, 21) ans= 729411 >> datenum(1997, 01, 21, 16, 33, 40) ans= 729411.690045541 Hm datevec chuyn mt xu k t dng ngy (dng datestr dng 0, 1, 2, 6, 13, 14, 15, hoc 16) hoc mt s dng ngy sang vector. >> c = datevec('12/ 24/ 1984') c= 1984 12 24 0 0 0 >> [yr, mo, day, hr, nim, sec] = datevec('24-Dec-1984 22') yr= 1984 mo= 12 day= 24 hr= 8 min= 22 sec=

08:

0 10.3 Cc hm v ngy Ngy ca tun c th tm t xu dng ngy hoc s dng ngy, dng hm weekday, MATLAB s dng quy c Sunday = 1 v Saturday = 7. >> [d d= 2 w= Mon >> [d w] = weekday('21-Dec-1994') d= 4 w= Wed Ngy cui thng c th tm bng hm eomday. Trong bt buc phi a vo nm, thng. >> eomday(1996, 2) ans= 29 % 1996 l nm w] = weekday(728647)

MATLAB c th to lch cho bt c thng no bn yu cu, v hin th n trong ca s lnh hoc t chng trong mt ma trn 6x7. >> calendar('7/ 17/ 95') Jul 1995 S M Tu W Th 0 0 0 0 0 2 3 4 5 6 9 10 11 12 13 16 17 18 19 20 23 24 25 26 27 30 31 0 0 0 >> S = calendar(1994, 12) S = 0 0 0 0 1 2 4 5 6 7 8 9 11 12 13 14 15 16 18 19 20 21 22 23 25 26 27 28 29 30 0 0 0 0 0 0 10.4 Cc hm v thi gian

F 0 7 14 21 28 0

S 1 8 15 22 29 0

3 10 17 24 31 0

Lnh tic v toc c th c dng i vi thi gian trong tnh ton: >> tic; plot(rand(5)); toc elapsed_time = 0.2200
1 0. 9 0. 8 0. 7 0. 6 0. 5 0. 4 0. 3 0. 2 0. 1 0 1 1. 5 2 2. 5 3 3. 5 4 4. 5 5

Hnh 10.1 >> tic; plot(rand(5)); toc elapsed_time = 0.1700


1 0. 9 0. 8 0. 7 0. 6 0. 5 0. 4 0. 3 0. 2 0. 1 01 1. 5 2 2. 5 3 3. 5 4 4. 5 5

Hnh 10.2 Ch s khc nhau v hm thi gian gia elapsed_time i vi lnh plot, lnh plot th hai nhanh hn v MATLAB to hnh dng ca s v dch cc hm cn thit vo trong nh. Hm cputime tr v tng s thi gian ca CPU (Central Processing Unit), tnh theo giy, trong thi gian MATLAB dng t khi n c khi ng ln. Hm etime tnh khong thi gian gia hai vector thi gian. Cc vector phi l vector hng gm 6 phn t, ging nh kt qu tr v trong lnh clock v datevec. Ti thi gian hin ti etime khng chuyn gia thng v nm. Tt c cc hm c th s dng tnh ton thi gian. >> t0 = cputime; pause(5); cputime - t0 ans = 5 >> t1 = clock; pause(2); etime(clock,t1) ans = 2.0400 Bn hy xem help v MATLAB CD tm hiu thm v nhng hm ny. 10.5 V th vi hm ngy v thi gian i khi n rt c ch v th trong dng xu ngy v thi gian cho mt hoc hn mt cc nhn. Hm datetick t ng vi cng vic ny. Nu th c v, dng s ngy cho mt hoc hn mt trc, th hm datetick s vit cc nhn cho im nh du. V d sau v hnh 10.3: >> t = (1900:10:1990)'; >> p = [75.995; 91.972; 105.771; 123.203; 131.669; 150.697; 179.323; 203.212; 226.505; 249.633];

>> plot(datenum(t,1,1),p) >> datetick('x','yyyy') % use 4-digit year on the x-axis >> title('Population by year') Chng ta c th to biu ct ca cng ty bn hng bn t thng 11 nm 1994 n thng 12 nm 1995 (Hnh 10.4): >> y = [1994 1994 1995*ones(1,12)]'; >> m = [11 12 (1:12)]'; >> s=[1.1 1.3 1.2 1.4 .16 1.5 1.7 1.6 1.8 1.3 1.9 1.7 1.6 1.95]'; >> bar(datenum(y,m,1),s) >> datetick('x','mmmyy') >> ylabel('$ Million') >> title('Monthly Sales')
260 240 220 200 180 160 140 120 100 80 60 1900 1920 1940 1960 1980 2000 Popul t i nby yar a o e

Hnh 10.3
M hl Sal es ont y 2

1. 5 $M i i n lo

0. 5

0 O 94 ct

Jan95

Apr 95

Jul 5 9

O 95 ct

Jan96

Hnh 10.4 V d: Tm th su ngy 13 By gi chng ta c gii thiu cc lnh v thi gian, hy dng chng to mt s hm c ch. Nu bn l ngi cn thn, bn mun bit bao gi th su ngy 13 xy ra. Hm M_file s cho bn nhng thng tin ny. function m=friday(start) % FRIDAY Date of the next Friday the 13th % FRIDAY display the next occurrence of Friday the % 13th % FRIDAY(START) start the search at the date % specified by START % M=FRIDAY return the date number of the next Friday % the 13th if nargin==0 start=now; % use the current date if none end % was supplied [yr,mo,da]=datevec(start); da=da+6-weekday(start); % Start with the Friday in % this week start=datenum(yr,mo,da,0,0,0); while 1 [yr,mo,da]=datevec(start);

if (weekday(start)==6)&(da==13) break; end start=datenum(start+7); % skip to the next Friday end if nargout==0 disp(['Friday,'datestr(start,1)]) % Display the % the result else m=start; % or return the resulting date end % number Sau khi chy chng trnh ta c kt qu: >> friday Friday,13-Aug-1999 Nu bn mun c cnh bo cho ton b nm, xem hm fridays: function F=fridays(ynum) % FRIDAY List the Friday the 13ths in the year ynum. % M=FRIDAY return the date numbers found. % if nargin==0 [ynum dummy]=datevec(now); % use the current date if end % non was supplied MM=[]; trynum=datenum(ynum,1,13,0,0,0); % check January 13 first trynum=friday(trynum); % find the first one [tyr dummy]=datevec(trynum); while tyr==ynum % May be there are more this year MM=[MM;trynum]; trynum=friday(trynum+7); % skip to the next week [tyr dummy]=datevec(trynum); end if nargout==0 disp('Fridays'); % Display the results disp(datestr(MM,1)) % Display the result else F=MM; % or return the vector of end % date number

chng 11

VNG LP IU KHIN

C
c ngn ng lp trnh v my tnh c kh nng lp trnh u cp n mt c im l cho php bn iu khin vng lp ca cc cu lnh da trn nhng cu trc ca n. Nu bn tng s dng nhng c im ny th phn ny s rt n gin i vi bn. Mt khc nu vng lp iu khin l mi i vi bn th n s rt rc ri, nu nh vy, th bn hy nghin cu n t t. Vng lp iu khin rt hu ch v c ng dng rt rng ri, n lm cho cc php ton c thc hin mt cch thun tin hn v nhanh hn. MATLAB a ra cc dng vng lp c iu khin l: vng lp for, vng lp while, cu trc if-else-end v cu trc switchcase. V cc cu trc thng hon thin cc lnh ca MATLAB, nn chng thng xut hin trong M_file, hn l trong cu lnh nh trc tip ti du nhc ca MATLAB. 11.1 Vng lp for Vng lp for cho php mt nhm lnh thc hin lp li mt s ln c nh. C php ca vng lp for nh sau: for end Cc cu lnh gia hai trng thi for v end c thc hin mt ln cho tt c cc ct ca mng (array). Ti mi ln lp li, x c gn cho phn t ct tip theo nh trong sut n ln ca vng lp, x = array(:, n). V d: >> for n = 1:10 x = array commands % Khi cc lnh

x(n) = sin(n*pi/10); end >> x x= Columns 1 through 7 0.3090 0.5878 0.8090 0.9511 1.0000 0.9511 0.8090

Columns 8 through 10 0.5878 0.3090 0.0000

Ni mt cch khc, trng thi th nht yu cu: Cho n bng t 1 n 10, tnh gi tr ca tt c cc trng thi cho n trng thi k tip trng thi end. u tin trong vng lp for n=1, tip theo n=2, v c nh vy cho n trng hp n=10. Sau trng hp n=10, vng lp for kt thc, v tt c cc lnh sau trng thi end ca vng lp c thc hin. Vng lp for khng th b kt thc bng cch gn li bin iu khin n trong vng lp: >> for n = 1:10 x(n) = sin(n*pi/10); n = 10; end >> x x= Columns 1 through 7 0.3090 0.5878 0.8090 Columns 8 through 10 0.5878 0.3090 0.0000

0.9511

1.0000

0.9511

0.8090

Trng thi 1:10 l mt trng thi to ln mng MATLAB tiu chun. Bt c kiu mng no ca MATLAB u c chp nhn trong vng lp for: >> data = [3 9 45 6; 7 16 -1 5] data = 3 9 45 6 7 16 -1 5 >> for n = data x = n(1)-n(2) end x = -4 x = -7 x = 46 x = 1

Bnh thng vng lp for c th lng vo nhau: >> for n = 1:5 for m = 5:-1:1 A(n,m) = n^2+m^2; end disp(n) end 1 2 3 4 5 >> A A = 2 5 10 17 26 5 8 13 20 29 10 13 18 25 34 17 20 25 32 41 26 29 34 41 50 Khng nn dng vng lp for khi m tng ng vi vic ta dng mng tnh ton. Nh trong v d trc ta cng c th dng mng tnh ton: >> n = 1: 10; >> x = sin(n*pi/10) x = Columns 1 through 7 0.3090 0.5878 0.8090 0.8090 Columns 8 through 10 0.5878 0.3090 0.0000

0.9511

1.0000

0.9511

Trong hai trng hp nh trn, trng hp th hai ta dng mng tnh ton cng c kt qu nh vy, nhng n nhanh hn v cc tho tc cng t hn. tng tc tnh ton, mng cn phi c khi to trc khi thc hin vng lp for (hoc vng lp while). Trong v d trc c mi ln lnh trong vng lp for c tnh, kch c ca bin x li tng ln 1. iu ny lm cho MATLAB mt thi gian cp nht thm b nh cho x trong mi vng. rt ngn bc ny, v d v vng lp for trc vit li nh sau: >> x = zeros(1,10); % Khi to b nh cho x >> for n = 1: 10 x = sin(n*pi/10); end By gi ch cn thay i gi tr ca cc phn t ca x.

11.2 Vng lp while Vng lp while thc hin lp li mt nhm lnh mt s ln c nh, nhng khng bit trc c s ln lp li. C php ca vng lp while nh sau: while biu thc iu kin khi cc lnh.. end khi cc lnh.. gia hai trng thi while v end c thc hin lp i lp li khi tt c cc biu thc iu kin l ng. Thng thng gi tr ca iu kin a ra kt qu l mt s, nhng nu cc kt qu a ra l mt mng th vn hp l. Trong trng hp mng, tt c cc phn t trong mng kt qu a ra phi l True (ng). C th tham kho v d di y: >> num = 0; ESP = >> while (1+ESP) ESP num end >> num num= 53 >> ESP = 2*ESP ESP= 2.2204e-16 1; > 1 = ESP/ 2; = num + 1;

V d ny a ra cch tnh gi tr c bit eps ca MATLAB, n l mt s dng nh nht, c th cng vi 1 c mt s ln hn 1 dng cho gii hn chnh xc. y chng ta dng ch hoa EPS chc chn rng gi tr eps ca MATLAB khng ghi ln. Trong v d ny, gi tr ca EPS bt u bng 1, trong khi iu kin (1+EPS)>1 l True ( cho n khc khng), cc lnh trong vng lp while c tnh, gi tr ca EPS tip tc c chia i, gi tr ca EPS nh i, m cng EPS vi 1 th n l s nh nht m ln hn 1. Do my tnh s dng s c nh c 16 ch s nn khi gi tr nh qu th n lm trn bng 0, v khi iu kin (EPS+1)> 1 False (sai) v vng lp while dng li. Cui cng EPS c nhn vi 2 v sau ln chia cui cng cho 2 th vng lp dng li. 11.3 Cu trc if-else-end Nhiu khi chng ta cn nhng cu lnh c thc hin theo mt iu kin no . Trong ngn ng lp trnh, logic ny c cung cp bi cu trc if-else-end. C php ca cu trc ny nh sau: if biu thc iu kin khi cc lnh... end

Khi cc lnh gia hai trng thi if v end c thc hin khi tt biu thc iu kin l ng. Trong trng hp iu kin bao gm cc iu kin con, th tt c cc iu kin con c tnh v tr v mt trng thi logic ca iu kin. V d: >> apple = 10 % s to >> cost = apple*25 cost= 250 >> if apple > 5 cost = (1-20/100)*cost; end >> cost cost 200

% b i 20%

Trong trng hp c hai iu kin thay i, cu trc if-else-end l: biu thc iu kin khi cc lnh c thc hin nu iu kin l ng else khi cc lnh c thc hin nu iu kin l sai end if Khi c ba hoc nhiu iu kin thay i, cu trc ca n s l: if biu thc iu kin 1 khi cc lnh c thc hin nu iu kin 1 l ng elseif biu thc iu kin 2 khi cc lnh c thc hin nu iu kin 2 l ng elseif biu thc iu kin 3 khi cc lnh c thc hin nu iu kin 3 l ng elseif biu thc iu kin 4 . . . else khi cc lnh c thc hin nu khng c iu kin no ng. end Trong mu dng ny th khi biu thc iu kin u tin ng th cc cu lnh sau khng c kim tra na, cc cu trc if-else-end cn li c b qua. Hn na cu lnh else cui c th khng cn cho vo. i vi cu trc if-else-end, chng ta cng c th lng vo cc vng lp for v while: >> EPS = 1; >> for num = 1:100

EPS = EPS/ 2; if (1+EPS)< 1 EPS = EPS*2 break end end EPS = 2.2204e-16 >> num num= 53 V d ny a ra cch khc tnh s eps. Trong v d, khi lnh break c thc hin th MATLAB nhy ra khi vng lp n ang thc hin. Khi lnh break xut hin trong mt vng lp for hoc while trong cc vng lp nng nhau th n ch nhy ra khi mt vng lp cha n ch n khng nhy ra khi tt c cc vng lp. 11.4 Cu trc switch-case Khi mt chui cc lnh nh gi da trn mt biu thc th hoc biu thc iu kin vi nhiu gi tr th khc nhau, ngi ta thng dng cu trc switch-case. Cu trc switch-case c dng nh sau: switch biu thc iu kin case gi tr th 1 khi lnh 1 case { gi tr th 2, gi tr th 3, gi tr th 4} khi lnh 2 otherwise khi lnh 3 end y biu thc iu kin phi l dng s hoc dng chui, nu biu thc iu kin l dng s th lnh case s th xem gi tr ca biu thc c bng gi tr th i hay khng. Nu biu thc iu kin l mt chui th lnh case s so snh chui vi gi tr th i. Trong v d trc, biu thc iu kin c em so snh vi gi tr th 1, nu chng bng nhau th khi lnh u tin c thc hin, m cc khi lnh tip theo cho n trc trng thi end dc b qua, nu chng khng bng nhau th iu kin tip tc c em so snh vi gi tr th 2, gi tr th 3, gi tr th 4, nu mt trong cc gi tr ny bng biu thc iu kin th khi lnh 2 c thc hin. Nu tt c cc lnh so snh ca case u khng ng th khi lnh 3 c thc hin. Ch rng trong cu trc switch-case c it nht mt nhm lnh phi c thc hin. Sau y l mt v d v cu trc switch-case: x = 2.7; units = 'm'; switch units % Chuyn x ra centimeters case {'inch','in'}

y=x*2.54; {'feet','ft'} y=x*2.54*12; case {'meter','m'} y=x/ 100; case {'millimeter','mm'} y=x*10; case {'centimeter','cm'} y=x; otherwise disp(['khng bit units: ' units]) y=nan; case end Khi thc hin v d ny th gi tr cui cng ca y l: y=0.027. V d: Vn v li xut Vn : mua mt t, bn phi vay 10,000$ vi li xut hng thng l 8.9%, trong 3 nm gc v li c tnh nh th no sau mi ln chi tr. Ngoi ra phn tin cn li sau mi ln chi tr l bao nhiu? Gii php: T chng 2, s tin chi tr P hng thng cho khon vay A dollar vi li xut hng thng l R, tnh trong M thng l: P = A. Ti ln chi tr u tin, tin li phi tr l Ip1= R.A. Gi s s tin phi tr l P th tin gc phi tr l Pr1= P - Ip1 v s tin cn li sau ln chi tr th nht l B1=A - Pr1. Trong tt c cc ln chi tr sau tin li phi tr l Ipm= R.Bm-1 v s tin cn li l Bm= Bm-1 - Prm. S dng cc thng tin ny th chng trnh MATLAB s nh sau: function amort % amort.m script file A=10000; % amount of loan M=3*12; % number of months R=8.9; % annual interest rate r=(R/100)/12; % monthly interest rate P=A*(r*(1+r)^M/((1+r)^M-1)); % payment required B=zeros(M,1); %storage for balance remaining per month Ip=B; % storage for interest paid per month Pr=B; % storage for principle paid per month for m=1:M if m==1 % compute interest when balance is Ip(m)=r*A; % original amount else Ip(m)=r*B(m-1); end Pr(m)=P-Ip(m); % principle paid this month

if m==1 % compute balance remaining after payment B(m)=A-Pr(m); else B(m)=B(m-1)-Pr(m); end end format bank disp(['Amount=' num2str(A)]) disp(['Interest Rate=' num2str(R)]) disp(['Number of months = ' num2str(M)]) disp(['Payment =' num2str(P)]) disp(' ') disp(' Amortization Schedule') disp(' Payment Balance Interest Principle') disp([(1:M)' B Ip Pr]) format short g Chy chng trnh ny th kt qu nh sau: >> Amount=10000 Interest Rate=8.9 Number of months = 36 Payment =317.5321 Amortization Schedule Balance Interest 9756.63 74.17 9511.46 72.36 9264.48 70.54 9015.65 68.71 8764.99 66.87 8512.46 65.01 8258.07 63.13 8001.78 61.25 7743.60 59.35 7483.49 57.43 7221.47 55.50 6957.49 53.56 6691.56 51.60 6423.66 49.63 6153.77 47.64 5881.88 45.64 5607.97 43.62 5332.03 41.59 5054.04 39.55 4773.99 37.48 4491.87 35.41

Payment 1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.00 10.00 11.00 12.00 13.00 14.00 15.00 16.00 17.00 18.00 19.00 20.00 21.00

Principle 243.37 245.17 246.99 248.82 250.67 252.53 254.40 256.28 258.19 260.10 262.03 263.97 265.93 267.90 269.89 271.89 273.91 275.94 277.99 280.05 282.12

22.00 23.00 24.00 25.00 26.00 27.00 28.00 29.00 30.00 31.00 32.00 33.00 34.00 35.00 36.00

4207.65 3921.33 3632.88 3342.29 3049.55 2754.63 2457.53 2158.22 1856.70 1552.94 1246.92 938.64 628.07 315.19 -0.00

33.31 31.21 29.08 26.94 24.79 22.62 20.43 18.23 16.01 13.77 11.52 9.25 6.96 4.66 2.34

284.22 286.33 288.45 290.59 292.74 294.91 297.10 299.31 301.53 303.76 306.01 308.28 310.57 312.87 315.19

V d ny minh ho cu trc lp for v if-else-end. N cng minh ho vic s dng script M_file. tnh ton mt khon cho vay bt k bn ch cn thay i d liu vo phn u ca chng trnh v bn chy li n. V d: Chui ln xung Vn : cho x0 l mt s nguyn bt k. Gi s chui xk c nh ngha nh sau: v xk+1 = xk/ 2 nu xk l chn xk+1 = 3xk+ 1 nu xk l l

Chui ny c thuc tnh g nu chui s dng li khi xk =1, chui phn k hay hi t v 1. Gii php: Chng ta ch cn vng lp while xt xem khi no xk= 1 v s dng cu trc if-else-end thc hin vic tnh ton dy xk. Trong MATLAB th chng trnh nh sau: function up_down % up_down.m script file for up/down sequence proplem x=zeros(500,1); %preallocate storage for x(k) x(1)=round(abs(input('Enter a number> '))); k=1; while (x(k)>1)&(k<500) if rem(x(k),2)==0 % x(k) is even x(k+1)=x(k)/2; else % x(k) is old x(k+1)=3*x(k)+1; end k=k+1; % increment sequence counter end x=x(x>0) % keep values generated only and dispay them M=0:499; plot(M,x)

Kt qu ca chng trnh ny kh th v, v d vi x=2m , trong m l mt s nguyn th chui s rt ngn (ti sao?), hn na bt c khi no gi tr ca mt s hng trong chui l lu tha ca 2 th chui s nhanh chng dng li, nhng i vi nhng s x tng i nh th kt qu l mt chui kh th v. V d x1=27. Hu nh tt c cc gi tr ban u u sinh ra mt chui c gi tr rt ngu nhin nh hnh v di y vi x(1)=837799. Liu bn c dm kt lun chui ny hi t hay khng! th kt qu ca chng trnh vi x(1)=837799 l:

3x10 2. 5 2

1. 5 1 0. 5 00

100

200

300

400

500

Hnh 11.1

chng 12

HM M_FILE

hi bn s dng cc hm MATLAB nh inv, abs, angle, v sqrt, MATLAB nhn gi tr m bn truyn vo, da vo kt qu , tnh ton kt qu ca hm v tr li cho bn kt qu tnh ton. Cc lnh tnh ton bng hm cng nh cc bin trung gian c to ra bi cc lnh ny bn u khng nhn thy, tt c nhng g bn trng thy ch l cc gi tr nhp vo v cc gi tr a ra, v vy c th coi mt hm nh mt ci hp en. Cc thuc tnh ny lm cho hm tr ln rt hu dng i vi cc lnh tnh ton m phi dng n cc hm ton hc phc tp thng xut hin khi bn gii quyt nhng vn ln. Da vo u im ny, MATLAB cung cp mt cu trc bn c th t to mt hm cho mnh di dng mt M_file. Hm flipup di y l mt v d v vic dng hm M_file: function y=flipup(x) % FLIPUP Flip matrix in up/down directiopn. % FLIPUP(x) return x with columns preserved and rows flipped % in the up/down direction. For example. % % x = 1 4 becomes 3 6 % 2 5 2 5 % 3 6 1 4 % % See also FLIPLR, ROT90, FLIPDIM. % Copyright (c) 1984-96 by the MathWork, Inc. % $Revision: 5.3 $ $Date: 1996/10/24 18: 41: 14 $ if ndim(x)~=2 error( X must be a 2-D matrix.);

end [m, n] = size(x); y = x(m: -1: 1, :); Mt hm M_file c v rt ging vi mt script file bi v chng cng l cc file vn bn v cng c phn m rng l .m. im khc nhau gia script file v cc hm M_file l cc hm M_file khng c nhp vo t ca s lnh m thng qua mt trnh son tho vn bn t bn ngoi. Hm M_file cn khc vi script file ch n ch thng tin vi MATLAB thng qua cc bin truyn vo cho n v thng qua cc bin ra m n to ln, cc bin trung gian bn trong hm th khng xut hin hay tng tc vi mi trng ca MATLAB. Nh bn c th thy v d trc, dng u tin ca hn M_file nh ngha file ny nh mt hm v ch ra tn ca n, tn ny chnh l tn file nhng khng c phn m rng l .m ng thi n cng nh ngha lun bin vo v ra. Chui cc dng lnh tip theo l cc li ch thch, s xut hin khi ta dng lnh >>help, >>help flipud, hoc >>helpwinflipud dng lnh help u tin gi l dng H1 chnh l dng hin ra khi dng lnh lookfor. Cui cng phn cn li ca file ny cha cc lnh ca MATLAB to ln cc bin ra. 12.1 Cc quy lut v thuc tnh Hm M_file phi tun theo nhng quy lut v thuc tnh nht nh, ngoi ra chng cn c mt s tnh cht rt quan trng bao gm: *) Tn hm v tn file phi l mt, v d hm flipud phi c lu trong file vi ci tn l flipud.m. *) Ln u tin MATLAB thc hin hm M_file n s m file vn bn tng ng v dch cc dng lnh ca file ra mt dng m lu trong b nh nhm mc ch tng tc thc hin cc li gi hm tip theo. Nu trong hm c cha li gi hm M_file khc th cc hm cng c dch vo trong b nh. *) Cc dng ghi li ch thch cho ti dng u tin khng phi l ch thch trong hm M_file l nhng dng vn bn, n s hin ra khi bn s dng lnh help. V d: >>help flipud s tr v 9 dng u tin trong hm M_file ni trn. Dng u tin l dng H1, n s xut hin khi bn dng ln look for. *) Mi hm c mt khng gian lm vic ring tch bit so vi mi trng MATLAB, mi quan h duy nht gia cc bin trong hm vi mi trng MATLAB l cc bin vo v ra ca hm . Nu trong thn hm gi tr b thay i th s thay i ny ch tc ng bn trong ca hm m khng lm nh hng n cc bin ca mi trng MATLAB. Cc bin c to ra bn trong mt hm th ch nm trong khng gian lm vic ca hm v c gii phng khi hm kt thc, v vy khng th s dng thng tin ca ln gi trc cho ln gi sau. *) S cc tham s vo v ra khi mt hm c gi th ch c tc dng bn trong hm , bin nargin cha cc tham s a vo cn bin nargout cha cc gi tr a ra, trong thc t th cc bin ny thng c s dng xc nh gi tr ra da vo s lng cc i s a vo. V d xt hm linespace sau: function y=linespace(d1, d2, n) % LINESPACE Linearly spaced vector. % LINESPACE(x1, x2) generates a row vector of 100 linearly

% equally spaced points betwin x1 and x2. % % LINESPACE(x1, x2, N) generates N points betwin x1 and x2. % % See also LOGSPACE, :. % Copyright (c) 1984-96 by the MathWork, Inc. % $Revision: 5.3 $ $Date: 1996/10/24 18: 41: 14 $ if nargin==2 n = 100; end y = [d1 + (0: n-2)*(d2-d1)/ (n-1) d2]; y nu li gi ca ngi s dng ch truyn vo hai i s th linespace tr v gi tr 100, nhng nu s i s l 3, v d nh linespace(0,10,50) th i s th 3 s quyt nh s cc im d liu. *) Cc hm c th dng chung cc bin vi hm khc, vi mi trng MATLAB v c th quy nu nh cc bin c khai bo l ton cc. c th truy cp n cc bin trong mt hm hoc trong mi trng MATLAB th cc bin phi c khai bo l bin ton cc trong mi hm s dng n. Hm tic v toc sau y m t mt v d v vic s dng bin ton cc: function tic % TIC Start a stopwatch timer. % The sequence of lnhs % TIC, operation, TOC % prints the time required for the operation. % % See also TOC, CLOCK, ETIME, CPUTIME. % Copyright (c) 1984-96 by the MathWork, Inc. % $Revision: 5.3 $ $Date: 1996/10/24 18: 41: 14 $ % TIC simple stores CLOCK in a global variable global TICTOC TICTOC = clock; function t = toc % TOC Read the stopwatch timer. % TOC, by itself, prints the elapsed time in t, % instead of printing it out. % % See also TIC, ETIME, CLOCK, CPUTIME. % Copyright (c) 1984-96 by the MathWork, Inc. % $Revision: 5.3 $ $Date: 1996/10/24 18: 41: 14 $ % TOC uses ETIME and the value of clock saved by TIC.

global TICTOC if nargout< 1 elapsed_time = etime(clock, TICTOC); else t = etime(clock, TICTOC); end Trong hm tic th bin TICTOC c khai bo l bin ton cc v gi tr ca bin ny c c thng qua vic gi hm clock. Sau trong hm toc, bin TICTOC cng c khai bo l bin ton cc lm cho toc c kh nng truy cp n bin TICTOC trong hm tic, s dng gi tr ca bin ny toc s tnh c khong thi gian tri qua k t khi hm tic c thi hnh. Mt iu quan trng cn nh l bin TICTOC ch tn ti trong khng gian lm vic ca tic v toc nhng khng tn ti trong mi trng MATLAB. *) Vic thi hnh hm M_file s kt thc khi gp dng cui cng ca file hoc gp dng lnh return. Lnh return gip ta kt thc mt hm m khng cn phi thi hnh ht cc lnh ca hm . *) Hm error ca MATLAB s hin th mt chui ln ca s lnh v dng thc hin hm, tr iu khin v cho ca s lnh v bn phm. Hm ny rt hu dng cnh bo vic s dng hm khng ng mc ch. V d nh cu lnh sau: if end y nu val khng phi l s th hm error s hin ln chui cnh bo v tr iu khin cho ca s lnh v bn phm. *) Mt M_file c th cha nhiu hm. Hm chnh trng M_file ny phi c t tn trng vi tn ca M_file nh cp n trn. Cc hm khc c khai bo thng qua cu lnh function c vit sau hm u tin. Cc hm con ch c s dng bi hm chnh, c ngha l ngoi hm chnh ra th khng c hm no khc c th gi c chng. Tnh nng ny cung cp mt gii php hu hiu gii quyt tng phn ca hm chnh mt cch ring r lm gim bt cc kh khn khi ta lp trnh mt hm ln. Ni tm li, hm M_file cung cp cho ta mt phng php n gin m rng kh nng ca MATLAB. Trong thc t rt nhiu hm ca MATLAB l cc hm M_file. V d: Hm tr dn theo thi hn Vn : Gi s c mt khon cho vay A dollar, vi li sut hng thng l R% v phi tr trong vng M thng. Hy vit mt hm M_file th hin: - Lch chi tr nu nh ban u cha bit cc s liu a ra. - S tin chi tr hng thng nu bit mt s liu ra. - S tin chi tr hng thng v mt ma trn s cha lch thanh ton nu bit trc hai i s ra. Gii php: Trong chng 2, s tin phi chi tr hng thng P cho khon cho vay A dollar vi t gi li xut l R, tr trong M thng: P = A. length(val) > 1 error(VAL phi l gi tr s!)

Ti ln chi tr u tin, tin li phi tr l Ip1= R.A. Gi s s tin phi tr l P th tin gc phi tr l Pr1= P - Ip1 v s tin cn li sau ln chi tr th nht l B1=A - Pr1 . Trong tt c cc ln chi tr sau tin li phi tr l Ipm= R.Bm-1 v s tin cn li l Bm= Bm-1 - Prm. S dng cc thng tin ny th chng trnh MATLAB s nh sau: function [P,S]=loan(a,r,m) %LOAN Loan Payment and Amortization Table. % (H1 help line) %P=LOAN(A,R,M) computes the monthly payment on a loan %amount of a, having an annual intereat rate of R, % to be paid off in equal amounts over M months. % %[P,S]=LOAN(A,R,M) also returns % an amortization table S, %which is an M-by-4 matrix % where S(:,1)=Payment Number, %S(:,2)=Remaining Balance, S(:,3)=Interest Paid, and %S(:,4)=Principle Paid. % %If no output arguments are provided % the table is displayed. %Start with some error checking if nargin<3 error('Three input argument are required.') end if fix(m)~=m error('Number of Months Must be Integer.') end % Now calculate rm=(r/100)/12; % Monthly interest rate p=a*(rm*(1+rm)^m/((1+rm)^m-1)); % payment required if nargout==1 % done if only payment is required. P=p; % copy out into output variable return end B=zeros(m,1); % storage for balance remaining per month Ip=B; % storage for interest paid per month Pr=B; % storage for principal paid per month for i=1:m % creat table data if i==1 % compute interest when balance is orginnal amout Ip(i)=rm*a; else % balance is B(i-1) Ip(i)=rm*B(i-1);

end Pr(i)=p-Ip(i); %principal paid this month if i==1 % compute balance remainig after payment B(i)=a-Pr(i); else B(i)=B(i-1)-Pr(i); end end B(abs(B)<0.001)=0; % set near zero balance to zero s=[(1:m)' B Ip Pr]; if nargout==0 % display table disp(['Amount = ' num2str(a)]) disp(['Interest rate = ' num2str(r)]) disp(['Number of month = ' int2str(m)]) disp(['Payment = ' num2str(p)]) disp(' ') disp(' Amortization Schedule') disp(' Payment Balance Interest Principle') fprintf(' %5.0f %12.2f %12.2f %12.2f\n', s') % better formatting else % two output arguments requested P=p; S=s; end V d: Gii m mu trn cc in tr Vn : Gi tr ca mt in tr dng trong mch in c tnh thng qua cc vch mu in trn thn ca n. i vi mt in tr vi chnh xc l 5% th c 3 di mu, tm gi l A, B, C. Gi tr s c gn cho mi mu c tnh nh sau: Mu Gi tr tng ng en Nu Da cam Vng Xanh l cy Xanh da tri Tm Xm Trng 789 0123456

Nu A, B, C l cc gi tr ca cc mu trn gii mu th gi tr ca cc in tr l: R = (10.A + B).10C S dng cc thng tin ny, hy to mt M_file tr v gi tr ca in tr ng vi bt k mt in tr chun no. Gii php: Vn ny yu cu mt chui cc thao tc v so snh thc hin s chuyn i trong bng trn. Gii php ca MATLAB l: function r=resistor(a, b, c) %RESISTOR(A, B, C) Resistor value from color code. %RESISTOR(a, B, C) returns the resistace %value of resistor %given its three color bands, A, B, C.

%A, B, C must be one of the %following character strings: % %'black', 'brown', 'red', 'orange', 'yellow', %'green', 'blue', 'violet', 'gray', 'white' % first some error checking if nargin~=3 error('Three input arguments required') end if ~ischar(a)|~ischar(b)|~ischar(c) error('Inputs Must be Character Xus') end %now solve problem vals=zeros(1,3); % string cell array of three inputs abc={a,b,c}; % tring cell aray thr input for i=1:3 %do each color band in turn band=lower(abc(i)); %get (i)th input and make lower case if strncmp(band,'bla',3) % black (compare min # of) vals(i)=0; % chars for unique match) elseif strncmp(band,'br',2) %brown vals(i)=1; elseif strncmp(band,'r',1) %red vals(i)=2; elseif strncmp(band,'o',1) %orange vals(i)=3; elseif strncmp(band,'y',1) %yellow vals(i)=4; elseif strncmp(band,'gre',3) %green vals(i)=5; elseif strncmp(band,'blu',3) %blue vals(i)=6; elseif strncmp(band,'v',1) %violet vals(i)=7; elseif strncmp(band,'gra',3) %gray vals(i)=8; elseif strncmp(band,'w',1) %white vals(i)=9; else error(['Unknown Color Band.']) end end if vals(1)==0 error('First Color Band Cannot Be Black.')

end r=(10*vals(1)+vals(2))*10^vals(3); S dng hm ny cho mt vi v d: >> resistor('brown', 'black', 'red') ans= 1000

chng 13

PHN TCH D LIU

B
i v MATLAB l mt ng dng hng ma trn nn n d dng thc hin cc phn tch thng k trn cc tp d liu, trong khi theo mc nh MATLAB coi cc tp d liu c lu tr trong cc mng ct, vic phn tch d liu c th thc hin theo bt c chiu no. l tr khi c ch nh theo mt cch khc, cc ct ca mt mng d liu th hin cc thng s o khc nhau, mi hng th hin mt gi tr mu ca cc thng s o . V d gi s nhit ban ngy (tnh theo C) ca 3 thnh ph tnh trong mt thng (31 ngy c ghi li v gn cho mt bin l temps trong mt script M_file, khi chy M_file th gi tr ca temps c a vo mi trng MATLAB, thc hin cng vic ny, bin temps cha: >> temps temps= 12 15 12 14 12 11 15 8 19 12 14 11 9 8

8 9 5 8 6 9 9 10 7 7 10 8 7 8

18 22 19 23 22 19 15 20 18 18 19 17 23 19

15 8 10 12 9 12 12 10 13 9 10 14 12 13 15 13 12

8 10 7 7 8 8 8 9 12 10 6 7 5 7 10 11 12

18 20 17 22 19 21 20 17 18 20 22 21 22 18 23 24 22

Mi hng cha nhit ca mt ngy no , cn mi ct cha nhit ca mt thnh ph. cho d liu tr ln d dng hn, hy g vo nh sau: >> >> >> >> >> d=1:31; % number the days of the month plot(d,temps) xlabel('Day of month') ylabel('Celsius') title('Daily High Tempratures in three Cities')

24 22 20 18 C si us el 16 14 12 10 8 6 40 5

D l yH g Tem at r esi t r eeC t s ai i h pr u n h ii e

10

15 20 D of m onh ay t

25

30

35

Hnh 13.1 Lnh plot va dng trn y minh ho thm mt cch s dng. Bin d l mt vector di 31, trong khi bin temps l mt ma trn 31x3. Cho trc nhng d liu ny, lnh plot s trc mi ct ca bin temps cho vo d. minh ho mt vi kh nng phn tch d liu ca MATLAB, hy xt cc lnh sau, da trn d liu v nhit cho: >> avg_temp = mean(temps) avg_temp= 11.9677 8.2258

19.8710

V d trn ch ra rng thnh ph th 3 l c nhit trung bnh cao nht, y MATLAB tnh nhit trung bnh ca mi ct mt cch ring r. Nu tnh trung bnh c 3 thnh ph th:

>> avg_avg = mean(avg_temp) avg_avg= 13.3548 Khi m cc gi tr u vo trong mt hm phn tch d liu l mt vector hng hay ct th MATLAB ch n gin l tin hnh cc php ton trn vector v tr v gi tr s. Bn cng c th dng mng thc hin cng vic ny: >> avg_temp = mean(temps,1) % Ging nh trn, tnh cho cc ct avg_temp = 11.9677 8.2258 19.8710 >> avr_tempr = mean(temps,2) % Tnh cho mi hng avr_tempr = 12.6667 15.3333 12.0000 15.0000 13.3333 13.0000 13.0000 12.6667 14.6667 12.3333 14.3333 12.0000 13.0000 11.6667 13.6667 12.3333 11.3333 13.6667 12.0000 13.6667 13.3333 12.0000 14.3333 13.0000 12.6667 14.0000 13.0000 12.6667 16.0000 16.0000 15.3333 y l gi tr nhit trung bnh c ba thnh ph trong tng ngy.

Xt bi ton tm s chnh lch nhit ca mi thnh ph so vi gi tr trung bnh, c ngha l avg_temp(i) phi b tr i bi ct th i ca bin temps. Bn khng th ra mt cu lnh nh sau: >> temps-avg_temp ??? Error using ==> Matrix dimensions must agree. Bi v thao tc ny khng phi l cc thao tc nh ngha trn mng (temps l mt mng 31x3, cn avg_temp l mt mng 1x3). C l cch dng vng lp for l n gin nht: >> for i = 1:3 tdev(:,i) = temps(:,i)- avg_temp(i); end >> tdev tdev = 0.0323 -0.2258 -1.8710 3.0323 0.7742 2.1290 0.0323 -3.2258 -0.8710 2.0323 -0.2258 3.1290 0.0323 -2.2258 2.1290 -0.9677 0.7742 -0.8710 3.0323 0.7742 -4.8710 -3.9677 1.7742 0.1290 7.0323 -1.2258 -1.8710 0.0323 -1.2258 -1.8710 2.0323 1.7742 -0.8710 -0.9677 -0.2258 -2.8710 -2.9677 -1.2258 3.1290 -3.9677 -0.2258 -0.8710 3.0323 -0.2258 -1.8710 -3.9677 0.7742 0.1290 -1.9677 -1.2258 -2.8710 0.0323 -1.2258 2.1290 -2.9677 -0.2258 -0.8710 0.0323 -0.2258 1.1290 0.0323 -0.2258 0.1290 -1.9677 0.7742 -2.8710 1.0323 3.7742 -1.8710 -2.9677 1.7742 0.1290 -1.9677 -2.2258 2.1290 2.0323 -1.2258 1.1290 0.0323 -3.2258 2.1290 1.0323 -1.2258 -1.8710 3.0323 1.7742 3.1290 1.0323 2.7742 4.1290

0.0323

3.7742

2.1290

Khi thc hin phng php ny ta thy n chm hn so vi cc cu lnh c MATLAB thit k ring dng cho mng. Khi ta nhn bn bin avg_temp kch thc ca n bng vi kch thc ca temps. Sau thc hin php tr th s nhanh hn rt nhiu: >> tdev = temps - avg_temp(ones(31,1),:) tdev = 0.0323 -0.2258 -1.8710 3.0323 0.7742 2.1290 0.0323 -3.2258 -0.8710 2.0323 -0.2258 3.1290 0.0323 -2.2258 2.1290 -0.9677 0.7742 -0.8710 3.0323 0.7742 -4.8710 -3.9677 1.7742 0.1290 7.0323 -1.2258 -1.8710 0.0323 -1.2258 -1.8710 2.0323 1.7742 -0.8710 -0.9677 -0.2258 -2.8710 -2.9677 -1.2258 3.1290 -3.9677 -0.2258 -0.8710 3.0323 -0.2258 -1.8710 -3.9677 0.7742 0.1290 -1.9677 -1.2258 -2.8710 0.0323 -1.2258 2.1290 -2.9677 -0.2258 -0.8710 0.0323 -0.2258 1.1290 0.0323 -0.2258 0.1290 -1.9677 0.7742 -2.8710 1.0323 3.7742 -1.8710 -2.9677 1.7742 0.1290 -1.9677 -2.2258 2.1290 2.0323 -1.2258 1.1290 0.0323 -3.2258 2.1290 1.0323 -1.2258 -1.8710 3.0323 1.7742 3.1290 1.0323 2.7742 4.1290 0.0323 3.7742 2.1290 y avg_temp(ones(31,1),:) s nhn bn hng u tin (v l hng duy nht) ca bin avg_temp thnh 31 bn, to ln mt ma trn 31x3. Trong ct th i chnh l avg_temp(i). >> max_temp = max(temps) max_temp= 19 12 24

Cu lnh tm ra nhit ln nht mi thnh ph trong thng . >> [max_temp,x] = max(temps) max_temp= 19 12 24 x= 9 23 30 Cho bit gi tr nhit ln nht mi thnh ph v gi tr ch s hng x, ti gi tr ln nht xut hin, trong v d ny x cho bit ngy nng nht trong thng. >> min_temp = min(temps) min_temp= 8 5 15 Cho bit nhit thp nht mi thnh ph. >> [min_temp, n] = min(temps) min_temp= 8 5 15 n= 8 3 7 cho bit gi tr nhit thp nht mi thnh ph v ch s hng n, ti gi tr thp nht xy ra. Trong v d ny, n chnh l ngy lnh nht trong thng. >> s_dev = std(temps) s_dev= 2.5098 1.7646

2.2322

Cho bit chnh lch chun ca bin temps. >> daily_change daily_change = 3 1 -3 -4 2 3 -2 -2 -1 3 4 0 -7 1 11 -3 -7 0 2 3 -3 -2 = diff(temps) 4 -3 4 -1 -3 -4 5 -2 0 1 -2

-2 -1 7 -7 2 2 -3 3 0 -2 3 -4 1 4 -2 1 2 -2 -1

-1 1 0 1 -2 0 1 0 0 1 3 -2 -4 1 -2 2 3 1 1

6 -4 -1 2 -3 5 -3 2 -1 -3 1 2 2 -1 1 -4 5 1 -2

Cho bit s khc nhau v nhit gia cc ngy lin tip chnh l chnh lch nhit ca ngy hm sau so vi ngy hm trc. Trong v d ny, hng u tin ca daily_change l chnh lch nhit gia ngy u tin v ngy th hai trong thng. 13.1 Cc hm phn tch d liu Phn tch d liu trong MATLAB c thc hin thng qua cc ma trn hng ct, cc bin khc nhau c lu gi trong cc ct khc nhau v mi hm th hin gi tr ca bin mt thi im quan st nht nh. Cc hm thng k ca MATLAB gm c:

Cc hm phn tch d liu cplxpair(x) cross(x,y) cumprod(x) cumprod(x,n) cumsum(x) cumsum(x,n) cumtrapz(x,y) cumtrapz(x,y,n) del2(A) diff(x) diff(x,m) diff(x,m,n) dot(x,y) gradient(Z,dx,dy) histogram(x) max(x), max(x,y) max(x,n) mean(x) mean(x,n) median(x) median(x,n) min(x), min(x,y) min(x,n) prod(x) prod(x,n) rand(x) randn(x) sort(x) sort(x,n) sortrows(A) std(x), std(0) std(x,1) std(x, flag, n) subspace(A,B) sum(x) sum(x,n) trapz(x,y) trapz(x,y,n)

Xp xp cp phc lin hp Tch cho vector Tch tch lu theo ct Tch tch lu theo chiu n Tng tch lu theo ct Tng tch lu theo chiu n Tch cho tch lu Tch cho tch lu theo chiu n Ton t ri rc Laplacian 5 im Tnh chnh lch gia cc phn t Tnh s ra cp m ca cc phn t Tnh s ra cp m ca cc phn t theo chiu n Tch v hng ca hai vector Gradient vi phn Biu hnh ct Phn t ln nht Phn t ln nht theo chiu n Gi tr trung bnh ca ct Gi tr trung bnh theo chiu n Gi tr ca phn t gia ca ct Gi tr ca phn t gia theo chiu n Phn t nh nht Phn t nh nht theo chiu n Tch cc phn t trong ct Tch cc phn t theo chiu n S ngu nhin phn b u S ngu nhin phn b bnh thng Xp xp cc ct theo th t tng dn Xp xp theo chiu n Xp xp cc hng theo th t tng dn lch chun ca ct chun ho theoN-1 lch chun ca ct chun ho theoN lch chun theo chiu n Gc gia hai im Tng cc phn t trong mi ct Tng cc phn t theo chiu n Tch cho ca y=f(x) Tch cho theo chiu n

chng 14

A THC

14.1 Cc nghim ca a thc Tn nghim ca a thc l gi tr a thc bng khng, l mt bi ton thng gp trong thc t. MATLAB gii quyt nhng bi ton ny v ng thi cung cp nhng cng c tnh ton a thc. Trong MATLAB mt a thc c biu din bng mt vector hng cc h s vi bc gim dn. V d a thc x4-12x3+25x+116 c nhp vo nh sau: >> p = [1 p= 1 -12 -12 0 25 0 116] 25 116

Nh rng mc dnh cho h s 0 cng phi c g vo nu khng MATLAB s khng hiu c h s ca bi thc bc my l khng. S dng dng ny th nghim ca mt a thc c th tm c bng cch dng hm roots: >> r = roots(p) r= 11.7374 2.7028 -1.2251 + 1.4672i -1.2251 - 1.4672i Bi v trong MATLAB c a thc v cc nghim ca n u l vector nn MATLAB ngm quy c rng a thc l vector hng, cn cc nghim l cc vector ct. Nu bit trc nghim ca mt a thc th ta d dng bit c a thc . Trong MATLAB lnh poly s thc hin cng vic ny: >> pp = poly(r) pp=

1 -12 -1.7764e-14 >> pp(abs(pp)< 1e-12 = 0 khng 1 -12 0 25

25 116 % Gn nhng phn t qu nh bng 116

Bi v trong tnh ton thng gp nhng sai s nn i khi kt qu ca lnh poly cho ra cc a thc c cc h s gn bng khng v cc a thc c phn o rt nh nh c ch ra trn, cc gi tr bng khng c th c lm trn bng cc cng c v mng. Tng t nh vy, ta c th lm trn mt s phc tr thnh mt s thc bng hm real. 14.2 Nhn a thc Hm conv thc hin nhn hai a thc (thc ra l hai ma trn), xt tch ca hai a thc sau: a(x) = x3+2x2+3x+4 v b(x) = x3+4x2+9x+16 >> a = [1 2 3 4]; b = [1 4 >> c = conv(a,b) c= 1 6 20 50 9 16];

75

84

64

Kt qu l c(x) = x6 +6x5 +20x4 +50x3+75x2+84x+64 khi ta nhn nhiu a thc vi nhau th ta phi s dng lnh conv nhiu ln. 14.3 Php cng a thc MATLAB khng cung cp cc hm trc tip thc hin php cng hai a thc, dng php cng ma trn ch c tc dng khi hai a thc l hai vector c cng kch thc. V d nh cng hai a thc a(x) v b(x) trn: >> d = a + b d= 2 6

12

20

Kt qu l d(x)=2x3+6x2+12x+20. Khi hai a thc c bc khc nhau th a thc c bc thp hn phi c thm vo cc h s 0 cho bc ca n c cng bc vi a thc c bc cao hn. Xt php cng hai a thc c v d trn: >> e = c + [0 0 e= 1 6 0 d] 20 52 81 96 84

Kt qu l e(x)=x6+6x5+20x4+52x3+81x2+84. Cc gi tr 0 cn phi c thm vo pha u ca vector ch khng phi pha ui, bi v cc h s phi tng ng vi cc h s bc cao ca x.

Nu bn mun, bn c th to mt hm M_file thc hin php cng a thc tng qut: function p=polyadd(a,b) %POLYADD Polynomial addition %POLYADD(A,B) adds the polynomials A and B if nargin<2 error(Not enough input arguments) end a=a(:).; %make sureinputs are row vectors b=b(:).; na=length(a); %find lengths of a and b nb=length(b); p=[zeros(1,nb-na) a]+[zeros(1,na-nb) b]; % pad with zeros as necessary By gi c th minh ho cho vic dng hm polyadd, hy xt v d trc y: >> f = polyadd(c,d) f= 1 6 20

52

81

96

84

Kt qu cng ging nh a thc e trn. Tt nhin polyadd cng c th dng thc hin php tr. >> g = polyadd(c,-d) g= 1 6 20 14.4 Chia hai a thc Trong mt s trng hp ta phi chia a thc ny cho mt a thc khc, trong MATLAB cng vic ny c thc hin bi hm deconv, s dng cc a thc b v c trn ta c: >> [q,r] = deconv(c,b) q= 1 2 3 r= 0 0 0

48

69

72

44

4 0 0 0 0

Kt qu ny ch ra rng c em chia cho b th c a thc l q v a thc d l r trong trng hp ny a thc d l a thc 0 bi v c l a thc chia ht cho q (nh rng trn y ta nhn c a thc c bng cch em nhn a thc a vi a thc b) 14.5 o hm

Bi v d dng tnh c vi phn ca mt a thc nn MATLAB a ra hm polyder tnh vi phn a thc: >> h = polyder(g) h= 6 30

80

144

138

72

14.6 Tnh gi tr ca mt a thc R rng rng bn c th cng, tr, nhn, chia, o hm mt a thc bt k da trn cc h s ca n, bn cng c th d dng tnh c gi tr cc a thc ny. Trong MATLAB hm polyval s thc hin cng vic ny: >> x = linspace(-1,3); S chn 100 im d liu gia -1 v 3 >> p = [1 4 -7 -10];

Dng a thc p(x) = x3+4x2-7x-10 >> v = polyval(p,x); Tnh gi tr ca p(x) ti cc gi tr ca x v lu tr kt qu vo trong mng v. Sau kt qu s c v ra bng lnh plot >> plot(x, v), title(x^3+4x^2-7x-10), xlabel(x)
35 30 25 20 15 10 5 0 -5 - 10 - 151 - 0. 5 0 0. 5 1 x 1. 5 2 2. 5 3
2 x3 +4x- 7x10 -

Hnh 14.1 14.7 Phn thc hu t i khi bn gp nhng bi ton lin quan n t s ca hai a thc hay cn gi l phn thc hu t, v d nh cc hm truyn hay cc hm xp x pade c dng nh sau:

Trong MATLAB phn thc cng c m phng bng hai a thc ring r. V d nh: >> n=[1 -10 100] n = 1 -10 100 % a numerator

>> d=[1 10 100 0] % a dimominator d = 1 10 100 0 >> z=roots(n) % the zeros of n(x)/d(x) z = 5.0000 + 8.6603i 5.0000 - 8.6603i >> p=roots(d) % the poles of n(x)/d(x) p = 0 -5.0000 + 8.6603i -5.0000 - 8.6603i o hm ca phn thc ny theo bin x c tnh da trn hm polyder: >> [nd,dd]=polyder(n,d) nd = -1 20 -100 -2000 -10000 dd = Columns 1 through 6 1 20 300 2000 10000 0 Column 7 0 y nd v dd l t thc v mu thc ca o hm. Mt thao tc thng thng khc l tm phn d ca phn thc. >> [r,p,k]=residue(n,d) r = 0.0000 + 1.1547i 0.0000 - 1.1547i 1.0000 p = -5.0000 + 8.6603i -5.0000 - 8.6603i 0 k = [] Trong trng hp ny hm residue tr v cc h s m rng phn thc tng phn r, cc nghim ca phn thc l p v phn thng chia ht ca phn thc l k. Nu bc ca t s nh hn bc ca mu s th phn thc chia ht s bng khng. Trong v d trn th m rng phn thc tng phn ca phn thc cho l:

Nu cho trc cc a thc ny th phn thc ban u s tm c bng cch s dng hm residue: >> [nn,dd]=residue(r,p,k) nn = 1.0000 -10.0000 100.0000 dd = 1.0000 10.0000 100.0000

V vy trong trng hp ny, hm residue c th thc hin c vic chuyn i hai chiu tu thuc vo s lng cc tham s vo v ra truyn cho n.

chng 15

php ni suy v mn ho ng cong

T
rong cc lnh vc ng dng s, nhim v ca chng ta l phi biu din s liu, thng l cc s o bng cc chc nng phn tch. C hai cch gii quyt vn ny, trong phng php ni im (interpolation) th d liu c coi l ng v ci chng ta cn l cch biu din d liu khng nm gia cc gi tr o c, theo phng php th hai gi l phng php mn ho ng cong (curve fitting or regression), bn tm mt ng cong khng gy khc m ph hp nht vi d liu c, nhng khng cn thit phi i qua mt cch chnh xc bt k mt in no trn bng s liu. H15.1 minh ho hai phng php trn, ch o nh du cc im biu din d liu, cc on thng bng nt lin ni cc ng biu din d liu li vi nhau theo phng php ni im cn ng chm chm l nmt ng cong v theo phng php mn ho d liu. 15.1 Mn ho ng cong Phng php mn ho ng cong lin quan n vic tr li hai cu hi c bn, l ng cong th no th ph hp vi d liu nht v cu hi th hai l phi s dng loi ng cong no. Ph hp nht c th hiu theo nhiu cch v do c nhiu ng cong, v vy chng ta phi bt u t u?. Nu ph hp nht l gim nh n mc ti thiu tng sai s qun phng ti mi im biu din d liu, so vi gi tr tng ng trn ng cong th ng cong ph hp nht s l mt ng thng v mt ton m ni phng php ny c gi l phng php xp x a thc. Nu nh khi nim ny cn kh hiu i vi bn th xin hy xem li hnh 15.1 khong cch theo chiu dc gia ng cong d liu v cc im biu din d liu gi l sai s ca im , bnh phng khong cch ny ln v cng tt c chng li ta c tng bnh phng sai s. ng cong chm chm l ng cong lm cho bnh phng sai s l nh nht v c gi l ng cong ph hp nht. T qun phng b nht l cch ni tt ca cm t Tng bnh phng sai s b nht.
12 10 8 y=f x) ( 6 4 2 0 - 20 0. 2 0. 4 x 0. 6 0. 8 1 SecondO C ur er Fi t ng der v ti

Hnh 15.1

Trong MATLAB hm polyfit s gii quyt vn xp x ng cong qun phng b nht. minh ho cho vic s dng hm ny, chng ta hy bt u bng cc d liu c trong hnh v. >> x = [0 .1 .2 .3 .4 .5 .6 .7 .8 .9 1]; >> y =[-.447 1.978 3.28 6.16 7.08 7.34 7.66 9.56 9.48 9.30 11.2]; s dng hm polyfit, chng ta phi truyn cho n d liu trn v bc ca a thc m chng ta mun ph hp vi d liu, nu chng ta chn bc n l 1 th ng cong xp x gn nht s l ng thng. Phng php ny c gi l phng php xp x tuyn tnh. Mt khc nu chng ta chon n=2 th chng ta s tm c mt tam thc bc hai. V d: >> n = 2; >> p = polyfit(x,y,n) p = -9.8108 20.1293

-0.0317

Kt qu ca polyfit l mt vector biu din h s ca mt a thc bc hai. y a thc l y= -9.8108x2+20.1293x-0.0317. so snh mc xp x ca a thc vi cc im d liu chng ta hy v hai ng: >> xi = linspace(0,1,100); Dng ny to ra d liu trc x chun b v a thc >> z = polyval(p,xi) Dng ny gi hm polyval ca MATLAB tnh gi tr ca a thc p ti cc im xi >> plot(x,y,'-o',xi,z,':') V cc im c to l x v y, nh du cc im ny bng ch o sau ni cc im ny bng cc on thng. Ngoi ra n cn v d liu ca a thc xi v z dng ng chm chm. >> xlabel('x'),ylabel('y=f(x)') >> title('Second Oder Curver Fitting') To nhn cho ng cong va v. Kt qu ca cc lnh trn y l mt th c gii thiu trn. Vic chn bc ca a thc khng phi l ngu nhin, nu c hai im th xc nh mt ng thng, tc l mt a thc bc nht, ba im th xc nh mt parabol bc hai. C nh vy, xc nh mt ng cong bc n, cn c n+1 im. V vy, trong v d trc c 11 im d liu, chng ta c th chn bc ca a thc l t 1 n 10. Tuy nhin, do tnh cht s hc ca cc a thc bc cao rt phc tp nn bn khng nn chn bc ca a

thc ln hn mc cn thit. Ngoi ra khi bc ca a thc tng ln th s xp x cng km hn, v vy cc a thc bc cao c th b o hm nhiu ln trc khi o hm ca chng bng khng. V d cho mt a thc bc 10: >> pp = polyfit(x,y,10) pp = 1.0e+006 * Columns 1 through 7 -0.4644 2.2965 -4.8773 5.8233 -4.2948 2.0211 -0.6032 Columns 8 through 11 0.1090 -0.0106 0.0004 -0.0000 >> format short e % change display format >> pp.' % display polynomial coefficients as a column ans = -4.6436e+005 2.2965e+006 -4.8773e+006 5.8233e+006 -4.2948e+006 2.0211e+006 -6.0322e+005 1.0896e+005 -1.0626e+004 4.3599e+002 -4.4700e-001 Lu kch thc ca vector h s a thc trong trng hp ny so vi ng cong bc hai trc y, ng thi cng lu s khc nhau gia s hng nh nht v s hng ln nht trong a thc vo khong 107. Hy th v ng cong ny v so snh vi d liu gc v vi ng cong bc hai. >> >> >> >>
16 14 12 10 y=f x) ( 8 6 4 2 0 - 20 0. 2 0. 4 x 0. 6 0. 8 1

zz = polyval(pp,xi); % evalute 10th order polynomial plot(x,y,'o',xi,z,:,xi,zz) % plot data xlabel('x'),ylabel('y=f(x)') title('2nd and 10th Order Curver Fitting')
2ndand 10hO r er C ver Fi t g t d ur ti n

Hnh 15.2 Trn hnh 15.2, d liu gc c nh du o, ng cong bc hai c v bng nt chm chm, cn ng cong bc 10 c v bng nt m. n nt gn sng xut hin gia cc im d liu bn pha tri v bn pha phi ca ng cong bc 10. Da vo th ny th r rng rng ci chit l cng nhiu cng tt khng th p dng c y. 15.2 Ni im mt chiu

Nh gii thiu th ni im c nh ngha nh l mt phng php d on gi tr ca hm gia nhng im cho trc. Ni im l mt cng c hu hiu khi chng ta khng th nhanh chng tinh c gi tr ca hm ti cc im trung gian. Phng php ny c s dng rng ri i vi d liu l gi tr ca cc php o thc nghim hoc l kt qu ca cc chui tnh ton di. C th v d n gin nht ca vic ni im chnh l phng php v tng im ca MATLAB, tc l v nhng on thng ni nhng im d liu lin tip to ln mt th. y l phng php ni im tuyn tnh, n cho rng cc gi tr ca hm nm gia hai im cho trc s ri vo khong gia hai u ca on thng ni hai im . Hin nhin l khi s lng cc im d liu tng ln v khong cch gia chng gim i th phng php ni im tuyn tnh cng tr ln chnh xc. >> >> >> >> >>
1 0. 8 0. 6 0. 4 0. 2 si ( x n ) 0 - 0. 2 - 0. 4 - 0. 6 - 0. 8 - 10 1 2 3 x 4 5 6 7 Li ear I ner ol at o n t p in

x1 = linspace(0,2*pi,60); x2 = linspace(0,2*pi,6); plot(x1,sin(x1),x2,sin(x2),'-') xlabel('x'),ylabel('sin(x)') title('Linear Interpolation')

Hnh 15.3 C hai th cng v mt hm sine nhng th 60 im th mn hn th 6 im. Cng ging nh phng php xp x ho ng cong, y chng ta cng phi thc hin mt s la chn, c rt nhiu cch ni hai im, tu thuc vo gi nh m chng ta la chn. Hn na chng ta c th ni cc im trong khng gian khng phi l mt chiu. Ni nh th nu bn c d liu phn nh mt hm ph thuc vo hai bin z=f(x,y), bn c th ni gi tr nm gia hai im c x v y khc nhau tm ra gi tr trung gian ca hai im. MATLAB cung cp mt s hm ni l : interp1 ni cc d liu mt chiu, interp2 ni cc d liu hai chiu, interp3 ni cc d liu ba chiu, interpn ni cc d liu c s chiu ln hn 3. Sau y chng ta s xem xt cc d liu mt v hai chiu. minh ho vic ni d liu mt chiu, hy xt v d sau, kh nng ca thnh gic, v d nh mc m thanh b nht hay cn gi l ngng nghe ca tai ngi thay i theo tn s, d liu do ngi thng k c cho nh sau: >> >> >> 14 -8 >> Hz = [20:10:100 200:100:1000 1500 2000:1000:10000]; % Frequencies in Hertz spl = [76 66 59 54 49 46 43 40 38 22 ... 9 6 3.5 2.5 1.4 0.7 0 -1 -3 ... -7 -2 2 7 9 11 12]; % sound pressure level in dB

Ngng nghe c chun ho bng 0dB ti tn s 1000Hz, bi v tn s tri trong mt di rt rng nn khi v cc im d liu chng ta logarithm ho trc x.

>> >> >> >>

semilogx(Hz,spl,'-o') xlabel('Frequency, Hz') ylabel('Relative Sound Presure Level1, dB') title('Threshold of Human Hearing')

Da vo hnh 15.4 ta thy tai ngi nhy cm ht i vi cc m thanh trong khong 3kHz. Da vo cc s liu ny, chng ta hy d on ngng nghe tn s 2,5kHz bng mt vi cch khc nhau. >> s = interp1(Hz,spl,2.5e3) %linear interpolation s = -5.5000e+000 >> s = interp1(Hz,spl,2.5e3,'linear') %linear interpolation again s = -5.5000e+000 >> s = interp1(Hz,spl,2.5e3,'cubic') % cubic interpolation s = -5.8690e+000 >> s = interp1(Hz,spl,2.5e3,'spline') % spline interpolation s = -5.8690e+000 >> s = interp1(Hz,spl,2.5e3,'nearest')% nearest-neighbor s = -8 Hy n s khc nhau trong cc kt qu, hai gi tr u tin tr v mt cch chnh xc gi tr c v trn hnh ti tn s 2,5kHz bi v MATLAB ni cc im mt cch tuyn tnh gia cc im d liu trn th cc ng cong a thc, v d nh a thc bc 3 s xp x ho cc im trn th theo cc cch khc nhau, kt qu l cc ng cong ny tng i ph hp vi cc d liu m n i qua trn th nhng khc bit kh xa so vi phng php ni bng ng thng.
80 70 R at i eS ond Pr sur eLevel , dB el v u e 1 60 50 40 30 20 10 0 - 101 10 102 Fr equncy, H e z 103 104 Thr esol dof H an H i g h um ear n

Hnh 15.4 V vy bn chn cch no gi quyt mt bi ton cho trc?, trong nhiu trng hp th ch cn ni mt cch tuyn tnh l , trong thc t th chnh l phng php mc nh khi cc ng cong cng gn vi cc on thng th cng km chnh xc nhng ngc li tc tnh ton nhanh, iu ny c bit quan trng khi tp d liu ln. Mt phng php tiu tn nhiu thi gian, cho ra kt qu p mt nhng khng hiu qu. Trong thc t mt trong nhng tc dng ch yu ca phng php ni im bng hm bc 3 hoc cao

hn l mn ho d liu, c ngha l cho trc mt tp d liu ta c th dng phng php ny tnh ra gi tr ca hm nhng thi im nht nh bt k. V d: >> Hzi = linspace(2e3,5e3); % look closely near minimum >> spli = interp1(Hz,spl,Hzi,'cubic');% interpolate near minimum >> i = find(Hz>=2e3&Hz<=5e3); >> % find original data indices near minimum >> semilogx(Hz(i),spl(i),'-o',Hzi,spli) % plot old and new data >> xlabel('Frequency, Hz') >> ylabel('Relative Sound Presure Level1, dB') >> title('Threshold of Human Hearing') >> grid on

-2 R at i eS ond Pr sur eLevel , dB el v u e 1 -3 -4 -5 -6 -7 -8 -9 3 10

Thr esol dof H an H i g h um ear n

104 Fr equncy, H e z

Hnh 15.5 Trn hnh 15.5 ng gch gch s dng phng php ni im tuyn tnh, ng lin nt l mt hm bc 3, cn nhng im d liu gc c nh du bi ch o. Bng cch nng cao phn gii trn trc tn s v s dng ng bc 3 th cc s liu v ngng nghe m chng ta d on c s mn hn. Cn ch rng dc ca ng bc 3 khng thay i mt cch t ngt khi i qua im d liu nh l khi s dng phng php ni tuyn tnh. Vi b d liu trn chng ta c th d on c tn s m ti tai ngi nhy cm nht i vi m thanh. >> [sp_min,i] = min(spli) % minimum and index of minimum sp_min = -8.4245e+000 i = 45 >> Hz_min = Hzi(i) % frequency at minimum Hz_min = 3.3333e+003 Tai ngi nhy cm nht i vi m thanh c tn s khong 3.3kHz. Trc khi cp n vic xp x ho hai chiu th chng ta cn nhn r hai hn ch ln ca interp1 l: Th nht khi yu cu tnh ton ngoi khong ca mt bin c lp. V d nh interp1(Hz, spl, 1e5) th s sinh ra kt qu NaN. Th hai l cc bin c lp phi n iu, ngha l cc bin c lp phi lun tng hoc l lun gim. Trong v d trn ca chng ta th trc tn s Hz lun tng. 15.3 Xp x ho hai chiu

Xp x ho hai chiu da trn cng mt nguyn l ca xp x ho mt chiu. Tuy nhin nh tn ca n ch ra, xp x ho hai chiu l xp x mt hm ph thuc vo hai bin c lp z = f(x, y). hiu r khi nim ny, ta hy xt v d sau: Mt cng ty thm him i dng, cn thm him mt vng bin, c 0.5Km theo hnh vung th su ca y bin li c o v ghi li mt phn ca d liu thu thp c lu trong mt chng trnh MATLAB di dng mt M_file c tn l ocean.m nh sau: function ocean % ocean depth data x=0:.5:4; % x-axis (veries across the rows of z) y=0:.5:6; % y-axis ( varies down the columns of z) z=[100 99 100 99 100 99 99 99 100 100 99 99 99 100 99 100 99 99 99 99 98 98 100 99 100 100 100 100 98 97 97 99 100 100 100 99 101 100 98 98 100 102 103 100 100 102 103 101 100 102 106 104 101 100 99 102 100 100 103 108 106 101 99 97 99 100 100 102 105 103 101 100 100 102 103 101 102 103 102 100 99 100 102 103 102 101 101 100 99 99 100 100 101 101 100 100 100 99 99 100 100 100 100 100 99 99 99 99 100 100 100 99 99 100 99 100 99]; th ca d liu trn c v bi cc lnh sau: mesh(x,y,z) xlabel('X-axis, Km') ylabel('Y-axis, Km') zlabel('Ocean depth, m') title('Ocean depth Measurements')

O ceandept hM easur em s ent

110 O ceandept h, m

105 100

95 6 4 2 Y- axsKm i, 0 0 1 X- ai sKm x , 2 3 4

Hnh 15.6 S dng cc d liu ny th su ca mt im bt k nm trong khu vc kho st c th tnh c da vo hm interp2. V d: >> zi = interp2(x,y,z,2.2,3.3) zi = 1.0392e+002

>> zi = interp2(x,y,z,2.2,3.3,'linear') zi = 1.0392e+002 >> zi = interp2(x,y,z,2.2,3.3,'cubic') zi = 1.0419e+002 >> zi = interp2(x,y,z,2.2,3.3,'nearest') zi = 102 Cng ging nh trong trng hp xp x ho mt chiu, xp x ho hai chiu cng c nhiu phng php, m phng php n gin nht l phng php ni bng on thng, hay cn gi l ni tuyn tnh. Mt ln na chng ta c th xp x ho cho th tr ln mn hn vi phn gii cao hn: xi=linspace(0,4,30); % finer x-axis yi=linspace(0,6,40); % finer y-axis [xxi,yyi]=meshgrid(xi,yi); % grid of all combinations of xi and yi zzi=interp2(x,y,z,xxi,yyi,'cubic'); % interpolate mesh(xxi,yyi,zzi) % smoothed data hold on [xx,yy]=meshgrid(x,y); % grid original data plot3(xx,yy,z+0.1,'ok') % plot original data up a bit to show nodes hold off

Hnh 15.7 y hm meshgrid c dng to mng xp x ho bao ph ton b nhng im yu cu nm trong im kho st. Nh trong hnh 15.7, hm meshgrid thc hin iu bng cch to ra mt mng hai chiu da trn cc vector xi v yi, s dng mng ny chng ta c th d on c ch nng nht ca y bin. >> zmax = max(max(zzi)) zmax= 108.05 >> [i,j] = find(zmax==zzi); >> xmax = xi(j) xmax= 2.6207 >> ymax = yi(j) ymax=

2.9231

chng 16

phn tch s liu

ho d vic gii mt bi ton tch phn hoc tnh gi tr ca mt hm l tng i phc tp, nhng i vi my tnh th ch n gin l vic x l cc s liu. Lnh vc ny ca tin hc v ton hc c gi l x l s liu. Nh bn c th d on, MATLAB cung cp cc cng c gii quyt vn ny. Trong chng trnh nychng ta xem xt cch s dng cc cng c . 16.1 V th Cho n thi im ny th vic v th ca mt hm vn ch n gin da trn vic tnh gi tr ca hm ti mt s im ri rc, v dng cc im biu din cc hm ti cc gi tr ri rc . Trong nhiu trng hp th gii php ny l c th chp nhn c. Tuy nhin c mt s hm th tng i bng phng mt s khong no nhng li tr ln t bin mt s gi tr nht nh. S dng phng php v truyn thng trong trng hp ny c th lm mt i tnh chn thc ca th. V vy MATLAB cung cp cho ta mt hm v th thng minh, gi l fplot. Hm ny tnh ton mt cch cn thn hm s cn v v m bo mt cch chc chn rng tt c cc im c bit c biu din trn th. Hm flot nhn vo l tn ca hm cn v di dng mt chui k t, v gi tr cn v di dng mng gm hai phn t cha gi tr u v gi tr cui. V d: >> fplot('humps',[0 2]) >> title('FPLOT of humps') Tnh cc gi tr ca hm humps nm gia 0 v 2 v th hin th trong hnh 16.1. Trong v d ny humps l mt hm M_file thit k sn.

FPLO of hum T ps 100 80 60 40 20 0 - 20 0

0. 5

1. 5

Hnh 16.1 function [out1,out2] = humps(x) %HUMPS A function used by QUADDEMO, ZERODEMO and FPLOTDEMO. % Y = HUMPS(X) is a function with strong maxima near x = .3 % and x = .9. % % [X,Y] = HUMPS(X) also returns X. With no input arguments, % HUMPS uses X = 0:.05:1. % % Example: % plot(humps) % % See QUADDEMO, ZERODEMO and FPLOTDEMO. % % Copyright (c) 1984-98 by The MathWorks, Inc. $Revision: 5.4 $ $Date: 1997/11/21 23:26:10 $

if nargin==0, x = 0:.05:1; end y = 1 ./ ((x-.3).^2 + .01) + 1 ./ ((x-.9).^2 + .04) - 6; if nargout==2, out1 = x; out2 = y; else out1 = y; end Hm fplot lm vic vi bt c mt hm M_file no c mt gi tr vo v mt gi tr ra, ngha l ging nh hm humps trn, bin ra y tr v mt mng c cng kch thc vi bin vo x. Mt li thng thng xy ra khi s dng hm fplot cng ging nh khi s dng cc hm phn tch s khc l b quyn du nhy n tn hm cn v. Hm fplot cn du nhy n trnh nhm ln tn hm vi cc bin trong mi trng MATLAB. i vi cc hm n gin c biu din bng mt chui cc k t. V d y = 2.ex sin(x) th hm fplot c th v c th ca hm trn m khng cn phi to ra mt M_file. thc hin iu ch cn vit hm cn v di dng mt chui k t c s dng x l bin s c lp.

>> f = '2*exp(-x).*sin(x)'; y hm trn. f(x) = 2.e-xsin(x) c nh ngha bng cch s dng php nhn ma

>> fplot(f,[0 8]) >> title(f), xlabel('x') V th ca hm nm trong khong t 0 n 8 to ra th nh hnh 16.2.

2* ep( - ) . sn( ) x x *i x 0. 7 0. 6 0. 5 0. 4 0. 3 0. 2 0. 1 0 - 0. 1 0 1 2 3 4 x 5 6 7 8

Hnh 16.2 Da trn nhng tnh nng c bn ny, hm fplot c nhng kh nng rt mnh, hy xem phn tr gip trc tuyn ca MATLAB hiu r hn v cch dng hm ny. 16.2 Cc tr ca mt hm Ngoi vic s dng phng php v th thu c nhng thng tin trc quan v hm, chng ta cn cn phi bit thm nhng thng tin v mt s thuc tnh nht nh ca hm. Trong nhiu trng hp chng ta cn phi bit cc cc tr ca hm , l cc cc i, cc cc tiu. V mt ton hc th cc tr c tm theo phng php gii tch bng cch tnh o hm ca hm v tm nhng im m ti o hm bng 0. iu ny rt d hiu nu bn xem li th ca hm humps ni trn. Nhng im m th ca hm nh ln cao l nhng im cc i, cn nhng im th lm xung thp nht l nhng im cc tiu. R rng rng khi hm c nh ngha mt cch n gin th phng php gii tch c th d dng thc hin c, tuy nhin i vi mt s hm cho d vic tnh o hm l kh d dng th vic tm nghim ca o hm th li khng phi l n gin.Trong nhng trng hp ny, v trong nhng trng hp kh c th tm ra cch phn tch o hm, th cn thit phi tm hm v cng v s lng. MATLAB cung cp hai hm thc hin vic ny, l fmin v fmins , hai hm ny tng ng tm gi tr cc tiu ca cc hm mt chiu v hm n chiu. Ta ch quan tm n fmin trong phn ny. Hn na fmin c th tm thy trong help trc tuyn. Bi v max ca f(x) hon ton tng ng vi min ca -f(x) , nn fmin v fmins , c hai u c dng tm gi tr ln nht v nh nht. minh ho php cc tiu ho v cc i ho, hy xem v d trc mt ln na.T hnh 16.2 c mt gi tr cc i gn xmax =0.7 v mt gi tr nh nht gn xmin =4. iu ny c th cho php ta xem nh xmax=/40.785, xmin=5/43.93. Vit ra mt script-file dng ch son tho thun tin v s dng fmin tm ra s ny: function ex_fmin.m %ex_fmin.m fn='2*exp(-x)*sin(x)'; xmin=fmin(fn,2,5) % define function for min % search over range 2<x<5

emin=5*pi/4-xmin x=xmin; ymin=eval(fn) fx='-2*exp(-x)*sin(x)'; xmax=fmin(fn,0,3) emax=pi/4-xmax x=xmax; ymax=eval(fn) Chy M_file ny th kt qu nh sau: xmin = 3.9270 emin = 1.4523e-006 ymin = -0.0279 xmax = 3.0000 emax = -2.2146 ymax = 0.0141

% find error % eval needs x since fn has x % as its variable % evaluate at xmin % definr function for max: % note minus sign % search over range 0<x<3 % find error % eval needs x since fn has x % as its variable %evaluate at xmax

Kt qu ny hon ton ph hp vi th trc . Ch rng fmin lm vic ni chung l nh fplot. V d ny cn gii thiu hm eval , hm ny nhn mt xu k t v gii thch n nh l xu c nh vo t du nhc ca MATLAB. Cui cng, mt iu quan trng cn ch khc l vic ti thiu ho lin quan n vic tm gi tr nh nht, fmin s c lng hm tm gi tr ny. Qu trnh tm kim s tn thi gian nu nh hm c mt lng php tnh ln, hoc l hm c nhiu hn mt gi tr cc tiu trong di tm kim. Trong mt s trng hp, qu trnh ny khng tm ra c p s. Khi m fmin khng tm c gi tr nh nht th n dng li v a ra li gii thch. 16.3 Tm gi tr khng Nu nh bn quan tm n vic tm kim khi hm tin ra v cng, th i khi rt l quan trng tm ra khi no hm qua 0 v khi no qua cc gi tr khng i

Mt ln na MATLAB cung cp cho ta cng c gii quyt vn ny. Hm fzero tm gi tr 0 ca mng mt chiu. lm sng t, chng ta cng xem li v d v hm humps mt ln na: >> xzero = fzero('humps',1.2) % look for zero near 1.2 xzero = 1.2995 >> yzero = humps(xzero) % evaluate at zero yzero = 3.5527e-15 Nh vy, gi tr 0 gn vi 1.3. Nh thy trn, qu trnh tm kim gi tr 0 c th khng c kt qu. Nu khng tm thy , n dng li v a ra gii thch. Hm frzero bt buc phi c cung cp tn cho n mi khi n c gi n. fzero cho bit ti u hm bng 0 hoc n cn c th tm ra gi tr khi no hm bng hng s. V d tm x f(x)= c, th ta phi nh ngha li hm g(x) nh sau: g(x)= f(x)- c, v hm fzero tm gi tr ca x g(x)= 0, tng ng f(x)= c.

16.4 Php ly tch phn MATLAB cung cp cho ta ba hm tnh cc php ton lin quan n tch phn: trapz, quad v quad8. Hm trapz cho ta gi tr xp x tch phn pha di hm bng cch ly tng cc min hnh thang ca cc im d liu nh trong hnh 16.4. Nh thy trong hnh 16.4, cc min hnh thang c lp c gi tr c lng di mc thc t. Nu ta chia nh ra nh php ni suy tuyn tnh th s xp x ca hm s cao hn. V d nu ta gp i s lng cc hnh thang c, th xp x tng ln nh hnh v 16.5.
50 40 30 20 10 0 - 10 -1 - 0. 5 0 0. 5 1 1. 5 2

Hnh 16.4
80 70 60 50 40 30 20 10 0 - 10 -1 - 0. 5 0 0. 5 1 1. 5 2

Hnh 16.5 Tnh ton cc vng ny bng hm y = humps(x) vi -1<x<2 , s dng trapz cho mi hnh trn ta c: >> x = -1:.3:2; % rough approximation >> y = humps(x); >> area = trapz(x,y) % call trapz just like the plot command area = 21.8453 >> x = -1:.15:2; % better approximation >> y = humps(x);

>> area = trapz(x,y) area = 25.8523 Thng thng th kt qu ca chng l khc nhau, da trn s lng cc min c chia trong hnh v. Tuy nhin, khng c g m bo rng qu trnh xp x no l tt hn, ngoi tr s ng n ca php ton, hin nhin khi bn thay i mt cch c lp cc vng hnh thang, v nh lm cho n nh i th chc chn l kt qu s chnh xc hn nhiu. Hm quad v quad8 u l cc hm c cch tnh nh nhau. S nh gi ca c hai hm l rt cn thit t kt qu chnh xc. Hn na xp x ca chng l cao hn so vi hnh thang n, vi quad8 c kt qu chnh xc hn quad. Cc hm ny c gi ging nh gi fzero: >> area = quad('humps',-1,2) area = 26.3450 >> area = quad8('humps',-1,2) area = 26.3450 % find area between -1 and 2

bit thm chi tit v hm ny , bn hy xem trn h tr gip ca MATLAB. 16.5 Php ly vi phn So snh vi php ly tch phn, ta thy php ly vi phn kh hn nhiu. Php ly tch phn cho c mt vng hoc c tnh v m ca hm trong khi php ly vi phn ch ly ti mt im no y, hay cn gi l c tnh vi m ca hm. Kt qu l php tnh vi phn s khng n nh khi c tnh ca hnh thay i trong khi php tnh tch phn th t chu nh hng hn. Bi v php tnh tch phn l kh nn ngi ta c trnh nhng php tnh no m khng th thc hin c, c bit khi d liu ly tch phn l kt qu ca thc nghim. V d, chng ta hy xem xt v d lm trn hnh trong chng 15: >> x = [0 .1 .2 .3 .4 .5 .6 .7 .8 .9 1]; >> y = [-.447 1.978 3.28 6.16 7.08 7.34 ... 7.66 9.56 9.48 9.30 11.2]; % data >> n = 2; % order of fit >> p = polyfit(x,y,n) % find polynomial coefficients p = -9.8108 20.1293 -0.0317 >> xi = linspace(0,1,100); >> z = polyval(p,xi); % evaluate polynomial >> plot(x,y,'o',x,y,xi,z,':') >> xlabel('x'),ylabel('y=f(x)') >> title('Second Order Curve Fitting') Vi phn trong trng hp ny c s dng bng cch s dng hm o hm polyder:

>> pd = polyder(p) pd = -19.6217 20.1293 Vi phn ca a thc y = -9.8108x2+20.1293x-0.0317 l dx/dy = -19.6217x+20.1293. Bi v o hm ca mt da thc cng c v v tnh gi tr ging nh l i vi a thc: >> >> >> >> z = polyval(pd,xi); % evaluate derivative plot(xi,z) xlabel('x'),ylabel('dy/dx') title('Derivative of a Curve Fit Polynomial')

SecondO der C veFi t ng r ur ti 12 10 8 y=f x) ( 6 4 2 0 -2 0 0. 2 0. 4 x 0. 6 0. 8 1

Hnh 16.6
D i at v of aC veFi Pol yno i l er v i e ur t ma 25 20 dy/ x d 15 10 5 00 0. 2 0. 4 x 0. 6 0. 8 1

Hnh 16.7 Trong trng hp ny xp x a thc l mt hm bc hai v o hm ca n tr thnh hm bc nht. MATLAB cung cp mt hm tnh ton o hm mt cch s b da vo d liu m t mt s hm, hm ny c tn l diff, n tnh ton chnh lch gia cc phn t trong mng. Bi v o hmm c nh ngha nh sau:

nn o hm ca hm f(x) c th c tnh mt cch s b da vo cng thc: khi h>0 Gi l s ra ca y chia cho s ra ca x, do hm diff tnh ton s khc nhau gia cc phn t trong mng nn o hm c th c tnh mt cch xp x da vo hm diff: >> >> >> >> >> >> >>
Appr oi m e D i vt i eU sngD FF x at er a v i I 30 25 20 dy/ x d 15 10 5 0 -5 0 0. 2 0. 4 x 0. 6 0. 8 1

dy = diff(y)./diff(x); % compute differences and use array division xd = x(1:length(x)-1); % create new x axis array since dy is shorter than y plot(xd,dy) title('Approximate Derivative Using DIFF') ylabel('dy/dx'),xlabel('x')

Hnh 16.8 Do hm diff tnh ra s khc nhau gia cc phn t nn kt qu ca v d trn l mt mng c s phn t t hn mng ban u mt phn t. V vy v c th ca o hm th phi b i mt phn t ca mng x. So sng hai th cui cng th thy hin nhin rng o hm tnh bng phng php gn ng khc xa so vi thc t. 16.6 Phng trnh vi phn C th bn kh quen vi thc t l rt nhiu h thng vt l u c m t bng phng trnh vi phn. Do vy phn sau y i vi bn c th kh hp dn. Mt phng trnh vi phn thng m t tc thay i ca mt bin s trong h thng theo s thay i ca mt bin khc trong h thng hoc theo kch thch bn ngoi. Phng trnh vi phn thng thng c th c gii nh cc phng php gii tch hoc s dng cng c ton k hiu ca MATLAB. Trong nhng trng hp m phng trnh vi phn khng th gii c bng phng php gii tch th vic s dng phng php s hc tr ln kh hiu qu. minh ho hy xt phng trnh Van Der Pol, phng trnh biu din mt b dao ng.

Tt c cc phng php ton hc gii phng trnh dng ny u s dng mt phng trnh vi phn cao cp hn, tng ng vi mt tp phng trnh vi phn bc nht. i vi phng trnh vi phn trn th cch gii ny c thc hin bng cch nh ngha hai bin trung gian: t y1 = x, v y2 = suy ra:

i vi cc h phng trnh nh th ny MATLAB cung cp mt tp cc hm ODE gii xp x ho chng mt cch s hc. Trong quyn hng dn ny chng ta khng c kh nng nu ht nhng ni dung v ng dng ca tng hm trong b ODE. tm hiu thm v cc hmm ODE ng dng trong rt nhiu bi ton th d, hy g >> odedemo ti du nhc ca MATLAB. Trc ht chng ta hy xt v d sau y, chnh l v d ode45. Chng ta phi vit mt hmm M_file tr v cc o hm nu bit trc cc gi tr tc thi ca y1 v y2. Trong MATLAB cc o hm c cho bi cc vector ct, trong trng hp ny gi l yprime. Tng t y1 v y2 c vit di dng vector ct y. Kt qu ca mt hm M_file nh sau: function yprime=vdpol(t,y); % VDPOL(t,y) returns the state derivatives of % the Van der Pol equation: %

% x''-mu*(1-x^2)*x+x=0 % % let y(1)=x and y(2)=x' % % then y(1)'=y(2) % y(2)'=mu*(1-y(1)^2)*y(2)-y(1) mu=2; % choose 0< mu < 10 yprime=[y(2) mu*(1-y(1)^2)*y(2)-y(1)]; % output must be a column Gi s thi gian ko di t 0 n 30 giy, v d tspan=[0 vdpol th li gii cho bi ton nh sau: >> tspan = [0 30]; >> yo = [1;0]; >> ode45('vdpol',tspan,yo); Khi s dng hm m khng c i s ra, cc hm ODE s t ng chn nhng thi im thch hp tnh o hm. c th truy nhp c d liu, ta ch cn cung cp cho hm nhng thng s ra. >> [t,y] = ode45('vdpol',tspan,yo); y t l mt vector ct cha nhng thi im tnh o hm, cn y l mt ma trn cha hai ct v cc hng length(t), hng u tin ca ma trn y cha bin s y(1), hng th hai l bin s y(2). Da vo nhng c im ny chng ta c th v c th pha, l th gia y(2) v y(1): >> plot(y(:,1),y(:,2))
4 3 2 1 0 -1 -2 -3 -4 0 5 10 15 20 25 30

30]. Sau s dng lnh

Hnh 16.9

Cc hm ODE ca MATLAB u c tr gip trc tuyn, mi hm u c cc i s cng nh cch s dng ring, nu bn mun nghin cu thm th hy tham kho thm phn tr gip trc tuyn ca chng.
4 3 2 1 0 -1 -2 -3 -4 -3 -2 -1 0 1 2 3

Hnh 16.10

chng 17

ho trong h to phng

rong ton b phn hng dn s dng ca cun sch ny, mt s c tnh v ho ca MATLAB s ln lt c gii thiu, v trong chng ny v chng tip theo chng ta s lm sng t thm v nhng c tnh ca MATLAB. 17.1 S dng lnh Plot Nh bn thy v d trc , phn ln cc cu lnh v th trong mt phng u l lnh plot.Lnh plot ny s v th ca mt mng d liu trong mt h trc thch hp, v ni cc im bng ng thng. Di y l mt v d m bn thy trc (Hnh 17.1): >> x = linspace(0,2*pi,30); >> y = sin(x); >> plot(x,y) V d ny to 30 im d liu trong on 0 x 2 theo chiu ngang th, v to mt vector y khc l hm sine ca d liu cha trong x. Lnh plot m ra mt ca s ho gi l ca s figure, trong ca s ny n s to chia ph hp vi d liu, v th qua cc im, v th c to thnh bi vic ni cc im ny bng ng nt lin. Cc thang chia s v du c t ng cp nht vo, nu nh ca s figure tn ti, plot xo ca s hin thi v thay vo l ca s mi.

1 0. 8 0. 6 0. 4 0. 2 0 - 0. 2 - 0. 4 - 0. 6 - 0. 8 - 10 1 2 3 4 5 6 7

Hnh 17.1 By gi cng v hm sine v cosine trn cng mt th >> z = cos(x); >> plot(x,y,x,z)
1 0. 8 0. 6 0. 4 0. 2 0 - 0. 2 - 0. 4 - 0. 6 - 0. 8 - 10 1 2 3 4 5 6 7

Hnh 17.2 V d ny cho thy bn c th v nhiu hn mt th trn cng mt hnh v, bn ch vic a thm vo plot mt cp i s, plot t ng v th th hai bng mu khc trn mn hnh. Nhiu ng cong c th cng v mt lc nu nh bn cung cp cc cp i s cho lnh plot. Nu nh mt trong cc i s l ma trn v i s cn li l vector, th lnh plot s v tng ng mi ct ca ma trn vi vector : >> W = [y;z] >> plot(x,W)
1 0. 8 0. 6 0. 4 0. 2 0 - 0. 2 - 0. 4 - 0. 6 - 0. 8 - 10 1 2 3 4 5 6 7

% xy dng mt ma trn sine v cosine % v cc ct ca W vi x

Hnh 17.3 Nu nh bn thay i trt t cc i s th th s xoay mt gc bng 90 . >> plot(W,x)


7 6 5 4 3 2 1 0- 1 - 0. 5 0 0. 5 1

Hnh 17.4 Nu lnh plot c gi m ch c mt i s, v nh plot(Y) th hm plot s a ra mt kt qu khc, ph thuc vo d liu cha trong Y. Nu gi tr ca Y l mt s phc, Plot(Y) tng ng vi plot ( real(Y ) ) v plot ( imag(Y ) ), trong tt c cc trng hp khc th phn o ca Y thng c b qua. Mt khc nu Y l phn thc th plot(Y) tng ng vi plot(1:length(Y), Y). 17.2 Kiu ng, du v mu Trong v d trc, MATLAB chn kiu nt v solid v mu blue v green cho th. Ngoi ra bn c th khai bo kiu mu, nt v ca ring bn bng vic a vo plot mt i s th 3 sau mi cp d liu ca mng. Cc i s tu chn ny l mt xu k t, c th cha mt hoc nhiu hn theo bng di y:

biu tng mu biu tng du biu tng kiu nt v b xanh da tri g xanh l cy r c xanh xm m tm y vng k en w trng triangle (up) < triangle(left) > triangle(right) p pentagram h hexagram nt lin : ng chm -. ng gch-chm -ng gch-gch Nu bn khng khai bo mu th MATLAB s chn mu mc nh l blue. Kiu ng mc nh l kiu solid tr khi bn khai bo kiu ng khc. Cn v du, nu khng c du no c chn th s khng c kiu ca du no oc v. Nu mt mu, du, v kiu ng tt c u cha trong mt xu, th kiu mu chung cho c du v kiu nt v. khai bo mu khc cho du, bn phi v cng mt d liu vi cc kiu khai bo chui khc nhau. Di y l mt v d s dng cc kiu ng, mu, v du v khc nhau: >> plot(x,y,' b:p',x,z,' c-',x,z,' m+')
1 0. 8 0. 6 0. 4 0. 2 0 - 0. 2 - 0. 4 - 0. 6 - 0. 8 - 10 1 2 3 4 5 6 7

Hnh 17.5a 17.3 Kiu th Lnh colordef cho php bn la chn kiu hin th. Gi tr mc nh ca colordef l white . Kiu ny s dng trc to , mu nn, nn hnh v mu xm sng, v tn tiu ca trc mu en. Nu bn thch nn mu en, bn c th dng lnh colordef black. Kiu ny s cho ta nn trc to en, nn hnh v mu ti xm, v tiu trc mu trng. 17.4 th li, hp cha trc, nhn, v li ch gii Lnh grid on s thm ng li vo th hin ti. Lnh grid off s b cc nt ny, lnh grid m khng c tham s i km theo th s xen k gia ch on v off. MATLAB khi to vi grid off . Thng thng trc to c nt gn kiu solid nn gi l hp cha trc. Hp ny c th tt i vi box off v box on s khi phc li. Trc ng v trc ngang c th c nhn vi lnh xlabel v ylabel. Lnh title s thm vo th tiu nh. Dng hm sine v cosine minh ho: >> x = linspace(0,2*pi,30);

>> y = sin(x); >> z = cos(x); >> plot(x,y,x,z)


1 0. 8 0. 6 0. 4 0. 2 0 - 0. 2 - 0. 4 - 0. 6 - 0. 8 -1 0 1 2 3 4 5 6 7

Hnh 17.5b >> >> >> >> box off xlabel('Independent variable X') ylabel('dependent variable Y and Z') title('Sine and Cosine Curve')
1 0. 8 dependent i bl Yand Z var a e 0. 6 0. 4 0. 2 Si e andC ne C ve n osi ur 0 - 0. 2 - 0. 4 - 0. 6 - 0. 8 - 10 1 2 3 4 5 n I dependent var al eX ib 6 7

Hnh 17.6 Bn c th thm nhn hoc bt c chui k t no vo bt c v tr no bng cch s dng lnh text. C php ca lnh ny l : text (x, y,string) trong x, y l to tm bn tri ca chui vn bn. thm nhn vo hnh sine v tr (2.5, 0.7) nh sau: >> grid on, box on >> text(2.5,0.7,'sin(x)') Nu bn mun thm nhn m khng mun b hnh v khi h trc ang xt, bn c th thm chui vn bn bng cch di chut n v tr mong mun. Lnh gtext s thc hin vic ny. V d (Hnh 17.8): >> gtext('cos(x)')
1 0. 8 si ( x n ) dependent i bl Yand Z var a e 0. 6 0. 4 0. 2 Si e andC ne C ve n osi ur 0 - 0. 2 - 0. 4 - 0. 6 - 0. 8 - 10 1 2 3 4 5 n I dependent var al eX ib 6 7

Hnh 17.7
1 0. 8 si ( x n ) dependent i bl Yand Z var a e 0. 6 0. 4 0. 2 cos( ) x Si e andC ne C ve n osi ur 0 - 0. 2 - 0. 4 - 0. 6 - 0. 8 - 10 1 2 3 4 5 n I dependent var al eX ib 6 7

Hnh 17.8

17.5 Kin to h trc to MATLAB cung cp cho bn cng c c th kim sot hon ton hnh dng v thang chia ca c hai trc ng v ngang vi lnh axis. Do lnh ny c nhiu yu t, nn ch mt s dng hay dng nht c cp y. bit mt cch y v lnh axis, bn hy xem h tr gip help ca MATLAB hoc cc tham kho khc. Cc c tnh c bn ca lnh axis c cho trong bng di y: Lnh axis([xmin xmax ymin ymax]) M t Thit lp cc

V=axis axis auto axis (auto) axis manual axis xy axis ij axis square axis equal axis tightequal axis normal axis off axis on Th kim nghim mt s lnh axis cho th ca bn, s dng cc v d trc s cho ta kt qu nh sau: >> axis off % b trc to

V l mt vect Tr li gi tr xmin = min(x Gii hn than S dng (mc S dng h to Thit lp t Thit lp than Tng t nh ax Tt i ch Tt b ch Ngc li vi a

Si e andC ne C ve n osi ur

si ( x n )

cos( ) x

Hnh 17.9 >> axis on, grid off


Si e andC ne C ve n osi ur 1 0. 8 si ( x n ) D ependent Var abl Y andZ i e 0. 6 0. 4 cos( ) x 0. 2 0 - 0. 2 - 0. 4 - 0. 6 - 0. 8 - 10 1 2 3 4 5 I ndepenent Var i bl X d a e 6 7

% turn the axis on, the grid off

>>axis ij
Si e andC ne C ve n osi ur -1 - 0. 8 D ependent Var abl Y andZ i e - 0. 6 - 0. 4 - 0. 2 0 0. 2 0. 4 0. 6 0. 8 1 0 1 2 3 4 5 I ndepenent Var i bl X d a e 6 7 cos( ) x si ( x n )

Hnh 17.10 % turn the plot upside-down

Hnh 17.11 >> axis square equal


Si e andC ne C ve n osi ur 2 1. 5 D ependent Var abl Y andZ i e 1 si ( x n ) 0. 5 cos( ) x 0 - 0. 5 -1 - 1. 5 -2 0 1 2 3 4 I ndepenent Var i bl X d a e 5 6

% give axis two command at once

Hnh 17.12 % return to the defaults Hnh 17.13

>> axis xy normal


Si e andC ne C ve n osi ur 1 0. 8 si ( x n ) D ependent Var abl Y andZ i e 0. 6 0. 4 cos( ) x 0. 2 0 - 0. 2 - 0. 4 - 0. 6 - 0. 8 - 10 1 2 3 4 5 I ndepenent Var i bl X d a e 6 7

17.6 In hnh

in cc hnh m bn va v hoc cc hnh trong chng trnh ca MATLAB m bn cn, bn c th dng lnh in t bng chn hoc nh lnh in vo t ca s lnh: +) In bng lnh t bng chn: Trc tin ta phi chn ca s hnh l c s hot ng bng cch nhn chut ln n, sau bn chn mc bng chn Print t bng chn file. Dng cc thng s to ln trong mc bng chn Print Setup hoc Page Setup, th hin ti ca bn s c gi ra my in. +) In bng lnh t ca s lnh: Trc tin bn cng phi chn ca s hnh lm ca s hot ng bng cch nhn chut ln n hoc dng lnh figure(n), sau bn dng lnh in. >> print % prints the current plot to your printer

Lnh orient s thay i kiu in: Kiu mc nh l kiu portrait, in theo chiu ng, gia trang. Kiu in landscape l kiu in ngang v kn ton b trang. Kiu in tall l kiu in ng nhng kn ton b trang. thay i kiu in khc vi kiu mc nh, bn dng lnh orient vi cc thng s ca n nh sau: >> orient % What is the current orientation ans= portrait >> orient landscape % print sideways on the page >> orient tall % stretch to fill the vertical page Nu bn mun tm hiu k hn v chng th hy xem tr gip trc tuyn v chng. 17.7 Thao tc vi th Bn c th thm nt v vo th c sn bng cch dng lnh hold. Khi bn thit lp hold on, MATLAB khng b i h trc tn ti trong khi lnh plot mi ang thc hin, thay vo , n thm dng cong mi vo h trc hin ti. Tuy nhin nu nh d liu khng ph hp vi h trc to c, th trc c chia li . Thit lp hold off s b i ca s figure hin ti v thay vo bng mt th mi. Lnh hold m khng c i s s bt tt chc nng ca ch thit lp hold trc . Tr li vi v d trc: >> >> >> >> x = linspace(0,2*pi,30); y = sin(x); z = cos(x); plot(x,y)
1 0. 8 0. 6 0. 4 0. 2 0 - 0. 2 - 0. 4 - 0. 6 - 0. 8 - 10 1 2 3 4 5 6 7

Hnh 17.14 By gi gi nguyn th v thm vo ng cosine >> hold on >> ishold % hm logic ny tr v gi tr 1 (true) nu hold trng thi ON ans =

1 >> plot(x,z,'m') >> hold off >> ishold % hold by gi khng cn trng thi ON na. ans = 0 Ch rng kim tra trng thi ca hold ta c th dng hm ishold .

1 0. 8 0. 6 0. 4 0. 2 0 - 0. 2 - 0. 4 - 0. 6 - 0. 8 - 10 1 2 3 4 5 6 7

Hnh 17.15 Nu bn mun hai hay nhiu th cc ca s figure khc nhau, hy dng lnh figure trong ca s lnh hoc chn new figure t bng chn file, figure khng c tham s s to mt figure mi. Bn c th chn kiu figure bng cch dng chut hoc dng lnh figure(n) trong n l s ca s hot hot ng. Mt khc mt ca s figure c th cha nhiu hn mt h trc. Lnh subplot(m,n,p) chia ca s hin ti thnh mt ma trn mxn khong v th, v chn p l ca s hot ng. Cc th thnh phn c nh s t tri qua phi, t trn xung di, sau n hng th hai .v.v. . . V d: >> >> >> >> >> >> x = linspace(0,2*pi,30); y = sin(x); z = cos(x); a = 2*sin(x).*cos(x); b = sin(x)./(cos(x)+eps); subplot(2,2,1) % pick the upper left of % 2 by 2 grid of subplots >> plot(x,y),axis([0 2*pi -1 1]),title('sin(x)') >> subplot(2,2,2) % pick the upper right of the 4 subplots >> plot(x,z),axis([0 2*pi -1 1]),title('cos(x)') >> plot(x,z),axis([0 2*pi -1 1]),title('cos(x)') >> subplot(2,2,3)% pick the lowwer left of the 4 subplots >> plot(x,a),axis([0 2*pi -1 1]),title('2sin(x)cos(x)') >> subplot(2,2,4)%pick the lowwer right of the 4 subplots >> plot(x,b),axis([0 2*pi -20 20]),title('sin(x)/cos(x)')
si ( x n ) 1 0. 5 0 - 0. 5 - 10 1 0. 5 0 - 0. 5 - 10 2 4 6 2 4 2si ( xcos( ) n ) x 6 1 0. 5 0 - 0. 5 - 10 20 10 0 - 10 - 200 2 4 6 2 4 si ( x/ os( x n )c ) 6 cos( ) x

Hnh 17.6 17.8 Mt s c im khc ca th trong h to phng loglog tng t nh plot ngoi tr thang chia l logarithm cho c hai trc.

semilogx tng t nh plot ngoi tr thang chia ca trc x l logarithm cn thang chia trc y l tuyn tnh. semology tng t nh plot ngoi tr thang chia ca trc y l logarithm, cn thang chia trc x l tuyn tnh. area( x, y ) tng t nh plot (x,y) ngoi tr khong cch gia 0 v y c in y, gi tr c bn y c th c khai bo, nhng mc nh th khng. S hnh mi tiu chun c to thnh t lnh pie(a, b), trong a l mt vector gi tr v b l mt vector logic tu chn. V d:

>> a = [.5 1 1.6 1.2 .8 2.1]; >> pie(a,a==max(a)); >> title('Example Pie Chart')
Exam e Pi C t pl e har 7% 14% 29% 22% 11% 17%

Hnh 17.7 Mt cch khc quan st d liu l biu Pareto, trong cc gi tr trong cc vector c v thnh mt khi ch nht. V d dng vector a ni trn:

>> pareto(a); >> title('Example Pareto Chart')


Exam e Par t oC t pl e har 7 6 5 4 3 2 1 0 6 3 4 2 5 1 97% 83% 69% 56% 42% 28% 14% 0%

Hnh 17.18 >> >> >> >> >> >> i khi bn mun v hai hm khc nhau trn cng mt h trc m li s dng thang chia khc nhau, plotyy c th lm iu cho bn: x = -2*pi:pi/10:2*pi; y = sin(x);z = 2*cos(x); subplot(2,1,1),plot(x,y,x,z), title('Two Plots on the same scale'); subplot(2,1,2),plotyy(x,y,x,z) title('Two plots on difference scale.');
Tw Pl t ont e sam scal o os h e e 2 1 0 -1 - 2- 8 1 0. 5 0 - 0. 5 - 1- 8 -6 -4 -2 0 2 4 6 -6 -4 -2 0 2 4 Tw pot o di f r enc scl e. o l s n fe e a 6 8 2 1 0 -1 88 2

Hnh 17.19 th bar v stair c th sinh ra bi vic dng lnh bar, bar3, barh v stairs. Di y l v d:

>> x = -2.9:0.2:2.9; >> y = exp(-x.*x);

>> >> >> >> >> >> >> >> >> >> >> >>

subplot(2,2,1) bar(x,y) title('Bar chart of bell Curve') subplot(2,2,2) bar3(x,y) title('3-D Bar Chart of a Bell Cuve') subplot(2,2,3) stairs(x,y) title('Stair Chart of a Bell Curve') subplot(2,2,4) barh(x,y) title('Horizontal Bar Chart')

Hnh 17.20 rose(V) v mt biu trong to cc cho cc gc trong vector v, tng t ta cng c cc lnh rose(v,n) v rose(v,x) trong x l mt vector. Di y l mt v d:

>> v = randn(100,1)*pi; >> rose(v) >> title('Angle Histogram of Random Angle')


90 ando Angl H t gr am of R 7 m Angl e e io s 120 60 4. 667 6 150 2. 333 3 30 180 0 210 330 240 270 300

Hnh 17.21

chng 18

ho trong khng gian 3 chiu

M
ATLAB cung cp mt s hm hin th d liu 3 chiu nh cc hm v ng thng trong khng gian 3 chiu, cc hm v b mt v v khung dy v mu c th c s dng thay th cho chiu th t. 18.1 th ng thng. Lnh plot t trong khng gian hai chiu c th m rng cho khng gian 3 chiu bng lnh plot3. Khun dng ca plot3 nh sau: plot3 ( x1, y1, z1, S1, x2, y2, z2, S2, .... ), trong xn, yn v zn l cc vector hoc ma trn, v Sn l xu k t tu chn dng cho vic khai bo mu, to biu tng hoc kiu ng. Sau y l mt s v d: >> >> >> >> t = linspace (0, 10*pi); plot3(sin(t),cos(t),t) title ('Helix'),xlabel('sin(t)') ylabel('cos(t)'),zlabel('t')
H ei lx 40 30 20 10 0 1 0. 5 0 - 0. 5 cos( ) t -1 -1 - 0. 5 si ( t n ) 0 0. 5 1 t

Hnh 18.1

Ch rng: hm zlabel tng ng vi hm hai chiu xlabel v ylabel. Tng t nh vy, lnh axis cng c khun dng: axis ( [xmin xmax ymin ymax zmin zmax ] ) thit lp gii hn cho c 3 trc. V d : >> axis('ij') % thay i hng trc t sau ra trc
H ei lx 40 30 20 10 0 -1 - 0. 5 0 0. 5 cos( ) t 1 -1 - 0. 5 si ( t n ) 0 0. 5 1 t

Hnh 18.2 Hm text cng c khun mu nh sau: text ( x, y, z, string ) s t v tr xu string vo to x, y, z. 18.2 th b mt v li MATLAB nh ngha b mt li bng cc im theo hng trc z trn ng k hnh vung trn mt phng x-y. N to ln mu mt th bng cch ghp cc im gn k vi cc ng thng. Kt qu l n trng nh mt mng li nh c vi cc mt li l cc im d liu. th li ny thng c s dng quan st nhng ma trn ln hoc v nhng hm c hai bin. Bc u tin l a ra th li ca hm hai bin z = f (x, y ), tng ng vi ma trn X v Y cha cc hng v cc ct lp i lp li. MATLAB cung cp hm meshgrid cho mc ch ny. [ X, Y ] = meshgrid(x, y ), to mt ma trn X, m cc hng ca n l bn sao ca vector x, v ma trn Y c cc ct ca n l bn sao ca vector y. Cp ma trn ny sau c s dng c lng hm hai bin s dng c tnh ton hc v mng ca MATLAB. Sau y l mt v d v cch dng hm meshgrid. >> x = -7.5:.5:7.5; >> y = x; >> [X,Y] = meshgrid(x,y); X, Y l mt cp ca ma trn tng ng mt li ch nht trong mt phng x-y. Mi hm z=f(x,y) c th s dng tnh cht ny. >> R = sqrt(X.^2+Y.^2)+eps; >> % find the distance from the origin (0,0) >> Z = sin(R)./R; % calculate sin(r)/ r Ma trn R cha bn knh ca mi im trong [X,Y], n l khong cch t mi im n tm ma trn. Cng thm eps khng xy ra php chia cho 0. Ma trn Z cha sine ca bn knh chia cho bn knh mi im trong s . Cu lnh sau v th li: >> mesh(X,Y,Z)

Hnh 18.3 th trn l n sc. Tuy nhin bn c th thay i mu sc vi s tr gip ca MATLAB rt r dng nu bn c n phn colormaps.. Trong v d ny, hm mesh xp xp gi tr ca cc phn t ca ma trn vo cc im (X,Y,Z) trong khng gian ba chiu. mesh cng c th v mt ma trn n tng t nh vi mt i s; mesh(Z), s dng cc im (i,j,Z). Nh vy Z c v ngc li vi cc ch s ca n, trong trng hp ny mesh(Z) ch n gin l chia li khc cc trc x, y theo cc ch s ca ma trn Z. Bn hy th to v d cho trng hp ny?. th b mt ca cng mt ma trn Z trng nh th li trc , ngoi tr khong cch gia hai ng l khc nhau (gi l patchs). th loi ny dng hm surf, n c tt c cc i s nh hm mesh. Hy xem v d di y (Hnh 18.4): >> surf(X,Y,Z) lm r thm mt vi ch , chng ta cng quay li hm peaks a ra phn trc. th li trong khng gian 3 chiu ca hm ny c a ra nh sau (hnh 18.5): >> mesh(peaks) >> title('Mesh Plot of Peaks function') Hnh 18.4 `

Hnh 18.5 th ng vin cho ta thy c nng hoc cao ca hnh. Trong MATLAB th ng vin trong khng gian hai chiu tng t nh trong khng gian ba chiu nhng hm gi ca n l contour3. th s dng cc lnh s c minh ho trong bng khc mu. 18.3 Thao tc vi th MATLAB cho php bn khai bo gc t quan st c th trong khng gian ba chiu. Hm view(azimuth, elevation ) thit lp gc xem bng vic khai bo azimuth v elevation. Elevation m t v tr ngi quan st, xem nh l gc o bng trn h trc x-y. Azimuth m t gc trong h trc ni ngi quan st ng. iu ny c m t trong hnh 18.6.

Hnh 18.6 Azimuth c o bng t phn m trc y. Pha m trc y c th quay theo chiu kim dng h mt gc -37.5 t pha bn. Elevation l gc m ti mt bn thy c mt phng x-y. S dng hm view cho php bn c th quan st hnh v t cc gc khc nhau. V d nu elevation thit lp l m, th view s nhn hnh t pha di ln. Nu azimuth thit lp dng, th hnh s quay ngc chiu kim ng h t im nhn mc nh.Thm ch bn c th nhn trc tip t trn bng cch thit lp view(0,90 ). Thc ra th y l im nhn mc nh 2 chiu, trong x tng t tri qua phi, v y tng t trn xung di, khun dng view(2) hon ton ging nh mc nh ca view(0, 90 ), v view(3) thit lp mc nh trong khng gian 3 chiu.Vd:

Hnh 18.7 Lnh view c mt dng khc m rt tin ch khi s dng l view([X,Y,Z ]) cho php bn quan st trn mt vector cha h trc to decac trong khng gian 3 chiu. Khong cch t v tr bn quan st n gc to khng b nh hng. V d, view([0 10 0 ]), view([0 -1 0 ]) v

view(0, 0 ) cho cc kt qu nh nhau. Cc thng s azimuth v elevation m bn ang quan st c th ly li c bng cch dng [az, e] = view. Vd: >> view([-7 -9 7]) >> [az,el] = view az = -37.8750 el = 31.5475 Mt cng c hu dng khc l quan st th khng gian 3 chiu bi hm rotate3d. Cc thng s Azimtuh v elevation c th c tc ng bi chut, rotate3d on cho php chut can thip, rotate3d off khng cho php. Lnh hidden du cc nt khut. Khi bn v th, th mt s phn ca n b che khutbi cc phn khc, khi nu dng lnh ny th cc nt khut s b du i, bn ch c th nhn phn no trong tm nhn ca bn. Nu bn chuyn n hidden off, bn c th thy phn khut qua mng li. Di y l v d: >> >> >> >> >> mesh(peaks(20)+7) hold on pcolor(peaks(20)) hold off title('Mesh with hiden on')

Hnh 18.8

By gi hy b ch du cc nt khut i ta s thy s khc nhau:

>> hidden off >> title('Mesh with Hidden Off ')

Hnh 18.9 18.4 Cc c im khc ca th trong khng gian 3 chiu Hm ribbon(x, y ) tng t nh plot(x, y ) ngoi tr ct ca y c v nh l mt di ring bit trong khng gian ba chiu. Di y l th hnh sine:

Hnh 18.10 Hm clabel tng thm cao cho th ng vin. C ba mu clabel(cs), clabel(cs, V ) v clabel( cs, manual). clabel(cs), trong cs l cu trc ng vin c tr v t lnh contour, cs=contour(z), ly nhn tt c cc th ng vin vi cao ca n. V tr ca nhn c ly ngu nhin. clabel (c, manual) nh v nhn ng vin v tr kch chut tng t nh lnh ginput ni trn. Nhn phm Return kt thc vic to nhn ny. Hm contourf s v mt th ng vin kn, khng gian gia ng vin c lp y bng mu. Hai mu trng thi ca lnh mesh dng vi th li l: meshc v th li v thm ng vin bn di, meshz v th li v th c dng nh mn che. Hm waterfall c xem nh mesh ngoi tr mt iu l hm mesh ch xut hin hng x. C hai mu trng thi ca lnh surf, l surfc v mt th surf v thm ng bao bn di, surflvex v mt th surf nhng thm vo s chiu sng b mt t ngun sng. Cu trc tng qut l surfl( X,Y, Z, S, K ) trong X, Y,v Z tng t nh surf, S l mt vector tu chn trong h to decac (S=[Sx Sy Sz]) hoc trong to cu (S=[az,el]) ch ra hng ca ngun sng. Nu khng khai bo, gi tr mc nh ca S l 45 theo chiu kim ng h t v tr ngi quan st, S l mt vector tu chn ch ra phn ng gp tu thuc vo ngun sng bao quanh, s phn chiu nh sng v h s phn chiu (K=[ka,kd,ks,spread]).

>> colormap(gray) >> surfl(peaks) >> title('surf1 plot of peaks with default lighting') Hnh 18.11

fill3, phin bn 3 chiu ca fill, v mt a gic u trong khng gian ba chiu. Khun dng tng qut ca n l fill3(x, y, z, c), trong chiu ng ca a gic c ch bi ba thnh phn x, y, z. Nu c l mt k t, a gic s c lp y mu nh bng mu. c cng c th l mt vector hng c 3 thnh phn ([r g b]) trong r, g v b l cc gi tr gia 0 v 1 thay cho cc mu , xanh l cy v xanh da tri. Nu c l mt vector hoc ma trn, n c s dng nh mt ch s ch ra s mu. Nhiu a gic c th c to ra bng cch cho thm nhiu i s nh fill3 (x1, y1, z1,c1, x2, y2, z2, c2, ....). V d sau s v ngu nhin 4 tam gic vi mu:

>> color(cool) >> fill3(rand(3,4),rand(3,4),rand(3,4),rand(3,4)) bar3 v bar3h l phin bn 3 chiu ca bar v barh, bie3 l phin ban ca pie.

18.5 Bng mu Mu v biu mu c cp n trong mt s v d phn trc. Trong phn ny chng ta s ni r v chng. MATLAB nh ngha mt biu mu nh l mt ma trn c 3 ct. Mi hng ca ma trn nh ngha mt mu ring bit s dng cc s trong di 0 v 1. Nhng s ny ch ra cc gi tr RGB, nhy ca cc mu thnh phn , xanh l cy, v xanh da tri trong mt mu do cc thnh phn to ra. Mt s mu c bn c cho trong bng di y: 0 1 1 0 0 1 1 0 -5 -5 1 -49 Xanh l cy 0 1 0 1 0 1 0 1 -5 0 -62 1 Xanh da tri 1 0 0 1 0 1 1 -5 0 -40 -83 mu 0 trng xanh l cy xanh da tri vng tm lam xm xm trung bnh ti ng ngc xanh bin en

Di y l mt s hm ca MATLAB to ra bng mu trn: Function hsv hot gray bone copper pink M t bng mu Gi tr mu bo ho (HSV) en--vng-trng xm cn bng tuyn tnh xm c pha nh vi mu xanh sc thi ca mu ng mu hng nht nh

white flag jet prism cool lines summe autumn winter spring 18.6 S dng bng mu

trng hon ton xen k , trng, xanh da tri, v en s thay i mu bo ho c mu sc lng knh mu xanh tm mu ca nt v Bng ca xanh l cy v vng Bng ca v vng Bng ca xanh l cy v xanh da tri Bng ca magenta v yellow

Cu lnh colormap(M) ci t ma trn M nh l bng mu c s dng bi hnh hin ti. V d: colormap(cool) ci t mt version 64 u vo ca bng mu cool. Hm plot v plot3 khng dng bng mu trn, chng s dng cc mu lit k trong bng kiu ng, im nh du, mu ca plot. Phn ln cc hm v khc nh mesh, surf, contour, fill, pcolor v cc bin ca n, s dng bng mu hin ti. Sau y l mt v d dng tham s mu cho hm surf hin th gc quan st : >> >> >> >> >> [X,Y,Z]=peaks(30); surf(X,Y,Z,atan2(X,Y)) colormap(hsv),shading flat axis([-3 3 -3 3 -6.5 8.1]),axis off title('using a color Argument to surf')

Hnh 18.12 18.7 S dng mu thm thng tin Mu c th c dng thm thng tin vo th 3 chiu nu n c s dng to thnh chiu th t. Cc hm nh mesh v surf bin i mu dc theo trc z, tr khi mt i s mu c a ra nh surf(X,Y,Z) hon ton tng ng vi surf(X,Y,Z,t ) trong thnh phn th t c dng nh mt ch s trong biu mu. iu ny khin cho th y mu nhng li khng thng tin khi m trc z tn ti. Di y l mt s cch s dng i s mu thm thng tin hoc nhn mnh thng tin tn ti trong th >> x=-7.5: .5:7.5; y=x % create a data set >> [X,Y]=meshgrid(x,y); %create plaid data >> R=sqrt(X.^2+Y.^2) +eps % create radial data >> Z=sin(R)./R; % create sombrero >> subplot(2,2,1),surf(X,Y,Z),

>> >> >> >> >> >> >> >> >>

title('Color Varies with the Z_axis') subplot(2,2,2),surf(X,Y,Z,R), title('Color Varies With the Radius') subplot(2,2,3),surf(X,Y,Z,del2(Z)), title('Color Varies with Curvature') [dZdx,dZdy]=gradient(Z); %compute the slope dZ=sqrt(dZdx.^2+dZdy.^2) %compute the slope's manitude subplot(2,2,4),surf(X,Y,Z,dZ) title('Color Varies With the slope Magnitude')

Hnh 18.13 18.8 Hin th bng mu. Bn c th hin th bng mu theo mt s cch sau. Mt trong nhng cch l xem tt c cc phn t trong trong mt ma trn bng mu mt cch trc tip: >> hot(8) ans = 0.3333 0.6667 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000

0 0 0 0.3333 0.6667 1.0000 1.0000 1.0000

0 0 0 0 0 0 0.5000 1.0000

Thm vo , hm pcolor c th c s dng biu din mt bng mu. Hy th v d ny mt vi ln bng cch dng cc hm colormap khc nhau v thay i tham s n: >> >> >> >> >> colormap(jet(n)) n=8; colormap(jet(n)) pcolor([1:n+1;1 :n+1]') title('using pcolor to display a colormap') Hnh 18.4 Hm colorbar thm mt thanh mu ng hoc thanh mu ngang (cn chnh mu ) vo ca s hnh v ca bn, a ra biu mu cho trc hin ti. colorbar( h) nh v thanh mu ngang di hnh v hin ti ca bn. colorbar( v) nh v thanh mu ng v bn phi

hnh v ca bn. colorbar khng c i s th l thm mt thanh mu ngang, nu thanh mu ny khng tn ti hoc l cp nht nu n tn ti. >> >> >> >> >> [X,Y,Z] = peaks; mesh(X,Y,Z ); colormap(hsv) axis([-3 3 -3 3 colorbar

-6

8])

Hnh 18.5 18.9 Thit lp v thay i bng mu. Thc t colormaps l cc ma trn, c ngha l bn c th thao tc chng ging nh bt k mt ma trn no khc. Hm brighten nh vo c im ny thay i colormap tng hoc gim nhy ca cc mu m.Bighten(n) cng vi bighten(-n) phc hi colormap ban u. Lnh newmap=brighten(n) to mt thanh mu sng hn hoc ti hn ca colormap hin ti m khng lm thay i bi mu hin ti. Lnh newmap=brighten(cmap,n) iu chnh phin bn ca thanh mu c khai bo m khng lm nh hng n colormap hin ti hoc cmap. brighten(gcf, n) lm sng tt c cc i tng trong hnh v hin ti. Bn c th to mt colormap ca ring bn bng cch a ra mt ma trn mymap m hng,3 ct v ci t n cng vi colormap(mymap) mi gi tr trong mt ma trn colormap phi thuc khong t 0 n 1. Nu bn c gng s dng mt ma trn vi nhiu hn hoc t hn 3 ct hoc cha mt gi tr no b thua 0 hoc ln 1 colormap s a ra thng bo li. Bn c th kt ni cc colormap theo kiu ton hc. Mc d kt qu i khi khng th on trc c. V d, biu c tn gi l pink : >> pinkmap = sqrt (2/3*gray+1/3*hot); Bi v colormap l cc ma trn, chng c th c v th. Lnh rgbplot s v th cc gi tr ca colormap tng t nh lnh plot, nhng s dng mu , mu xanh l cy v xanh da tri cho nt v. rgbplot(gray) cho bit c ba mu tng tuyn tnh v ng u. Lnh rgbplot vi mt s colormap khc nh jet, hsv, v prism. Gi tr hin ti ca cmin v cmax c tr li bng caxis khng c i s. Chng thng l nhng gi tr ln nht v nh nht ca d liu, caxis([cmin cmax ]) s dng colormap nguyn bn cho d liu trong di gia cmin v cmax, nhng im d liu ln hn cmax s b chia ra thnh cc mu kt hp vi cmax. V nhng im d liu c gi tr nh hn cmin s b chia ra thnh cc mu kt hp vi cmin. Nu cmin nh hn min(data) hoc cmax ln hn max(data ), th cc mu kt hp vi cmin hoc cmax s khng bao gi c s dng ; ch mt phn nh ca colormap c s dng. caxis(auto) s hi phc gi tr mc nh ca cmin v cmax. V d sau c minh ho trong colorplate4 .

>> pcolor([1:17;1:17]') >> title('Default color range') >> colormap(hsv(8)) >> axis('auto') >> colorbar >> caxis ans = 1 17 Hnh 18.6

chng19

Mng t bo v cu trc

M
ATLAB 5.0 gii thiu 2 loi d liu mi c tn gi l mng t bo v cu trc. Mng t bo c xem nh mt mng ca cc s nh phn hoc l nh b cha c th lu gi nhiu kiu d liu khc nhau. Cu trc l nhng mng d liu hng i tng xy dng cng vi tn cc trng c th cha nhiu kiu d liu khc nhau, bao gm mng t bo v cc cu trc khc. Cu trc cung cp cho ta phng tin thun li nhm cc kiu d liu khc nhau. Nhng kiu d liu mi ny, mng t bo v cu trc to cho bn kh nng t chc d liu thnh cc gi rt thun tin. 19.1 Mng t bo Mng t bo l nhng mng MATLAB m cc phn t ca n l cc t bo. Mi t bo trong mng t bo cha cc kiu d liu ca MATLAB bao gm mng s, vn bn, i tng c trng, cc mng t bo v cu trc. V d mt t bo ca mng t bo c th l mng s, loi khc l kiu chui vn bn, loi khc l vector cc gi tr s phc. Cc mng t bo c th c xy dng vi s chiu ln hn 2, tuy nhin cho thun tin khi xt ngi ta ly s chiu l 2 . 19.2 Xy dng v hin th mng t bo Mng t bo c th c xy dng bng cch dng cu lnh gn, hoc ch nh mng trc bng cch s dng hm t bo sau gn d liu cho mng. Nh mi loi mng khc, mng t bo c th to ra bng cch gn d liu cho tng t bo c lp cng mt thi im. C hai cch khc nhau thm nhp vo mng t bo. Nu bn s dng c php mng tiu chun, bn phi cc t bo trong du ngoc { }. V d:

>> >> >> >>

A(1, A(1, A(2, A(2,

1) = {[1 2 3: 4 5 6 : 2) = {2 + 3 i}; 1) = {' A text string '}; 2,) = {12: -2 :0};

9]};

Du ngoc nhn bn pha phi ca du bng ch ra rng biu thc l mt t bo, hay cn gi l ch s t bo. Cch vit sau tng ng vi cch vit trn: >> >> >> >> A{1, A{1, A{2, A{2, 1 2 1 2 } = [1 2 3 : 4 5 } = 2+3i ; } = 'A text string ' ; } = 12 : -2 : 0 ; 6 : 7 8 9 ];

Du ngoc nhn bn tri ch ra rng A l mt mng t bo v biu thc t bn trong l khai bo t bo. MATLAB hin th mng A nh sau: >> A A = [3X3 double] 2.0000+ 3.0000 i ' A text string '[1x7 double ] hin th ni dung ca mi t bo trong mng t bo ta dng hm celldisp, hin th ni dung ca ring mt t bo, truy nhp vo t bo c s dng du ngoc nhn.Vi d : >> A{2,2} MATLAB hin th s cu trc ho mng t bo trong mt ca s bng vic gi hm cellplot. Hm cell lm vic vi mng t bo bng vic to ra cc mng trng theo kch c ca mng. V d : >> C= cell ( 2, 3 ) C= [] [] [] [] [] [] 19.3 T hp v khi phc mng t bo Nu bn gn d liu cho t bo ngoi s chiu hin c ca mng. MATLAB s t ng m rng mng v in vo gia ma trn s rng. Ch khi nim { } thay cho ma trn t bo rng v [ ] thay cho mng s ma trn rng. S dng du mc vung kt ni mng t bo: >> C= [A B]

C= [3x3 double ] Smith' 'A text string ' 5 ] 2.0000+ 3.0000i [1x2 double] ' John [2.0000+3.0000i] [

[1x7 double]

>> C=[A;B] C = [3x3 double ] 2.0000 + 3.0000 i ' A text string ' [ 1x7 double ] [ 1x2 double ] ' John Smith' [ 2.0000+ 3.0000i ] [ 5 ] Mt tp con cc t bo c th c tch ra to thnh mt mng t bo mi. Nu D l mt mng t bo 3x3, ngi ta c th tch ra to thnh mt mng t bo mi 2x2 nh sau: >> F = D(2:2,2:3); Hm reshape c th c s dng thay i cu hnh ca mt mng t bo nhng khng th dng thm vo hoc bt i t bo. >> X = cells(3,4); >> size(X) ans = 3 4 >> X= reshape(X,6,2); >> size(Y) ans = 6 2 19.4 Truy nhp vo trong mng t bo truy nhp d liu cha trong cc phn t ca mng t bo, s dng du ngoc nhn. Dng du ngoc n thm nhp mt phn t nh l mt t bo. truy nhp ni dung ca phn t trong mng t bo, kt ni cc biu thc nh sau: >> x = B{2,2} x = 5 >> class(x) ans= double >> y = B[2,2] y = [5] >> class(y) % truy nhp ni dung ca t bo.

% truy nhp vo bn thn t bo.

ans= cell >> B{1,1} (1,2) ans= 2 truy nhp di cc phn t trong mng t bo, s dng hm deal >> [a,b] = deal(B{2,:1}) a = 2.0000+ 3.0000i b = 5 Hm deal cn mt danh sch cc bin phn bit nhau bi du phy. Biu thc B{2, :} c th s dng mi ni v du phy dng phn tch danh sch cc bin. Do , B{2, :} tng ng vi B(2,1) v B(2,2). 19.5 Mng t bo ca chui k t Mt trong nhng ng dng ph bin ca mng t bo l xy dng mt mng vn bn. Mng chui k t tiu chun i hi tt c cc chui u c chung di. Bi v mng t bo c th cha nhiu kiu d liu khc nhau trong mi phn t, chui k t trong mng t bo khng c gii hn ny. V d: >> T = {' Tom';' Disk'} T= 'Tom' 'Disk' 19.6 Cu trc Cu trc l nhng i tng MATLAB c tn b cha d liu cn gi l fields. Nh mi phn t ca mng t bo, trng cu trc c th c bt c mt kiu d liu no. Chng khc ch cu trc trng c truy nhp bng tn ph bin hn l ch s, v khng c s hn ch no v ch s cng nh cu hnh ca cc trng cu trc. Cng ging nh mng t bo, cu trc c th c nhm li vi nhau to thnh mng v mng t bo. Mt cu trc n l mt mng cu trc 1x1. 19.7 Xy dng mng cu trc Cu trc s dng du . truy nhp vo trng. Xy dng mt cu trc n gin nh gn d liu vo cc trng c lp. V d sau to mt bn ghi client cho th vin kim tra. >> client.name = ' John Doe'; % truy nhp vo phn t th hai ca % vectortrong t bo

>> client.cost = 86.50; >> client.test.AIC = [6.3 6.8 7.1 7.0 6.7 6.5 6.3 6.4] >> client.test.CHC = [2.8 3.4 3.6 4.1 3.5]; >> client client = name L ' John Doe ' cost :86.50 test : [1x1 struct] >> client.test ans= AIC:6.3000 6.8000 7.1000 7.0000 6.7000 6.5000 6.3000 6.4000 CHC:2.8000 3.4000 3.6000 4.1000 3.5000 By gi to bn ghi client th hai: >> client(2).name = ' Alice Smith '; >> client(2).cost = 112.35; >> client(2).test.AIC = [5.3 5.8 7.0 6.5 6.7 5.5 5.9 ] >> client(2).test.CHC =[ 3.8 6.3 3.2 3.1 2.5 ] >> client client = 1x2 struct array with field name cost test

6.0

Cu trc cng c th c xy dng bng cch dng hm struct to trc mt mng cu trc. C php l: ( field. V1, field2, V2, .... ) trong field1, field2, .v.v... l cc trng, v cc mng V1, V2, v.v.... phi l cc mng t bo c cng kch thc., cng s t bo, hoc gi tr. V d, mt mng cu trc c th c to ra nh sau: >> N ={' John Doe ', ' Alice Smith'}; >> C = {86.50, 112.35 }; >> P = {[10.00 20.00 45.00]; >> bills = struct('name',N,'cost',C,'payment',P) bils= 1x2 struct array with fields name cost payment 19.8 Truy nhp vo cc trng cu trc

Bi v ni dung cu trc l tn nhiu hn l ch s, nh trong trng hp mng t bo, tn ca cc trng trong cu trc phi c bit n truy nhp d liu cha trong chng. Tn ca cc trng c th c tm thy trong trong ca s lnh, n gin l ch vic nhp vo tn ca cu trc. Tuy nhin trong M-file, mt hm cn thit c to ra cp nht cc tn trng . Hm fieldname tr li mt mng t bo c cha tn ca cc trng trong mt cu trc. >> T = fieldnammes(bills) T = ' name ' ' cost ' ' payment ' C hai phng php truy nhp vo trng cu trc. Ch s trc tip s dng k thut ch mc thch hp, nh phng php truy nhp trng cu trc, v ch s mng thch hp truy nhp vo mt s hoc mt mng t bo. Sau y l mt v d da trn cu trc bills v client xt trn: >> bills.name ans = John Doe ans= Alice Smith >> bills(2).cost ans= 112.3500 >> bills(1) ans= name : ' John Doe ' cost : ' 86.5000 ' payment: 10.000 20.0000

45.0000

>> baldue = bills(1).cost - sum(bills(1).payment ) baldue= 6.5000 >> bills(2).payment(2) ans = 12.3500 >> client(2).test.AIC(3) ans= 7.000 Phng php ch mc trc tip thng c s dng truy nhp gi tr trng. Tuy nhin, cc M-file nu tn cc trng c gi ra t hm fieldnames, th hm getfield v setfield c th c s dng truy nhp d liu trong cu trc. V d : >> getfield(bills,{1},'name' ) % tng t nh bills(1).name

ans= John Doe >> T = fieldnames(bills); >> getfriend(bills,{2},T{3},{2})%tng t nh s(2),payment(2) ans= 12.3500 V d sau tr li cu trc c cha cng kiu d liu nh cu trc nguyn thu vi mt gi tr b thay i. Dng lnh tng ng ca client(2).test.AIC(3) = 7.1. l: >> client = setfield(client,{2 },'test', 'AIC ',{3},7.1) client= 1x2 struct array with fields name cost test >> client(2).test.AIC(3) ans= 7.1000 Mt trng c th c thm vo trong mt mng cu trc ch n gin bng cch gn gi tr cho trng cu trc mi. >> client(1).addr = {' MyStreet';' MyCity '} client = 1x2 struct array with fields name cost test addr Mt trng c th c b i khi cu trc ( hoc mt mng cu trc ) bng lnh rmfield. S= rmfield ( S, field ) s b i trng field t cu trc S. S= rmfield ( S, F ), trong F l mt mng t bo ca tn cc trng, b i nhiu hn mt trng t cu trc S ti mt thi im. >> client = rmfield( client,' addr ') client = 1x2 struct array with fields name cost test 19.9 S nghch o v hm kim tra

S nghch o gia cc mng t bo v cc cu trc bng cch dng hm struct2cell v cell2struct . Tn trng phi c cung cp y cho cell2struct v b mt i khi chuyn thnh mt mng t bo t mt cu trc. S chuyn i t mng s v mng xu k t thnh mng t bo bng cch s dng hm num2cell v cellstr. Ngc li chuyn i t mt mng t bo thnh mng k t bng hm char. Mc d hm class tr v kiu kiu d liu ca i tng, class vn khng thun tin s dng kim tra kiu d liu. Hm isa(x, class ) tr li true nu x l mt i tng kiu class . V d, isa ( client, struct ) s tr li true. thun tin, mt s hm kim tra s khc c sn trong th vin chng trnh nh: isstruct, iscell, ischar, isnumeric, v islogical.

Chng 20

Biu tng ca hp cng c ton hc

c chng trc, bn bit c MATLAB mnh ra sao trn phng din lp trnh, tnh ton. Mc d kh nng tnh ton ca n rt mnh, tuy nhin n vn cn c nhng hn ch. Nh mt my tnh, MATLAB c s s dng cc con s. N nhn cc s (123/4) hoc cc bin (x =[ 1 2 3 ]). Hp cng c ton hc l mt tp hp cc cng c ( hm ) MATLAB s dng nhm gii cc bi ton. C cc cng c t hp, n gin ho, tch phn, vi phn v gii cc php ton i s v php ton vi phn. Cc cng c khc s dng trong i s hc tuyn tnh chuyn i chnh xc dng nghch o, nh thc v cc khun mu tiu chun. Cc cng c trong Symbolic Math Tollbox c to nn t chng trnh phn mm mnh c tn l Maple@ pht trin khi u t trng i hc Waterloo Ontario, Canada v by gi l phn mm ca hng Waterloo Maple Software. Khi bn yu cu MATLAB thc hin mt php ton, n s s dng cc hm ca Symbolic Math Tollbox lm vic ny v tr li kt qu ca s lnh. 20.1 Biu thc v cc i tng c trng MATLAB c s s dng mt s cc kiu i tng khc nhau lu tr gi tr. Bin s hc dng lu tr gi tr s hc, v d nh x=2, mng k t lu tr chui vn bn, v nh :

t = A text string . Hp cng c ton hc c trng dng nhng i tng ton hc thay th cc bin v cc ton t, v d: x = sym ( x ). Cc i tng ton hc c s dng bi MATLAB trong nhiu trng hp tng t nh cc bin s hc v chui c s dng. Biu thc ton hc l nhng biu thc c cha i tng ton hc thay th cho cc s, hm, ton t.v cc bin. Cc bin khng yu cu phi nh ngha trc. Thut ton l cng c thc hnh gii quyt nhng bi ton trn c s bit cc nhng quy lut v s nhn dng cc biu tng c a ra, chnh xc nh ci cch bn gii bng i s hc v s tnh ton.. Cc ma trn ton hc l nhng mng m phn t ca n l cc i tng ton hc hoc cc biu thc. 20.2 To v s dng cc i tng c trng i tng c trng c xy dng t nhng chui k t hoc cc bin s hc s dng hm sym. V d x = sym ( x ) to ra mt bin c trng x, y = sym ( y ) to ra mt bin c trng y, y = sym ( 1/3 ) to ra mt bin c trng y mang gi tr 1/3. Gi s bin c trng c nh ngha, n c th c s dng trong cc biu thc ton hc tng t nh cc bin s hc c s dng trong MATLAB . Nu nh cc bin x, y c to ra trc th lnh z= (x+y) / ( x-2 ) s to mt bin mi z bi v biu thc m n thay th c mang mt hay nhiu bin c trng x hoc y. Mt i tng s hc c th chuyn thnh i tng c trng. Di y l mt v d: >> m = magic(3) m = 8 1 3 5 4 9 >> M = sym(m) M = [ 8, 1, [ 3, 5, [ 4, 9, >> det(M) ans = -360 % to mt ma trn s 6 7 2 % to mt ma trn c trng t m 6 ] 7 ] 2 ] % xc nh nh thc ca ma trn c trng M

V d ny xy dng mt ma trn vung 3x3, chuyn i thnh ma trn c trng, v tm nh thc ca ma trn. Hm sym cho php bn la chn nh dng cho s hin thi c trng ca gi tr s. C php l: S = sym ( A, fmt ) trong A l gi tr s hoc ma trn cn fmt l mt c tnh nh dng tu chn, c th l f , r , e , hoc d . Gi tr mc nh l r . Nu chn f tng ng h ch s lc phn, r tng ng ch s hu t, e tng t nh r nhng dng chnh tc hm m, cn d tng ng ch s h thp phn. Di y l mt s v d v s hin th ca mt s nh dng tu chn:

Lnh format short format long format short e format long e format short g format long g format hex format bank format rat format + sym ( 1/3, f ) sym ( 1/3, r ) sym ( 1/3, e ) sym ( 1/3, d )

Dng hin th 1/3 0.3333 0.333333333333333 3.3333e-001 3.333333333333333e-001 0.33333 0.333333333333333 3fd5555555555555 0.33 1/3 + 1.555555555555 *2^(-2) 1/3 1/3-eps/12 .333333333333333333314829616256

Lp double double double double double double double double double double sym sym sym sym

S khc nhau gia cc nh dng c trng c th gy ra mt s hn n. V d: >> sym(1/3)- sym(1/3,'e') ans = 1/12*eps >> double(ans) ans = 1.8504e-17 % li du m s hu t % nh dng thp phn

20.3 S biu din biu thc c trng ca MATLAB MATLAB c cc biu thc c trng ging nh l biu thc c cha i tng c trng khc nhau gia chng v bin s, biu thc, php ton nu khng chng gn ging nh biu thc MATLAB c bn. Sau y l mt vi v d ca biu thc c trng. Biu thc tng trng S trnh by trong MATLAB x=sym( x ) y= x=sym(x) M= syms x a b

x=sym(x) cos(x2)-sin(2x) syms(a , b , c ,d ); f= f = int (x^3/ sqrt (1-x),a, b) Cc hm c trng ca MATLAB cho php bn thao tc nhng biu thc ny theo nhiu cch khc nhau. V d:

>> x = sym('x') >> diff(cos(x)) ans = -sin(x)

% to mt bin c trng x % i ca cos(x ) vi bin s l x

>> sym('a','b','c','d' )% to bin s c trnga, b, c v d >> M = [a, b, c, d] % to mt ma trn c trng M = [a, b] [c, d] >> det(M) % tm nh thc ca ma trn c trng M ans = a*b - b*c Trong v d u tin, x c nh ngha nh mt bin c trng trc khi n c s dng trong biu thc, tng t nh vy bin s phi c gn mt gi tr trc khi chng c s dng. iu ny cho php MATLAB xem xt cos(x) nh mt biu thc c trng, v do vy dif(cos(x)) l mt php ton c trng hn l mt php ton s hc. Trong v d s 2, hm syms thng c nh ngha l mt s bin s c trng. syms(a, b ) tng ng vi a = sym('a'); b= sym('b' ); . MATLAB bit rng M=[a, b; c, d ] l mt ma trn c trng bi v n cha ng mt bin s c trng, v do det(M) l mt php ton c trng. Trong MATLAB, cu lnh func arg tng ng vi func(arg), trong func l mt hm, cn arg l mt chui i s k t. MATLAB phn bit syms a b c d v syms(a, b, c, d ) l tng ng nhng nh cc bn bit cng thc u tin d thc hin hn. Chng ta xem xt k hn v d th hai nu trn: >> a = 1; b = 2; c = 3; d = 4 % nh ngha bin s a n d >> M = [a,b;c,d] % M l mt ma trn s M= 1 2 3 4 >> size(M) %M l mt ma trn bc hai ans = 2 2 >> class(M) % C nhng loi i tng no l M? ans = double >> M = '[a, b; c, d ]' % M l mt chui c trng M = [a, b :c, d ] >> size(M) % M l mt vector hng ca 9 k t ans = 1 9 >> class( M )

ans = char >> M = sym('[a,b;c,d ]') % mt i tng c trng nhng % khng phi l mt ma trn M= [a,b;c,d] >> size(M) % M l mt vector 3 phn t (2 du phy ) ans = 1 3 >> class(M) ans = sym >> syms a b c d % nh ngha bin s c trng a n d >> M = [a,b;c,d] % M l mt ma trn c trng M = [a, b] [c, d] >> size(M) ans = 2 2 >> class(M) ans = sym >> a = 1; b = 2 ; syms c d % nh ngha mt bin c nh t a >> M = [a,b;c,d] M= [1, 2] [c, d] >> size(M) ans= sym % M l mt ma trn c trng t a n d

Trong v d ny, M c nh ngha theo 5 cch: Kiu th nht: n gn ging vi ma trn bc hai. Kiu th hai l mt chui k t. Kiu th ba l mt i tng c trng hp l, nhng n khng th s dng trong mi trng hp. Kiu th t l mt ma trn bc hai. Kiu cui cng cho thy bin s l bin c trng c kt hp trong biu thc c trng to thnh ma trn c trng. Biu thc c trng khng c bin c gi l hm c trng. Khi hm c trng hin th, chng i khi kh m phn bit c vi s nguyn. V d:

>> f=sym(3) %to mt hng c trng f= 3 >> class(f) % kiu ca i tng f l g ans= sym >> g = sym(pi) g= pi >> class(g) ans= sym >> h = sym(sin(pi/4)) h= sqrt(1/2) >> class(h) ans= sym 20.4 Bin c trng Khi lm vic vi biu thc c trng c nhiu hn mt bin c trng, chnh xc hn mt bin l bin c lp. Nu MATLAB khng ch ra u l bin c lp th n s nhn bin no gn x nht theo th t ch ci. Bin c lp i khi cn c gi l bin t do. Bn c th yu cu MATLAB ch ra bin no trong biu thc c trng. bit c ta s dng hm findsym: >> syms a s t u omega i j >> findsym(a*t+s/(u+3),1) ans = u >> findsym(sin(a+omega),1) ans = omega >> findsym(3*i + 4*j) ans = ' ' % nh ngha cc bin c tng % u l gn x nht % omega gn x nht % i v j tng t nh sqrt(-1)

Nu findsym khng tm thy bin c trng, n s tr li chui rng. 20.5 Php ton trn biu thc c trng Gi s bn to to c biu thc c trng, bn rt c th mun thay i n bng bt c cch no. Bn mun ly ra mt phn ca biu thc, kt hp hai biu thc hoc tm mt gi tr s ca mt biu thc c trng. C rt nhiu cng c cho php bn lm iu ny.

Tt c cc hm c trng, ( vi vi im c bit s ni phn sau) da trn cc biu thc c trng v cc mng c trng. Kt qu ging nh mt s nhng n l mt biu thc c trng. Nh chng ta ni trn, bn c th tm ra u l kiu s nguyn, mt chui c trng hoc mt i tng c trng bng cch s dng hm class t MATLAB c s.

20.6 Tch cc t s v mu s Nu biu thc ca bn l mt a thc hu t hoc c th m rng ti mt a thc hu t tng ng ( bao gm ton b cc phn t ca t s c chung mu s), bn c th tch t s v mu s bng cch s dng hm numden. V d: m = x2, f = a x2/( b-x) g = 3 x 2 /2 + 2 x /3 -3/5. 2 h = (x + 3)/ ( 2 x - 1 ) + 3x/(x-1) numden t hp hoc hu t ho biu thc nu cn thit, v tr li kt qu t s v mu s. Cu lnh MATLAB c thc hin nh sau: >> sym x a b % to mt s bin c trng >> m = x^2 % to mt biu thc n gin m = x^2 >> [n,d] = numden(m) % tch t s v mu s. n = x^2 d = 1 >> f = a*x^2/(b-x) % to mt biu thc lin quan f = a*x^2/(b-x) >> [n d] = numden(f) % tch t s v mu s. m = -a*x^2 d= -b + x Hai biu thc u tin cho ta kt qu nh mong mun >> g = 3/2*x^2 + 2*x - 3/4 g = 3/2*x^2 + 2*x - 3/4 >> [n,d] = numden(g) n = 6*x^2 + 8*x - 3 % to mt biu thc khc. % hu t ho v tch cc phn

d = 4 >> h = (x^2 + 3)/(2*x - 1) + 3*x/(x - 1) % tng ca a thc hu t h = x^3 + 5*x^2 - 3 d= (2*x - 1)*(x - 1) >> h2 = n/d % to li biu thc cho h h2 = (x^2 + 3)/(2*x - 1) + 3*x/(x - 1) Hai biu thc g v h c hu t ho hoc tr v biu thc n gin vi mt t s v mu s, trc khi cc phn t c tch c th chia t s cho mu s to li biu thc nguyn gc. 20.7 Php ton i s tiu chun Mt s php ton tiu chun c th biu din trn biu thc c trng s dng cc ton t quen thuc. V d cho hai hm: f = 2x2 + 3x - 5 g = x2 - x + 7

>> sym('x') % nh ngha mt bin s c trng >> f = (2*x^2 + 3*x - 5) % nh ngha biu thc c tng f v g f= (2*x^2 + 3*x - 5 ) >> x^2 - x + 7 g = x^2 - x + 7 >> f + ans = 3*x^2 + 2*x + 2 >> f - g % tm biu thc ca f-g ans = x^2 + 4*x - 12 >> f*g % tm mt biu thc ca f*g ans = (2*x^2 + 3*x -5 ) *( x^2 - x + 7) >> f/g % tm mt biu thc ca f/g ans = (2*x^2 + 3*x - 5 )/(x^2 - x + 7) >> f ^(3*x) % tm nt biu thc cho f3x ans = (2*x^2 + 3*x - 5)*3*x Thc s l mt php ton trn bt c biu thc no cha t nht mt bin s c trng s cho kt qu ca mt biu thc c trng, bn hy t hp cc biu thc c nh to nhng biu thc mi. V d:

>> a = 1; b = 3/2

>> f = sin(a - x) ans= -sin(x-1) >> g = sin(b*x^2) ans= sin(3/2*x^2) >> b*f/(g - 5)+ x % kt hp chng ans = -3/2*sin(x - 1)/(sin(3/2*x^2)- 5 )+ x )

; x = sym('x'); % to mt s v % nhng bin s c trng % to mt s biu thc

Tt c cc php ton ny u thc hin tt vi cc i s l mng. 20.8 Cc php ton nng cao MATLAB c th biu din nhiu php ton nng cao hn biu thc c trng. Hm compose kt hp f(x ) v g ( x) thnh f ( g(x)). Hm finverse tm hm nghch o ca mt biu thc v hm symsum tm tng c trng ca mt biu thc. V d : ) f = 1/ ( 1 + x2 ) g = sin ( x ) h = x/ ( 1 + u 2 ) k = cos ( x+v

>> syms x u v % nh ngha 3 bin c trng >> f = 1/(1+x^2) % to 4 biu thc >> g = sin(x) >> h = x/(1 + u^2) >> k = cos(x + v) >> compose(f,g) % tm biu thc ca f( g ( x )) ans = sym(1/(1 + x^2)) compose c th c s dng cc hm m c cc bin c lp khc nhau. >> compose(h,k) % cho h( x), k ( x ), tm h( k(x) ) ans= cos(x + v)/(1 + u^2) >> compose(h,k,u,v) % cho h( u), k( v ), tm h( k( v)) ans = x/(1 + cos(2*v)^2) Hm nghch o ca mt biu thc, gi l f(x), l biu thc g (x) m tho mn iu kin g( f (x)) = x. V d hm nghich o ca ex l ln(x), do vy ln(ex) =x. Hm nghch o ca sin(x) l arcsin(x), v hm nghch o ca 1/tan(x) l arctan(1/x). Hm finverse tr thnh

hm nghch o ca mt biu thc. Ch finverse tr li duy nht mt kt qu thm ch nu kt qu khng l duy nht. >> syms x a b c d z % nh ngha mt s bin c trng >> finverse(1/x) % nghch o ca 1/x l x ans = 1/x >> finverse(x^2) % tm mt trong cc gii php g(x2 ) =x ans = x^(1/2) >> finverse(a*x + b) % tm gii php g(f(x)) = x ans = -(b - x)/a >> finverse(a*b + c*d - a*z,a) tm gii php g(f(a))=a ans= -(c*d - a)/(b - z) Hm symsum tm tng c trng ca mt biu thc. C 4 c php ca hm: symsum(f) tr li tng , symsum(f,s) tr li tng , symsum(f,a,b) tr li tng , cn hm symsum(f, a, b, s) tr li tng . Chng ta cng xem xt tng , tr li x3/3-x2/2+x/6 >> syms x n >> symsum(x^2) ans = 1/3*x^3 - 1/2*x^2 + 1/6*x 20.9 Hm nghch o Mc ny trnh by cc cng c chuyn i biu thc c trng sang gi tr s v ngc li. C mt s rt t cc hm c trng c th tr thnh gi tr s. Hm sym c th chuyn i mt chui hoc mt mng s thnh s biu din c trng; hm double thc hin ngc li. duble chuyn i mt hng c trng ( mt biu thc c trng khng c bin) thnh gi tr s c kiu xc nh double. >> phi = sym('(1 + sqrt(5))/2') phi = (1 + sqrt(5))/2 >> double(phi) % nghch o ca gi tr s ans = 1.6180 Hai cch trn cho ta cng mt kt qu. Bn lm vic vi a thc trn MATLAB c bn, s dng vector m cc phn t ca n l cc h s ca a thc. Hm c trng sym2poli chuyn i mt a thc c trng thnh vector ca h h s . Hm poli2sym th lm ngc li, v bn hy khai bo bin s dng trong php ton cui cng.

>> x = sym('x') >> f = x^3 + 2*x^2 - 3*x + 5 % f l a thc c trng f = x^3 + 2*x^2 - 3*x + 5 >> n = sym2poli(f) % tch vector cc h s n = 1 2 -3 5 >> poly2sym(n) % to li a thc ca x ( mc nh ) ans = x^3 + 2*x^2 - 3*x + 5 >> s = sym('s') % nh ngha s nh l bin c trng >> poly2sym(n,s) % to li a thc ca f ans= s^3 + 2*s^2 - 3*s + 5 20.10 S thay th bin s Gi s bn c mt biu thc c trng ca x, v bn mun i bin thnh y. MATLAB cung cp cho bn cng c thay i trong biu thc c trng, gi l subs. C php l: subs( f, old, new ), trong f l mt biu thc c trng, old l bin hoc biu thc c trng, v new l bin c trng, biu thc hoc ma trn hoc mt gi tr s hoc ma trn. Ni dung ca new s thay th old trong biu thc f. Di y l mt s v d: >> syms a alpha b c s x % nh ngha mt vi bin c trng >> f = a*x^2 + b*x + c % to mt hm f(x) f = a*x^2 + b*x + c >> subs(f,x,s) % thay th xbng s trong biu thc ca f ans= a*s^2 + b*s + c >> subs(f,a,[alpha;s]) % thay th a bng ma trn c trng a ans= [alpha*x^2 + b*x + c] [s*x^2 + b*x + c] >> g= 3*x^2 + 5*x - 4 % to mt hm khc g= 3*x^2 + 5*x - 4 >> h = subs(g,x,2) % new l mt gi tr s h = 18 >> class(h) % biu din kt qu l mt ni dung c trng ans = sym

V d trc biu din cch subs to h s, v sau lm n gin ho biu thc. T kt qu ca h s l mt ni dung c trng, MATLAB c th rt gn n thnh mt gi tr n. Ch rng subs l mt hm c trng, n tr thnh mt biu thc c trng, mt ni dung c trng thm ch n l mt s. nhn mt s chng ta cn s dng hm double chuyn i chui . >> double(h) ans= 18 >> class(ans) ans= double 20.11 Php ly vi phn Php ly vi phn ca mt biu thc c trng s dng hm diff theo mt trong 4 mu sau: >> syms a b c d x s % nh ngha mt vi bin c trng >> f = a*x^3 + x^2 - b*x - c % nh ngha mt biu thc c trng f = a*x^3 + x^2 - b*x - c >> diff(f) % ly vi phn ca f vi x l bin mc nh ans = 3*a*x^2 + 2*x - b >> diff(f,a) % ly vi phn ca f vi a thay cho x ans = x^3 >> diff(f,2) % ly vi phn f hai ln vi ? ans= 6*a*x + 2 >> diff(f,a,2) % vi phn 2 ln vi ? ans= 0 Hm diff cng c th thao tc trn mng. Nu f l mt vector c trng hoc ma trn, diff( f) ly vi phn mi phn t trong mng: >> f = [a*x,b*x^2;c*x^3,d*s] % to mt mng c trng f = [ a*x b* x^2 ] [ c*x^3 d*s ] Ch rng hm diff cng s dng trong MATLAB c bn tnh php vi phn s hc ca mt vector s v ma trn. 20.12 Php tch phn % chuyn i mt biu thc c trng thnh mt s % biu din kt qu l mt gi tr s

Hm tch phn int(f ) trong f l biu thc tng trng, s tm ra mt biu thc tng trng F khc sao cho diff(F)=f. Nh bn thy trong phn nghin cu php tnh, php tch phn phc tp hn php vi phn.Tch phn hoc o hm khng tn ti di mt hnh dng khp kn; hoc n c th tn ti nhng phn mm khng tm ra n hoc phn mm c th tm ra n nhng khng b nh hoc thi gian chy. Khi MATLAB khng tm thy php tnh o hm n a ra cnh bo v s thay th tng trng php tch phn khng th s dng vi hm pretty. >> x = sym('x'); >> p = int(log(x)/exp(x^2)) % ly tch phn Warning:Explicit integral could not be found. In C:\MATLAB\toolbox\symbolic\@sym\int.m at line 58 p = int(.... >> pretty(p) ans = output from pretty Hm tch phn, cng nh hm vi phn u c nhiu hn mt c php. int(f) s tm mt php tnh tch phn theo cc bin c lp mc nh, cn int(f, s ) tm php ly tch phn theo bin c trng s. Khun mu int( f, a, b ) v int (f, s, a, b ), trong a, b l cc bin s, tm ra biu thc c trng cho php ly tch phn theo cn t a n b. Tng t cho hm int(f, m, n ) v int ( f, s, m, n ). >> syms x s m n % nh ngha mt s bin >> f = sin(s + 2*x) % to mt hm tng trng f= sin(s+2*x) >> int(f) % php ly tch phn theo bin x ans= -1/2*cos(s+2*x) >> int(f,s) % php ly tch phn theo i s s ans= -cos(s + 2*x) >> int(f,pi/2,pi) % ly tch phn theo bin x vi cn t pi/2 n pi ans= -cos(s) >> int(f,s,pi/2,pi) % ly tch phn theo s, cn t pi/2 n pi ans= 2*cos(x)^2 - 1 - 2*sin(x)*cos(x) >> g = simple(int(f,m,n)) % ly tch phn theo x, cn t m n n g = -1/2*cos(s + 2*n) + 1/2*cos(s + 2*m) Trong v d ny, hm simple c s dng n gin ho kt qu ca php ly tch phn. Chng ta s nghin cu thm v hm simple sau ny.

Cng nh hm diff, hm ly tch phn int trn mi phn t ca mng c trng: >> syms a b c d x s % nh ngha mt s bin c trng >> f = [a*x,b*x^2;c*x^3,d*s] % xy dng mt mng c trng f= [a*x, b*x^2 ] [c*x^3, d*s ] >> int(f) % ly tch phn mng cc phn t theo i s x ans = [1/2*a*x^2, 1/3*b*x^3] [1/4*c*x^4, d*s*x] V d : Gii php c trng ca mt phng php tnh ton c in Fox Mulder, ang gim st trn mt mi nh ca mt to cao c Roswell, New Mexico, trong khi ang n ba tra th anh ta cht pht hin ra mt vt c hnh dng k l trn khng cao 50 m. Anh ta ly mt qu c chua chn ra khi chic ti eo sau lng, t vo cnh ca mi nh ri nm mnh qu c chua vo khng trung. Qu c chua c bay ln vi vn tc ban u l v0 = 20 m/s. Mi cao 30 m so vi mt t, thi gian bay ca n l t giy. Hi khi no n t n cao cc i, cao m qu c chua t ti so vi mt t? Khi no th qu c chua chm ti mt t? Gi s rng khng c lc cn ca khng kh v gia tc ph thuc vo sc ht l khng i l a =-9.7536 m/s2. Chng ta chn mt t cao l 0, y = 0 l mt t v y = 30 l nh ca to nh. Vn tc tc thi s l v = dy/dt, v gia tc s l a = d2y/dt2 . Do nu ly tch phn mt ln gia tc, ta s c vn tc tc thi, cn tch phn vn tc ta s c cao y. >> t = sym('t'); % nh ngha bin dc trng thi gian >> digits(5); % chnh xc 5 ch s >> a = sym('-9.7536') % gia tc o bng m/s2 a = -9.7536 >> v = int(a,t) %vn tc xem nh hm thi gian v = -9.7536*t >> v = v + 20 % thi im t=0 vn tc l 20m/s v = -9.7536*t + 20 >> y = int(v,t) %tm cao y thi im t bng cch ly tch phn y = -4.8768*t^2+20.*t >> y = y + 30 % cao khi t=0 l 30 m y = -4.8768*t^2 + 20.*t + 30 Kim tra xem kt qu c ng khng, nu nh chng ta thay t=0 vo trong biu thc, ta c:

>> yo = subs(y,t,0) yo = 30. kt qu ng nh cao qu c chua trc khi n c nm. By gi chng ta c vn tc v v tr l hm ca thi gian t. cao cc i khi m qu c chua ngng ln v bt u ri xung. tm im ny, ta tm gi tr ca t khi v=0 bng cch dng hm solve. Hm ny tm im khng ca biu thc c trng, hay ni cch khc, solve(f), trong f l hm ca x, tm x khi cho f(x) =0. >> t_top = solve(v) t_top = 2.0505 % tm gi tr ca t khi v(t)=0

Bi v solve l mt hm c trng, n tr li mt hng c trng ( thm ch n trng nh mt s). By gi chng ta tm cao cc i, thi im t = 2.0505 s. >> y_max = subs(y, t, t_top ) % thay th t bi t_top trong y y_max = 50.505 Ch rng hm subs c cng gi tr nh chng ta lm trc khi chng ta kim tra biu thc y, subs s thay bin c trng 2.0505 vo cc gi tr t trong biu thc. By gi chng ta tm thi gian qu c chua chm mt t. >> t_splat = solve(y) t_splat = [ -1.1676 ] [ 5.2686 ] % qu c chua chm mt t khi y =0

Do kt qu l s m v qu c chua khng th chm t trc khi n c nm i, v nghim th hai mi l nghim c ngha. T suy ra cao ca qu c chua thi im t giy c cho bi phng trnh y = -9.7536t2 + 20t + 30, qu c chua t ti cao cc i 50.505m so vi mt t v thi im t = 2.0505 s, v n chm mt t thi im t = 5.2686 s 20.13 V th biu thc c trng c mt tng tt hn v chuyn g xy ra vi qu c chua, chng ta v kt qu ca tr chi ny. Gi v tr ca qu c chua ( cao) c miu t bng biu thc y = (- 4.8768)*t^2 + 20*t + 30 >> ezplot(y) % v cao qu c chua

-4.8768*t^2+20.*t+30

50

40

30

20

10

-10

-20

-30 0 1 2 3 t 4 5 6

Nh bn thy, ezplot v th hm c trng trong di -2 t 2.

20.14 nh dng v n gin ho biu thc i khi MATLAB tr li mt biu thc c trng qu kh c th c. Mt s cng c c sn tr gip lm cho biu thc d c hn. Trc tin l hm pretty. Lnh ny hin th biu thc c trng theo mt khun mu tng t nh ku ton hc. Chng ta hy xem s m rng chui Taylor: >> x = sym('x'); >> f = taylor(log(x+1)/(x-5)) f = -1/5*x+3/50*x^2-41/750*x^3+293/7500*x^4-1207/37500*x^5 >> pretty(f) 2 41 3 293 4 -1/5 x + 3/50 x - --- x + ---- x 750 7500 1207 5 ----- x 37500

Biu thc c trng c th a ra di nhiu dng tng t nhau. MATLAB s dng mt s lnh n gin ho hoc thay i khun mu trong biu thc c trng. >> x = sym('x');

>> f = (x^2 - 1)*(x - 2)*(x - 3) % to mt hm f = (x^2 - 1)*(x - 2)*(x - 3) >> collect(f) % gom tt c cc mc nh nhau ans = x^4 - 5*x^3 + 5*x^2 + 5*x - 6 >> horner(ans) ans = -6 + (5 + (5 + (-5 + x)*x)*x)*x >> factor(ans) % biu din di dng mt a thc ans = (x - 1)*(x - 2)*(x - 3)*(x + 1) >> expand(f) ans = x^4 - 5*x^3 + 5*x^2 + 5*x - 6 simplify l mt cng c rt mnh, mc ch c bn l n gin ho biu thc di nhiu kiu khc nhau nh: tch phn v lu tha phn s; lut s m v hm log; v Bessel, hnh hc v hm gamma. Mt vi v d s minh ho iu ny: >> syms x y a >> simplify(sin(x)^2 + 3*x + cos(x)^2 - 5) ans = -4 + 3*x >> simplify(log(2*x/y)) ans = log(2) + log(x/y) >> simplify((-a^2 + 1)/(1 - a)) ans = a + 1 20.15 Tm tt v mt s c im khc Biu thc c trng s phc trong c php MATLAB c th c trnh by theo mt hnh mu m ta c th d ng c bng vic s dng hm pretty. C th c nhiu kiu tng t nhau ca biu thc c trng, mt s chng th d dng s dng hn mt s khc trong nhng tnh hung khc nhau. MATLAB a ra mt s cng c thay i khun dng trong biu thc. l :

Cng c collect factor expand simplify simple

M t Gom tt c cc mc ging nhau Biu din di dng mt a thc M rng tt c cc mc n gin ho cc biu thc Tm biu thc tng ng c chui k t ngn nht

Hm c trng MATLAB c th c s dng chuyn biu thc c trng thnh phn thc., cho mt a thc hu t th int( f ) s ly tch phn hm ny, v diff( f ) s ly vi phn hm ny. V d:

>> s = sym('s'); >> Y =(10*s^2 + 40*s + 30 )/(s^2 + 6*s + 8) Y = (10*s^2 + 40*s + 30)/(s^2 + 6*s + 8) >> diff(int(Y)) ans = 10 - 15/(s + 4) - 5/(s + 2) >> pretty(ans) 15 5 10 - ----- - ----s + 4 s + 2 K thut ny cng tht l hu ch khi ta mun ti gin a thc trong c bc cao hn mu s. >> x = sym('x'); >> g = (x^3 + 5)/(x^2 - 1) g = (x^3 + 5)/(x^2 - 1) >> diff(int(g)) ans = x + 3/(-1+ x) - 2/(x + 1) >> pretty(ans) 3 2 x + ------ - -----1 + x x + 1 20.16 T lm Tm gi tr ca e vi chnh xc 18,29,30 v 31 s. Ch rng kt qu gn vi mt gi tr s nguyn nht, nhng khng hon ton l mt s nguyn. >> vpa('exp(pi*sqrt(163))',18) 20.17 Gii phng trnh Phng trnh c trng c th c gii bng cng c ton hc c sn trong MATLAB. Mt s c gii thiu, mt s s c chng minh phn sau. 20.18 Gii phng trnh i s n gin

Hm solve gn biu thc c trng v 0 trc khi gii n: >> syms a b c x >> solve(a*x^2 + b*x + c) ans = [1/2/a*(-b + (b^2 - 4*a*c)^(1/2))] [1/2/a*(-b - (b^2 - 4*a*c)^(1/2))] Kt qu l mt vecto c trng m cc phn t ca n c dng nh trn . gii php ton c cha du bng, gii mt chui c cha biu thc: >> solve('a*x^2 + b*x - (-c)') ans = [1/2/a*(-b + (b^2 - 4*a*c)^(1/2))] [1/2/a*(-b (b^2 - 4*a*c)^(1/2))] Nu nh bn mun gii i s khc so vi bin s mc nh th bn c th khai bo trong solve nh sau: >> solve(a*x^2 + b*x + c,b) ans = -(a*x^2 + c)/x Php ton c th gii bng cch gn biu thc cho 0. By gi chng ta s gii cos(x)=sin(x) v tan(x) =sin(2x) theo x, v qui kt qu ca chng v bin f v t: >> f = solve(cos(x)- sin(x)) f = 1/4*pi >> t = solve(tan(x)- sin(2*x)) t = [ 0] [ pi] [ 1/4*pi] [ -3/4*pi] Kt qu di dng s: >> double(f) ans = 0.7854 >> double(t) ans = 0 3.1416

0.7854 -2.3562 20.19 Mt vi php ton i s C th gii vi php ton cng mt lc. Cu lnh [a1, a2, ..., an ] = solve(f1, f2, ...,fn ) gii n php ton cho cc bin mc nh v tr li kt qu trong a1, a2, ..., an. Tuy nhin bin mc nh s c lu tr . V d: >> syms x y >> [a1 a2] = solve(x^2 + x^y + y - 3, x^2 - 4*x + 3) a1 = [ 1] [ 3] a2 = [ 1] [ -(6*log(3)+lambertw(1/729*log(3)))/log(3)] 20.20 Php ton vi phn Thng thng php ton vi phn rt kh gii, MATLAB cung cp cho bn mt s cng c mnh tm kt qu ca php ton vi phn. Hm dsolve s gii cc php ton vi phn v cho ta kt qu. C php ca dsolve khc vi phn ln cc hm khc. i s ca hm phi l xu k t thay v biu thc, v nh xu cha mt du =. iu ny r rng l khc so vi hm solve, m i s ca n phi l mt biu thc c trngkhng c du =. Php ton vi phn c nhn ra bng k hiu ch hoa D v D2, D3, v.v... .Bt k mt ch no theo sau Ds u ph thuc vo bin. Php ton ( d2y/dt2 ) c thay bi chui k t D2y=0. cc bin c lp c th c ch ra, hoc nu khng s mc nh l t. V d gii php ton (dy,dt) - 1+2y2: >> clear >> dsolve('Dy=1+y^2') ans = tan(t - C1) trong C1 l hng s. Cng bi ton trn nhng cho gi tr ban u l y(0) =1 th s c kt qu sau: >> dsolve('Dy=1+y^2, y(0)=1') ans = tan(t+1/4*pi) 20.21 Mt vi php ton tch phn

Hm dsolve c th gii nhiu php ton vi phn cng mt lc. Khi gii nhiu php ton vi phn dsolve tr cc bin vo mt cu trc hoc mt vector nh solve lm. Ch dsolve xp xp cc bin trc khi c lp trc khi tr. V d: Gii php ton sau: df/dt = 3f + 4g dg/d = -4f + 3g >> [f,g] = dsolve('Df = 3*f + 4*g, Dg = -4*f + 3*g') f = exp(3*t)*cos(4*t)*C1 + exp(3*t)*sin(4*t)*C2 g = -exp(3*t)*sin(4*t)*C1 + exp(3*t)*cos(4*t)*C2 20.22 Ma trn v i s tuyn tnh Ma trn c trng v vector l cc mng m phn t ca n l cc biu thc c trng. chng c th c to bi hm sym: >> syms a b c s t >> A = [a,b,c;b,c,a;c,a,b] A = [ a, b, c] [ b, c, a] [ c, a, b] >> G = [cos(t),sin(t);-sin(t),cos(t)] G = [ cos(t), sin(t)] [ -sin(t), cos(t)] Kch thc ca ma trn c trng c th tm c bng hm chun size v length. V d: >> syms a b c d e f >> S = [a,b,c;d,e,f] S = [ a, b, c] [ d, e, f] >> h = size(S) h = 2 3 >> [m,n] = size(S) m = 2 n = 3 >> length(S) ans =

3 Phn t ca mng c trng cng c truy nhp tng t nh mng s >> syms ab cd ef gh >> G = [ab,cd,ef,gh] G = [ ab, cd, ef, gh] >> G(1,2) ans = cd 20.23 Php ton i s tuyn tnh Php nghch o v nh thc ca ma trn c tnh bi hm: inv v det >> H = sym(hilb(3)) H = [1, 1/2, 1/3] [1/2, 1/3, 1/4] [1/3, 1/4, 1/5] >> det(H) ans = 1/2160 >> J = inv(H) J = [ 9, -36, 30] [-36, 192, -180] [ 30, -180, 180] >> det(J) ans = 2160

20.24 Hm bc v xung Hm step, u(t) v hm impulse, (t) thng c dng trong h thng. Hm bc Ku(ta ) trong K l hng s c nh ngha nh sau: Ku(t-a) =0 nu t<a v Ku(t-a)= K nu T>=a. Di y l hm bc:

20.25 Bin i Laplace

Php bin i laplace bin i t min t sang min s. Hm ca n nh sau: L(s) = >> syms a s t w >> f = exp(-a*t)*cos(w*t) f = exp(-a*t)*cos(w*t) >> L = laplace(f,t,s) L= (s + a)/((s + a)^2 + w^2) >> pretty(L) s + a ------------2 2 s + a) + w 20.26 Bin i Fourier Hm bin i Fourier v Fourier ngc nh sau: F() = f(t)= MATLAB dng w thay cho trong biu thc c trng >> syms t w >> f=t*exp(-t^2) f = t*exp(-t^2) >> f=fourier(f,t,w) % bin i fourier s dng tham s t v w f = -1/2*i*pi^(1/2)*w*exp(-1/4*w^2) >> ifourier(f,w,t) % timbin i fourier ngc ans = 1/2*4^(1/2)*t*exp(-t^2) >> simplify(ans) ans = t*exp(-t^2)

chng 21

hp cng c h thng iu khin

21.1 S biu din bng th Phn ln cc cng c trong Hp cng c h thng iu khin u c lun gii d hiu trn c 2 phng din hm truyn v khng gian trng thi. Thm vo h thng nhiu u vo, nhiu u ra (MIMO) c sinh ra t vic to ra ma trn B, C, v D c i hi s chiu. S biu din hm truyn MIMO c hnh thnh do s dng ma trn t bo lu tr trong nhng a thc hm truyn tng ng. V d : >> num = { 10, >> den= { [ 1 %mng t u ra. Hm truyn lin tc H(s)= = m<=n MATLAB: :num = [ N1 N2 ...Nm+1 ], den =[ D1 D2 ... Dn+1 ] Ri rc H(z) = = m<=n MATLAB: num [N1 -1 N2 ... Nn+1 ], den = [ D1 D2 ..... Dn+1] ( mu z ) H(z) == MATLAB: num = [ N1 N2 . Nn+1], den =[ D1 D2 ... D n+1 ] Zero-pole-Gain Lin tc H(s)== m<n P=[ P1 ; ......Pn ] Ri rc H(z)= = Z = [Z1 ; Z2 ; ....Zm], P=[ P1 ; ......Pn ] Khng gian trng thi [ 1 10]; -1, [3 0 ] } ; % mng t bo 10 ], [1 6 10 ]; [ 1 0 ], [1 3 3 ] ; % bo bc hai thay cho h thng c 2 u vo v 2

MATLAB: K, Z = [Z1 ; Z2 ; ....Zm], m<= n MATLAB: K,

lin tc x= Ax + Bu MATLAB : A, B, C, D Ri rc = C x[n] + Du[n] =

y = Cx + Du x[n+1] = Ax[n] + B u[n] MATLAB : A, B, C, D

y[n]

C mt s tng quan t nhin 1-1 gia ch s mng t bo v ch s ma trn hm truyn. 21.2 i tng LTI MATLAB cung cp mt cch tm lc mng d liu tng quan thnh cc i tng tuyn tnh, bt bin theo thi gian, hoc cc i tng LTI. iu ny gio cho vic qun l chng c d dng. V d: >> my_sys= zpk( z, p, k ) Zero/ pole / gain from input 1 to output: 1 s Zero / pole / gain from input 2 to output: 3 ( s+1 ) -------------(s+10) (s+2) xy dng mt i tng LTI zero-pole-gain c tn l my_sys c cha h thng 2 u vo v mt u ra. Cng nh vy: >> H = tf( num, den ) Transfer function from input 1 to output... 10 #1: ......... s+10 -1 #2: ..... s Transfer function from input 2 to output ... s+10 #1:.............. s^2+6 s+10 3s+1 #2: ............ s^2 + 3 s + 3 to mt hm truyn i tng LTI t mng t bo num v den nhp vo trc . Cng nh vy h thng hin ti hin th mt ch d hiu.

Cui cng, i tng LTI khng gian trng thi c hnh thnh nh sau: >> a = [ 0 =0; 1 ; -2 -4 ] ; b = [ 0 1 ]; c = [ 1 1 ] ; d

% inh ngha ma trn khng gian trng thi >> system2=ss( a, b, c, d) a= x1 x2 x1 0 1.00000 x -2.00000 -4.00000 b = u1 x1 0 x2 1.00000 c = x1 x2 y1 1.00000 1.00000 d= u1 y1 0

H thng lin tc theo thi gian Trong trng hp ny, h thng s xc nh cc thnh phn bin gn vi mi phn t v xc nhn h thng l lin tc theo thi gian. xy dng mt h thng gin on theo thi gian, s dng hm zpk, tf, v hm ss, bn nht thit phi khai bo chu k ly mu km theo vi h thng c xem nh l mt i s u vo cui cng.V d: >> dt_sys = tf ( [ 1 0.2 ], [ 1 -1 ], 0.01 ) hm truyn z+0. ........... z-1 thi gian ly mu : 0.01 H thng ri rc theo thi gian ny c chu k ly mu l : 0.01 21.3 Khi phc d liu Gi s i tng LTI c to dng, th d liu trong c th tch ra bng cch s dng hm tfdata, zpkdata, v ssdata. V d : >> [nz, dz ]= tfdata (dt_sys ) nz = % tch ra nh l mng t bo

[1x2 dz =

double ]

[1x2 double ] >> [ n z, dz ] = tfdata (dt_sys, 'v' ) % chch ra nh l vector z = [ -0.2 ] p = [ 1 ] k = 1 >> [z, p, k ] =zpkdata ( dt_sys, 'v' ) % chch ra nh l vector z = -0.2 p = 1 k = 1 >> [ a, b, c, d ] = ssdata(dt_sys) % chch ra ma trnkhng gian trng %thi s a = 1 b = 1 c = 1.2 d = 1 Nu nh mt i tng LTI c xy dng th n c th c tch ra theo bt c mt mu no. 21.4 S nghch o i tng LTI Bn cnh vic tch ccc i tng LTI thnh nhiu kiu khc nhau, chng cn c th c chuyn i thnh cc dng khc nhau bng cch s dng cc hm t to. V d : >> t = tf ( 100, [1 Hm truyn : 6 100]) % xy dng mt hm truyn.

100 ................. s^2 + 6 s + 100 >> sst = ss(t ) a = x1 x1 -6.00000

x2 -6.25000

x2 b = x1 x2 c = y1 d =

16.00000 u1 2.00000 0 x1 0 x2 3.12500

u1 y1 0 H thng lin tc theo thi gian. >> zpkt = zpkt(t) Zero / pole / gain: 100 ................. (s^2+ 6 s + 100 ) 21.5 Thut ton i tng LTI S dng i tng LTI cng cho php bn thit lp thut ton s khi. V d, hm truyn lp ca mt h thng hi tip l G( s ) . Th hm truyn lp gn nht ca l : T(s ) = G(s ) ( 1 + G(s) ). Trong MATLAB, iu ny bt u: >> g = tf( 100, [1 Hm truyn: 100 ............ s^2 + 6 s >> t = g/(1+g) hm truyn: 100 s^2 + 600 s ............................... s^4 + 12 s^3 + 136 s^2 + 600 s >> t = minreal(t) % thit lp hm hu pole-zero Hm truyn: 100 ................... 6 0]) % hm truyn lp

s^2 + 6 s + 100 21.6 Phn tch h thng Hp dng c h thng iu khin( The Control System Toolbox ) c cp n vic phn tch h thng s v thit k hm. hon thin ti liu ny, hy xem help trc tuyn. hiu c mt s c im ca, hy tham chiu n i tng LTI open-loop v closed-loop. >> g = zpk ( [ ], [ 0, Zero/pole/gain : -5, -10 ], 100 ) % h thng open-loop

100 .................... s (s+5 ) ( s+ 10 ) >>t =minreal ( g /( 1 +g ) ) H thng closed-loop Zero / pole/ gain: 100 ..................................... (s+11.38 ) ( s^2 + 3.62 s ) + 8.789 ) Poles ca h thng ny l: >>pole( t ) ans = -11.387 -1.811 + 2.3472 i -1.811 + 2.3472 i th Bode ca h thng c cho nh hnh v:

Hnh 21.1 th Bode n gin ca h thng closed-loop l: >> bode(t) Hnh 21.2 p ng xung ca h thng >> step(t) Hnh 21.3

Ngoi cc phng php nu trn, hp cng c h thng iu khin cn a ra thm cho bn lnh tr gip ltiview. Hm ny cho php bn la chn cc i tng LTI t ca s lnh v quan st cc p ng khc nhau trn mn hnh. 21.7 Danh sch cc hm ca hp cng c h thng iu khin S hnh thnh cc kiu LTI ss zpk tf dss filt set ltiprops Phn tch d liu ssdata zpkdata tfdata dssdata get c tnh ca cc loi class size isempty isct isdt isproper issiso isa Xy dng kiu khng gian trng thi Xy dng kiu zero-pole-gain Xy dng kiu hm truyn Ch r kiu ho php khng gian trng thi ch r b lc s Thit lp hoc sa i c th ca LTI Tr gip tri tit cho c tnh TTI

Tch ma trn khng gian trng thi Tch d liu zero-pole-gain Tch t s v mu s Ch ra verion ca ssdata Truy nhp c tnh gi tr ca LTI kiu model ( ss , zpk , or tf ) S chiu ca u vo/ u ra True cho kiu LTI rng True cho kiu lin tc theo thi gian True cho loi gin on theo thi gian True cho kiu LTI ci tin True cho h thng mt u vo/ mt u ra Kim tra Loi LTI c a ra

S nghch o ss zpk tf c2d d2d Chuyn i thnh khng gian trng thi Chuyn i thnh zero-pole-gain Chuyn i thnh hm truyn Chuyn i t lin tc sang gin on Ly mu li h thng ri rc hoc thm tr u vo

Cc php ton + v *

Cng v tr h thng LTI ( mc song song ) Nhn h thng LTI (mc ni tip )

\ / . [...] inv

Chia tri: sys1\sys2 ngha l: inv (sys1)*sys2 Chia phi: sys1/sys2 c ngha sys1*inv(sys2 ) Hon v ngc Hon v u vo/u ra S kt ni h thng LTI ngang/ dc Nghch o h thng LTI

ng hc pole, eig tzero pzma dcgai norm covar damp esort dsort pade p ng thi gian step impulse inittial khi to lsim Ltiview gensig stepfun p ng tn s bode sigma nyquist nichols ltiview evalfr margin Lin kt h thng append parallel series feeback star

H thng poles S truyn h thng cc s 0 Biu Pole-Zero nh hng DC ( tn s thp) Ch tiu h thng LTI Covar of response ln nhiu trng Tn s t nhin v s suy gim cc h thng Xp xp cc tnh lin tc bi phn thc Xp xp cc tnh ri rc bi bin Xp x pade ca thi gian tr p ng bc p ng xung p ng h thng khng gian trng thi vi trng thi p ng u vo tu p ng phn tch GUI Pht sinh tn hiu u vo cho lsim Pht sinh u vo n v -bc

th Bode ca p ng tn s th gi tr tn s duy nht th Nyquist Biu Nichols p ng phn tch GUI p ng tn s ti mt tn s nht nh Gii hn pha v tng ch

Nhm h thng LTI bi vic thm cc u ra v u vo Kt ni song song ( tng t overload + ) Kt ni ni tip ( tng t overload * ) Kt ni hi tip hai h thng Tch s star( kiu lin kt LFT )

connect khi Dng c thit k c in rlocus acker place estime Cng c thit k LQG lqr, dlqr lqry lqrd kalman lqgrreg Kalman

Chuyn ho t kiu khng gian trng thi sang c tnh biu

Qu tch nghim S thay th cc SISO S thay th cc MIMO Khun dng b nh gi

B iu chnh hi tip v phng trnh bc hai tuyn tnh B iu chnh LQ vi u ra ph B bin i LQ ri rc sang lin tc B nh gi Kalman B bin i LQG c a ra t tng ch LQ v b nh gi

Gii quyt php ton ma trn lyap dlyap care dare S biu din crtldemo jetdemo tin vn chuyn trc thng diskdemo milldemo trn kalmdemo

Gii phng trnh Lyapunop lin tc Gii ph ng trnh Lyapunop ri rc Gii phng trnh i s Riccati lin tc Gii phng trnh i s Riccati ri rc

Gii thiu n hp cng c h thng iu khin Thit k kinh in b chng suy gim m ca phng Thit k b iu khin s a cng iu khin LQG SISO v MIMO ca h thng cn thp Thit k b lc Kalman v m phng

Chng 22

Hp dng c x l tn hiu

22.1 Phn tch tn hiu Hp cng c x l tn hiu cung cp cng c cho kim tra v phn tch tn hiu; kim tra v phn tch tn s ca n hoc ph v xy dng b lc. chng ta xy dng mt tn hiu nhiu sau phn tch n. >> t = linspace(0,10,512); % trc thi gian >> x = 3*sin(5*t)- 6*cos(9*t)+ 5*randn(size(t)); % tn hiu vi nhiu Gaussian >> plot(t,x) % th tn hiu

Hnh 22.1 >> >> >> >> x = fft(x); X = fft(x); Ts = t(2)- t(1); Ts = t(2)- t(1)

Ts = 0.0196 >> Ws = 2*pi/Ts; >> Wn = Ws/2 Wn = 160.5354

>> W = linspace(0,Wn,length(t)/2); >> Xp = abs(X(1:length(t)/2)); >> plot(w,Xp) Hnh 22.2 >> >> >> >> >> i = find(w<=20); plot(w(i),Xp(i)) grid xlabel('tan so, rad/s') titile('Pho bien do cua tin hieu nhieu')
Pho bien do cua tin hieu nhieu 1400

1200

1000

800

600

400

200

0 0 5 10 tan so, rad/s 15 20

Hnh 22.3

chng 23

tr gip

23.1 Ca s lnh tr gip MATLAB tr gip mt s lnh gip bn truy nhp thng tin nhanh chng v cc lnh ca MATLAB hoc cc hm bn trong ca s lnh, bao gm help, lookfor,whatsnew, v info. 23.1.1 Lnh help Lnh help ca MATLAB l cch n gin nht nhn tr gip nu bn bit c topic ca ci cn tr gip. Nhp vo lnh help topic, mn hnh s hin th ni dung ca topic nu nh n tn ti. V nh: >> help sqrt SQRT Square root. SQRT(x) is the square root of the elements of x. complex results are produced if X is not positive See also SQRT Bn s nhn c tr gip ca MATLAB v hm cn bc hai. Mt khc, nu nh bn nhp vo dng lnh sau: >> help cows cows not found th MATLAB s khng bit g v cows. Bi v hm ny khng c trong th vin mu.

Ch : trong v d trn, SQRT c vit ch hoa. Tuy nhin khi s dng sqrt khng bao gi l ch in, do MATLAB l mt ngn ng cht ch nn SQRT s khng c bit n v qu trnh s sinh ra li. >> SQRT (2) ??? SQRT ( | Missing operator, coma, or semicolon. tm tt, tn hm c in hoa cho d c nhng khi s dng, hm s dng k t thng thng. Lnh help hot ng tt nu nu nh bn bit chnh xc topic m bn mun tr gip m iu ny thng kh thc hin, help hng dn bn trc tip truy tm chnh xc cc topic m bn mun, bn ch n gin nhp vo help m khng c topic. >> help HELP topics MATLAB : general MATLAB : ops MATLAB : lang MATLAB : elphun MATLAB : specfun MATLAB : matfun MATLAB : datafun MATLAB : polyfun MATLAB : funfun MATLAB : sparfun MATLAB : graph2d MATLAB : graph3d MATLAB : specgraph MATLAB : graphics MATLAB : uitools MATLAB : strfun MATLAB : iofun MATLAB : timefun MATLAB : datattypes MATLAB : MacOS MATLAB : demos MATLAB : specmat MATLAB : local MATLAB : cntol MATLAB : signal MATLAB : symbolic

- mc ch chung ca cu lnh - cc ton t v cc k hiu c bit - xy dng ngn ng lp trnh. - cc hm ton hc s ng - cc hm ton hc c bit - hm ma trn - i s hc tuyn tnh - hm bin i fourier v phn tch d liu - cc a thc v php ni suy - phng n gii cc ODE v cc hm ca hm - ma trn sparfun - ho 2 chiu - ho 3 chiu - th ph - thao tc ho - cc cng c giao tip ngi s dn v ho - xu k t - tp vo / ra - ngy thng v thi gian - cu trc v kiu d liu - cc hm trong Macintosh - v d v minh ho - ma trn c bit - tham chiu - hp cng c h thng iu khin - hp cng c x l tn hiu - hp cng c ton hc

Thm tr gip trong th mc: topic, nhp vo help topic 23.1.2 Lnh lookfor

Lnh lookfor cung cp s tr gip bng vic tm kim tt c cc dng u ca help topic,v cc M-file trn ng dn m MATLAB ang tm, v tr li danh sch tt c cc file cha t kho m bn khai bo..Mt iu rt quan trng l t kho khng cn thit l mt lnh ca MATLAB. V d: >> lookfor complex CONJ complex conjugate IMAG complex imaginary part REAL complex real part CDF2RDF complex diagonal form to real block diagonal form RSF2CSF real block diagonal form to complex diagonal form CPLXPAIR sort numbers into complex conjugata pairs T kho complex khng phi l mt lnh ca MATLAB, nhng n vn c tm ra phn help gm 6 lnh ca MATLAB. Nu mun bit thng itn v cc lnh ny, hy nhp vo t lnh help. V d: >> help CONJ CONJ complex CONJ (x) is the complex conjugate of X For a complex x, CONJ(X ) = REAL (X) - i * IMAG( X) See also : REAL, IMAG, I, J 23.1.3 Lnh whatsnew v info ng nh tn gi ca n, whatsnew v info hin th nhng thng tin v nhng thay i v nhng s ci tin MATLAB v hp dng c ca n, nu dng m khng c i s, th info s hin th nhng thng tin chung v MATLAB, phong php tip cn MathWorks, cn nu dng c i s, v nh: whatsnew MATLAB hoc info signal, th file Readme cha thng tin Toolbox s hin th, nu n tn ti. 23.2 Ca s tr gip Mt s m rng ca h thng tr gip trong MATLAB5 l ca s help mi. Lnh helpwin s m ra ca s mi trn mn hnh ca bn v bn c th dng chut i di chuyn thanh sng n mc no m bn quan tm. Nu dng lnh helpwin m khng c tham s, th a s help c dng nh hnh sau:

Hnh 23.1 Nhn kp vo bt c topic no hin th trong ca s help, s hin th mt ca s mi cha cc topic con hoc cc hm gn vi n.

Hnh 23.2 Nhn kp vo bt c biu tng no trong s hin th thng tin tr gip v mc .

Hnh 23.3

cc M- File ca Student Edition


HELP Topic

Th mc MATLAB Toolbox / local MATLAB / general MATLAB / ops MATLAB / lang MATLAB / elmat MATLAB / elfun MATLAB / specfun MATLAB / matfun MATLAB / datdfun MATLAB / polyfun MATLAB / funfun MATLAB / sparfun MATLAB / graph2d MATLAB / graph3d MATLAB / specgraph MATLAB / graphics MATLAB / uitools MATLAB / strfun MATLAB / iofun MATLAB / timefun MATLAB / datattypes MATLAB / MacOS MATLAB / demos MATLAB / specmat MATLAB / local MATLAB / control MATLAB / signal MATLAB / symbolic Tp tham chiu startup finish MATLABrc pathdef docopt printopt Cc lnh tham chiu cedit

Quyn u tin mc ich chung ca cu lnh Cc ton t v cc k t c bit Xy dng ngn ng lp trnh Thao tc ma trn v ma trn c s Cc hm ton hc c s Cc hm ton hc c bit i s hc tuyn tnh -cc hm ma trn Bin i Fourier v phn tch d liu a thc v cc phn thc

Phng n gii c ma trn sparfun ho 2 chiu ho 3 chiu th ph Thao tc ho Cc cng c giao Xu k t Tp vo / ra Ngy thng v th Cu trc v kiu Cc hm trong M V d v minh h Matrn c bit Tham chiu Hp cng c h Hp cng c x Hp cng c to

To file khi to To file kho File khi to ch Tm ng dn mc nh Web duyt qua cc mc nh My in mc nh Thit lp dng lnh son tho

terminal colordef graymon chnh sng whitebg Thng tin cu hnh hostid license version

Thit lp u cui ho Thit lp mu mc nh Thit lp ca s ho mc nh cho loi mn hnh cn Thay i mu nn ca trc

Ch s nhn din cc MATLAB server ch Ch s ng k Ch s phin bn MATLAB Mc ch chung ca cu lnh

Thng tin chung help helpwin helpdesk demo whatsnew Readme

Tr gip trc tuyn, hin th vn bn ti cc dng lnh Tr gip trc tuyn,ca s truy xut Tra nhanh thng tin v cc thc mc Chy cc chng trnh mu Hin th cc file Readme Thng tin mi cp nht MATLAB 5

Qun l khng gian lm vic who whos clear pack load save quit Qun l ng dn path addpath rmpath editpath Qun l cc hm v lnh what type edit lookfor which

Danh sch cc bin hin ti Danh sch cc bin hin ti, khun dng di Xo b cc bin v hm khi b nh Hp nht khng gian lm vic Np cc bin vo khng gian lm vic t a Lu cc bin vo a Thot khi mc hin ti MATLAB

Nhn/to ng dn Thm th mc theo ng dn Ri th mc t t ng dn Sa i ng dn Danh sch cc file c trng ca MATLAB trong th mc Danh sch file-M Son tho filr-M Tm kim tt c cc file-M theo t kho Xc nh cc hm v file

pcode inmem mex Cu lnh iu khin echo more diary format Hot ng ca lnh h thng cd pwd dir delete getenv ! dos unix vms web computer M-file g ri debug dbstop dbclear dbcont dbstack dbstatus dbstep dbtype dbup dbquit dbmex

To file-P Danh sch cc hm trong b nh Bin dch hm MEX

Ly li lnh t file-M Kim sot u ra cc trang ca s lnh Lu gi vn bn Thit lp nh dng cho u ra

Thay i th mc lm vic hin ti Hin th th mc lm vic hin ti Danh sch th mc Xo file Ly li bin mi trng Thc hin cu lnh ca h iu hnh Thc hin lnh dos v tr li kt qu Thc hin lnh unix v tr li kt qu Thc hin lnh VMS DCL v tr li kt qu M trnh xt duyt Web Loi my tnh

Danh sch cc lnh g ri To im ngt Di chuyn im ngt Tip tc thc hin lnh Hin th cc hm gi ngn xp Danh sch cc im ngt Thc hin mt hoc nhiu dng Danh sch file-M vi s lng dng Thay i phm vi khng gian lm vic a phng Thot khi ch g ri file- MEX g ri ( ch cho UNIX ) Cc ton t v cc k t c bit

Cc ton t plus (+) uplus (+) minus ( - ) uminus (-) mtimes(. *) times (*)

Cng Cng unary Tr Tr unary Nhn ma trn Nhn mng

mpower (^) power (. ^ ) mldivide (\ ) mrdivide /) ldivide (.\ ) mdivide (./ ) kron Ton t quan h eq (== ) ne ( ~= ) lt (< ) gt (>) le (<= ) ge (>= ) Ton t logic and ( & ) or ( | ) not ( ~ ) xor any all Cc ton t Bitwise bitand bitcmp bitor bitmax bitset bitget bitshift Thit lp cc k t union unique intersect setdiff setxor ismember Cc k t c bit colon () []

Lu tha ma trn Lu tha mng Chia tri ma trn Chia phi ma trn Chia tri mng Chia phi ma trn Sn phm c cng

Bng Khng bng Nh thua Ln hn Nh thua hoc bng Ln hn hoc bng

Logic v Logic hoc Logic ph nh Logic hoc ph nh True nu mi phn t ca vector khc khng True nu tt c cc phn t khc khng

Bitwise and Bit hon thnh Bitwise OR Maximum floating point integer Thit lp bt Nhn bt Dch bit Thit lp lin kt Ch s duy nht Thit lp s giao nhau To s khc nhau Thit lp hoc ph nh True nu thit lp cc thnh vin Du hai chm Du ngoc n Du ngoc vung

{} . . .. ... , ; % ! = transpose(.) ctranspose( ) horzcat [, ] vertcat[; ] subsasgn bsref subsindex

Du ngoc nhn Chm thp phn Truy nhp cu trc trng Th mc m S tip tc Du phy Du chm phy Ch thch Lin quan cu lnh ca h iu hnh Gn Nhy Chuyn v Chuyn v s pc lin hp Gp chui theo chiu ngang Gp chui theo chiu ng Gn subscripted Tham chiu subscripted Ch s subscripted Cu trc ngn ng lp trnh

Cu lnh iu khin if elseif else end for while break return pause Thi hnh v nh gi eval feval evalin builtin assignin run Script, hm, v cc bin script function global mfilename lists

iu kin thc hin cu lnh Dng vi if Dng vi if Kt thc lnh if, for, while Lp li cu lnh mt s ln vng lp while Thot khi vng lp for v while Tr v t hm gi Tm dng cho ti khi nhn mt phm bt k Thc hin xu vi biu thc MATLAB Thc hin hm ch ra bi xu nh gi cc biu thc trong khng gian lm vic Thc hin cc hm c to bi phng php xp chng Gn cc bin trong khng gian lm vic Chy script

V script MATLAB v file-M Thm hm mi nh ngha bin ton cc Tn v cc M-file ang thc hin hin ti Du phy phn chia cc danh sch

exist ngha hay khng isglobal Thao tc vi cc i s nargchk nargin nargout varagin varaout inputname Hin th thng bo error warning lasterr errortrap disp fprintf sprintf u vo tng h input keyboard pause uimenu uicontrol

Kim tra xem cc bin hoc cc hm c c nh True nu l bin ton cc

Cng nhn s lng cc i s u vo S lng hm cc i s u vo S lng hm cc i s u ra Danh sch cc i s u vo, di cc bin Danh sch cc i s u ra, di cc bin Tn i s u vo

Hin th thng bo li v hm hu Hin th thng bo cnh bo Thng bo li trc B qua li trong qu trnh kim tra Hin th mt mng Hin th thng bo nh dng Ghi d liu nh dng vo mt xu

Nhc ngi s dng nhp vo Gi bn phm t M-file i ngi s dng nhp d liu vo To giao din bng chn-ngi s dng To giao din ngi iu khin

Ma trn c bn v thao tc vi ma trn Ma trn c bn zeros ones eye repmat rand randn linspace logspace meshgrid :

Mng s khng Mng s 1 Nhn dng ma trn Ti to v mng S ngu nhin xp xp ng u S ngu nhin xp xp thng thng Vector khng gian tuyn tnh Vector khng gian logarthm Mng X-Y cho th 3 chiu Vector khng gian thng thng v ch s trong ma trn

Thng tin mng c s size length ndims disp isempty isequal isnumaric islogical logical

Kch c ma trn di vector S chiu Hin th ma trn hoc vn bn True nu l ma trn trng True nu ma trn l ng nht True cho mng s True cho mng logic Chuuyn i gi tr s thnh logic

Thao tc vi ma trn reshape diag tril triu fliplr flippud flipdim rot90 find end sub2ind ind2sub

Thay i kch c Ma trn ng cho v ng cho ca ma trn Trch pha di ra ma trn tam gic Trch pha trn ra ma trn tam gic Ma trn flip theo hng tri /phi Ma trn flip theo hng trn/ di Ma trn flip dc theo chiu khai bo Quay i mt gc 90 Tm ch s phn t khc khng Ch s cui Ch s tuyn tnh t multiple subscripts Multiple subscripts t ch s tuyn tnh

Hng v cc bin c bit ans eps realmax realmin pi i, j inf NaN isNaN isinf isfinite flops

Tr li kt qu khi biu thc khng c gn Vit di dng du phy ng S du phy ng dng ln nht S du phy ng dong nh nht 3.1415926535897... n v o V cng Khng phi l mt s True nu NaN True nu s phn t l khng v cng True nu s phn t l v cng m s ch s sau du phy ng

Cc bin c bit ans

Tr li kt qu khi biu thc khng c gn

eps pi i, j inf NaN clock date flops nargin narout

chnh xc sau du phy ng

Khng phi dng s ng h tng Ngy m s hot ng ca du phy ng S lng cc i s ca hm vo S lng cc i s hm ra

Cc loi ma trn c bit comban diag eye gallery hadamar hankel hilb invhilb linspace logspace magic meshdom ones rand toeplitz vander zeros

Bu bn ng cho Nhn dng B mt Hadamard Hankel Hilbert Hilbert o Vector Vector Vung Magic Thc hin cho mesh plots Hng Cc phn t ngu nhin Toeplitz Vandermonde Khng

Cc hm ton hc thng thng

Cc hm lng gic sin cos tan asin acos atan atan2 sinh cosh tanh asinh acosh atanh

Hm sine Hm cosine Hm tangent Hm arcsine Hm arccosine Hm arctangent Hm arctan gc phn t Sine hyperpolic Cosine hyperpolic Tangent hyperpolic Arcsine hyperpolic Arccosine hyperpolic Arctangent hyperpolic

Cc hm ton hc abs angle sqrt real imag conj round fix floor ceil sign rem exp log log10

Tr tuyt i hoc bin s phc Gc pha Cn bc hai Phn thc Phn o Phc lin hp Lm trn n s nguyn gn nht Lm trn n khng Lm trn n m v cng Lm trn n v cng Hm du S lu li hoc cc khi ( modulus) Hm m c s e Logarithm t nhin Log 10 c s

Cc hm c bit airy besselj

Hm airy Hm Bessel loi th nht

bessely besselh besseli besselk beta betainc betaln erf erfc ellipk ellipj gamma gammaln inverf rat

Hm Bessel loi th hai Hm Bessel loi th ba ( hm Hankel ) Sa i hm Bessel loi th nht Sa i hm Bessel loi th hai Hm beta Hm beta khng hon ton Hm logarithm beta Hm li Hm li thnh phn Php tch phn elliptic Hm elliptic Jacobian Hm gamma Hm logarithm gamma Hm li ngc Xp x

Hm l thuyt s hc factor isprime primes gcd lcm rat rats perms nchoosek

H s s khai True nu l s s khai Danh sch cc s s khai B chia chung ln nht Php nhn chung nh nht Xp x hu t u ra hu t S hon v S t hp chp K ca N phn t ho

Trang ho plot loglog semilogx semilogy polar mesh contour meshdom bar

th tuyn tnh X-Y th loglog X-Y th semi-log X-Y th semi-log X-Y th to cc Mt li khng gian 3 chiu th ng bao Min trong ca th li Biu hnh ch nht

errorbar title xlabel ylabel grid text gtext ginput

Thm vo errorbars Tiu th Nhn trc x Nhn trc y K ng li trong th Vn bn v tr bt k Vn bn v tr con tr Nhp ho

iu khin ca s ho axis zoom hold shg clg subplot

Cn chnh trc to v hnh dng ca n Co vo hoc dn ra th Gi th trn mn hnh Hin th th nn mn hnh Xo th trn mn hnh Tch ca s ho ho TRONG KHNG GIAN 3 chiu

Lnh ho thng thng plot3 mesh surf fill3 Cn chnh mu colormap caxis shading hidden brighten Chiu sng surfl lighting material specular diffuse surfnorm

V ng thng v im trong khng gian 3 chiu B mt khng gian 3 chiu T mu b mt khng gian 3 chiu in y a gic 3 chiu

Tra ca bng mu S phn chia bng mu gi Ch lm bng Ch du cc nt Bng tra cu mu ti hoc sng

Lm bng b mt khng gian 3 chiu bng chiu sng Ch chiu sng Ch phn chiu t nhin S phn chiu S phn chiu khuch tn B mt thng thng

Bng mu hsv hot gray pink white bone . .. iu chnh trc axis zoom grid box hold axes Ch thch ho title xlabel ylabel zlabel colorbar text gtext

Bng gi tr mu bo ho Bng mu en- trng- - vng Bng mu chia theo xm tuyn tnh Mu hng Mu trng Mu xm pha ln xanh da tri

iu chnh hnh dng v phn chia Co vo hoc dn ra th ng k li Hp cha trc to Lu th hin ti Xy dng trc mt v tr bt k

Tiu ho Nhn trc x Nhn trc y Nhn trc z Hin th thanh mu Ch thch vn bn Di vn bn n v tr chut

chui k t

Khi qut chung char double cellstr blanks deblank eval Kim tra chui schar iscellstr isletter isspace

To mt chui k t Chuyn chui sang m s k t To mng chui t bo t mng k t Xu rng Di chuyn cc xu rng Thc hin xu vi biu thc MATLAB

True True True True

nu l chui k t ( xu ) nu l mng chui t bo nu l ch hoa trong bng ch ci nu l k t rng

Cc php ton vi chui strcat strvcat strcmp strncmp findstr strjust strrep strtok upper lower Chui v vn bn abs num2str int2str settr sprintf hex2num

Kt ni xu Kt ni dc xu So snh chui So snh N k t u tin ca chui Tm mt xu bn trong xu khc Mng k t ng u Thay th chui bng chui khc Tm th bi trong chui Chuyn chui sang ch hoa Chuyn chui sang k t thng thng

Chuyn i t chui sang gi tr ASCII i t s thnh chui i s nguyn sang chui Thit lp c ch rng ma trn l mt chui i s sang chui Chuyn i chui t h 16 sang dng s

file input/output

M v ng file fopen fclose File nh phn i/o fread fwrite File i/o nh dng fscanf fprintf fgetl fgets input V tr file ferror

M file ng file

c d liu nh phn t file Vit d liu nh phn ln file

c d liu nh dng t file Ghi d liu nh dng ln file c dng lnh t file, thay bng dng mi c dng lnh t file, gi nguyn dng mi Hin th ngi dng nhp vo

Kim ra trng thi file

feof fseek ftell frewind Cc hm xut nhp file load save dlmread dlmwrite Xut nhp file o imread imwrite imfinfo Xut nhp file audio auwrite auread wavwrite wavread Ca s lnh I / O clc home disp input pause

Kim tra xem kt thc file hay cha Thit lp b ch th v ch file Nhn t b ch th v tr file Rewind file

Np khng gian lm vic t file-MAT Lu gi khng gian lm vic vo file - MAT c file phn nh ASCCI Ghi file phn nh ASCCI c phn o t file ho Ghi phn o ln file ho Tr li thng tin v file ho Ghi file m thanh NEXT/ SUN ( . au ) Ghi file m thanh NEXT/ SUN ( . au ) Ghi file Microsoft WAVE ( . wav ) c file Microsoft WAVE ( . wav )

Xo ca s lnh a con tr v u vn bn Hin th mng Thng bo cho ngi s dng nhp vo i t ngi s dng tr li

thi gian v ngy

Gi v ngy hin ti now date clock Cc hm c bn datenum datestr datevec

Gi v ngy hin ti hin th dng s Gi v ngy hin ti hin th dng chui Gi v ngy hin ti hin th dng vector

S ngy ni tip Chui thay th ngy Thnh phn ngy thng

Hm ngy thng calendar weekday eomday datetick

Lch Ngy trong tun Kt thc thng Du tick nh dng cho ngy thng

Hm m cputime tic, toc etime pause

Thi gian cpu tnh theo n v giy B m ngng hot ng Thi gian thit lp Dng trong mt giy

kiu d liu v cu trc

Kiu d liu double sparse char cell struct uint8 inline

Chuyn i thnh double Tto mt ma trn khng lin tc Xy dng mng k t To mng t bo Xy dng hoc chuyn i thnh mng cu trc Chuyn i thnh s nguyn khng du 8 bit Xy dng i tng INLINE

Hm ca mng nhiu chiu cat ndims ndgrid permute ipermute shiftdim

Mng kt ni S chiu To thnh mng cho cc hm N-D v php ni suy Php ni suy s chiu ca mng Nghch o php ni suy s chiu ca mng Chuyn dch s chiu

Hm ca mng t bo cell celldisp cellplot num2cell deal

To mng t bo Hin th ni dung ca mng t bo Hin th thut ho mng t bo Chuyn i mng s thnh mng t bo Phn pht u vo n u ra

cell2struct struct2cell iscell

Chuyn i mng t bo thnh mng cu trc Chuyn i mng cu trc thnh mng t bo True nu l mng t bo

hm cu trc struct fieldsnames getfield setields isfield istruct

To hoc chuyn i thnh mng cu trc Nhn tn trng cu trc Nhn li ni dung ca trng cu trc Thit lp ni dung trng cu trc True nu trng trong mng cu trc True nu l mng t bo

chuyn i d liu ng

Hm t DDE ddeadv ddeexec ddeinit ddereq ddeterm ddeunadv

Thit lp b gim st lin kt a xu ra thc hin Khi to s giao tip DDE Yu cu d liu t cc ng dng Kt thc s giao tip DDE Ci b b gim st lin kt

V d v s th hin

MATLAB/matrn intro inverter matmanip Ca s lnh clc home format disp fprintf

Gii thiu php ton ma rn c bn trong MATLAB Gii thch ma trn o Gii thiu php nhn ma trn

Xo ca s lnh a con tr v u dng Thit lp dng hin th kt qu Hin th ma trn hoc vn bn In s c nh dng

echo General hlep demo who what size lengh clear computer ^C quit exit

Cho php gi li cu lnh

Phng tin tr gip Chy cc chng trnh mu Danh sch cc bin trong b nh Danh sch cc M-file trn a S chiu ca hng v ct di vector Xo khng gian lm vic Loi my tnh Hu bin a phng Lt thc chng trnh Tng t nh quit

(Lp trnh )Programming v file-M input keyboard error function eval feval echo exist casesen global startup getenv menu etime

Nhp s t bn phm Gi bn phm nh M-file Hin th thng bo li nh ngha hm Vn bn c gii thch trong cc bin Hm nh gi c gi ra bi chui Cho php gi li cu lnh Kim tra xem c bin tn ti hay khng Thit lp nhy ca case nh ngha cc bin ton cc Khi to M-file Nhn chui mi trng La chn t bng chn Elapsed time (khng k n thi gian)

Cc file trn a chdir delete diary dir load save type what fprintf pack

i th mc hin ti Xo file Ghi mc Th mc ca file trn a Np cc bin t file Lu cc bin nn file Lit k hm hoc file Hin th cc M-file trn a Vit vo file Nn b nh qua save

a thc poly roots roots1 polyval polyvalm conv deconv residue polyfit

a thc c trng Nghim a thc- phng php ma trn bu bn Nghim a thc- phng php Laguerre c lng a thc c lng a thc ma trn Php nhn Php chia Khai trin a thc S diu chnh chnh lch a thc Cc hm ma trn v i s tuyn tnh

Phn tch ma trn norm normest rank det trace null orth rref subspace Php ton tuyn tnh \ v / inv cond condest lu luinc Gi tr duy nht svd svds poly polyeig condeig qz schur Hm ma trn expm

Ch tiu ma trn hoc vector nh gi ma trn hai ch tiu Hng ma trn nh thc Tng cc phn t trn ng cho chnh Khng gian trng Tnh trc giao Rt gn hng theo hnh bc thang Gc gia hai s m

Li gii php ton tuyn tnh; s dng help slash Ma trn o S iu kin i vi ma trn o nh gi s iu kin mt ch tiu S tm tha s LU Tim tha s LU khng hon thnh

S phn tch gi tr duy nht Mt s gi tr duy nht a thc c trng Vn ca a thc S iu kin vi hy vng S tm tha s cho hm suy rng S phn tch chui

Ma trn theo hm m

logm sqrtm funm

Ma trn logarithm Ma ttrn bc hai nh gi chung hm ma trn

Tm tha s tiu chun qrdelete qrinsert rsf2csf balance

Xo b th mc t s tm th mcQR Gi vo th mc trong s tm tha s QR Mu ng cho thc ti mu ng cho phc tp Cn bng tng chnh xc

Bin i fourier v phn tch d liu

Php ton c bn max min mean median std sum prod hist trapz cumsum cumprod cumtrapz

Thnh phn ln nht Thnh phn nh nht Gi tr trung bnh Gi tr trung tuyn lch gc chun Tng ca cc s hng Kt qu ca cc phn t Biu Hnh thang s nguyn Tng tch lyca cc phn t Kt qu tch luca cc phn t S nguyn tch lu bc thang

Sai phn c hn diff gradient del2 Filtering and convolution ( np, cun ) filter filter2 conv conv2 convn deconv

Sai phn v o hm xp x Gradient xp x Laplacien ri rc

B lc s mt chiu B lc s 2 chiu Php nhn a thc v s nn li Nn 2 chiu Nn n chiu Gii nn v chia a thc

Bin i fourier fft fft2 fftn ifft ifft2 ifftn

Bin i fourier ri rc Bin i fourier ri rc 2 chiu Bin i fourier ri rc n chiu Bin i fourier ri rc ngc Bin i fourier ri rc hai chiu Bin i fourier ri rc n chiu

a thc v php ni suy

Php ni suy interpl interplq interpft interp2 interp3 interpn griddata

Php ni suy mt chiu ( tra bng ) Php ni suy tuyn tnh mt chiu nhanh Php ni suy mt chiu s dng phng php FFT Php ni suy hai chiu ( tra bng ) Php ni suy ba chiu ( tra bng ) Php ni suy n chiu ( tra bng ) iu chnh b mt v li d liu hm v gii php ODE

Optimization and Root Finding fmin fmins fzero

Ti thiu hm mt bin Ti thiu hm vi bin Tm hm mt bin khng

Numaric Integration quad quad8 dblquad i tng hm inline inline

Tch phn nh gi v s lng, phng php trt t thp Tch phn nh gi v s lng, phng php trt t cao hn Tch phn hai ln nh gi v s lng

Xy dng i tng INLINE

argnames formula char

Tn i s Th thc hm Chuyn i i tng INLINE thnh mng k t ma trn ri rc

Cc ma trn khng lin tc c bn speye Ma trn ng nht thc khng lin tc sprand Ma trn ngu nhin phn chia mt cch khng lin tc ng nht sprandn Ma trn ngu nhin phn chia mt cch khng lin tc thng thng sprandsy Ma trn i xng ngu nhin khng lin tc spdiags Ma trn khng lin tc c to thnh t ng cho Full to Sparse Conversion sparse full find spconvert

To ma trn khng lin tc Chuyn i ma trn khng lin tc thnh ma trn y Tm ch s cc phn t khc khng Nhp vo t nh dng ma trn khng lin tc bn ngoi