|
So, I'm trying to run a random code that should randomize the results with a variable number of particles. As it stands, the code will keep running until the time runs out but will not stop when it goes above the allocated number of particles. I tried putting that in the while statement, but it doesn't seem to be working. The code is below:
function[]=mult(a0,b0,k,tf)
%%This function uses multiple particles in a
%%bistable system and a transition rate from state a to state b of k.
runs=a0+b0;
for i=1:runs
t=0;
a=a0;
b=b0;
rand('state', sum(100*clock));
tt=[];
aa=[];
bb=[];
while ((a+b)<=(a0+b0) && t<=tf)
tt=[tt t];
aa=[aa a];
bb=[bb b];
r=rand(1);
if r>k
anew=a+1;
bnew=b-1;
else
anew=a-1;
bnew=b+1;
end
a=anew;
b=bnew;
t=t+1;
end
end
plot(tt,aa,'k','linewidth',2)
set(gca,'Fontname','Helvetica','Fontsize',16);
xlim([0,tf])
xlabel('Time','Fontsize',16)
ylabel('State','Fontsize',16)
set(gca,'Box','off');
title(['Gillespie algorithm with multiple particles and ', num2str(k*100),'% chance of switching'],'Fontsize',14)
print -depsc2 mult.eps
|