0
votes

The function called DicePlot simulates rolling 10 dice 5000 times.

The function 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 where each bin in the histogram represents a possible value of for the sum of the dice.

The mean and standard deviation of the 1 ⇥ 5000 sums of dice values will be computed, and the probability density function of normal distribution (with the mean and standard deviation computed) on top of the relative frequency histogram will be plotted.

Below is my code so far - What am I doing wrong? The graph shows up but not the extra red line on top? I looked at answers like this, and I don't think I'll be plotting anything like the Gaussian function.

% 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)
Y = normpdf(X)
xlabel('sum of dice values')
ylabel('relative frequency')
title(['NumDice = ',num2str(NumDice),' , NumRolls = ',num2str(NumRolls)]); 
  end

It is supposed to look like enter image description here

But it looks like

enter image description here

2
Can you please clarify your problem? - Rasman
Yeah, what's your issue, the code seems to run fine for me other than your variable NumDice used on the last line is never defined. - jerad
sorry i don't understand what "they run more stimulations" mean. can you clarify? i'm following the problem exactly i just don't know what i'm doing wrong :( - Priya Sharma
The problem is that you aren't telling us why you are unhappy with your plot. The only real difference I see between the two plots you linked is that your plot is missing this overplotted red line. But I have no idea why you would expect that line to be there, since you don't have any code to plot such a line. Why are people upvoting this question? - Dan Becker

2 Answers

2
votes

The red line is not there because you aren't plotting it. Look at the documentation for normpdf. It computes the pdf, it doesn't plot it. So you problem is how do you add this line to the plot. The answer to that problem is to google "matlab hold on".

2
votes

Here's some code to get you going in the right direction:

% Normalize your distribution 
normalizedDist = distr/sum(distr);
bar(normalizedDist ,1); 
hold on

% Setup your density function using the mean and std of your sample data
mu   = mean(SumDice);
stdv = std(SumDice);
yy   = normpdf(xx,mu,stdv);
xx   = linspace(0,60);

% Plot pdf
h = plot(xx,yy,'r'); set(h,'linewidth',1.5);