MATLAB: Relative Frequency Histograms and Probability Density Functions

graphprobability density functionrelative frequency

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

Best Answer

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.