0

DicePlotと呼ばれる関数は、10個のサイコロを5000回振るのをシミュレートします。

この関数は、各ロールの10個のサイコロの値の合計を計算します。これは1⇥5000のベクトルになり、ビンのエッジが選択された相対度数ヒストグラムをプロットします。ヒストグラムの各ビンは、合計の可能な値を表します。サイコロの。

ダイス値の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

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 に答える
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 に答える