Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
Modelling reactions usting randsamples
    조회 수: 2 (최근 30일)
  
       이전 댓글 표시
    
I am quite new on matlab so sorry for bothering you. I am trying to model the following reactions (all are molecules)
A+A-----C rate constant k1=0.001 A decreases by 2 Also given that a reaction happens the probability is k1*A(A-1)*R
A+B-----D rate constant k2=0.01 A and B decreases by 1 probability is A*B*(k2)/(R
u-----A rate constant k3=1.2 A increases by 1 probability is (k3)/R
u----B rate constant k4=1) B increases by 1 probability is (k4)/R
And t can be calculated from the exprnd function
Now i have that the total rate is R= A(A-1)*k1+AB*k2+k3+k4
The thing i need to is to create an array of three columns time, A and B and an appropriate number of columns. Now using the rate constants above create a for loop, calculate total R, calculate t from exprnd function, and finally use a random sample to determine which reaction occurs at t+dt. Then i would need to update the values of A, B and time in my inital vector accordingly. PLEASE i am quite new with matlab may someone help me writing this code!
I have written the following, but its just producing the final value of A and B
iterations= 15
A= 2
B= 2
T=0
tC= zeros([iterations+1,3])
%tC(1,:)= [T,A,B]
k1=0.001;
k2=0.01;
k3=1.2;
k4= 1;
for n=1:iterations
    rate1= k1*A.*(A-1);
    rate2= A.*B*k2;
    rate3= k3;
    rate4= k4;
    rate=A.*(A-1)*k1+A.*B*k2+k3+k4; 
    taus=exprnd(1./(k*rate),[n+1,1]);
    p1=k1*A*(A-1)/rate;
    p2=A*B*k2/rate;
    p3=k3/rate;
    p4= k4/rate;
    y=randsample([1,2,3,4],1,true,[p1,p2,p3,p4]);
    if y==1
        A=A-2
    elseif y==2
        A=A-1
        B=B-1
    elseif y==3
        A=A+1
    elseif y==4
        B=B+1
    end
        A=(A)
    end
tC(:,1)=taus
tC(:,2)=A
tC(:,3)=B
PLEASE HELP ME
댓글 수: 0
답변 (0개)
이 질문은 마감되었습니다.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
