% File name: uniform_dist.m
% Generate random numbers having the Poisson
% distribution.
% Two plots are produced. One on left is the
% plot of numbers that result.
% One on right is a histogram.
n=1000; %number of samples produced
x=rand(1,n); %generate n uniformly distributed
% sample
mu = 5; % value for mu
% Calculate Poison CDF
exp_mu = exp(-mu);
CDF(1) = exp_mu;
for
k = 2:50p(k) = mu^k*exp_mu/factorial(k);
CDF(k) = p(k)+CDF(k-1);
end
% Generate Poisson-distributed random numbers
% using the inversion method.
for
i = 1:ntemp=find(CDF >= x(i));
if size(temp,2) == 0y(i) = 0;
elsey(i) = temp(1)-1;
end
end
subplot(1,2,1)
% Plot the values of all the numbers generated
plot(y,'k');%plot the resulting data
box off
axis square
xlabel('Sample index')
ylabel('Random number value')
% Plot a histogram of the data to view the pdf
%x = 0:0.1:1;
subplot(1,2,2)
hist(y)
box off
axis square
xlabel('Bins')
ylabel('Bin count')
Go back to book Homepage | Go back to book's matlab Homepage | Go back to book's Chapter 1 Homepage |
Copyright © 2001 Northstar Digital Design, Inc.