Probability function of people at a party.

1 visualizzazione (ultimi 30 giorni)
David
David il 2 Ott 2013
Commentato: David il 2 Ott 2013
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 Commenti
Image Analyst
Image Analyst il 2 Ott 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 il 2 Ott 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.

Accedi per commentare.

Risposta accettata

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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Birthdays in Help Center e File Exchange

Tag

Non è stata ancora inserito alcun tag.

Community Treasure Hunt

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

Start Hunting!

Translated by