Probability function of people at a party.

1 回表示 (過去 30 日間)
David
David 2013 年 10 月 2 日
コメント済み: David 2013 年 10 月 2 日
So, you're asked to write a function that computes the probability of two people at a party having the same birthday. The probability function itself is
P(n) = 0, n = 1
P(n) = 1 - (capital pi from k = 1 to k = n - 1)(1 - k/365), 2 <=n <= 365
P(n) = 1, n >= 366
The function is used to calculate the minimum value of m (your chosen n)n for which P(m) >= q. Where q (0, 1], i.e. an arbitrary probability. The function should accept q as an input and return m as an output.Here's my code:
function [m] = Prob(q)
m = 0; %assign m initial value of 0
P = 0; %assign P a value of 0 for now so that while loop will start
while P < q %runs until P >= q
m = m + 1; %increments m as long as condition above is true
if m == 1 %checks first condition
P = 0;
elseif m >= 2 && m <= 365 %checks second condition
S = 1; %variable that stores the product of (1 - k/365) from 1 -> m-1
for k = 1: m - 1
S = S * (1 - k/365);
P = 1 - S; %calculates P by subtracting S(the total product) from 1
else %checks third condition
P = 1; %assigned 1 if m >= 365
end
end
Does this seem ok? So hard to debug it when you're not actually in MATLAB.
  2 件のコメント
Image Analyst
Image Analyst 2013 年 10 月 2 日
Why not wait until you have MATLAB running to debug it yourself rather than asking us to do it now for you?
David
David 2013 年 10 月 2 日
I'm not so much asking ye to debug it yourselves as to have a quick look at the concept of the code and see if it makes general sense. I can refine it myself later on.

サインインしてコメントする。

採用された回答

Doug Hull
Doug Hull 2013 年 10 月 2 日
I did not run everything, but here is a problem:
if m = 1 %checks first condition
= is a statement. == is a question.
You want the question for here. There may be other problems.
Doug
  1 件のコメント
David
David 2013 年 10 月 2 日
Was writing it in notepad so must've thrown that in there by mistake!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

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

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by