finding probability in Matlab

3 ビュー (過去 30 日間)
SSBGH
SSBGH 2022 年 10 月 16 日
回答済み: Torsten 2022 年 10 月 16 日
i have a question which it is the following:
Create a MATLAB function that approximates the probability in the following experiment with simulations!Two fair dice are thrown. Find the probability that the sum of the numbers obtained is 8.
The number of simulation should be N = 10^3. The variable p should store the approximation, i.e. the relative frequency.
i wrote the following code but it is not working any body got an idea how to solve this question?
function p = sim()
N = 10^3;
s=0;
k=0;
for i=1:1000000
p=randi(6,1,2);
if sum(p)==8
k=k+1;
end
end
end

回答 (1 件)

Torsten
Torsten 2022 年 10 月 16 日
You didn't define p (which should be k/1000000).
But instead of 1000000, you are told to use N = 10^3 , aren't you ?
And the variable s is superfluous - so you should delete it.

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by