我的函数名为DicePlot,模拟掷10个骰子5000次。在该函数中,它计算每卷的10个骰子的值的总和,这将是一个1×5000矢量,并绘制相对频率直方图,其中以相同的方式选择柱状图中的每个柱状图中的骰子和的可能值。
计算1×5000个骰子值之和的平均值和标准差,并在相对频率直方图的顶部绘制正态分布的概率密度函数(计算的平均值和标准差)。
我已经完成了所有的工作,但我对如何绘制概率密度函数感到困惑。任何帮助都是非常感谢的。谢谢!
作为参考,图表应该是这样的!

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发布于 2012-11-21 14:53:31
我在我对your previous question的回答中添加了代码,以便在直方图的顶部绘制一个缩放的高斯pdf。添加的两个关键功能如下: 1)使用hold on和hold off在同一张图上获得直方图和曲线图。2)将normpdf的输出缩放到适当的大小,使其与直方图的比例相同。
还有一件事,我不禁注意到你还没有把我之前答案中的建议融入到你的函数中。有什么特别的原因吗?我当然不会加1你的问题,除非我能看到证据表明你已经将你过去的建议融入到你的工作中!现在你却让我听起来像我的高中老师!:-)
%#Define the parameters
NumDice = 2;
NumFace = 6;
NumRoll = 500;
%#Generate the rolls and obtain the sum of the rolls
AllRoll = randi(NumFace, NumRoll, NumDice);
SumRoll = sum(AllRoll, 2);
%#Determine the bins for the histogram
Bins = (NumDice:NumFace * NumDice)';
%#Build the histogram
hist(SumRoll, Bins);
title(sprintf('Histogram generated from %d rolls of %d %d-sided dice', NumRoll, NumDice, NumFace));
xlabel(sprintf('Sum of %d dice', NumDice));
ylabel('Count');
hold on
%#Obtain the mean and standard deviation of the data
Mu = mean(SumRoll);
Sigma = sqrt(var(SumRoll));
%#Obtain the Gaussian function using 4 standard deviations on either side of Mu
LB = Mu - 4 * Sigma; UB = Mu + 4 * Sigma;
Partition = (LB:(UB - LB) / 100:UB)';
GaussianData = normpdf(Partition, Mu, Sigma);
%#Scale the Gaussian data so the size matches that of the histogram
GaussianData = NumRoll * GaussianData;
%Plot the Gaussian data
plot(Partition, GaussianData, '-r');
hold offps,如果你不知道直方图应该是高斯的(因为中心极限定理),那么你也可以使用统计工具箱中的ksdensity来使用核函数来获得经验密度。
https://stackoverflow.com/questions/13487403
复制相似问题