Relative Frequency Histograms and Probability Density Functions

43 views (last 30 days)
Hi, I'm writing a function which simulates rolling 10 dice 5000 times. In the
function,I find the sum of values of the 10 dice of each roll, which will be a
1X5000 vector, and plot relative frequency histogram. Also I have to compute mean
and standard deviation of the 1x 5000 sums of dice values, and plot the
probability density function of normal distribution on top of the relative
frequency histogram.
I've got most of the code down, such as finding the sums, and my graph is
correct in shape, but it's not a relative frequency histogram but instead just
counts the number of occurrences of a value on the y-axis. I'm not sure how to
fix this. I think I'll be able to find the mean and standard deviation to make
the prob density function but help is always appreciated.
% function[]= DicePlot()
for roll=1:5000
diceValues = randi(6,[1, 10]);
SumDice(roll) = sum(diceValues);
end
distr=zeros(1,6*10);
for i = 10:60
distr(i)=histc(SumDice,i);
end
bar(distr,1)
xlabel('sum of dice values')
ylabel('relative frequency')
title(['NumDice = ',num2str(NumDice),' , NumRolls = ',num2str(NumRolls)]);
end

Accepted Answer

Image Analyst
Image Analyst on 21 Nov 2012
Is this homework, because your classmate just posted the same thing here: http://www.mathworks.com/matlabcentral/answers/54408-problems-with-graphing-my-histogram?
Just do
distr= hist(SumDice, numberOfBins); % No loop over i needed.
distr = distr / sum(distr);
to normalize by the number of counts in the distribution.
  2 Comments
SB
SB on 21 Nov 2012
Yes it is, and so numberOfBins is 50? The graph appears shifted all the way to the left (values are now showing up for sum as less than 10 somehow), so that doesn't seem to work. Also, for the mean, would it just be mean(SumDice(roll))? How would stdev work in this case?
Image Analyst
Image Analyst on 21 Nov 2012
You have to figure out how many bins there are in advance. Like this:
numberOfRolls = 1e7;
numberOfPairsOfDice = 5;
randomIntegers = randi(6, [numberOfRolls, numberOfPairsOfDice * 2]);
SumOf5PairsOfDice = sum(randomIntegers, 2); % Sum across columns
minSum = min(SumOf5PairsOfDice)
maxSum = max(SumOf5PairsOfDice)
numberOfBins = maxSum - minSum + 1
[counts binCenters] = hist(SumOf5PairsOfDice, numberOfBins); % No loop over i needed.
counts = counts / sum(counts);
bar(binCenters, counts, 'BarWidth', 1);
grid on;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
Yes, the mean would be that and the stddev would be std(SumOf5PairsOfDice) - basically just like the mean.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!