Genetic Engineering in Plants — Transgenic Plants 第十章 植物的基因工程.
第 4 章 用 MATLAB 設計程式 Programming with MATLAB
-
Upload
virginia-vin -
Category
Documents
-
view
100 -
download
0
description
Transcript of 第 4 章 用 MATLAB 設計程式 Programming with MATLAB
Puff! The magic dragon, live by the tree!
第 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 ~ x3
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
Puff! The magic dragon, live by the tree!
優先順序•計算符號 +-/ 優於 關係運算子• 由左而右z=5>3~=1
z=(5>3)~=1
z=0
Puff! The magic dragon, live by the tree!
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>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)
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
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 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!
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
• 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!
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) (else)
• (if) (elseif) (else)
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
Puff! The magic dragon, live by the tree!
if statement
• if (logic expression 1)
statement 1
if (logic expression 2)
statement 2
end
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!
yxw
yxz
log3log
Puff! The magic dragon, live by the tree!
Flow chart
• 平行四邊形 ~ 輸入或輸出• 矩形 ~ 運算• 菱形 ~ 決策點• 箭頭方向 ~ 程式計算流程與順序
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)
(statement 1)
else
(statement 2)
end
• if x>=0
y=sqrt(x)
else
y=exp(x)-1
end
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
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
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
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
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'
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!
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
?string1='can not'?string2='cannot'?strcmp(string1, string2) % string compareans = 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/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
Puff! The magic dragon, 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 = 25x = 225x = 625x = 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 endend
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
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
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
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.
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
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
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
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
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
4
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
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-3endx = 5x = 2x = -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 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
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!