4 MATLAB Programming with MATLAB Puff The magic
第 4章 用MATLAB設計程式 Programming with MATLAB Puff! The magic dragon, live by the tree!
What is programming? • Programming computer program *. m • Make decision~ loop • Switch • 寫程式 Puff! The magic dragon, live by the tree!
4. 1 Relational operators 關係運算子 • ‘=‘ assignment operator; replacement operator • x=3 ~ x 3 Puff! The magic dragon, live by the tree!
? x=2 x= 2 ? y=5 y= 5 ? z=x<y z= 1 ? u=x==y u= 0 ? 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 Puff! The magic dragon, live by the tree! ? z=x(x<y) z= 6 ? z=5>2+7 z= 0 ? z=5>(2+7) z= 0
Puff! The magic dragon, live by the tree!
4. 2 Logical operators and Functions 邏輯運算子與函式 Puff! The magic dragon, live by the tree!
Boolean operator Puff! The magic dragon, live by the tree!
? 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 ? 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 Puff! The magic dragon, live by the tree! ~(4>5) 5>=4 5<x<10 (5<x)&(x<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!
Puff! The magic dragon, live by the tree!
• P. 179 • exclusive OR Puff! The magic dragon, live by the tree!
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 0 1 1 1 0 0 0 Puff! The magic dragon, live by the tree!
Puff! The magic dragon, live by the tree!
Puff! The magic dragon, live by the tree!
• • • 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!
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!
Ex. 4. 2 -1 • • • v 0=20; g=9. 81; A=40*pi/180; t_hit=2*v 0*sin(A)/g; t=[0: t_hit/100: t_hit]; h=v 0*t*sin(A)-0. 5*g*t. ^2; v=sqrt(v 0^2 -2*v 0*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!
Self testing • P. 183 • Write down the program…. Puff! The magic dragon, live by the tree!
4. 3 條件敘述式 Conditional Statements • • • pp. 183 -184 Statements read once! And thinking…. (if) (end) (if) (elseif) (else) Puff! The magic dragon, live by the tree!
if statement • 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 • if x>=0 y=sqrt(x) end • If x>=0, y=sqrt(x), end Puff! The magic dragon, live by the tree!
if statement • if (logic expression 1) statement 1 if (logic expression 2) statement 2 end Puff! The magic dragon, live by the tree!
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!
Puff! The magic dragon, live by the tree!
Puff! The magic dragon, live by the tree!
Structure chart • 顯示程式的組織 • 但不會顯示詳細的計算及決策的過程 • 大程式: 主程式+副程式 1+副程式 2+… • Use structure chart, e. g. next page… Puff! The magic dragon, live by the tree!
Puff! The magic dragon, live by the tree!
else statement • if (logical expression 1) • if x>=0 (statement 1) y=sqrt(x) else (statement 2) y=exp(x)-1 end Puff! The magic dragon, live by the tree!
x=[4, -9, 25]; if x<0 disp('x裡面有負值') else y=sqrt(x) end y= 2. 0000 0 + 3. 0000 i 5. 0000 x=[4, -9, 25]; if x>=0 y=sqrt(x) else disp('x裡面有負值') end x裡面有負值 Puff! The magic dragon, live by the tree! %因為每個元素不全為正 ~ false
Puff! The magic dragon, live by the tree! x=input('x= ') if x>=5 y=log(x) else if x>=0 y=sqrt(x) 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! if x>10 y=log(x) elseif x>=0 y=sqrt(x) else y=exp(x)-1 end
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 x Puff! The magic dragon, live by the tree!
Self testing • Draw the flow chart • Write down the program and solution after ur input! Puff! The magic dragon, live by the tree!
Strings and conditional statements • ? name='Leslie Student' • ? size(name) name = ans = Leslie Student 1 14 • ? number='123' number = 123 • ? size(number) ans = 1 3 Puff! The magic dragon, live by the tree!
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!
Strings and conditional statements • 大小寫空格都是有區別的 • Hellow ~= hellow • Cannot ~= Can not ? string 1='can not' ? string 2='cannot' ? strcmp(string 1, string 2) % string compare ans = 0 Puff! The magic dragon, live by the tree!
大小寫轉換 • ? lower('STRING') ans = string • ? upper('string') ans = STRING Puff! The magic dragon, live by the tree!
I/O (work/p 195. 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 • ? p 195 Do you want to continue? Y/N [Y]: y response = Y • ? p 195 Do you want to continue? Y/N [Y]: qwe response = Puff! The magic dragon, N live by the tree!
string operator help strfun % for more string operator Puff! The magic dragon, live by the tree!
Loops • Known loop ~ for-loop • Unknown loop ~ while-loop Puff! The magic dragon, live by the tree!
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!
Puff! The magic dragon, live by the tree!
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 Puff! The magic dragon, live by the tree!
specmat(5) ans = 1 1 1 2 3 4 5 1 1 1 3 4 5 6 10 15 10 10 15 15 Puff! The magic dragon, live by the tree!
Self testing • P. 197 (T 4. 4 -1) • Find some rules • Try to write a function to generate the matrix Puff! The magic dragon, live by the tree!
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 Puff! 10 The magic dragon, live by the tree!
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!
break 中斷 loop 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! y= 7 y= 6. 7823 y= 6. 4031 y= 5. 8310 y= 5 y= 3. 7417 y= 1
continue 避免錯誤造成的中斷 p 198. m x=[10, 1000, -10, 100]; y=Na. N*x; for k=1: length(x) if x(k)<0 continue end y(k)=log 10(x(k)); end y Puff! The magic dragon, live by the tree!
A=[1, 2, 3; 4, 5, 6]; n=3; for k=1: n v=A(: , k) end A=[1, 2, 3; 4, 5, 6]; for v=A disp(v) end 1 4 2 5 3 6 v= 1 4 v= 2 5 v= 3 6 Puff! The magic dragon, live by the tree!
Implied Loops 隱式迴圈 x=[0: 5: 100]; y=cos(x); for k=1: 21 x= (k-1)*5; y(k)=cos(x); end y=find(x>0) j=0; for i=1: length(x) if x(i)>0 j=j+1; y(j)=1; end Puff! The magic dragon, live by the tree!
Puff! The magic dragon, live by the tree!
Puff! The magic dragon, live by the tree!
Puff! The magic dragon, live by the tree!
while loops while 邏輯表示式 敘述 end 1. initial value set first 2. Stop when ‘false’ occurs Puff! The magic dragon, live by the tree!
while loops x=5; while x<25 disp(x) x=2*x-1; end 5 9 17 x=1; while x~=5 disp(x) x=x+1; end 1 2 3 Puff! The magic dragon, 4 live by the tree!
while x<10 x=x+1; y=2*x end x=0; while x<10 x=x+1; y=2*x end y= 2 y= 4 y= 6 y= 8 y= 10 y= 12 y= 14 y= 16 y= 18 y= 20 Puff! The magic dragon, live by the tree!
Puff! The magic dragon, live by the tree!
infinite loop x=8; while x~=0 x=x-3 end x= 5 x= 2 x= -1… Puff! The magic dragon, live by the tree!
Puff! The magic dragon, live by the tree!
Puff! The magic dragon, live by the tree!
Self testing pp. 208 • Important examples! Puff! The magic dragon, live by the tree!
4. 5 switch structure switch 輸入表示式 case 數值 1 Statement 1 case 數值 2 Statement 2. . . otherwise statement n end • switch input('angle=? ') • • • case 45 disp('東北方') case 135 disp('東南方') case 225 disp('西南方') case 315 disp('西北方') otherwise disp(' 未知方位') end Puff! The magic dragon, live by the tree!
t=[0: 100]; x=exp(-t). *sin(t); switch input('Type min, max, or sum. ', 's') case 'min' minimum=min(x) case 'max' maximum=max(x) case 'sum' total=sum(x) otherwise disp('u have not input the proper choice') end Puff! The magic dragon, live by the tree!
Debugging Matlab Programs • P. 210 -218 Puff! The magic dragon, live by the tree!
• Syntax errors (語法錯誤) • Runtime errors (運算時錯誤) Puff! The magic dragon, live by the tree!
Debugging a loop • Pp. 216 -218 Puff! The magic dragon, live by the tree!
4. 7 Applications to simulation • Ex 4. 7 -1 • P. 219 • Self testing Puff! The magic dragon, live by the tree!
Puff! The magic dragon, live by the tree!
• State transition diagram • x(k+1)=Cx(k)+b(k) Puff! The magic dragon, live by the tree!
Puff! The magic dragon, live by the tree!
Puff! The magic dragon, live by the tree!
Puff! The magic dragon, live by the tree!
Puff! The magic dragon, live by the tree!
Puff! The magic dragon, live by the tree!
Puff! The magic dragon, live by the tree!
Puff! The magic dragon, live by the tree!
Puff! The magic dragon, live by the tree!
Puff! The magic dragon, live by the tree!
Puff! The magic dragon, live by the tree!
Puff! The magic dragon, live by the tree!
Puff! The magic dragon, live by the tree!
- Slides: 83