Chuong 1- Tong Quan Ve Matlab

  • Published on
    03-Oct-2015

  • View
    10

  • Download
    3

DESCRIPTION

Tng quan matlab

Transcript

M phng mch lc s trn Matlab. Chng 1: Tng quan v Matlab Bin son : Nguyn Lng Nht - Nguyn Hu Hong PTITHCM 09/2011 1 Chng 1: TNG QUAN V MATLAB Matlab l mt phn mm ln trong lnh vc ton s, c vit tt t MATrix LABoration. Matlab thc hin trn c s chnh l php tnh vector v ma trn. Phn ct li ca chng trnh bao gm mt s hm ton hc. Cc chc nng xut nhp cng nh cc kh nng iu khin chu trnh m nh ta c th s dng cc scripts. 1. C bn v Matlab: - Sau khi khi ng Matlab ta c giao din nh sau: Hnh 1.1 Ca s chnh ca chng trnh Matlab Trong : - (1)l Command Windows: y l ca s chnh ca Matlab, l ni thc hin ton b qu trnh nhp liu v xut kt qu tnh ton. - (2)l Current Director: l th mc chnh m tt c cc file bn mun thc hin phi nm trong th mc ny. 1 2 3 M phng mch lc s trn Matlab. Chng 1: Tng quan v Matlab Bin son : Nguyn Lng Nht - Nguyn Hu Hong PTITHCM 09/2011 2 - (3)l Command History: Tt c cc lnh thc hin trong Command Window c lu tr ti y. 2. Cc thnh phn ca chng trnh Matlab: 2.1 Bin trong Mablab: - Bin lu tr gi tr m ta nhp vo. Cc bin trong Matlab l cc bin dng ma trn hoc vector. - Khai bo bin: tnbin = gitr. - V d: >>var_1 = 10; - S dng du ; sau khai bo bin khng xut gi tr ca bin ra mn hnh trong command window. 2.2 Cc hm ton trong Matlab: - Matlab c sn rt nhiu hm ton hc. G lnh help elfun hoc help datafun tham kho y . - Mt s hm ton hc thng dng: Cc hm ton sqrt(x) : Tnh cn bc hai. exp(x) : Tnh hm m c s e. log(x) : Tnh logarithm t nhin. log10(x) : Tnh logarithm c s thp phn. abs(x) : Ly gi tr tuyt i . sign(x) : Hm du. real(x) : Phn thc ca s phc. imag(x) : Phn o ca s phc. phase(x): Gc pha ca s phc. rem(x) : S d ca php chia x/y. round(x ): Lm trn s 0.5 = 1. ceil(x) : Lm trn ln. floor(x) : Lm trn xung sum(v) : Tng cc phn t vector. prod(v) : Tch cc phn t vector. min(v) : Phn t vector b nht. max(v) : Phn t vector ln nht. mean(v) : gi tr trung bnh cng. Cc hm lng gic sin(x) : Hm sin. cox(x) : Hm cos. tan(x) : Hm tan. atan(x) : Hm arctang +/- 900. atan2(x): Hm arctang +/- 1800. sinc(x) : Hm 2.3 Vector v ma trn: - S l ma trn v vector l mt im mnh ca Matlab. - Cc cch nhp ma trn hoc vector: o Nhp trc tip: >>vector_1 = [ 1 2 3]; o Nhp bng ton t : : >>start : increment : destination >>1:2:8 ans = 1 3 5 7 >>1:8 ans= 1 2 3 4 5 6 7 8 M phng mch lc s trn Matlab. Chng 1: Tng quan v Matlab Bin son : Nguyn Lng Nht - Nguyn Hu Hong PTITHCM 09/2011 3 o Nhp bng ones(row,col) ,zeros(row,col),v eye(line) : to ra cc ma trn 1, 0 v n v. >>ones(2,3) ans = 1 1 1 1 1 1 >>zeros(2,3) ans= 0 0 0 0 0 0 >>eye(3) ans= 1 0 0 0 1 0 0 0 1 2.4 Tnh ton vector ma trn. - .* ./ .^ : Cc php tnh vi tng phn t ma trn. - transpose(matrix) hay matrix : Chuynv ma trn. - inv(matrix) : o ma trn. - det(matrix) : Tnh nh thc ca ma trn. - eig(matrix) : Tnh cc tr ring ca ma trn - rank(matrix) : Hn ca ma trn. - diff(matrix) : Tnh vector sai phn. - conv(vector1,vector2) : Tnh chp hai ma trn Cc php so snh v php tnh logic: Php so snh = = : Php bng ~= : Khc < : Nh hn : Lnhn >= : Ln hn hoc bng. Php logic ~ : Php not & : Php and | : Php or xor : Php xor exits(x) : Tm s tn ti ca bin x 3. Cc Scripts ca Matlab: - Bn cnh kh nng nhp lnh trc tip, ta c th vit mt file bao gm nhiu hang lnh cho Matlab thc hin tun t nh mt ngn ng lp trnh. Cc file ny gi l script files v c phn m rng l .m. son tho file scripts ta c th s dng trnh son tho ca Matlab bng cchvo menu File/New/M-File. M phng mch lc s trn Matlab. Chng 1: Tng quan v Matlab Bin son : Nguyn Lng Nht - Nguyn Hu Hong PTITHCM 09/2011 4 Hnh1.2 Ca s chng trnh son M files. - thc hin scripts file th scripts file phi nm trong th mc hin hnh( Current Director). - T Command Window. G tn file scripts cn thc thi. Matlab s thc hin cc lnh trong file scripts v dng li nu c li xy ra hoc thc hin cho n khi kt thc. 4. Hin th trong Matlab: 4.1 Mt s hm v figure : figure[number] : To mi hay m mt figure. subplot(row,col,counter) : To subplot. clf : Xa ni dung ca figure. close(number) : ng figure mang s number close all : ng tt c cc figure. M phng mch lc s trn Matlab. Chng 1: Tng quan v Matlab Bin son : Nguyn Lng Nht - Nguyn Hu Hong PTITHCM 09/2011 5 Hnh 1.3 Cas figure. - Trc v in k t cho trc: axis( [x_min, x_max, y_min, y_max] ) : Chia trc 2D. axis( [ x_min, x_max, y_min, y_max, z_min, z_max ] ) : Chia trc 3D. axis(auto) : T ng chia trc. grid on (off) : To li hoc xa li zoom on(off) : Ct hoc co ging mng. xlabel(string) : M t trc x. ylabel(string) : M t trc y zlabel(string) : M t trc z title(string) : M t figure. text(x_value, y_value, string) : Ghi string ln figure ti v tr x_value v y_value. legend(string1,string2,, [position] : Ghi li ch thch. 4.2 th 2 chiu (2D graphics): Mtshmvthchiu: - stem(n,x) : V th im x[n]. - plot(x_value, y_value, [plotstyle]) :V th trn. - stairs(x_value, y_value[plotstyle]) : V th bc thang. M phng mch lc s trn Matlab. Chng 1: Tng quan v Matlab Bin son : Nguyn Lng Nht - Nguyn Hu Hong PTITHCM 09/2011 6 - loglog(x_value, y_value[plotstyle]) : V th logarithm. - semilogx(x_value, y_value[plotstyle]) : V trc x logarithm. - semilogy(x_value, y_value[plotstyle]) : V trc y logarithm. - fplot(function(x,y), [range]) : V hm tng minh - zplot(function(x,y), [range]) : V hm khng tng minh. - Hold on(off) : Gi cc th v. - Cc plotstyle: k : en. b :Xanh lam. c : Xm g :Xanh l cy. r : . m : Mu sm. y : Vng. w : Trng. - : Nt lin. -- : Nt t. : :Nt gch chm. . : Nt chm. o : Chm trn. * : Chm sao + : Du cng. x : Du nhn V d1: %vd1.m ( ylmtchthch). closeall; figure; x= -5:0.1:5; y= cos(pi*x); subplot(121); plot(x,y, 'R x'); hold on; fplot('2*sin(x)', [-5,5], 'k*'); subplot(122); t=(0:20)*0.9*pi; plot(cos(t), sin(t)); Ta s c hnh nh nh sau: M phng mch lc s trn Matlab. Chng 1: Tng quan v Matlab Bin son : Nguyn Lng Nht - Nguyn Hu Hong PTITHCM 09/2011 7 Hnh 1.4 Kt qu ca chng trnh vd1.m 4.3 th 3 chiu (3D graphics): biu din minh ha cc hm nhiu chiu ta thng s dng th 3D Sau y l mt s hm hin th th 3D: - [X,Y] = meshgrid(x_vector, y_vector) : Ma trn ta t vector. - plot3(x_value, y_value, z_value[plotstyle]) : V 3D im im nt. - surf(x_value, y_value, z_value[color]) : V th 3D mt. - mesh(x_value, y_value, z_value[color]) : V 3D li. - waterfall(x_value, y_value, z_value[color]) : V 3D thc nc. - contour(x_value, y_value, z_value) : V 2D nt ng mc. - box on/off : ng ngt box. - view(horizontal, vertical) : t gc hin th. - Colormap(name) : Chn mu. - casix(color_min, color_max) : Co dn thang mu. Mt s v d v hin th biu din th 3D. V d 2: Biu din th 3D dng hm plot3 %file name: vd2.m close all; figure; grid on; phi=(0:100)/100 *2*pi; plot3(sin(2*phi), cos(3*phi), phi, 'b*'); axis([0,(2*pi), 0,(2*pi), 0,(2*pi)]); xlabel('sin(2.phi)'); ylabel('cos(3.phi)'); zlabel('phi'); title('Vi du 2 - plot'); Kt qu ca chng trnh: M phng mch lc s trn Matlab. Chng 1: Tng quan v Matlab Bin son : Nguyn Lng Nht - Nguyn Hu Hong PTITHCM 09/2011 8 Hnh 1.5: Kt qu ca chng trnh vd2.m V d 3: Chng trnh biu din th 3D ca mt s hm. %------------------------------------------- %File name: vd3.m close all; x = 0:0.05:2; y = -1:0.2:1; [X,Y] = meshgrid(x,y) ; %; Tao ra ma tran dua tren cac vector x va y; Z = (Y+1).*cos(2*pi*X.^2) + (Y-1).*sin(2*pi*X.*2)/5; figure; subplot(221); surf(X,Y,Z); view(-40,30); title('surf(X, Y, Z); view(-40,,30)'); subplot(222); mesh(X,Y,Z); view(-20,30); title('mesh(X,Y,Z); view(-20,30);'); xlabel('grid off, box on'); grid off; box on; subplot(223); waterfall(X, Y, Z); M phng mch lc s trn Matlab. Chng 1: Tng quan v Matlab Bin son : Nguyn Lng Nht - Nguyn Hu Hong PTITHCM 09/2011 9 view(-20,10); title('waterfall(X,Y,Z); view(-20,10)'); subplot(224); contour(X,Y,Z); title('contour(X, Y, Z)'); Kt qu ca chng trnh: