In: Advanced Math
Matlab Code
Illustrate the fact that the average of a large number of
independent random variables will approximate a Gaussian by the
following:
a) to generate n random integers from a uniform distribution U(xl ,
xu).
b) to choose xl and xu randomly, in the range −100 ≤ xl < xu ≤
100 and the number of samples n randomly in the range 0 < n ≤
1000.
c) Generate and plot a histogram of the accumulation of 10⁴ points
sampled as just described.
d) Calculate the mean and standard deviation of your histogram, and
plot it.
e) Repeat the above for 10⁵ and 10⁶ points. Discuss your
results.
MATLAB Code:
close all
clear
clc
%% N = 1e4 number of samples
fprintf('For N = 10000
samples\n------------------------------------\n')
N_1 = 1e4; % Number of points
samples_1 = [];
while true
xl = randi([-100, 100], 1); % Random number between -100 and
100
xu = randi([-100, 100], 1);
n = randi(1000); % Random number between 1 and 1000
sorted_limits = sort([xl, xu]);
xl = sorted_limits(1); xu = sorted_limits(2);
new_samples = randi([xl, xu], 1, n);
new_length_of_samples = length(samples_1) +
length(new_samples);
if new_length_of_samples > N_1
samples_1 = [samples_1, new_samples(1:length(new_samples) -
(new_length_of_samples - N_1))];
break
else
samples_1 = [samples_1, new_samples];
end
end
M = mean(samples_1); % Mean
SD = std(samples_1); % Standard deviation
fprintf('Mean = %.8f, Standard Deviation = %.8f\n', M, SD)
figure, hist(samples_1, 1000)
title('Histogram (N = 10000 samples)')
%% N = 1e5 number of samples
fprintf('\nFor N = 100000
samples\n------------------------------------\n')
N_2 = 1e5; % Number of points
samples_2 = [];
while true
xl = randi([-100, 100], 1); % Random number between -100 and
100
xu = randi([-100, 100], 1);
n = randi([0, 1000]); % Random number between 0 and 1000
sorted_limits = sort([xl, xu]);
xl = sorted_limits(1); xu = sorted_limits(2);
new_samples = randi([xl, xu], 1, n);
new_length_of_samples = length(samples_2) +
length(new_samples);
if new_length_of_samples > N_2
samples_2 = [samples_2, new_samples(1:length(new_samples) -
(new_length_of_samples - N_2))];
break
else
samples_2 = [samples_2, new_samples];
end
end
M = mean(samples_2); % Mean
SD = std(samples_2); % Standard deviation
fprintf('Mean = %.8f, Standard Deviation = %.8f\n', M, SD)
figure, hist(samples_2, 10000)
title('Histogram (N = 100000 samples)')
%% N = 1e6 number of samples
fprintf('\nFor N = 1000000
samples\n------------------------------------\n')
N_3 = 1e6; % Number of points
samples_3 = [];
while true
xl = randi([-100, 100], 1); % Random number between -100 and
100
xu = randi([-100, 100], 1);
n = randi([0, 1000]); % Random number between 0 and 1000
sorted_limits = sort([xl, xu]);
xl = sorted_limits(1); xu = sorted_limits(2);
new_samples = randi([xl, xu], 1, n);
new_length_of_samples = length(samples_3) +
length(new_samples);
if new_length_of_samples > N_3
samples_3 = [samples_3, new_samples(1:length(new_samples) -
(new_length_of_samples - N_3))];
break
else
samples_3 = [samples_3, new_samples];
end
end
M = mean(samples_3); % Mean
SD = std(samples_3); % Standard deviation
fprintf('Mean = %.8f, Standard Deviation = %.8f\n', M, SD)
figure, hist(samples_3, 100000)
title('Histogram (N = 1000000 samples)')
Output:
For N = 10000 samples
------------------------------------
Mean = -0.98010000, Standard Deviation = 41.28366188
For N = 100000 samples
------------------------------------
Mean = 3.13892000, Standard Deviation = 48.93190457
For N = 1000000 samples
------------------------------------
Mean = -0.01954300, Standard Deviation = 47.89988022
Histograms:
From the last histogram, it can be observed that as we keep on increasing the number of points, the distribution does approach Gaussian.