Probability function of people at a party.

1 view (last 30 days)
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 Comments
Image Analyst
Image Analyst on 2 Oct 2013
Why not wait until you have MATLAB running to debug it yourself rather than asking us to do it now for you?
David
David on 2 Oct 2013
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.

Sign in to comment.

Accepted Answer

Doug Hull
Doug Hull on 2 Oct 2013
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 Comment
David
David on 2 Oct 2013
Was writing it in notepad so must've thrown that in there by mistake!

Sign in to comment.

More Answers (0)

Categories

Find more on Birthdays in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!