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 CenterFile Exchange에서 Birthdays에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by