% crv.m - plot various continuous random variable pdfs % (C) copyright 2001 by Yu Hen Hu % Created: 2/17/2001 clear all, % Uniform distribution a = 0; b = 2; dx=0.01; x=[a-0.5:dx:b+0.5]; f=[a <= x & x <= b]/(b-a); figure(1),subplot(231), plot(x,f), title(['uniform[' num2str(a) ',' num2str(b) ']']); axis([min(x) max(x) 0 max(f)*1.1]); cdf=f*triu(ones(length(x),length(x)))*dx; figure(2),subplot(231), plot(x,cdf), title(['CDF: uniform[' num2str(a) ',' num2str(b) ']']); axis([min(x) max(x) 0 max(cdf)*1.1]); % exponential distribution lambda=0.2; dx=0.05; x=[-1:dx:5/lambda]; f=[x>=0].*exp(-lambda*x)*lambda; figure(1),subplot(232), plot(x,f), title(['exp(' num2str(lambda) ')']); axis([min(x) max(x) 0 max(f)*1.1]); cdf=f*triu(ones(length(x),length(x)))*dx; figure(2),subplot(232), plot(x,cdf), title(['CDF: exp(' num2str(lambda) ')']); axis([min(x) max(x) 0 max(cdf)*1.1]); % Laplace pdf lambda=1; dx=0.05; x=[-5/lambda:dx:5/lambda]; f=0.5*lambda*exp(-lambda*abs(x)); figure(1),subplot(233), plot(x,f), title(['Laplace(' num2str(lambda) ')']); axis([min(x) max(x) 0 max(f)*1.1]); cdf=f*triu(ones(length(x),length(x)))*dx; figure(2),subplot(233), plot(x,cdf), title(['CDF: Laplace(' num2str(lambda) ')']); axis([min(x) max(x) 0 max(cdf)*1.1]); % Cauchy pdf lambda=1; dx=0.05; x=[-5/lambda:dx:5/lambda]; f=(lambda/pi)*ones(1,length(x))./(lambda^2+x.*x); figure(1),subplot(234), plot(x,f), title(['Cauchy(' num2str(lambda) ')']); axis([min(x) max(x) 0 max(f)*1.1]); cdf=f*triu(ones(length(x),length(x)))*dx; figure(2),subplot(234), plot(x,cdf), title(['CDF: Cauchy(' num2str(lambda) ')']); axis([min(x) max(x) 0 max(cdf)*1.1]); % Gaussian pdf m = 1; sigma=.4; dx=0.05; x=[-5*sigma:dx:5*sigma]+m; f=(1/(sqrt(2*pi)*sigma))*exp(-(0.5/sigma^2)*(x-m).*(x-m)); figure(1),subplot(235), plot(x,f), title(['Gaussian(' num2str(m) ',' num2str(sigma^2) ')']); axis([min(x) max(x) 0 max(f)*1.1]); cdf=f*triu(ones(length(x),length(x)))*dx; figure(2),subplot(235), plot(x,cdf), title(['CDF: Gaussian(' num2str(m) ',' num2str(sigma^2) ')']); axis([min(x) max(x) 0 max(cdf)*1.1]);