0

名为 DicePlot 的函数模拟掷 10 个骰子 5000 次。

该函数计算每个掷骰的 10 个骰子的值的总和,这将是一个 1 ⇥ 5000 向量,并绘制相对频率直方图,其中选择了 bin 的边缘,其中直方图中的每个 bin 代表总和的可能值的骰子。

将计算 1 ⇥ 5000 骰子值之和的平均值和标准偏差,并在相对频率直方图之上绘制正态分布的概率密度函数(计算平均值和标准偏差)。

到目前为止,以下是我的代码-我做错了什么?图表显示了,但顶部没有额外的红线?我看了这样的答案,我认为我不会绘制像高斯函数这样的东西。

% 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

它应该看起来像 在此处输入图像描述

但它看起来像

在此处输入图像描述

4

2 回答 2

2

这里有一些代码可以让你朝着正确的方向前进:

% 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);
于 2012-11-21T19:39:43.570 回答
2

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".

于 2012-11-21T19:36:01.960 回答