1 / 40

Chöông 2 : Laäp Trình M-file

Chöông 2 : Laäp Trình M-file. Chöông goàm caùc ñeà muïc : Laäp trình vôùi m file. Toùan töû so saùnh vaø toùan töû logic . Caáu truùc ñieàu khieån. Xöû lyù chuoåi (string). Maûng caáu truùc. Maûng cell. 1) Laäp Trình m file : Ñieåm maïnh cuûa matlab laø ngoân ngöõ laäp trình.

Download Presentation

Chöông 2 : Laäp Trình M-file

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chöông 2 : Laäp Trình M-file Chöông goàm caùc ñeà muïc : Laäp trình vôùi m file. Toùan töû so saùnh vaø toùan töû logic. Caáu truùc ñieàu khieån. Xöû lyù chuoåi (string). Maûng caáu truùc. Maûng cell.

  2. 1) Laäp Trình m file : • Ñieåm maïnh cuûa matlab laø ngoân ngöõ laäp trình. • File chöùa maõ nguoàn ngoân ngöõ laäp trình Matlab laø m-file. • Phaàn ñuoâi cuûa m-file laø .m. • Taïo moät m-file chöùa maõ nguoàn, choïn leänh New-file vaø choïn m-file. • Coù hai loïai m-file : script m-file vaø function m-file. • Script m-file : khoâng coù ñoái soá vaøo, vaän haønh trong vuøng workspace, chöùa caùc leänh vaø haøm cuûa matlab. • Function m-file : coù caùc ñoái soá vaøo ra. Cuù phaùp taïo haøm m-file laø function [arg1, . . ,argN] = <fun_name>( input_args ) <body of function>; % end of function

  3. Ví duï :Chöông trình sau laø moät ví duï minh chöùng cuûa moät script m-file. % Chöông trình veõ moät ñöôøng troøn coù taâm laø (3,2) vaø baùn kính laø 3. t = 0:pi/100:2*pi; x = 3 + 3*cos(t); y = 2 + 3*sin(t); plot(x,y), axis(‘square’),grid % End of program • Chöông trình sau laø moät ví duï minh chöùng cuûa moät haøm m-file. % Giaûi phöông trình baäc hai function [x1,x2] = quadroot(a,b,c) x = roots([a b c]); x1 = x(1); x2 = x(2); % End of function

  4. 2) Toùan Töû So Saùnh vaø Toùan Töû Logic : • Toùan töû so saùnh : • < : nhoû hôn. • <= : nhoû hôn hoaëc baèng. • > : lôùn hôn. • >= : lôùn hôn hoaëc baèng. • == : baèng nhau. • ~= : khoâng baèng nhau. • Toùan töû logic : • & : toùan töû vaø. • | : toùan töû hoaëc. • ~ : toùan töû phuû ñònh. 3) Caáu Truùc Ñieàu Khieån : • Leänh if, else vaø elseif :

  5. Leänh if : cuù phaùp laø if <logical expression> body of if statement; end Ví duï : Chöông trình sau laø moät ví duï minh chöùng söû duïng leänh if. %Begining of program str = input('Ve do thi sin(x)[y/n]','s'); if str == 'y' x = 0:pi/100:2*pi; y = sin(x); plot(x,y),grid xlabel('X'),ylabel('Y') title('Sin(x)') end % End of program

  6. Leänh if-else : cuù phaùp laø if <logical expression> body of if statement; else body of else statement; end Ví duï : Chöông trình sau laø moät ví duï minh chöùng söû duïng leänh if-else. %Begining of program num = input('Enter any number :'); if num >= 0.0 disp('Positive number'); else display('Negative number'); end %End of program

  7. Leänh if-elseif-else : cuù phaùp laø if <logical expression1> body of if statement; elseif <logical expression2> body of elseif statement; else body of else statement; end Ví duï : Chöông trình sau laø moät ví duï minh chöùng söû duïng leänh if-elseif-else. %Begining of program num = input('Enter any number :'); if num > 0.0 disp('Positive number'); elseif num == 0.0 display('Zero number'); else disp('Negative number') end %End of program

  8. Leänh switch : leänh choïn löïa moät trong caùc tröôøng hôïp ñeå thöïc hieän vôùi cuù phaùp laø switch <expression> case value1 statements; case valu2 statements; case valueN statements; otherwise statements; end

  9. Ví duï : Chöông trình sau laø moät ví duï minh chöùng söû duïng leänh switch. %Begining of program num = input('Enter number :'); switch num case -1 disp('Negative one'); case 0 disp('Zero'); case 1 disp('Positive one'); otherwise disp('Other number') end %End of program

  10. Leänh while : thöïc hieän voøng laëp khi bieåu thöùc logic true vôùi cuù phaùp laø while <logical exprerssion> body of while statements; end Ví duï : Chöông trình sau laø moät ví duï minh chöùng söû duïng leänh while. %Begining of program num = input('Enter integer number :'); fact = 1; while num > 1 fact = fact*num; num = num - 1; end fact %End of program

  11. Leänh for : thöïc hieän voøng laëp vôùi soá laàn ñöôïc xaùc ñònh. for index = start:increment:end body of for statements; end Ví duï : Chöông trình sau laø moät ví duï minh chöùng söû duïng leänh for. %Begining of program row = input('Enter number of rows :'); colum = input('Enter number of colums :'); for i = 1:row for j = 1:colum c(i,j) = 1/(i + j - 1); end end c %End of program

  12. Leänh break : keát thuùc voøng laëp coù ñieàu kieän. Ví duï : chöông trình sau laø moät ví duï minh chöùng. %Begining of program clear num = input('Enter integer number :'); fact = 1; while num > 1 if num < 0 break; else fact = fact*num; num = num - 1; end end fact %End of program

  13. Leänh continue : boû qua moät coâng ñoïan trong voøng laëp ñeå tieáp tuïc thöïc hieän voøng laëp môùi. Ví duï : Chöông trình sau laø moät ví duï minh chöùng. %Begining of program clear a = zeros(6); for i = 1:6 for j = 1:6 if i == j continue; else a(i,j) = i*j; end end end a %End of program

  14. Leänh return : Leänh thöôøng ñöôïc söû duïng trong haøm m-file boû qua phaàn coâng ñoïan cuûa haøm hoaë traû veà keát thuùc maõ baøn phím. • Ví duï : Chöông trình sau laø moät ví duï minh chöùng söû duïng leänh return. function d = deter(A) if isempty(A) d = 1; return else d = det(A); end Haøm con (sub function) : m-file coù theå chöùa nhieàu haøm, haøm ñaàu tieân laø haøm chính phaûi coù cuøng teân vôùi m-file, caùc haøm coøn laïi laø caùc haøm con phaûi coù khaùc teân vôùi m-file.

  15. Ví duï : Chöông trình sau laø moät ví duï minh chöùng. function [z,s] = main(x,y) z = total(x,y); s = subtract(x,y); function z = total(x,y) z = x + y; function s = subtract(x,y) s = x – y; %end of function • Haøm private : • haøm private laø haøm thöôøng truù trong thö muïc ñaëc bieät private. • Haøm cho pheùp ñöôïc söû duïng vôùi caùc haøm m-file khaùc thöôøng truù trong thö muïc cha. • Haøm private phaûi coù cuøng teân vôùi thö muïc private.

  16. 4) Xöû Lyù Chuoåi (String ) : • Chuoåi laø maûng moät chieàu kieåu kyù töï, trong ñoù moãi kyù töï ñöôïc bieåu dieãn maèng maõ ascii. • Chuoåi phaûi naèm trong hai daáu nhaùy ‘ ‘. Ví duï : Gaùn chuoåi Hello cho bieán str vôùi leänh laø >>str = ‘Hello’ • Haøm double vaø char : • double : chuyeån chuoåi kyù töï sang maõ soá ascii. Haøm coù cuù phaùp toång quaùt laø double(string) • char : chuyeån chuoåi döôùi daïng maõ ascii sang daïng chuoái. Haøm coù cuù phaùp toång quaùt laø char(string) Ví duï : Chuyeån chuoåi Hello cho treân sang daïng maõ soá ascii vôùi leänh laø

  17. >>str1 = double(str) Cho keát quaû laø str1 = 72 101 108 108 111 Chuyeån chuoåi str1 döôùi daïng maõ soá ascii sang daïng chuoåi vôùi leänh laø >>char(str1) Cho keát quaû laø ans = Hello • Haøm strcmp vaø haøm strcat : • strcmp : Haøm so saùnh hai chuoåi. Haøm coù cuù phaùp toång quaùt laø strcmp(string1,string2) Haøm traû veà logical true(1) neáu hai chuoåi laø baèng nhau, maët khaùc traû veà logical false(0). • strcat : haøm noái hai hoaëc nhieàu chuoåi. Haøm coù cuù phaùp laø strcat(str1,str2,str3, . . .)

  18. Haøm isletter vaø haøm ispace : • isletter : haøm xaùc ñònh chöõ caùi trong moät chuoåi kyù töï. Cuù phaùp cuûa haøm laø isletter(string) Haøm traû veà logical true(1) neáu ñoù laø chöõ caùi trong chuoåi; maët khaùc traû veà logical false(0). • ispace : haøm xaùc ñònh khoûang traéng trong moät chuoåi. Haøm coù cuù phaùp laø ispace(string) Haøm traû veà logical true(1) neáu ñoù laø khoûang traéng trong chuoåi; maët khaùc traû veà logical false(0). • Haøm int2str vaø haøm num2str : • int2str : haøm laøm troøn soá thaønh soá nguyeân vaø chuyeån soá ñöôïc laøm troøn naøy sang daïng chuoåi kyù töï. Haøm coù cuù phaùp laø int2str(number or array) • num2str : haøm chuyeån soá veà daïng chuoåi. Haøm coù cuù phaùp laø num2str(number or array)

  19. Array numeric char cell struct user-object storage double sparse 5) Maûng Caáu Truùc : Döõ lieäu Matlab coù saùu kieåu döõ lieäu cô baûn hay coøn goïi laø saùu lôùp döõ lieäu vôùi caáu truùc nhö hình

  20. Saùu lôùp döõ lieäu söû duïng trong Matlab : char, double, sparse, cell, struct vaø storage, moãi cuûa chuùng laø maûng nhieàu chieàu. • Lôùp array : lôùp döõ lieäu aûo. • Lôùp char vaø double : lôùp thöôøng xuyeân söû duïng. • Lôùp numeric : lôùp döõ lieäu aûo. • Lôùp cell : lôùp maûng döõ lieäu cell. • Lôùp sparse : lôùp döõ lieäu ma traän thöa. • Lôùp storage : lôùp aûo duøng caáp phaùt boä nhôù cho bieán. • Lôùp struct : lôùp döõ lieäu maûng caáu truùc. • Lôùp user_object : lôùp döõ lieäu cho ngöôøi söû duïng ñònh nghóa(daønh cho laäp trình höôùng ñoái töôïng).

  21. Maûng caáu truùc : • Maûng caáu truùc laø kieåu döõ lieäu coù theå chöùa nhieàu kieåu döõ lieäu khaùc nhau. • Moãi vuøng chöùa döõ lieäu khaùc nhau coù moät teân rieâng ñöôïc goïi laø field_name. • Cuù phaùp taïo maûng caáu truùc laø struct_array_name.field_name = <expression>; Trong ñoù, expression coù theå laø chuoåi, ñaïi löôïng voâ höôùng hoaëc maûng nhieàu chieàu. • Taïo maûng caáu truùc: patient.name = ‘John Doec’; patient.billing = 127.0; patient.test = [79 75 73;180 178 177.5;220 210 205]; patient(2).name = “Ann Lane’; patient(2).billing = 28.0; patient(2).test = [68 70 68;118 118 119;172 170 169];

  22. Truy caäp maûng caáu truùc : • Xem thoâng veà maûng caáu truùc vôùi leänh laø >>patient Hieån thò thoâng tin laø patient = 1×2 struct with fields name billing test • Xem noäi dung maûng caáu truùc vôùi leänh laø >> patient(1:2) • Xem phaàn töû thöù nhaát cuûa maûng caáu truùc vôùi leänh laø >>patient(1) • Xem phaàn töû thöù 2 vaø phaàn töû töông öùng cuûa tröôøng test vôùi leänh laø >> patient(2).test(2,2) • Löu yù : döõ lieäu maûng caáu truùc cho pheùp laäp trình höôùng ñoái töôïng trong Matlab.

  23. 5) Maûng cell : maûng cell laø moät kieåu döõ lieäu ñaëc bieät cuûa Matlab, trong ñoù moãi cell chöùa nhieàu kieåu döõ lieäu khaùc nhau. • Maûng cell coù chæ soá cell laø (i,j) vaø chæ soá noäi dung laø {i,j}. • Cuù phaùp gaùn döõ lieäu cho maûng cell baèng chæ soá laø cell_array_name(i,j) = { <celldata>}; • Cuù phaùp gaùn döõ lieäu cho maûng cell baèng chæ soá noäi dung laø cell_array_name{i,j} = <celldata>; • Ví duï : gaùn döõ lieäu cho maûng cell A baèng chæ soá laø >>A(1,1) = {[1 2 3;0 5 8;7 2 0]}; >>A(1,2) = {‘Anne Amith’}; >>A(2,1) = {3+7i}; >>A(2,2) = {-pi:pi/10:pi}; • Ví duï : gaùn döõ lieäu cho maûng cell A baèng chæ soá noäi dung laø >>A{1,1} = [1 4 3;0 5 8;7 2 0]; >>A{1,2} = ‘Anne Smith’; >>A{2,1} = 3+7i; >>A{2,2} = -pi:pi/10:pi;

  24. Truy caäp maûng cell : • Xem thoâng tin veà maûng cell vôùi leänh laø >>A Cho keát quaû laø [2×2 double] ‘Anne Smith’ [3.0000 +7.0000i] [1×2 double] • Truy caäp maûng cell duøng chæ soá noäi dung vôùi leänh laø >> C = A{1,2} Cho keát quaû laø C = ‘Anne Smith’ • Truy caäp maûng cell baèng chæ soá maûng vôùi leänh laø >> C = A(1,2) Cho keát quaû laø C = ‘Anne Smith’ • Huûy boû maûng cell vôùi leänh laø >>A = [ ]

  25. Ví duï :Laäp trình öùng duïng veõ caùc haønh tinh chuyeån ñoäng theo heä maët trôøi nhö hình

  26. solar.m data.m starts.m finals.m plots.m repeats.m Solve_th.m ellipse.m idnums.m names.m periods.m prompts.m fzero.m kepler.m quad8 Radius_2 solar.m • Ñeå laøm ñöôïc ñieàu ñoù, taäp caùc chöông trình caát trong caùc m-file ñöôïc moâ taû nhö hình data.m starts.m finals.m plots.m repeats.m ellipse.m idnums.m names.m periods.m orbits12.m orbits34.m globals.m

  27. Script file solar.m vôùi noäi dung laø %solar.m globals data starts finals plots repeats • Script file globals.m vôùi noäi dung laø %globals.m global Name Period Orbit global Start_t Start_th global Final_t

  28. Script file döõ lieäu data.m chöùa 5 cript file döõ lieäu khaùc ñoù laø % Data.m idnums % Gaùn chæ soá cho caùc ñoái töôïng names % Gaùn teân caùc ñoái töôïng periods % Chu kyø chuyeån ñoäng cuûa caùc haønh tinh orbits12 % Quõi ñaïo chuyeån ñoäng cuûa caùc haønh tinh orbits34 % Quó ñaïo chuyeån ñoäng cuûa caùc haønh tinh chieáu theo %goùc nghieâng • Script file idnums.data %idums.m Mercury = 1; Venus = 2; Earth = 3; Mars = 4; Jupiter = 5; Saturn = 6; Uranus = 7; Neptune = 8; Pluto = 9;

  29. Script file names.m %names.m Name = ''; Name(Mercury,:) = 'Mercury'; Name(Venus, :) = 'Venus '; Name(Earth, :) = 'Earth '; Name(Mars, :) = 'Mars '; Name(Jupiter,:) = 'Jupiter'; Name(Saturn, :) = 'Saturn '; Name(Uranus, :) = 'Uranus '; Name(Neptune,:) = 'Neptune'; Name(Pluto, :) = 'Pluto '; • Script file periods.m %period.m Period = zeros(9,1); Period(Mercury) = 0.24; Period(Venus) = 0.62; Period(Earth) = 1.00; Period(Mars) = 1.88; Period(Jupiter) = 11.86; Period(Saturn) = 29.46; Period(Uranus) = 84.0; Period(Neptune) = 164.8; Period(Pluto) = 247.7;

  30. C + d = constant d c þ 2b f f = ea 2a f2 = a2 – b2 Caùc haønh tinh chuyeån ñoäng theo quó ñaïo hình ellipse nhö hình

  31. Script file orbits12.m thieát laäp quan heä giöõa eccentricity(e) ôû coät thöù nhaát vaø minor axis (b) ôû coät thöù 2 cuûa maûng. %orbits12.m Orbit = zeros(9,4); Orbit(Mercury,1:2) = [0.39, 0.206]; Orbit(Venus, 1:2) = [0.72, 0.007]; Orbit(Earth, 1:2) = [1.00, 0.017]; Orbit(Mars, 1:2) = [1.52, 0.093]; Orbit(Jupiter,1:2) = [5.20, 0.048]; Orbit(Saturn, 1:2) = [9.54, 0.056]; Orbit(Uranus, 1:2) = [19.19, 0.047]; Orbit(Neptune,1:2) = [30.07, 0.009]; Orbit(Pluto, 1:2) = [39.46, 0.249]; • Scipt file orbits34.m chöùa goùc quay quanh truïc majar cuûa moãi haønh tinh vaø ñoä nghieâng. %orbits34.m Orbit(Mercury,3:4) = [0.00, 7.0]; Orbit(Venus, 3:4) = [0.00, 3.4]; Orbit(Earth, 3:4) = [0.00, 0.0]; Orbit(Mars, 3:4) = [0.00, 1.9]; Orbit(Jupiter,3:4) = [0.00, 1.3]; Orbit(Saturn, 3:4) = [0.00, 2.5]; Orbit(Uranus,3:4) = [0.00, 0.8]; Orbit(Neptune,3:4) = [0.00, 1.8]; Orbit(Pluto, 3:4) = [0.00, 17.1];

  32. Script file starts.m löu tröõ vò trí goùc baét taïi thôøi ñieåm start_t cuûa caùc haønh tinh. %starts.m Start_t = 1995.0 +3/365; Start_th = zeros(9,1); Start_th(Mercury) = 345.7083; Start_th(Venus) = 138.8194; Start_th(Earth) = 102.1532; Start_th(Mars) = 119.2770; Start_th(Jupiter) = 242.7067; Start_th(Saturn) = 348.7888; Start_th(Uranus) = 291.6482; Start_th(Neptune) = 293.7496; Start_th(Pluto) = 231.6706; Start_th = Start_th*pi/180;

  33. Script file finals.m %finals.m Final_t = prompts; Final_th = zeros(9,1); Final_th(Mercury) = solve_th(Mercury); Final_th(Venus) = solve_th(Venus); Final_th(Earth) = solve_th(Earth); Final_th(Mars) = solve_th(Mars); Final_th(Jupiter) = solve_th(Jupiter); Final_th(Saturn) = solve_th(Saturn); Final_th(Uranus) = solve_th(Uranus); Final_th(Neptune) = solve_th(Neptune); Final_th(Pluto) = solve_th(Pluto); Final_th

  34. Script file prompts.c %prompts.m function Final_t = prompts globals disp('..............................................'); disp([' Start time is : ', num2str(Start_t,10)]); disp('...............................................'); disp(['Final time can be a number : ', num2str(Start_t+5)]); disp(['or it can be an expression : ','Start_t+5']); Final_t = input('Enter Final time : '); disp('................................................'); disp([' Final time is : ',num2str(Final_t,10)]); • Function file sove_th.m function f_th = solve_th(id) global cur_id cur_id = id; th_guess = pi; f_th = fzero('kepler',th_guess);

  35. Leänh fzero goïi haøm kepler vôùi ñoái soá vaøo cuûa haøm ñeå giaûi goùc vò trí Final_th cuûa caùc haønh tinh vôùi caùc phöông trình laø

  36. Function file kepler.m function diff = kepler(Final_th) global cur_id globals tol = 1e-6; A = (1/2)*quad8('radius_2',Start_th(cur_id),Final_th,tol); a = Orbit(cur_id,1); e = Orbit(cur_id,2); b = a*sqrt(1-e^2); t = Final_t - Start_t; T = Period(cur_id); diff = A/(pi*a*b) - t/T; • Function file radiuus_2.m traû veà r(þ)2 trong haøm laáy tích phaân quad8. function r_2 = radius_2(th) global cur_id globals a = Orbit(cur_id,1); e = Orbit(cur_id,2); angle = Orbit(cur_id,3); r = a*(1-e^2)./(1 - e*cos(th - angle)); r_2 = r.^2;

  37. Script file plots.m veõ quó ñaïo chuyeån ñoäng cuûa caùc haønh tinh theo heä maët trôøi. %plots.m for ifig = 1:2 figure(ifig) if ifig == 1, from = Mercury; to = Mars; end if ifig == 2, from = Mars; to = Pluto; end for id = from:to a = Orbit(id,1); e = Orbit(id,2); angle = Orbit(id,3); ellipse(a,e,0,2*pi,angle) hold on h = ellipse(a,e,Start_th(id),Final_th(id),angle); set(h, 'LineWidth',4.0) th = 3*pi/2; r = a*(1 - e^2)./(1 - e*cos(th - angle)); [x,y] = pol2cart(th,r); text(x,y,Name(id, :)) end hold off axis equal axis('square') end

  38. Function file ellipse.m veõ hình ellipse. function [o1, o2] = ellipse(a,e,th1,th2,angle,x1,y1,npts) if nargin < 3, th1 = 0.0; end if nargin < 4, th2 = 2*pi; end if nargin < 5, angle = 0.0; end if nargin < 6, x1 = 0.0; end if nargin < 7, y1 = 0.0; end if nargin < 8, npts = 1 + 360*(th2 - th1)/(2*pi); end if nargin < 2 | nargin > 8 error('Wrong number of input argument') end th = linspace(th1,th2,npts); r = a*(1 - e^2)./(1 - e*cos(th - angle)); [x,y] = pol2cart(th,r); x = x + x1; y = y + y1; if nargout == 0 plot(x,y,'-'); end if nargout == 1 o1 = plot(x,y,'-'); end if nargout == 2 o1 = x; o2 = y; end if nargout > 2 error('Wrong number of output arguments') end

  39. Script file repeats.m thöïc hieän laëp laïi neáu caàn. %repeats.m another = input(['Do you wish to trace another Orbit ? [y/n]'],'s'); while strcmp(another, 'y') cont = input(['Do you wish to start from where you left off [y/n]'],'s'); if strcmp(cont,'y') Start_t = Final_t; Start_th = Final_th; end Finals Plots another = input(['Do you wish to trace another Orbit ?[y/n]'],'s'); end • Chaïy chöông trình goû leänh solar.

More Related