```Path: news.mathworks.com!not-for-mail
From: "Casi " <beatlesschic@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Matlab Code-Problem with loop.
Date: Fri, 3 Jun 2011 21:40:19 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 50
Message-ID: <isbkc3\$i9r\$1@newscl01ah.mathworks.com>
NNTP-Posting-Host: www-00-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1307137219 18747 172.30.248.45 (3 Jun 2011 21:40:19 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 3 Jun 2011 21:40:19 +0000 (UTC)
Xref: news.mathworks.com comp.soft-sys.matlab:730121

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
```