1 / 83

第 4 章 用 MATLAB 設計程式 Programming with MATLAB

第 4 章 用 MATLAB 設計程式 Programming with MATLAB. What is programming?. Programming  computer program *.m Make decision~ loop Switch 寫程式. 4.1 Relational operators 關係運算子. ‘=‘ assignment operator; replacement operator x=3 ~ x 3. ? x=[6,3,9]; ?y=[14,2,9]; ?z=(x<y) z =

Download Presentation

第 4 章 用 MATLAB 設計程式 Programming with MATLAB

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. 第4章 用MATLAB設計程式Programming with MATLAB Puff! The magic dragon, live by the tree!

  2. What is programming? • Programming  computer program *.m • Make decision~ loop • Switch • 寫程式 Puff! The magic dragon, live by the tree!

  3. 4.1 Relational operators關係運算子 • ‘=‘ assignment operator; replacement operator • x=3 ~ x3 Puff! The magic dragon, live by the tree!

  4. ?x=[6,3,9]; ?y=[14,2,9]; ?z=(x<y) z = 1 0 0 ?z=(x~=y) z = 1 1 0 ?z=(x>8) z = 0 0 1 ?z=x(x<y) z = 6 ?z=5>2+7 z = 0 ?z=5>(2+7) z = 0 ?x=2 x = 2 ?y=5 y = 5 ?z=x<y z = 1 ?u=x==y u = 0 Puff! The magic dragon, live by the tree!

  5. 優先順序 • 計算符號+-/ 優於 關係運算子 • 由左而右 z=5>3~=1 z=(5>3)~=1 z=0 Puff! The magic dragon, live by the tree!

  6. Puff! The magic dragon, live by the tree!

  7. 4.2 Logical operators and Functions邏輯運算子與函式 Puff! The magic dragon, live by the tree!

  8. Boolean operator Puff! The magic dragon, live by the tree!

  9. ?a=[4,3,12]; ?x=[6,3,9]; ?y=[14,2,9]; ?z=(x>y) & a z = 0 1 0 ?z=(x>y) & (x>a) z = 0 0 0 ?z=x>y&x>a z = 0 0 0 ?z=[5,-3,0,0]&[2,4,0,5] z = 1 1 0 0 ~(4>5) 5>=4 5<x<10 (5<x)&(x<10) ?x=[6,3,9]; ?y=[14,2,9]; ?z=~x>y z = 0 0 0 ?z=(~x)>y z = 0 0 0 ?z=~(x>y) z = 1 0 1 ?z=(x<=y) z = 1 0 1 Puff! The magic dragon, live by the tree!

  10. or ?x=[6,3,9]; ?y=[14,2,9]; ?z=x|y z = 1 1 1 ?z=[5,-3,0,0]|[2,4,0,5] z = 1 1 0 1 ?z=3<5|4==7 z = 1 ?z=(3<5)|(4==7) z = 1 Puff! The magic dragon, live by the tree!

  11. Puff! The magic dragon, live by the tree!

  12. P.179 • exclusive OR Puff! The magic dragon, live by the tree!

  13. Truth_table • x=[1,1,0,0]'; • y=[1,0,1,0]'; • Truth_table=[x,y,~x,x|y,x&y,xor(x,y)] • Truth_table = • 1 1 0 1 1 0 • 1 0 0 1 0 1 • 0 1 1 1 0 1 • 0 0 1 0 0 0 Puff! The magic dragon, live by the tree!

  14. Puff! The magic dragon, live by the tree!

  15. Puff! The magic dragon, live by the tree!

  16. x=[5,-3,0,0,8];y=[2,4,0,5,7]; • z=find(x&y) ~ 找非零元素, 傳回指標(index) • z = • 1 2 5 • x&y • ans = • 1 1 0 0 1 • values=y(x&y) • values = • 2 4 7 Puff! The magic dragon, live by the tree!

  17. Why use matlab? It seems just be finished by ur eyes! • Much data….. • Programming ro process these data! Puff! The magic dragon, live by the tree!

  18. Ex.4.2-1 • v0=20; g=9.81;A=40*pi/180; • t_hit=2*v0*sin(A)/g; • t=[0:t_hit/100:t_hit]; • h=v0*t*sin(A)-0.5*g*t.^2; • v=sqrt(v0^2-2*v0*g*sin(A)*t+g^2*t.^2); • u=find(h>=6&v<=16); • t_1=(u(1)-1)*(t_hit/100) • t_2=u(length(u)-1)*(t_hit/100) • plot( t,h,'y-',t,v,'go') Puff! The magic dragon, live by the tree!

  19. Self testing • P.183 • Write down the program…. Puff! The magic dragon, live by the tree!

  20. 4.3 條件敘述式Conditional Statements • pp.183-184 • Statements read once! And thinking…. • (if) (end) • (if) (else) • (if) (elseif) (else) Puff! The magic dragon, live by the tree!

  21. if statement • if x>=0 y=sqrt(x) end • If x>=0, y=sqrt(x), end • if (logic expression) statement end • z=0;w=0; if(x>=0)&(y>=0) z=sqrt(x)+sqrt(y) w=log(x)-3*log(y) end Puff! The magic dragon, live by the tree!

  22. if statement • if (logic expression 1) statement 1 if (logic expression 2) statement 2 end end Puff! The magic dragon, live by the tree!

  23. Program documentation, charts, and pseudocode (p.185) • One day, if other people try to read your program…! What will you do now for ur programming? • How to write a user-friendly program? • % more statement • Pseudo code • Flow chart (link between blocks) Puff! The magic dragon, live by the tree!

  24. Puff! The magic dragon, live by the tree!

  25. Puff! The magic dragon, live by the tree!

  26. Flow chart • 平行四邊形 ~ 輸入或輸出 • 矩形 ~ 運算 • 菱形 ~ 決策點 • 箭頭方向 ~ 程式計算流程與順序 Puff! The magic dragon, live by the tree!

  27. Structure chart • 顯示程式的組織 • 但不會顯示詳細的計算及決策的過程 • 大程式: 主程式+副程式1+副程式2+… • Use structure chart, e.g. next page… Puff! The magic dragon, live by the tree!

  28. Puff! The magic dragon, live by the tree!

  29. else statement • if x>=0 y=sqrt(x) else y=exp(x)-1 end • if (logical expression 1) (statement 1) else (statement 2) end Puff! The magic dragon, live by the tree!

  30. x=[4, -9, 25]; if x<0 disp('x裡面有負值') else y=sqrt(x) end y = 2.0000 0 + 3.0000i 5.0000 x=[4, -9, 25]; if x>=0 y=sqrt(x) else disp('x裡面有負值') end x裡面有負值 %因為每個元素不全為正 ~ false Puff! The magic dragon, live by the tree!

  31. x=input('x= ') if x>=5 y=log(x) else if x>=0 y=sqrt(x) end end x=input('x= ') if x>=5 y=log(x) elseif x>=0 y=sqrt(x) end Puff! The magic dragon, live by the tree!

  32. if x>10 y=log(x) elseif x>=0 y=sqrt(x) else y=exp(x)-1 end Puff! The magic dragon, live by the tree!

  33. x if x>10 y=log(x) if y>=3 z=4*y elseif y>=2.5 z=2*y else z=0 end else y=5*x z=7*x end Puff! The magic dragon, live by the tree!

  34. Self testing • Draw the flow chart • Write down the program and solution after ur input! Puff! The magic dragon, live by the tree!

  35. Strings and conditional statements • ?name='Leslie Student' name = Leslie Student • ?number='123' number = 123 • ?size(name) ans = 1 14 • ?size(number) ans = 1 3 Puff! The magic dragon, live by the tree!

  36. Strings and conditional statements • ?name='Leslie Student' name = Leslie Student • ?full_name=[name(1:6),' C.',name(7:14)] full_name = Leslie C. Student • ?full_name(8)=‘F‘ %更改 full_name = Leslie F. Student • ?findstr(full_name,‘e’) % 有出現e的位置, 空格也要算入 ans = 2 6 15 Puff! The magic dragon, live by the tree!

  37. Strings and conditional statements • 大小寫空格都是有區別的 • Hellow ~= hellow • Cannot ~= Can not ?string1='can not' ?string2='cannot' ?strcmp(string1, string2) % string compare ans = 0 Puff! The magic dragon, live by the tree!

  38. 大小寫轉換 • ?lower('STRING') ans = string • ?upper('string') ans = STRING Puff! The magic dragon, live by the tree!

  39. I/O (work/p195.m) response=input('Do you want to continue? Y/N [Y]:','s'); if(isempty(sesponse))|(response=='y')|(response=='Y') response='Y' else response='N' end • ?p195 Do you want to continue? Y/N [Y]:y response = Y • ?p195 Do you want to continue? Y/N [Y]:qwe response = N Puff! The magic dragon, live by the tree!

  40. string operator help strfun % for more string operator Puff! The magic dragon, live by the tree!

  41. Loops • Known loop ~ for-loop • Unknown loop ~ while-loop Puff! The magic dragon, live by the tree!

  42. for-loop for k=5:10:36 x=k^2 end x = 25 x = 225 x = 625 x = 1225 Puff! The magic dragon, live by the tree!

  43. Puff! The magic dragon, live by the tree!

  44. function A=specmat(n) A=ones(n); for r=1:n for c=1:n if (r>1) & (c>1) s=A(r-1,c)+A(r,c-1); if s<20 A(r,c)=s; else A(r,c)=max(A(r-1,c),A(r,c-1)); end end end end Puff! The magic dragon, live by the tree!

  45. specmat(5) ans = 1 1 1 1 1 1 2 3 4 5 1 3 6 10 15 1 4 10 10 15 1 5 15 15 15 Puff! The magic dragon, live by the tree!

  46. Self testing • P.197 (T4.4-1) • Find some rules • Try to write a function to generate the matrix Puff! The magic dragon, live by the tree!

  47. for-loop k=m:s:n • k=10:-2:4 k = 10 8 6 4 • ?j=10:6 j = Empty matrix: 1-by-0 • ?j=6:10 % s=1 j = 6 7 8 9 10 Puff! The magic dragon, live by the tree!

  48. for-loop k=m:s:n • ?k=10:10 k = 10 • ?k=1:0.33:2 k = 1.0000 1.3300 1.6600 1.9900 • Do not change the k value in the for-k-loop statement! It’s dangerous! • Do not use i & j as the parameter in the for-loop. They are usually defined as (-1)^0.5 in matlab. Puff! The magic dragon, live by the tree!

  49. break 中斷 loop y = 7 y = 6.7823 y = 6.4031 y = 5.8310 y = 5 y = 3.7417 y = 1 for k=1:10 x=50-k^2; if x<0 break end y=sqrt(x) end Puff! The magic dragon, live by the tree!

  50. continue 避免錯誤造成的中斷p198.m x=[10,1000,-10,100]; y=NaN*x; for k=1:length(x) if x(k)<0 continue end y(k)=log10(x(k)); end y Puff! The magic dragon, live by the tree!

More Related