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
軟體實作與計算實驗 1
Markov ChainRoots of nonlinear functionsNested FOR loops
Lecture 5II
軟體實作與計算實驗 2
State
1S 3S2S
軟體實作與計算實驗 3
Initial population
in denotes the initial population of being state iS
ni
i n
軟體實作與計算實驗 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
軟體實作與計算實驗 5
Transition probabilities
ijp denotes the probability of transition from toiS
1p
conditionUnitary
j
ij
jS
軟體實作與計算實驗 6
State
1S 3S2S
31p
12p
軟體實作與計算實驗 7
State transition
Populations after one time of state transition
Pv
mmmmm
m
m
n
nn
ppp
pppppp
2
1
21
22221
11211
, vP
軟體實作與計算實驗 8
State transition
Populations after one time of state transition
Populations after k times of state transition
vP'
vPP ''...k
軟體實作與計算實驗 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
軟體實作與計算實驗 10
Convergence
Change
Norm
new
new
vvvPv
'
)( newnorm vv
軟體實作與計算實驗 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
軟體實作與計算實驗 12
for i=1:k
v_new=P'*v
norm(v-v_new)<10^-6
Yes:converge
break v=v_new
軟體實作與計算實驗 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
軟體實作與計算實驗 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
軟體實作與計算實驗 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
軟體實作與計算實驗 16
while-loop
change>10^-6
change=1
v_new=P'*v
change=norm(v-v_new)v=v_new
軟體實作與計算實驗 17
change = 1while change < 10^-6
v_new=P' *v;change = norm(v-v_new);v=v_new
end
軟體實作與計算實驗 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
軟體實作與計算實驗 19
Nonlinear system
004
21
22
21
xxxx
軟體實作與計算實驗 20
A set of nonlinear functions
21212
22
21211
211
21121
),(4),(
),(),(
),F(
xxxxfxxxxf
xxfxxf
xx
軟體實作與計算實驗 21
function F = myfun(x) F(1) = x(1).^2 + x(2)^2-4; F(2) = x(1) - x(2);return
Function evaluation
軟體實作與計算實驗 22
x=linspace(-2,2);plot(x,x); hold on; plot(x,sqrt(4-x.^2))plot(x,-sqrt(4-x.^2))
軟體實作與計算實驗 23
Least square of nonlinear functions
i
ixxxxf 2
21,),(min
21
0),(04),(
SOLVE
21212
22
21211
xxxxfxxxxf
軟體實作與計算實驗 24
x0= ones(1,2)*2;x = lsqnonlin(@myfun,x0)
Find a zero
y=myfun(x);sum(y.^2)
CHECKING
軟體實作與計算實驗 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
軟體實作與計算實驗 26
012),(
0242),(
SOLVE
22212
22
21211
21
xxxxf
xxxxf
軟體實作與計算實驗 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
軟體實作與計算實驗 28
x0= ones(1,2)*2;x = lsqnonlin(@myfun2,x0)
Find a zero
y=myfun2(x);sum(y.^2)
CHECKING
軟體實作與計算實驗 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
軟體實作與計算實驗 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')
軟體實作與計算實驗 31
Demo_lsq_2c
source code
軟體實作與計算實驗 32
02),(022),(
SOLVE
21212
22
21211
xxxxfxxxxf
軟體實作與計算實驗 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
軟體實作與計算實驗 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')
軟體實作與計算實驗 35
x0= ones(1,2)*2;x = lsqnonlin(@myfun3,x0)
Find a zero
y=myfun3(x);sum(y.^2)
CHECKING
軟體實作與計算實驗 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
軟體實作與計算實驗 37
demo_lsq_hyperbola.m
two_hyperbola.m
軟體實作與計算實驗 38
Matrix Multiplication
C=A*BC(i,j)=A(i,:) *B(:,j) for all i,j
軟體實作與計算實驗 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
軟體實作與計算實驗 40
[n,d1]=size(A);[d2,m]=size(B);for i=1:nfor j=1:m C(i,j)=A(i, :) *B(:,j);endend
軟體實作與計算實驗 41
Constrained optimization
13
100102
22
21
12
2
1
xx
xxxx
Inequality constraint
Nonlinear equality
軟體實作與計算實驗 42
12
2
1
100102
xxxx
222
21,
)13(min21
xxxx
Constrained optimization
軟體實作與計算實驗 43
Constraints
Lower bound and upper boundEquality constraintInequality constraint
軟體實作與計算實驗 44
Constraints
Lower bound
Upper bound
],[]0,2[ 21 xx
]10,10[],[ 21 xx
Inequality constraints
0112
1
12
xx
xx
軟體實作與計算實驗 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
軟體實作與計算實驗 46
function y = fun(x)y = (x(1).^2+x(2).^2-13).^2;return
Objective function
軟體實作與計算實驗 47
Calling fmincon
x = fmincon(@fun,[1 1],A,b,Aeq,beq,lb,ub)
objective function initial
guesslinearequality
inequalityLower and upperbound
軟體實作與計算實驗 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(
軟體實作與計算實驗 49
Generative model
pa
pt pc
pg
A T C G
Flip-flop1 gcta pppp
gatcacaggt
軟體實作與計算實驗 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
軟體實作與計算實驗 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
軟體實作與計算實驗 52
Sequence generation
Emulation of a generative model for creation of an atcg sequence
軟體實作與計算實驗 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;
軟體實作與計算實驗 54
Mixture model
pa
pt pc
pg
A T C G
Flip-flop
pa
pt pc
pg
A T C G
Flip-flop1 2
軟體實作與計算實驗 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
軟體實作與計算實驗 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
軟體實作與計算實驗 57
Transition probability
2221
1211