% This script illustrates the central limit theorem % It adds n (for n=1to20) identically uniformly distributed random variables % and finds and plots the probability density function from the histogram. % The sum sequence in each case is normalized to have unit standard % deviation. % This m-file was written at the University of Wyoming in the Electrical % and Computer Engineering Department and is to be distributed without % cost. clc disp('This script displays the probability density function') disp('associated with the sum of n zero mean uniform random') disp('variables that have been appropriately normalized such that') disp('the sum has unity variance. It is easy to see how the density') disp('of the sum quickly approaches zero mean normal distribution with') disp('zero mean and unity variance as n increases.') disp(' ') disp(' Press Enter to Continue') pause clear all set(0,'DefaultAxesFontSize',12); set(0,'DefaultTextFontSize',12); N1=500000;%Length of Uniform Random Sequences rand('state',sum(100*clock)); % reset uniform PRNG sum1=zeros(1,N1); X=zeros(20,N1); factor=sqrt(12); bincenter=zeros(20,41); x1=linspace(-5,5,301); density=(1/(sqrt(2*pi)))*exp(-(x1.*x1)/2); bincenter=linspace(-5+(10/162),5-(10/162),81); deltax=10/81; for i=1:20; X(i,:)=(rand(1,N1)-0.5*ones(1,N1))*factor; end h=zeros(20,81); figure(1):clf; axis([-5 5 0 .6]); hold on box on plot(x1,density,'r'); hold on P=plot(bincenter,zeros(1,81)); hold on i=0; T=text(1,.5,['Terms in sum = ' num2str(i)]); i1=[1:1:20]; xlabel('State Variable, x'); ylabel('Probability Density, p(x)'); for i=1:20 sum1=sum1+X(i,:); RV=sum1/sqrt(i);%RV is a zero mean unit variavce vector h(i,:)=hist(RV, bincenter)/(N1*deltax); axis([-5,5 0 .6]); hold on set(P,'Xdata',bincenter,'Ydata',h(i,:)); hold on set(T,'String',['Terms in sum = ' num2str(i)]); pause(2) end