Run for loop 1000 times and get distribution of results
I have some code that runs a simulation of an equation in a for loop which results in a 1×128 array. I want to run a large number of these simulations, say 1000, so we have 1000 arrays of 1×128, 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;
endI have some code that runs a simulation of an equation in a for loop which results in a 1×128 array. I want to run a large number of these simulations, say 1000, so we have 1000 arrays of 1×128, 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 I have some code that runs a simulation of an equation in a for loop which results in a 1×128 array. I want to run a large number of these simulations, say 1000, so we have 1000 arrays of 1×128, 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 for loop, histogram MATLAB Answers — New Questions