How can we run many simulations at the same time? (for example for 1000 simulations)

I wrote this code, but it doesn't work properly for running 10 simulations:
for w=1:10
randn('state',100)
%stoichiometric matrix
V = [-1 1 0; -1 1 1; 1 -1 -1; 0 0 1];
%%%%%%%%%% Parameters andInitial Conditions %%%%%%%%%
nA = 6.023e23; % Avagadro’s number
vol = 1e-15; % volume of system
Y = zeros(4,1);
Y(1) = round(5e-7*nA*vol); % molecules of substrate
Y(2) = round(2e-7*nA*vol); % molecules of enzyme
c(1) = 1e6/(nA*vol); c(2) = 1e-4; c(3) = 0.1;
tfinal = 50;
L = 250;
tau = tfinal/L; % stepsize
counter=0;
T=zeros(L,1);
Exit1=zeros(L,1);
Exit2=zeros(L,1);
Exit3=zeros(L,1);
Exit4=zeros(L,1);
for k = 1:L
a(1) = c(1)*Y(1)*Y(2);
a(2) = c(2)*Y(3);
a(3) = c(3)*Y(3);
d(1) = tau*a(1) + sqrt(abs(tau*a(1)))*randn;
d(2) = tau*a(2) + sqrt(abs(tau*a(2)))*randn;
d(3) = tau*a(3) + sqrt(abs(tau*a(3)))*randn;
Y=Y+d(1)*V(:,1) + d(2)*V(:,2) + d(3)*V(:,3);
counter=counter+1;
T(counter)=counter;
Exit1(counter)=Y(1);
Exit2(counter)=Y(2);
Exit3(counter)=Y(3);
Exit4(counter)=Y(4);
% Recordor plot system state here if required
figure(1)
plot(T,Exit1,'-r')
hold on
plot(T,Exit2,'b-')
hold on
plot(T,Exit3,'-k')
hold on
plot(T,Exit4,'-y')
end
hold on
end

1 Comment

The randn('state') call is forcing the results to be the same for every iteration

Sign in to comment.

 Accepted Answer

@AT - please clarify what you mean "it doesn't work properly". What are you expecting to happen? Or are you getting the same results on each of the ten iterations because of
randn('state',100)
? I would consider removing this line of code as I think you are resetting the seed to the same value on each iteration of the loop. See Replace Discouraged Syntaxes of rand and randn for details on the behaviour of this function and why you may not want to use it.

4 Comments

I would like to run 10 simulations. (or 100 or 1000 simulations)
In this case, I used the "for" loop (The first line of my code). Could you please suggest another command that I should use instead of "for" to run many simulations at once and plot them in one plot? (Number of simulated paths = 10 or 1000 like this picture:)
Like I said below in my answer, "parfor". That will run them simultaneously instead of sequentially. But what's wrong with a for loop anyway?
What is the point of this line of code:
randn('state',100)
? You generate a 100 by 100 matrix and then throw it away because you don't save/store the numbers it returns. And I don't see in the help for randn() that it even takes a 'state' option.
@AT - you can still use the for loop (if you don't have the Parellel Computing Toolbox. Try putting the plot calls outside of the inner for loop like
for w=1:100
%stoichiometric matrix
V = [-1 1 0; -1 1 1; 1 -1 -1; 0 0 1];
%%%%%%%%%% Parameters andInitial Conditions %%%%%%%%%
nA = 6.023e23; % Avagadro’s number
vol = 1e-15; % volume of system
Y = zeros(4,1);
Y(1) = round(5e-7*nA*vol); % molecules of substrate
Y(2) = round(2e-7*nA*vol); % molecules of enzyme
c(1) = 1e6/(nA*vol); c(2) = 1e-4; c(3) = 0.1;
tfinal = 50;
L = 250;
tau = tfinal/L; % stepsize
counter=0;
T=zeros(L,1);
Exit1=zeros(L,1);
Exit2=zeros(L,1);
Exit3=zeros(L,1);
Exit4=zeros(L,1);
for k = 1:L
a(1) = c(1)*Y(1)*Y(2);
a(2) = c(2)*Y(3);
a(3) = c(3)*Y(3);
d(1) = tau*a(1) + sqrt(abs(tau*a(1)))*randn;
d(2) = tau*a(2) + sqrt(abs(tau*a(2)))*randn;
d(3) = tau*a(3) + sqrt(abs(tau*a(3)))*randn;
Y=Y+d(1)*V(:,1) + d(2)*V(:,2) + d(3)*V(:,3);
counter=counter+1;
T(counter)=counter;
Exit1(counter)=Y(1);
Exit2(counter)=Y(2);
Exit3(counter)=Y(3);
Exit4(counter)=Y(4);
% Recordor plot system state here if required
end
figure(1)
plot(T,Exit1,'-r')
hold on
plot(T,Exit2,'b-')
hold on
plot(T,Exit3,'-k')
hold on
plot(T,Exit4,'-y')
hold on
end
which produces something like the following for 100 iterations
It works. That's exactly what I wanted.
Thanks a million.

Sign in to comment.

More Answers (1)

Your loop is not running your simulations "at the same time". For that you'd need to use "parfor" instead of "for" and have the Parallel Computing Toolbox. Do you have that toolbox? If not, you'll have to run the simulations sequentially (like you're doing now) instead of at the same time.

Categories

Find more on Random Number Generation in Help Center and File Exchange

Tags

Asked:

AT
on 31 Dec 2021

Commented:

AT
on 4 Jan 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!