% campionamento di una esponenzionale di parametro 2

n=10000; lambda=1;
Y=-log(rand(1,n))/lambda;

dx=0.1; 
x=dx/2:dx:10-dx/2;
figure; 
N=hist(Y,x); 
bar(x,N/(n*dx),1); 
hold on;
x=[0:0.01:10]; plot(x,lambda*exp(-lambda*x),'r'); 

figure; 
cdfplot(Y); hold on;
plot(x,1-exp(-lambda*x),'r--'); 

V = exprnd(1,1,10000);

figure; 
N1=hist(V,x); 
bar(x,N1/(n*dx),1); 
hold on;