Making relative histogram for data, and a convergence diagram to see if enough trials have been run for monte carlo simulation.
2 ビュー (過去 30 日間)
古いコメントを表示
I have written a monte carlo code for a double dice throw that calculates the probability of the throw sum being 8 or over 8. I have to make a relative frequency histogram for the sum of the dice with 11 bins. And show a convergence diagram to show that enough trials have been done. I do not know which part of my code to plot into the histogram to make a relative frequency histogram. I also do not know how to make a convergence diagram to show enough trials were done.
count = 0;
for i=1:1000;
throwD1 = randi(6, 1);
throwD2 = randi(6, 1);
Throwsum = throwD1 + throwD2;
if Throwsum >=8
fprintf('Sum>=8');
count = count + 1;
else
fprintf('Sum<8');
end
end
fprintf('Number of throws with sum=7: %d\n', count);
nAllruns = 0;
n8 = 0;
for throwD1 = 1:6
for throwD2 = 1:6
nAllruns = nAllruns + 1;
if throwD1 + throwD2 >= 8
n8 = n8 + 1;
end
end
end
prob=n8 / nAllruns
0 件のコメント
採用された回答
Alan Stevens
2021 年 7 月 3 日
Something like the following?
N = 1000;
Throwsum = zeros(N,1);
Relativefreq = zeros(N,1);
count = 0;
for i=1:1000
throwD1 = randi(6, 1);
throwD2 = randi(6, 1);
Throwsum(i) = throwD1 + throwD2;
if Throwsum(i) >=8
count = count + 1;
end
Relativefreq(i) = count/i;
end
figure
histogram(Throwsum,11);
xlabel('Sum of two dice'), ylabel('Frequency')
figure
plot(1:N,Relativefreq),grid
xlabel('trials'),ylabel('Prob sum >= 8')
fprintf('Relative number of throws with sum >= 8: %d\n', count/N);
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Data Distribution Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!