%
%  macm 202 -- 02 mar 03 -- djm
%
%  w8pdf.m:  simulate exponential transition process
%
%     - use state diagram of figure 6.1
%     - same as w5exp.m

clear
mu = 1.0;

%  bins for histogram & pseudorandom seed

dt = 1/8;  mudt = mu*dt;
bins = (0:dt/mu:5/mu);
rand('state',7654321);

%  simulation of Nruns at dt

Nruns = 500;
simu_wait = zeros(Nruns,1);

%  simulation loop of figure 6.1

for k=1:Nruns;
    wait = 0;
    while (rand(1,1)>mudt)
        wait = wait + dt;
    end
    simu_wait(k,1) = wait;
end    

%  plot histogram of transition probabilities

figure(1); clf
count = hist(simu_wait,bins);
bar(bins,count/Nruns,'hist','w')
hold on
plot(bins,exp(-mu*bins)*dt,'r')
%plot(bins,(1-mudt).^(bins/dt)*dt,'g')
title('\bf simulation of exponential transition probability')
xlabel('\bf time, t')
ylabel('\bf p(transition during t->t+\Delta t)')
legend('\bf exponential limit')
axis([0 5 0 1.1*dt])

%  direct generation of Nruns exponential transition times!

rand_wait = -log(rand(Nruns,1))/mu;

figure(2); clf
count = histc(rand_wait,bins);
bar(bins,count/Nruns,'histc','w')
hold on
plot(bins,exp(-mu*bins)*dt,'r')
title('\bf direct generation of exponential transition probability')
xlabel('\bf time, t')
ylabel('\bf p(transition during t->t+\Delta t)')
legend('\bf exponential limit')
axis([0 5 0 1.1*dt])
