Simulating the Central Limit Theorem with Non Uniform Distribution in MATLAB

28 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));

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
iteration = iteration + 1;

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by