In the previous post, a simulation with 100 million trials was run on MATLAB to verify that in a scenario of two trains running independently with frequency of 10 minutes, the expected waiting time until the earlier train is 10/3 minutes. This was the code in a .m script, along with added comments for explanation:
%We will run 100 million trials, results to be stored in an arrayUpon running the script, the result comes out as a mean of 3.3334 (occasionally may get 3.3333) along with a standard deviation of 2.3572.
results = zeros(1,100000000);
for i=1:100000000
%Initiate the variable 'lowest' at 20 for now
%It will eventually record the time to the earlier train
%sure be less than 20
lowest = 20;
%Let both trains start at a random time t=(0,10)
%This is the first round of the trains
train1Start = 10*rand;
train2Start = 10*rand;
%There will be trains 10 minutes ahead, representing the second round
another1Train = train1Start+10;
another2Train = train2Start+10;
%Let the passenger board the platform at a random time t=(10,20)
passenger = 10*rand+10;
%The next train may be train 1 from first round
lowest = passenger-train1Start;
%Or it may be train 2 from the first round
lowest = min(lowest, passenger-train2Start);
%Or it may be train 1 from the second round
%Positive value verifies that passengers arrives become the train
if passenger-another1Train>=0
lowest = min(lowest, passenger-another1Train);
end
%Finally, the earlier train may be train 2 from the second round
if passenger-another2Train>=0
lowest = min(lowest, passenger-another2Train);
end
%Record the time to the earliest train into the results array
results(i) = lowest;
end
%At last, we want to return the results
disp('Mean:')
disp(mean(results))
disp('Standard deviation:')
disp(std(results))