Run for loop 1000 times and get distribution of results

5 vues (au cours des 30 derniers jours)
clearsnake
clearsnake le 20 Jan 2025
Commenté : clearsnake le 21 Jan 2025
I have some code that runs a simulation of an equation in a for loop which results in a 1x128 array. I want to run a large number of these simulations, say 1000, so we have 1000 arrays of 1x128, then get the distribution of the 128th element of each array, say in a histogram.
So run for loop 1000 times, take value of 128th column from each array for 1000 values, then plot histogram of results. The X axis would be 0 to 1 and the Y axis would be frequency of each value (of course).
I'm sure the solution is quite simple, but everything I've tried hasn't worked right and I can't figure out where I'm going wrong, so I'd appreciate some advice.
Xzero = 0;
T = 1;
N = 2^8;
dt = 1/N;
r=1;
G=0.7;
e=0.5;
M=1000;
dW = sqrt(dt)*randn(M,N);
W = cumsum(dW);
R = 2; Dt = R*dt; L = N/R;
Xem = zeros(1,L);
Xtemp = Xzero;
for j = 1:L
Winc = sum(dW(R*(j-1)+1:R*j));
Xtemp = Dt*r*(G-Xtemp) + sqrt((e*Xtemp)*(1-Xtemp))*Winc;
Xem(j) = Xtemp;
end

Réponse acceptée

Stephen23
Stephen23 le 21 Jan 2025
Modifié(e) : Stephen23 le 21 Jan 2025
T = 1;
N = 2^8;
dt = 1/N;
r = 1;
G = 0.7;
e = 0.5;
R = 2;
Dt = R*dt;
L = N/R;
M = 1000;
Xem = nan(M,L);
for ii = 1:M
dW = sqrt(dt)*randn(M,N);
W = cumsum(dW);
Xtemp = 0;
for jj = 1:L
Winc = sum(dW(R*(jj-1)+1:R*jj));
Xtemp = Dt*r*(G-Xtemp) + sqrt((e*Xtemp)*(1-Xtemp))*Winc;
Xem(ii,jj) = real(Xtemp);
end
end
display(Xem)
Xem = 1000×128
0.0055 0.0036 0.0043 0.0038 0.0082 0.0133 0.0137 0.0112 0.0020 0.0061 0.0155 -0.0063 0.0055 0.0090 0.0152 0.0038 0.0064 0.0065 0.0018 0.0038 0.0046 0.0027 -0.0004 0.0055 0.0024 0.0041 0.0058 0.0048 0.0041 0.0098 0.0055 0.0014 0.0073 0.0080 0.0047 0.0121 0.0085 -0.0064 0.0055 0.0066 0.0083 0.0094 -0.0027 0.0055 0.0026 0.0030 0.0107 -0.0004 0.0059 0.0066 0.0117 0.0159 0.0009 0.0095 0.0046 0.0046 -0.0026 0.0055 -0.0005 0.0062 0.0055 0.0067 -0.0042 0.0055 0.0013 0.0042 0.0104 0.0062 0.0057 0.0019 0.0033 0.0041 0.0062 0.0087 0.0036 0.0094 0.0102 0.0050 0.0030 0.0077 0.0055 -0.0027 0.0055 0.0036 0.0038 0.0009 0.0053 0.0040 0.0064 0.0084 0.0055 0.0115 -0.0060 0.0055 0.0010 0.0049 0.0043 0.0056 -0.0073 0.0055 0.0029 0.0014 0.0090 0.0080 0.0166 0.0170 -0.0094 0.0055 0.0021 0.0103 0.0141 0.0072 0.0098 0.0070 0.0054 0.0017 0.0068 0.0122 -0.0034 0.0055 0.0055 0.0100 0.0050 0.0137 0.0125 0.0115 0.0011 0.0037 0.0018 0.0070 0.0082 0.0063 0.0048 0.0040 0.0015 0.0065 0.0073 0.0122 0.0076 0.0004 0.0089 0.0066 0.0113 0.0037 0.0048 0.0015 0.0051 -0.0023 0.0055 -0.0008 0.0055 0.0092 -0.0030 0.0055 0.0034 0.0030 0.0002 0.0049 0.0041 0.0101 0.0126 0.0085 0.0143 -0.0090 0.0055 0.0013 0.0057 0.0001 0.0059 0.0055 0.0114 -0.0008 0.0055 0.0049 0.0070 0.0009 0.0029 0.0037 0.0082 -0.0020 0.0055 0.0027 0.0080 0.0082 0.0102 -0.0015 0.0055 0.0123 0.0140 -0.0017 0.0055 0.0032 0.0086 0.0013 0.0076 0.0092 0.0045 0.0052 0.0111 0.0118 0.0046 0.0039 -0.0040 0.0055 0.0017 0.0069 0.0070 0.0009 0.0033 0.0092 0.0055 0.0019 0.0023 0.0043 0.0049 0.0100 0.0068 0.0035 0.0018 0.0096 0.0060 0.0068 0.0043 0.0105 0.0141 0.0021 0.0070 0.0092 0.0073 0.0059 0.0073 0.0060 0.0066 0.0002 0.0035 0.0012 0.0074 0.0043 0.0052 0.0067 0.0055 0.0006 0.0052 0.0057 0.0010 0.0066 0.0054 0.0062 0.0075 0.0101 0.0064 0.0049 0.0091 0.0169 0.0080 0.0037 0.0020 0.0032 0.0085 0.0070 0.0151 0.0051 0.0114 0.0105 -0.0011 0.0055 0.0156 0.0145 0.0014 0.0015 0.0055 0.0040 0.0048 -0.0025 0.0055 0.0056 0.0061 0.0138 0.0115 0.0082 -0.0002 0.0055 0.0065 0.0097 0.0051 0.0008 0.0038 0.0088 0.0133 0.0059 0.0076 0.0131 -0.0056 0.0055 0.0036 -0.0003 0.0040 0.0149 0.0085 -0.0027
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  3 commentaires
Torsten
Torsten le 21 Jan 2025
Modifié(e) : Torsten le 21 Jan 2025
You never use W, so why do you compute it as
W = cumsum(dW);
Further, dW is a 2d-array. Are you sure that accessing
dW(R*(jj-1)+1:R*jj)
thus converting 2d- to 1d-indexing here, is correct ?
If yes: shouldn't it be
Winc = sum(dW(M*(jj-1)+1:M*jj));
instead of
Winc = sum(dW(R*(jj-1)+1:R*jj));
?
Further taking the real part of the results Xem - do you think it's correct ?
clearsnake
clearsnake le 21 Jan 2025
Hi, thanks for the reply. I think that W must be left over from a previous draft of my code, sorry about that.
For the 2D- to 1D- indexing change, I'd have to say I don't know enough to be sure it's correct. My initial approach before I tried something easier with the for loops was along the lines of Stephen's answer so it looked correct to me. I agree though that the two nested for loops is the most "direct" way though.
For taking the real part, the imaginary parts of the values I'm getting are so small they seem negligible to me.

Connectez-vous pour commenter.

Plus de réponses (1)

Akshat Dalal
Akshat Dalal le 21 Jan 2025
Hi,
You can run the above code inside another for loop from 1 to 1000. In each iteration, you can store the value of the 128th column in another array defined outside the first loop. This way, you store only the desired value in each iteration. You can then plot the histogram on this array as necessary.
Thanks
  1 commentaire
clearsnake
clearsnake le 21 Jan 2025
Hi, thanks for the answer. I've been trying something similar but it's going wrong and only adding the value to the 128th column of my results array, all the rest are left zero, maybe you can see what correction is needed:
Xzero = 0;
T = 1;
N = 2^8;
dt = 1/N;
r=1;
G=0.7;
e=0.5;
M=1000;
dW = sqrt(dt)*randn(M,N);
W = cumsum(dW);
R = 2; Dt = R*dt; L = N/R;
Xem = zeros(1,L);
Xtemp = Xzero;
results = zeros (1,M);
for i = 1:M
for j = 1:L
Winc = sum(dW(R*(j-1)+1:R*j));
Xtemp = Dt*r*(G-Xtemp) + sqrt((e*Xtemp)*(1-Xtemp))*Winc;
Xem(j) = Xtemp;
end
results(1,j) = results(1,j) + Xem(1,128);
end
histogram(real(results));

Connectez-vous pour commenter.

Catégories

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

Community Treasure Hunt

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

Start Hunting!

Translated by