![Page 1: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/1.jpg)
Puff! The magic dragon, live by the tree!
第 4章 用 MATLAB 設計程式Programming with MATLAB
![Page 2: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/2.jpg)
Puff! The magic dragon, live by the tree!
What is programming?
• Programming computer program *.m
• Make decision~ loop
• Switch
• 寫程式
![Page 3: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/3.jpg)
Puff! The magic dragon, live by the tree!
4.1 Relational operators關係運算子
• ‘=‘ assignment operator; replacement operator
• x=3 ~ x3
![Page 4: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/4.jpg)
Puff! The magic dragon, live by the tree!
?x=2x = 2?y=5y = 5?z=x<yz = 1?u=x==yu = 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
?z=x(x<y)
z =
6
?z=5>2+7
z =
0
?z=5>(2+7)
z =
0
![Page 5: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/5.jpg)
Puff! The magic dragon, live by the tree!
優先順序•計算符號 +-/ 優於 關係運算子• 由左而右z=5>3~=1
z=(5>3)~=1
z=0
![Page 6: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/6.jpg)
Puff! The magic dragon, live by the tree!
![Page 7: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/7.jpg)
Puff! The magic dragon, live by the tree!
4.2 Logical operators and Functions邏輯運算子與函式
![Page 8: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/8.jpg)
Puff! The magic dragon, live by the tree!
Boolean operator
![Page 9: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/9.jpg)
Puff! The magic dragon, live by the tree!
?x=[6,3,9];?y=[14,2,9];?z=~x>yz = 0 0 0?z=(~x)>yz = 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
~(4>5)
5>=4
5<x<10
(5<x)&(x<10)
![Page 10: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/10.jpg)
Puff! The magic dragon, live by the tree!
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
![Page 11: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/11.jpg)
Puff! The magic dragon, live by the tree!
![Page 12: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/12.jpg)
Puff! The magic dragon, live by the tree!
• P.179
• exclusive OR
![Page 13: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/13.jpg)
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 1 0• 1 0 0 1 0 1• 0 1 1 1 0 1• 0 0 1 0 0 0
![Page 14: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/14.jpg)
Puff! The magic dragon, live by the tree!
![Page 15: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/15.jpg)
Puff! The magic dragon, live by the tree!
![Page 16: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/16.jpg)
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
![Page 17: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/17.jpg)
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!
![Page 18: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/18.jpg)
Puff! The magic dragon, live by the tree!
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')
![Page 19: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/19.jpg)
Puff! The magic dragon, live by the tree!
Self testing
• P.183
• Write down the program….
![Page 20: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/20.jpg)
Puff! The magic dragon, live by the tree!
4.3 條件敘述式Conditional Statements
• pp.183-184
• Statements read once! And thinking….
• (if) (end)
• (if) (else)
• (if) (elseif) (else)
![Page 21: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/21.jpg)
Puff! The magic dragon, live by the tree!
if statement
• if (logic expression)statementend
• z=0;w=0;if(x>=0)&(y>=0)z=sqrt(x)+sqrt(y)w=log(x)-3*log(y)end
• if x>=0y=sqrt(x)
end• If x>=0, y=sqrt(x), end
![Page 22: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/22.jpg)
Puff! The magic dragon, live by the tree!
if statement
• if (logic expression 1)
statement 1
if (logic expression 2)
statement 2
end
end
![Page 23: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/23.jpg)
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)
![Page 24: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/24.jpg)
Puff! The magic dragon, live by the tree!
![Page 25: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/25.jpg)
Puff! The magic dragon, live by the tree!
yxw
yxz
log3log
![Page 26: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/26.jpg)
Puff! The magic dragon, live by the tree!
Flow chart
• 平行四邊形 ~ 輸入或輸出• 矩形 ~ 運算• 菱形 ~ 決策點• 箭頭方向 ~ 程式計算流程與順序
![Page 27: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/27.jpg)
Puff! The magic dragon, live by the tree!
Structure chart
• 顯示程式的組織• 但不會顯示詳細的計算及決策的過程
• 大程式 : 主程式 + 副程式 1+ 副程式 2+…
• Use structure chart, e.g. next page…
![Page 28: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/28.jpg)
Puff! The magic dragon, live by the tree!
![Page 29: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/29.jpg)
Puff! The magic dragon, live by the tree!
else statement
• if (logical expression 1)
(statement 1)
else
(statement 2)
end
• if x>=0
y=sqrt(x)
else
y=exp(x)-1
end
![Page 30: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/30.jpg)
Puff! The magic dragon, live by the tree!
x=[4, -9, 25];if x<0disp('x 裡面有負值 ')elsey=sqrt(x)endy = 2.0000 0 + 3.0000i 5.0000
x=[4, -9, 25];if x>=0y=sqrt(x)elsedisp('x 裡面有負值 ')endx 裡面有負值
% 因為每個元素不全為正 ~ false
![Page 31: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/31.jpg)
Puff! The magic dragon, live by the tree!
x=input('x= ')if x>=5y=log(x)else if x>=0 y=sqrt(x) endendx=input('x= ')if x>=5y=log(x)elseif x>=0 y=sqrt(x)end
50
5ln
xx
xxy
![Page 32: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/32.jpg)
Puff! The magic dragon, live by the tree!
if x>10y=log(x)elseif x>=0 y=sqrt(x)else y=exp(x)-1end
01
100
10ln
xe
xx
xx
yx
![Page 33: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/33.jpg)
Puff! The magic dragon, live by the tree!
if x>10 y=log(x) if y>=3 z=4*y elseif y>=2.5 z=2*y else z=0 endelse y=5*x z=7*xend
x
![Page 34: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/34.jpg)
Puff! The magic dragon, live by the tree!
Self testing
• Draw the flow chart
• Write down the program and solution after ur input!
![Page 35: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/35.jpg)
Puff! The magic dragon, live by the tree!
Strings and conditional statements
• ?name='Leslie Student'
name =
Leslie Student
• ?number='123'
number =
123
• ?size(name)
ans =
1 14
• ?size(number)
ans =
1 3
![Page 36: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/36.jpg)
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
![Page 37: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/37.jpg)
Puff! The magic dragon, live by the tree!
Strings and conditional statements• 大小寫空格都是有區別的• Hellow ~= hellow• Cannot ~= Can not
?string1='can not'?string2='cannot'?strcmp(string1, string2) % string compareans = 0
![Page 38: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/38.jpg)
Puff! The magic dragon, live by the tree!
大小寫轉換• ?lower('STRING')
ans =
string
• ?upper('string')
ans =
STRING
![Page 39: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/39.jpg)
Puff! The magic dragon, live by the tree!
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• ?p195Do you want to continue? Y/N [Y]:yresponse =Y• ?p195Do you want to continue? Y/N [Y]:qweresponse =N
![Page 40: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/40.jpg)
Puff! The magic dragon, live by the tree!
string operator
help strfun % for more string operator
![Page 41: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/41.jpg)
Puff! The magic dragon, live by the tree!
Loops
• Known loop ~ for-loop
• Unknown loop ~ while-loop
![Page 42: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/42.jpg)
Puff! The magic dragon, live by the tree!
for-loop
for k=5:10:36
x=k^2
end
x = 25x = 225x = 625x = 1225
![Page 43: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/43.jpg)
Puff! The magic dragon, live by the tree!
![Page 44: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/44.jpg)
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 endend
![Page 45: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/45.jpg)
Puff! The magic dragon, live by the tree!
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
![Page 46: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/46.jpg)
Puff! The magic dragon, live by the tree!
Self testing
• P.197 (T4.4-1)
• Find some rules
• Try to write a function to generate the matrix
![Page 47: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/47.jpg)
Puff! The magic dragon, live by the tree!
for-loop k=m:s:n
• k=10:-2:4k = 10 8 6 4• ?j=10:6j = Empty matrix: 1-by-0• ?j=6:10 % s=1j = 6 7 8 9 10
![Page 48: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/48.jpg)
Puff! The magic dragon, live by the tree!
for-loop k=m:s:n• ?k=10:10k = 10• ?k=1:0.33:2k = 1.0000 1.3300 1.6600 1.9900• Do not change the k value in the for-k-loop state
ment! 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.
![Page 49: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/49.jpg)
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
y = 7y = 6.7823y = 6.4031y = 5.8310y = 5y = 3.7417y = 1
![Page 50: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/50.jpg)
Puff! The magic dragon, live by the tree!
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));endy
![Page 51: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/51.jpg)
Puff! The magic dragon, live by the tree!
A=[1,2,3;4,5,6];for v=A disp(v)end 1 4
2 5
3 6
A=[1,2,3;4,5,6];n=3;for k=1:n v=A(:,k)endv = 1 4v = 2 5v = 3 6
![Page 52: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/52.jpg)
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
end
![Page 53: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/53.jpg)
Puff! The magic dragon, live by the tree!
![Page 54: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/54.jpg)
Puff! The magic dragon, live by the tree!
![Page 55: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/55.jpg)
Puff! The magic dragon, live by the tree!
![Page 56: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/56.jpg)
Puff! The magic dragon, live by the tree!
while loops
while 邏輯表示式 敘述end
1. initial value set first
2. Stop when ‘false’ occurs
![Page 57: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/57.jpg)
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
4
![Page 58: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/58.jpg)
Puff! The magic dragon, live by the tree!
while x<10 x=x+1; y=2*xend
x=0;while x<10 x=x+1; y=2*xend
y = 2y = 4y = 6y = 8y = 10y = 12y = 14y = 16y = 18y = 20
![Page 59: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/59.jpg)
Puff! The magic dragon, live by the tree!
![Page 60: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/60.jpg)
Puff! The magic dragon, live by the tree!
infinite loop
x=8;while x~=0 x=x-3endx = 5x = 2x = -1…
![Page 61: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/61.jpg)
Puff! The magic dragon, live by the tree!
![Page 62: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/62.jpg)
Puff! The magic dragon, live by the tree!
![Page 63: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/63.jpg)
Puff! The magic dragon, live by the tree!
Self testing pp.208
• Important examples!
![Page 64: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/64.jpg)
Puff! The magic dragon, live by the tree!
4.5 switch structure
switch 輸入表示式case 數值 1
Statement 1case 數值 2
Statement 2...otherwise
statement nend
• switch input('angle=? ')• case 45
• disp(' 東北方 ')
• case 135
• disp(' 東南方 ')
• case 225
• disp(' 西南方 ')
• case 315
• disp(' 西北方 ')
• otherwise
• disp(' 未知方位 ')
• end
![Page 65: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/65.jpg)
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
![Page 66: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/66.jpg)
Puff! The magic dragon, live by the tree!
Debugging Matlab Programs
• P.210-218
![Page 67: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/67.jpg)
Puff! The magic dragon, live by the tree!
• Syntax errors ( 語法錯誤 )
• Runtime errors ( 運算時錯誤 )
![Page 68: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/68.jpg)
Puff! The magic dragon, live by the tree!
Debugging a loop
• Pp.216-218
![Page 69: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/69.jpg)
Puff! The magic dragon, live by the tree!
4.7 Applications to simulation
• Ex 4.7-1
• P.219
• Self testing
![Page 70: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/70.jpg)
Puff! The magic dragon, live by the tree!
![Page 71: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/71.jpg)
Puff! The magic dragon, live by the tree!
• State transition diagram
• x(k+1)=Cx(k)+b(k)
![Page 72: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/72.jpg)
Puff! The magic dragon, live by the tree!
![Page 73: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/73.jpg)
Puff! The magic dragon, live by the tree!
![Page 74: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/74.jpg)
Puff! The magic dragon, live by the tree!
![Page 75: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/75.jpg)
Puff! The magic dragon, live by the tree!
![Page 76: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/76.jpg)
Puff! The magic dragon, live by the tree!
![Page 77: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/77.jpg)
Puff! The magic dragon, live by the tree!
![Page 78: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/78.jpg)
Puff! The magic dragon, live by the tree!
![Page 79: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/79.jpg)
Puff! The magic dragon, live by the tree!
![Page 80: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/80.jpg)
Puff! The magic dragon, live by the tree!
![Page 81: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/81.jpg)
Puff! The magic dragon, live by the tree!
![Page 82: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/82.jpg)
Puff! The magic dragon, live by the tree!
![Page 83: Puff! The magic dragon, live by the tree! 第 4 章 用 MATLAB 設計程式 Programming with MATLAB](https://reader036.vdocuments.site/reader036/viewer/2022081420/56649eb35503460f94bbaaf4/html5/thumbnails/83.jpg)
Puff! The magic dragon, live by the tree!