Stopping Criteria for Monte Carlo Simulations
5 views (last 30 days)
Show older comments
Hi, I did used uniform distribution to sample a variable for computation. I did not want to specify the number of iteration but rather I wanted to simulate continuously until a certain criteria is met. For the criteria, I used error margin (standard error). The problem is, the simulation wouldn't end, it keeps repeating itself. I have added my code below. I am really new to matlab and have no idea what I'm doing wrong. Please I really need help on this. Any other ideas on how to re-code this whole thing is warmly welcomed.
I really need help!! Thanks in advance
Ruby
NB: sorry about the formatting of my code. This is my first time. I tried very hard to format but was not getting it right so I ended deleting all comments. I hope my coding and the presentation makes sense. Thanks
MATLAB code
%%Loading files
clear all
close all
clc
% +++++++++++++++++++++++++INPUT PARAMETERS++++++++++++++++++++++++++++++++ L = 16597.36277; m = 5/3; i0_given = 40; td_given = 50;
i0 = i0_given*(1/3600)*(1/1000); td = td_given*60;
k = 1; s0 = 0.08344686;
%% +++++++++++++++++++++++++MONTE CARLO SIMULATION+++++++++++++++++++++++++
for num_iter= 1:inf;
a=0.001; b=0.4; manning_rnd = a + (b-a).*rand(num_iter,1);
manning_new = mean(manning_rnd)
alph=(1/k)*manning_new*(s0)^0.5; te = L^(1/m)/(alph*i0^(m-1))^(1/m);
if te < td
% rising limb
delta=(te-0)/20;
t1 = 0:delta:te;
qL1 = alph*(i0*t1).^m;
h1 = i0*t1;
t = td;
qL2 = i0*L;
h2 = (qL2/alph).^(1/m);
r = (qL2-0.0000001)/20;
qL3 = qL2-r:-r:0.0000001;
h3 = (qL3/alph).^(1/m);
% t2 = td+(L/(alph*h3.^(m-1))-(y3/i0))/m;
t2 =td+((1/m)*((L./(alph*h3.^(m-1)))-(h3/i0)));
qL = [qL1,qL2,qL3];
h = [h1,h2,h3];
t = [t1,td,t2];
else if te > td
tp = td-(td/m)+L/(m*alph*(i0*td)^(m-1));
delta=(td-0)/20;
t1 = 0:delta:td;
qL1 = alph*(i0*t1).^m;
h1 = i0*t1;
t = tp;
qL2 = alph*(i0*td).^m;
h2 = (qL2/alph).^(1/m);
r = (qL2-0.0000001)/20;
qL3 = qL2-r:-r:0.0000001;
h3 = (qL3/alph).^(1/m);
t2 =td+((1/m)*((L./(alph*h3.^(m-1)))-(h3/i0)));
end
end
qL = [qL1,qL2,qL3];
hd = [h1,h2,h3];
td = [t1,tp,t2];
%% covergence or stoping criteria
ConfidenceLevel = 0.95; Zvalue = norminv(1-((1-ConfidenceLevel)/2)); sigma = 0.119603621;
for i = 1:num_iter
while (i<max(size(num_iter))); StandardError(i) = (Zvalue*sigma)/num_iter(i)^0.5; if StandardError <= 0.01; disp('Stopping now') break end end end StandardError(i) end
0 Comments
Answers (1)
Ryan G
on 27 Sep 2012
Edited: Ryan G
on 27 Sep 2012
The problem is the for loop should be inside the while loop. Right now nothing changes each iteration of the while loop.
Beyond that what I believe you want to do is something like this for that loop:
counter = 0;
while (counter<numel(num_iter));
StandardError(counter) = (Zvalue*sigma)/manning_rnd(counter)^0.5;
if StandardError <= 0.01;
disp('Stopping now')
counter = numel(num_iter)
end
counter = counter+1;
end
Thing is you also define
num_iter = 1:inf;
This doesn't really make sense. What you would want to do is
num_iter = 1:10000 %some really big number but not so big it runs forever
I didn't really read through all that code, just the loop at the bottom. This should get you going.
Furthermore, you could skip this altogether using an optimization function if you are interested in is achieving a result within a tolerance, such as fmincon
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!