"Darren Rowland" wrote in message <idhjaa$d07$1@fred.mathworks.com>...
> Kamrul,
>
> Try this code. I put the separate parts into functions so the biggest change is that there is now a loop which maintains a sum of the wait times for each passenger.
> The average is calculated once the number of trials is complete.
>
> You could view a histogram of the waiting times using hist(averageWait). Another plot you could try is plot(sort(averageWait))
>
> Hth
> Darren
>
> function averageWait = simulateBus(sTime,maxPassengers,nTrials)
> % SIMULATEBUS  determine average waiting times for passengers at bus stop
> % sTime: operation time of simulation (seconds)
> % maxPassengers: max. no. of passengers to arrive at stop
> % nTrials: no. of simulation runs to perform
>
> nSeats = 60;
> par = 5/60; % arrival rate of passengers (pax/second)
> bf = 1/600; % 10 min headway
> sqc = 1; % square of coefficient of variation of bus headway
> TotalWait = zeros(1,maxPassengers);
> WaitCount = zeros(1,maxPassengers);
>
> for nT = 1:nTrials
> pat = getPassengerTimes(sTime,maxPassengers,par);
> bat = getBusTimes(sTime,bf,sqc);
> %waiting time calculation
> pwt = nan(size(pat)); % pax waiting time
> for nBus = 1:length(bat)
> pcanboard = find(pat <= bat(nBus) & isnan(pwt));
> pcanboard (nSeats+1:end) = [];
> pwt(pcanboard) = bat(nBus)  pat(pcanboard);
> end
> pwt(isnan(pwt)) = sTime;
> % Accumulate waiting times
> TotalWait(1:length(pwt)) = TotalWait(1:length(pwt)) + pwt;
> WaitCount(1:length(pwt)) = WaitCount(1:length(pwt)) + 1;
> end
> iWait = WaitCount>0;
> % Find average wait time per passenger
> averageWait = TotalWait(iWait)./WaitCount(iWait);
>
> function pat = getPassengerTimes(sTime,maxPassengers,par)
> % Returns an array of passenger arrival times (seconds)
> % sTime: operation time of simulation
> % maxPassengers: max. no. of passengers to arrive at stop
> % par: arrival rate of passengers (pax/second)
> ap = rand(1,maxPassengers); % arrival probability of passengers
> pint = expinv(ap,1/par); % passenger inter arrival time
> pat = cumsum(pint);
> pat(pat>sTime) = []; % actual passenger arrival time
>
> function bat = getBusTimes(sTime,bf,sqc)
> % Returns an array of bus arrival times (seconds)
> % sTime: operation time of simulation
> % bf: bus headway
> % sqc: square of coeffient of variation of bus headway
> bp = rand(1,100); % bus arrival probability
> bint = gaminv(bp,1/sqc,sqc/bf); % bus inter arrival time at stop
> bat = cumsum(bint);
> bat(bat>sTime) = []; % actual bus arrival time
Hi Darren,
Thanks for your kind help. The current code is applicable to calculated passenger waiting time at first stop only. I have included a code to calculate passenger load inside bus ( i, e. number of passenger in bus). I assume each passenger take 3 second time to board the bus and calculated bus departure time from a stop 1. Now, I am trying to find the passenger waiting time for next stops assuming there are 4 stops in the route. I have calculated bus arrival time at stop 2 as bus departure time from stop2+travel time of bus from stop 1 to stop2. I assume some portion of passenger inside bus will alight at stop 2 and 3. (As example, when bus arrives at 2nd stop, 40 % of passengers carried by bus from 1st stop alight at second stop. Similarly, when bus arrives at 3rd stop, 80 % of passengers carried by bus from 2nd stop alight at 3rd stop. When buses arrive at last stop (stop 4), all
passengers inside bus will alight at stop 4. Then, buses again pick passenger from stop 4 and run toward stop 1 similar way as they come from stop 1 to 4. I have tried a code but it is not working. Could you please, help me ?
function averageWait = simulateBus(sTime,maxPassengers,nTrials)
% SIMULATEBUS  determine average waiting times for passengers at bus stop
% sTime: operation time of simulation (seconds)
% maxPassengers: max. no. of passengers to arrive at stop
% nTrials: no. of simulation runs to perform
nSeats = 60;
par = 5/60; % arrival rate of passengers at stop 1(passenger/second)
par2=3/60;%% arrival rate of passengers at stop 2(passenger/second
par3=2/60;%passenger arrival time at stop 3
par4=0/60;%passenger arrival time at stop 4(no passenger arrive at stop 4)
ap1=0;% alighting proportion of passenger 1st stop is zero (no passenger alight at first stop)
ap2=0.4;%alighting proportion of passenger at stop2 arriving from previous stop (stop 1)
ap3=0.8;%alighting proportion of passenger at stop3 arriving from previous stop (stop 2)
ap4=1.0;%alighting proportion of passenger at stop4 arriving from previous stop (stop 3).All passenger will alight
t=300; %bus travel time from one stop to next stop(second)
b=3;%boarding time for each passenger(second)
nstop=4;
maxPassengers=30;
nTrials=3;
sTime=7200;%(sec)
bf = 1/600; % 10 min headway
sqc = 1; % square of coefficient of variation of bus headway
TotalWait = zeros(1,maxPassengers);
WaitCount = zeros(1,maxPassengers);
% nap2=pl1*ap2;% number alighting passenger at stop2
% nSeats2=(nSeatspl1+nap2);% number of space avaiable in bus when it arrives at stop2
% nSeats2=round(c2);
for nT = 1:nTrials
pat = getPassengerTimes(sTime,maxPassengers,par);
bat = getBusTimes(sTime,bf,sqc);
%waiting time calculation
pwt = nan(size(pat)); % passenger waiting time
for nBus = 1:length(bat)
pcanboard = find(pat <= bat(nBus) & isnan(pwt));
pcanboard (nSeats+1:end) = [];
pl1(nBus)=length(pcanboard);%pl1 = passenger load inside a bus leaving stop 1
bdt1(nBus)=bat(nBus)+length(pcanboard)*b; % bdt1 =bus departure time from stop 1
bat2(nBus)=bdt1(nBus)+t ;% bat2 =bus arrival time time at stop 2 from stop 1
pwt(pcanboard) = bat(nBus)  pat(pcanboard);
end
pwt(isnan(pwt)) = sTime;
% Accumulate waiting times
TotalWait(1:length(pwt)) = TotalWait(1:length(pwt)) + pwt;
WaitCount(1:length(pwt)) = WaitCount(1:length(pwt)) + 1;
end
iWait = WaitCount>0;
% Find average wait time per passenger
averageWait = TotalWait(iWait)./WaitCount(iWait);
pl1
bdt1
bat2
hist(averageWait)
function pat = getPassengerTimes(sTime,maxPassengers,par,nstop)
% Returns an array of passenger arrival times (seconds)
% sTime: operation time of simulation
% maxPassengers: max. no. of passengers to arrive at stop
% par: arrival rate of passengers (passenger/second)
ap = rand(1,maxPassengers); % arrival probability of passengers
pint = expinv(ap,1/par);% passenger inter arrival time
pat = cumsum(pint);
pat(pat>sTime) = []; % actual passenger arrival time
function bat = getBusTimes(sTime,bf,sqc)
% Returns an array of bus arrival times (seconds)
% sTime: operation time of simulation
% bf: bus headway
% sqc: square of coeffient of variation of bus headway
bp = rand(1,3); % bus arrival probability
bint = gaminv(bp,1/sqc,sqc/bf);% bus inter arrival time at stop
bat = cumsum(bint);
bat(bat>sTime) = []; % actual bus arrival time
