Simulating the Central Limit Theorem with Non Uniform Distribution in MATLAB

17 views (last 30 days)
How can I simulate the Central Limit Theorem with a Non-Uniform Distribution function in MATLAB, including the "rand" function?

Answers (2)

Alfonso Nieto-Castanon
Alfonso Nieto-Castanon on 2 Aug 2015
Edited: Alfonso Nieto-Castanon on 2 Aug 2015
x = mean(rand(100,1000));
hist(x);

Image Analyst
Image Analyst on 2 Aug 2015
Edited: Image Analyst on 2 Aug 2015
rand() generates numbers from a uniform distribution function, non "Non Uniform", though of course any given array generated from rand() won't have all the same values -- so not sure what you mean there.
Anyway, here's some code:
% Startup code.
clc; % Clear command window.
clearvars; % Get rid of variables from prior run of this m-file.
workspace; % Make sure the workspace panel with all the variables is showing.
close all; % Close all imtool figures.
format long g;
format compact;
fontSize = 20;
% Define initial data
% m = ones(1, 10) % Pick uniform, or random data to start with.
m = rand(1, 10)
iteration = 0;
again = 1;
while again == 1 && iteration < 12
% Plot it:
plot(m, 'b.-', 'LineWidth', 2, 'MarkerSize', 30);
grid on;
caption = sprintf('This was after %d convolutions.', iteration);
title(caption, 'FontSize', fontSize);
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Do the convolution for this iteration:
m = conv(m,m, 'full');
% Normalize it so we don't overflow.
m = m / max(m);
% See if user wants to do another iteration.
promptMessage = sprintf('This was after %d convolutions.\nDo you want to do another iteration,\nor Cancel to abort processing?', iteration);
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
again = false; % Bail out
break;
end
iteration = iteration + 1;
end
  3 Comments
Image Analyst
Image Analyst on 7 Mar 2020
I would not call those "corrections". You're doing a different scenario. From Wikipedia Central Limit Theorem: "In probability theory, the central limit theorem (CLT) establishes that, in some situations, when independent random variables are added, their properly normalized sum tends toward a normal distribution (informally a "bell curve") even if the original variables themselves are not normally distributed."
Note that your code does not use random numbers at all. It does not even call rand() at all like the original poster asked you: "How can I simulate the Central Limit Theorem with a Non-Uniform Distribution function in MATLAB, including the "rand" function?"

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!