Problems with graphing my histogram
3 views (last 30 days)
Show older comments
endMy function called DicePlot, simulates rolling 10 dice 5000 times. In the function, it calculates the sum of values of the 10 dice of each roll, which will be a 1 × 5000 vector, and plot relative frequency histogram with edges of bins being selected in the same manner where each bin in the histogram should represent a possible value of for the sum of the dice.
The mean and standard deviation are computed of the 1 × 5000 sums of dice values and the probability density function of normal distribution (with the mean and standard deviation that is computed) on top of the relative frequency histogram is plotted.
I have everything done, but my graph looks way different from the picture.
This is how the graph is supposed to look like: http://i1308.photobucket.com/albums/s617/pingchu93/YEAHBUDDY_zps6690a18a.jpg
This is my graph: http://i1308.photobucket.com/albums/s617/pingchu93/ScreenShot2012-11-21at92701AM_zpse8470573.jpg
What am I doing wrong? I feel like I took a overly complicated route of doing this problem, is there an easier way to go about it?
function DicePlot
%#Define the parameters
NumDice = 2;
NumFace = 6;
NumRoll = 500;
%#Generate the rolls and obtain the sum of the rolls
AllRoll = randi(NumFace, NumRoll, NumDice);
SumRoll = sum(AllRoll, 2);
%#Determine the bins for the histogram
Bins = (NumDice:NumFace * NumDice)';
%#Build the histogram
hist(SumRoll, Bins);
title(sprintf('Histogram generated from %d rolls of %d %d-sided dice', NumRoll, NumDice, NumFace));
xlabel(sprintf('Sum of %d dice', NumDice));
ylabel('Count');
hold on
%#Obtain the mean and standard deviation of the data
Mu = mean(SumRoll);
Sigma = sqrt(var(SumRoll));
%#Obtain the Gaussian function using 4 standard deviations on either side of Mu
LB = Mu - 4 * Sigma; UB = Mu + 4 * Sigma;
Partition = (LB:(UB - LB) / 100:UB)';
GaussianData = normpdf(Partition, Mu, Sigma);
%#Scale the Gaussian data so the size matches that of the histogram
GaussianData = NumRoll * GaussianData;
%Plot the Gaussian data
plot(Partition, GaussianData, '-r');
hold off
0 Comments
Answers (1)
Image Analyst
on 21 Nov 2012
You need to do this:
plot(Partition, GaussianData, 'ro-');
to get the markers on the red line. Also, they rolled more than one pair since their sum's mean is around 35 while yours is around 7. I suspect they rolled 5 pairs at a time. Secondly, they divided by the number of counts in the histogram to get the bar height normalized like a PDF would have it.
0 Comments
See Also
Categories
Find more on Histograms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!