% Exercise 1.24: MATLAB has a built-in Gaussian random number generator % randn with mean mu = 0 and standard deviation sigma = 1. Test this % random variable by constructing an incidence histogram; i.e., divide % [-4,4] into say 80 subintervals (bins), call randn 1000 times, and % count the number of times the variable falls into each bin. Normalize % by the number of calls and graph against (1.15). % % Answer (developed for MATLAB 6.1) : % N = 1000; % number of trials a = -4; % left-hand side of the interval b = 4; % right-hand side of the interval nb = 80; % number of bins d = (b-a)/nb; % size of one bin bins = zeros(1,nb); % initialize the bins for i=1:N % perform N trials r = randn; % generate random number if r >= a & r <= b % only treat number in [a,b] k = floor((r-a)/d)+1; % index of the bin bins(k) = bins(k) + 1; % update the bins end; end; plot(bins*(b-a)/N); % normalized plot hold on % we make graph of (1.15) s = a:0.1:b; % range for sampling f = exp(-s.^2/2)/sqrt(2*pi); % sample the function plot(f) % graphs normal distribution