570 likes | 716 Views
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.
E N D
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. Si • Its next state may be any one of possible states • State transition from Si to Sj 軟體實作與計算實驗
Transition probabilities denotes the probability of transition from to 軟體實作與計算實驗
State 軟體實作與計算實驗
State transition Populations after one time of state transition 軟體實作與計算實驗
State transition Populations after one time of state transition Populations after k times of state transition k 軟體實作與計算實驗
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 for v = 100 600 300 P = 0.1000 0.4500 0.4500 0.3333 0 0.6667 0.5000 0 0.5000 k=1 k=10 k=20 k=100 軟體實作與計算實驗
Convergence Change Norm 軟體實作與計算實驗
Convergence The answer converges if 軟體實作與計算實驗
for i=1:k v_new=P'*v norm(v-v_new)<10^-6 Yes:converge break v=v_new 軟體實作與計算實驗
for i=1:k v_new=P' *v; if norm(v_new-v)<10^-6 break else v=v_new end end 軟體實作與計算實驗
Exercise II Draw a flow chart to illustrate finding by a for-loop Implement your flow chart by a matlab function 軟體實作與計算實驗
change=1 for i=1:k change>10^-6 v_new=P'*v v_new=P'*v norm(v-v_new)<10^-6 change=norm(v-v_new) v=v_new Yes:converge break v=v_new 軟體實作與計算實驗
while-loop change=1 change>10^-6 v_new=P'*v change=norm(v-v_new) v=v_new 軟體實作與計算實驗
change = 1 while change < 10^-6 v_new=P' *v; change = norm(v-v_new); v=v_new end 軟體實作與計算實驗
Exercise III Draw a flow chart to illustrate finding by a while-loop Implement your flow chart by a matlab function 軟體實作與計算實驗
Nonlinear system 軟體實作與計算實驗
A set of nonlinear functions 軟體實作與計算實驗
Function evaluation function F = myfun(x) F(1) = x(1).^2 + x(2)^2-4; F(2) = x(1) - x(2); return 軟體實作與計算實驗
x=linspace(-2,2); plot(x,x); hold on; plot(x,sqrt(4-x.^2)) plot(x,-sqrt(4-x.^2)) 軟體實作與計算實驗
Least square of nonlinear functions 軟體實作與計算實驗
Find a zero x0= ones(1,2)*2; x = lsqnonlin(@myfun,x0) y=myfun(x); sum(y.^2) CHECKING 軟體實作與計算實驗
Multiple roots v=[] demo_lsq.m for i=1:n x0= ones(1,2)*2; x = lsqnonlin(@myfun,x0) v=[v;x]; plot(x(1),x(2),'o'); 軟體實作與計算實驗
Function evaluation function F = myfun2(x) F(1) = 2*x(1).^2 + x(2)^2-24; F(2) = x(1).^2 - x(2).^2+12; return 軟體實作與計算實驗
Find a zero x0= ones(1,2)*2; x = lsqnonlin(@myfun2,x0) y=myfun2(x); sum(y.^2) CHECKING 軟體實作與計算實驗
Multiple roots v=[] for i=1:n x0= ones(1,2)*2; x = lsqnonlin(@myfun2,x0) v=[v;x]; plot(x(1),x(2),'o'); 軟體實作與計算實驗
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') 軟體實作與計算實驗
Demo_lsq_2c source code 軟體實作與計算實驗
Function evaluation function F = myfun3(x) F(1) = x(1).^2 -2* x(2)^2-2; F(2) = x(1).*x(2) -2; return 軟體實作與計算實驗
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') 軟體實作與計算實驗
Find a zero x0= ones(1,2)*2; x = lsqnonlin(@myfun3,x0) y=myfun3(x); sum(y.^2) CHECKING 軟體實作與計算實驗
Multiple roots v=[] for i=1:n x0= ones(1,2)*2; x = lsqnonlin(@myfun3,x0) v=[v;x]; plot(x(1),x(2),'o'); 軟體實作與計算實驗
demo_lsq_hyperbola.m two_hyperbola.m 軟體實作與計算實驗
Matrix Multiplication • C=A*B • C(i,j)=A(i,:) *B(:,j) for all i,j 軟體實作與計算實驗
Nested FOR Loops [n,d1]=size(A);[d2,m]=size(B) for i=1:n for j=1:m C(i,j)=A(i,:) *B(:,j) 軟體實作與計算實驗
[n,d1]=size(A);[d2,m]=size(B); for i=1:n for j=1:m C(i,j)=A(i, :) *B(:,j); end end 軟體實作與計算實驗
Constrained optimization Inequality constraint Nonlinear equality 軟體實作與計算實驗
Constrained optimization 軟體實作與計算實驗
Constraints • Lower bound and upper bound • Equality constraint • Inequality constraint 軟體實作與計算實驗
Constraints Lower bound Upper bound Inequality constraints 軟體實作與計算實驗
Demo_conop2 demo_conop2.m 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).^2 return function y = fun(x) y = (x(1).^2+x(2).^2-13).^2; return 軟體實作與計算實驗
Objective function function y = fun(x) y = (x(1).^2+x(2).^2-13).^2; return 軟體實作與計算實驗
Calling fmincon x = fmincon(@fun,[1 1],A,b,Aeq,beq,lb,ub) objective function initial guess linear equality inequality Lower and upper bound 軟體實作與計算實驗
Random variable • Sample space S={A,T,C,G} • X is a discrete random variable with sample space S. 軟體實作與計算實驗
Flip-flop pa pt pg pc T C G A Generative model gatcacaggt 軟體實作與計算實驗
Partition 0 1 Knots partition [0,1] into four intervals respectively with lengths, 軟體實作與計算實驗