My 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 i’m confused on how to plot the probability density function. any help is appreciated. thanks!
for reference the graph is supposed to look like!
function DicePlot ( throw_num, die_num )
throw_num=5000
die_num= 10
throws = rand ( throw_num, die_num );
throws = ceil ( 6 * throws );
for i = die_num : die_num*6
j = find ( score == i );
y(i-die_num+1) = length ( j ) / throw_num;
end
bar ( x, y )
xlabel ( 'Score' )
ylabel ( 'Estimated Probability' )
score_ave = sum ( score(1:throw_num) ) / throw_num;
score_var = var ( score );
return
end
I’ve added to the code from my answer to your previous question to plot a scaled Gaussian pdf over the top of your histogram. The two key additions are as follows: 1) Use
hold onandhold offto get the histogram and plot on the same figure. 2) Scale the output ofnormpdfto the appropriate size so it is on the same scale as the histogram.One other thing, I can’t help but notice you haven’t incorporated the suggestions from my previous answer into your function yet. Any particular reason for this? I certainly will not +1 your question unless I can see evidence that you’ve incorporated the suggestions you’ve had in the past into your work! And now you’ve gone and made me sound like one of my high-school teachers! 🙂
ps, if you didn’t know a priori that the histogram should be Gaussian (because of a central limit theorem), then you could also use
ksdensityfrom the statistics toolbox to get the empirical density using a kernel function.