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

3 vues (au cours des 30 derniers jours)
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 commentaire
Walter Roberson
Walter Roberson le 31 Déc 2021
The randn('state') call is forcing the results to be the same for every iteration

Connectez-vous pour commenter.

Réponse acceptée

Geoff Hayes
Geoff Hayes le 31 Déc 2021
@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 commentaires
Geoff Hayes
Geoff Hayes le 31 Déc 2021
@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
AT
AT le 4 Jan 2022
It works. That's exactly what I wanted.
Thanks a million.

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 31 Déc 2021
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.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by