%script to simulate the problem of a miner trapped in a mine
%with 3 doors to selected. One door leads to freedom in 3 hrs,
%second door leads back to the mine in 5 hrs, 3rd door leads
%back to the mine in 7 hrs. Find expected time to leave the mine
%by Nasser Abbasi. For math 502 CSUF probability and statistics 10/18/07
%example output:
%
%number of simulations=20000, Time to escape = 15.054550
%----- configuration section ----------%
close all;
TRUE = 1;
FALSE = 0;
total_simulation_time = 0;
t1 = 3; %time to escape for first door
t2 = 5; %time to escape for second door
t3 = 7; %time to escape for third door
number_of_simulations = 40000;
current_simulation_number = 0;
number_of_doors = 3;
randn('state',010101);
figure(1);
set(gcf,'Doublebuffer','on') ;
grid off
axis([1 number_of_simulations 12 16])
hold on;
%------ algorithm start --------------------%
while current_simulation_number < number_of_simulations
this_simulation_time = 0;
escaped = FALSE;
while not(escaped)
door_number = ceil(number_of_doors.*rand(1,1));
switch door_number
case 1
escaped = TRUE;
this_simulation_time = this_simulation_time + t1;
case 2
this_simulation_time = this_simulation_time + t2;
case 3
this_simulation_time = this_simulation_time + t3;
end
end
total_simulation_time = total_simulation_time + this_simulation_time;
current_simulation_number = current_simulation_number + 1;
if mod(current_simulation_number,50)==0
current_average_time_to_escape = ...
total_simulation_time/current_simulation_number;
title(sprintf('Number of simulations=%d\nExpected time to escape=%f hrs',...
current_simulation_number,current_average_time_to_escape));
xlabel('simulation number'); ylabel('expected time (hr)');
plot(current_simulation_number,current_average_time_to_escape,'.','MarkerSize',5);
drawnow;
end
end
fprintf('\nNumber of simulations=%d\nTime to escape = %f\n',...
number_of_simulations,...
total_simulation_time/number_of_simulations );