83
Puff! The magic dragon, live by the tree! 第4第 用 MATLAB 用用用用 Programming with MATLAB

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

Embed Size (px)

DESCRIPTION

第 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

Citation preview

Page 1: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

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

Page 2: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

What is programming?

• Programming computer program *.m

• Make decision~ loop

• Switch

• 寫程式

Page 3: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

4.1 Relational operators關係運算子

• ‘=‘ assignment operator; replacement operator

• x=3 ~ x3

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

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

優先順序•計算符號 +-/ 優於 關係運算子• 由左而右z=5>3~=1

z=(5>3)~=1

z=0

Page 6: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Page 7: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

4.2 Logical operators and Functions邏輯運算子與函式

Page 8: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Boolean operator

Page 9: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Page 12: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

• P.179

• exclusive OR

Page 13: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Page 15: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Page 16: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Self testing

• P.183

• Write down the program….

Page 20: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Page 25: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

yxw

yxz

log3log

Page 26: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Flow chart

• 平行四邊形 ~ 輸入或輸出• 矩形 ~ 運算• 菱形 ~ 決策點• 箭頭方向 ~ 程式計算流程與順序

Page 27: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Structure chart

• 顯示程式的組織• 但不會顯示詳細的計算及決策的過程

• 大程式 : 主程式 + 副程式 1+ 副程式 2+…

• Use structure chart, e.g. next page…

Page 28: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Page 29: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

大小寫轉換• ?lower('STRING')

ans =

string

• ?upper('string')

ans =

STRING

Page 39: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

string operator

help strfun % for more string operator

Page 41: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Loops

• Known loop ~ for-loop

• Unknown loop ~ while-loop

Page 42: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Page 44: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with 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

y = 7y = 6.7823y = 6.4031y = 5.8310y = 5y = 3.7417y = 1

Page 50: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Page 54: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Page 55: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Page 56: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

while loops

while 邏輯表示式 敘述end

1. initial value set first

2. Stop when ‘false’ occurs

Page 57: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Page 60: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

infinite loop

x=8;while x~=0 x=x-3endx = 5x = 2x = -1…

Page 61: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Page 62: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Page 63: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Self testing pp.208

• Important examples!

Page 64: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

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: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Debugging Matlab Programs

• P.210-218

Page 67: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

• Syntax errors ( 語法錯誤 )

• Runtime errors ( 運算時錯誤 )

Page 68: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Debugging a loop

• Pp.216-218

Page 69: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

4.7 Applications to simulation

• Ex 4.7-1

• P.219

• Self testing

Page 70: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Page 71: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

• State transition diagram

• x(k+1)=Cx(k)+b(k)

Page 72: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Page 73: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Page 74: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Page 75: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Page 76: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Page 77: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Page 78: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Page 79: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Page 80: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Page 81: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Page 82: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!

Page 83: 第 4 章   用 MATLAB 設計程式 Programming with MATLAB

Puff! The magic dragon, live by the tree!