% ARRIVAL file arrival.m Arrival time to a set of states % Version of 1/2/96 Revised 7/31/97 for version 4.2 and 5.1 % Calculates repeatedly the arrival % time to a prescribed set of states. % Assumes the procedure chainset has been run. r = input('Enter the number of repetitions '); disp('The target state set is:') disp(e) st = input('Enter the initial state '); if ~isempty(st) s1 = MS(st==states); % Initial state number else s1 = 1; end clear T % Trajectory in state numbers (reset) S = zeros(1,r); % Arrival time for each rep (reset) TS = zeros(1,r); % Terminal state number for each rep (reset) for k = 1:r R = zeros(1,ms); % Indicator for target state numbers R(E) = ones(1,ne); % reset for target state numbers s = s1; T(1) = s; i = 1; while R(s) ~= 1 % While s is not a target state number u = rand(1,1); s = ((A(s,:) < u)&(u <= B(s,:)))*MS'; i = i+1; T(i) = s; end S(k) = i-1; % i is the number of stages; i-1 is time TS(k) = T(i); end [ts,ft] = csort(TS,ones(1,r)); % ts = terminal state numbers ft = frequencies fts = ft/r; % Relative frequency of each ts [a,at] = csort(TS,S); % at = arrival time for each ts w = at./ft; % Average arrival time for each ts RES = [states(ts); fts; w]'; disp(' ') if r == 1 disp(['The arrival time is ',int2str(i-1),]) disp(['The state reached is ',num2str(states(ts)),]) N = 0:i-1; TR = [N;states(T)]'; disp('To view the trajectory of states, call for TR') else disp(['The result of ',int2str(r),' repetitions is:']) disp('Term state Rel Freq Av time') disp(RES) disp(' ') [t,f] = csort(S,ones(1,r)); % t = arrival times f = frequencies p = f/r; % Relative frequency of each t dbn = [t; p]'; AV = dot(t,p); SD = sqrt(dot(t.^2,p) - AV^2); MN = min(t); MX = max(t); disp(['The average arrival time is ',num2str(AV),]) disp(['The standard deviation is ',num2str(SD),]) disp(['The minimum arrival time is ',int2str(MN),]) disp(['The maximum arrival time is ',int2str(MX),]) disp('To view the distribution of arrival times, call for dbn') disp('To plot the arrival time distribution, call for plotdbn') end