lecture 5ii
DESCRIPTION
Lecture 5II. Markov Chain Roots of nonlinear functions Nested FOR loops. State. Initial population. denotes the initial population of being state. State transition. A person has a current state, e.g. S i Its next state may be any one of possible states State transition from S i to S j. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/1.jpg)
軟體實作與計算實驗 1
Markov ChainRoots of nonlinear functionsNested FOR loops
Lecture 5II
![Page 2: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/2.jpg)
軟體實作與計算實驗 2
State
1S 3S2S
![Page 3: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/3.jpg)
軟體實作與計算實驗 3
Initial population
in denotes the initial population of being state iS
ni
i n
![Page 4: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/4.jpg)
軟體實作與計算實驗 4
State transition
A person has a current state, e.g. Si Its next state may be any one of possible
statesState transition from Si to Sj
![Page 5: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/5.jpg)
軟體實作與計算實驗 5
Transition probabilities
ijp denotes the probability of transition from toiS
1p
conditionUnitary
j
ij
jS
![Page 6: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/6.jpg)
軟體實作與計算實驗 6
State
1S 3S2S
31p
12p
![Page 7: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/7.jpg)
軟體實作與計算實驗 7
State transition
Populations after one time of state transition
Pv
mmmmm
m
m
n
nn
ppp
pppppp
2
1
21
22221
11211
, vP
![Page 8: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/8.jpg)
軟體實作與計算實驗 8
State transition
Populations after one time of state transition
Populations after k times of state transition
vP'
vPP ''...k
![Page 9: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/9.jpg)
軟體實作與計算實驗 9
Exercise I
Draw a flow chart to calculate populations after k times of state transitions using for-loops
Write a matlab function to implement the flow chart
Find the result forP =
0.1000 0.4500 0.4500 0.3333 0 0.6667 0.5000 0 0.5000
v =
100 600 300
k=1k=10k=20k=100
![Page 10: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/10.jpg)
軟體實作與計算實驗 10
Convergence
Change
Norm
new
new
vvvPv
'
)( newnorm vv
![Page 11: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/11.jpg)
軟體實作與計算實驗 11
Convergence
The answer converges if
8
6
10)(
10)(
e.g.number positive small a than less is )(
new
new
new
norm
norm
norm
vv
vv
vv
![Page 12: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/12.jpg)
軟體實作與計算實驗 12
for i=1:k
v_new=P'*v
norm(v-v_new)<10^-6
Yes:converge
break v=v_new
![Page 13: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/13.jpg)
軟體實作與計算實驗 13
for i=1:kv_new=P' *v;
if norm(v_new-v)<10^-6 break else v=v_new
endend
vP k)'(lim k
![Page 14: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/14.jpg)
軟體實作與計算實驗 14
Exercise II
Draw a flow chart to illustrate finding
by a for-loopImplement your flow chart by a matlab
function
vP k)'(lim k
![Page 15: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/15.jpg)
軟體實作與計算實驗 15
for i=1:k
v_new=P'*v
norm(v-v_new)<10^-6
Yes:converge
break v=v_new
change>10^-6
change=1
v_new=P'*v
change=norm(v-v_new)v=v_new
![Page 16: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/16.jpg)
軟體實作與計算實驗 16
while-loop
change>10^-6
change=1
v_new=P'*v
change=norm(v-v_new)v=v_new
![Page 17: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/17.jpg)
軟體實作與計算實驗 17
change = 1while change < 10^-6
v_new=P' *v;change = norm(v-v_new);v=v_new
end
![Page 18: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/18.jpg)
軟體實作與計算實驗 18
Exercise III
Draw a flow chart to illustrate finding
by a while-loopImplement your flow chart by a matlab
function
vP k)'(lim k
![Page 19: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/19.jpg)
軟體實作與計算實驗 19
Nonlinear system
004
21
22
21
xxxx
![Page 20: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/20.jpg)
軟體實作與計算實驗 20
A set of nonlinear functions
21212
22
21211
211
21121
),(4),(
),(),(
),F(
xxxxfxxxxf
xxfxxf
xx
![Page 21: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/21.jpg)
軟體實作與計算實驗 21
function F = myfun(x) F(1) = x(1).^2 + x(2)^2-4; F(2) = x(1) - x(2);return
Function evaluation
![Page 22: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/22.jpg)
軟體實作與計算實驗 22
x=linspace(-2,2);plot(x,x); hold on; plot(x,sqrt(4-x.^2))plot(x,-sqrt(4-x.^2))
![Page 23: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/23.jpg)
軟體實作與計算實驗 23
Least square of nonlinear functions
i
ixxxxf 2
21,),(min
21
0),(04),(
SOLVE
21212
22
21211
xxxxfxxxxf
![Page 24: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/24.jpg)
軟體實作與計算實驗 24
x0= ones(1,2)*2;x = lsqnonlin(@myfun,x0)
Find a zero
y=myfun(x);sum(y.^2)
CHECKING
![Page 25: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/25.jpg)
軟體實作與計算實驗 25
Multiple roots
x0= ones(1,2)*2;x = lsqnonlin(@myfun,x0)v=[v;x];plot(x(1),x(2),'o');
v=[]
for i=1:n
demo_lsq.m
![Page 26: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/26.jpg)
軟體實作與計算實驗 26
012),(
0242),(
SOLVE
22212
22
21211
21
xxxxf
xxxxf
![Page 27: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/27.jpg)
軟體實作與計算實驗 27
function F = myfun2(x) F(1) = 2*x(1).^2 + x(2)^2-24; F(2) = x(1).^2 - x(2).^2+12;return
Function evaluation
012),(
0242),(22
212
22
21211
21
xxxxf
xxxxf
![Page 28: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/28.jpg)
軟體實作與計算實驗 28
x0= ones(1,2)*2;x = lsqnonlin(@myfun2,x0)
Find a zero
y=myfun2(x);sum(y.^2)
CHECKING
![Page 29: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/29.jpg)
軟體實作與計算實驗 29
Multiple roots
x0= ones(1,2)*2;x = lsqnonlin(@myfun2,x0)v=[v;x];plot(x(1),x(2),'o');
v=[]
for i=1:n
![Page 30: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/30.jpg)
軟體實作與計算實驗 30
x=linspace(-5,5);plot(x,sqrt(24-2*x.^2),'r');hold on; plot(x,-sqrt(24-2*x.^2),'r')plot(x,sqrt(12+x.^2),'b')plot(x,-sqrt(12+x.^2),'b')
![Page 31: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/31.jpg)
軟體實作與計算實驗 31
Demo_lsq_2c
source code
![Page 32: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/32.jpg)
軟體實作與計算實驗 32
02),(022),(
SOLVE
21212
22
21211
xxxxfxxxxf
![Page 33: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/33.jpg)
軟體實作與計算實驗 33
function F = myfun3(x) F(1) = x(1).^2 -2* x(2)^2-2; F(2) = x(1).*x(2) -2;return
Function evaluation
02),(022),(
SOLVE
21212
22
21211
xxxxfxxxxf
![Page 34: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/34.jpg)
軟體實作與計算實驗 34
x=linspace(-3,3);x=x(find(abs(x) > 0.1));plot(x,sqrt((x.^2-2)/2),'r');hold on; plot(x,-sqrt((x.^2-2)/),'r')x=linspace(0.5,3);plot(x,2./x,'b');x=linspace(-0.5,-3);plot(x,2./x,'b')
![Page 35: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/35.jpg)
軟體實作與計算實驗 35
x0= ones(1,2)*2;x = lsqnonlin(@myfun3,x0)
Find a zero
y=myfun3(x);sum(y.^2)
CHECKING
![Page 36: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/36.jpg)
軟體實作與計算實驗 36
Multiple roots
x0= ones(1,2)*2;x = lsqnonlin(@myfun3,x0)v=[v;x];plot(x(1),x(2),'o');
v=[]
for i=1:n
![Page 37: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/37.jpg)
軟體實作與計算實驗 37
demo_lsq_hyperbola.m
two_hyperbola.m
![Page 38: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/38.jpg)
軟體實作與計算實驗 38
Matrix Multiplication
C=A*BC(i,j)=A(i,:) *B(:,j) for all i,j
![Page 39: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/39.jpg)
軟體實作與計算實驗 39
Nested FOR Loops
C(i,j)=A(i,:) *B(:,j)
[n,d1]=size(A);[d2,m]=size(B)
for i=1:n
for j=1:m
![Page 40: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/40.jpg)
軟體實作與計算實驗 40
[n,d1]=size(A);[d2,m]=size(B);for i=1:nfor j=1:m C(i,j)=A(i, :) *B(:,j);endend
![Page 41: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/41.jpg)
軟體實作與計算實驗 41
Constrained optimization
13
100102
22
21
12
2
1
xx
xxxx
Inequality constraint
Nonlinear equality
![Page 42: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/42.jpg)
軟體實作與計算實驗 42
12
2
1
100102
xxxx
222
21,
)13(min21
xxxx
Constrained optimization
![Page 43: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/43.jpg)
軟體實作與計算實驗 43
Constraints
Lower bound and upper boundEquality constraintInequality constraint
![Page 44: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/44.jpg)
軟體實作與計算實驗 44
Constraints
Lower bound
Upper bound
],[]0,2[ 21 xx
]10,10[],[ 21 xx
Inequality constraints
0112
1
12
xx
xx
![Page 45: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/45.jpg)
軟體實作與計算實驗 45
Demo_conop2
function demo_conop2()lb =[-2 0];ub =[10,10];Aeq=[-1 1];beq=[0];A=[];b=[];x = fmincon(@fun,[1 1],A,b,Aeq,beq,lb,ub)x(1).^2+x(2).^2returnfunction y = fun(x)y = (x(1).^2+x(2).^2-13).^2;return
demo_conop2.m
![Page 46: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/46.jpg)
軟體實作與計算實驗 46
function y = fun(x)y = (x(1).^2+x(2).^2-13).^2;return
Objective function
![Page 47: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/47.jpg)
軟體實作與計算實驗 47
Calling fmincon
x = fmincon(@fun,[1 1],A,b,Aeq,beq,lb,ub)
objective function initial
guesslinearequality
inequalityLower and upperbound
![Page 48: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/48.jpg)
軟體實作與計算實驗 48
Random variable
Sample space S={A,T,C,G}X is a discrete random variable with
sample space S.
apAX )Pr(
cpCX )Pr(tpTX )Pr(
gpGX )Pr(
![Page 49: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/49.jpg)
軟體實作與計算實驗 49
Generative model
pa
pt pc
pg
A T C G
Flip-flop1 gcta pppp
gatcacaggt
![Page 50: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/50.jpg)
軟體實作與計算實驗 50
Partition
ap0 1cta
ta
a
pppnppn
pn
3
2
1
1n 2n 3n
Knots partition [0,1] into four intervals respectively with lengths,
321 and , nnn
gcta pppp and ,,
tp cp gp
![Page 51: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/51.jpg)
軟體實作與計算實驗 51
r=rand;Tag= (r<=c(1))+ 2*(r<=c(2) & r > c(1))+3*(r<=c(3) & r > c(2))+4*(r<=c(4) & r > c(3))switch Tagcase 1
s=[s 'A'];case 2
s=[s 'T'];case 3
s=[s 'C'];case 4
s=[s 'G'];end
![Page 52: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/52.jpg)
軟體實作與計算實驗 52
Sequence generation
Emulation of a generative model for creation of an atcg sequence
![Page 53: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/53.jpg)
軟體實作與計算實驗 53
FOR Loops
r=rand;switch r…end
input p,mn=length(p)p_sum=0;s=[];
for i=1:n for j=1:m
p_sum=p_sum+p(i);c(i)=p_sum;
![Page 54: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/54.jpg)
軟體實作與計算實驗 54
Mixture model
pa
pt pc
pg
A T C G
Flip-flop
pa
pt pc
pg
A T C G
Flip-flop1 2
![Page 55: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/55.jpg)
軟體實作與計算實驗 55
Mixture model
According to probabilities, and each time one of two joined models is
selected to generate a characterCreated characters are collected to form
an atcg sequence
1 2
![Page 56: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/56.jpg)
軟體實作與計算實驗 56
Hidden Markov model
1 2
pa
pt pc
pg
A T C G
pa
pt pc
pg
A T C G
11 21
22
12
![Page 57: Lecture 5II](https://reader036.vdocuments.pub/reader036/viewer/2022070503/568158c5550346895dc60e8e/html5/thumbnails/57.jpg)
軟體實作與計算實驗 57
Transition probability
2221
1211