# Simulating Poisson process in Matlab

Below is a simple Matlab code to simulate a Poisson process. The interarrival times were computed and recorded in int_times. The times are then grouped into bins of 10 seconds in width and the counts are stored in count.

lambda=1/60; % arrival rate per second (1 minute per packet)
T=10*3600; % simulation time in second (10 hours)
delta=0.1; % simulation step size in second
N=T/delta; % number of simulation steps
event=zeros(N,1); % array recording at each step if a "packet" arrived.
% initialize it to zeros
R=rand(size(event)); % generate a random array (with elements in [0,1]) of the same size as "event"
event(R<lambda*delta)=1; % set each element of event to 1 with probability lambda*delta
inds=find(event==1); % getting indices of arrivial
int_times=diff(inds)*delta; % interarrival times in seconds
edges=0:10:400; % define histogram bin
count=histc(int_times,edges);

The histogram of the absolute counts of the interarrivial is then plotted with the commands below.

figure; bar(edges,count,'histc'); % draw histogram of absolute count



Finally, the counts are normalized and compared with the theoretical result ($\lambda \exp(-\lambda T)$).

figure; bar(edges,count/sum(count)/(edges(2)-edges(1)),'histc'); % draw histogram of normalized counts
hold;plot(edges,lambda*exp(-lambda*edges),'r'); % plot theoretical result
legend('simulation','theoretical');