Effacer les filtres
Effacer les filtres

Probability function of people at a party.

2 vues (au cours des 30 derniers jours)
David
David le 2 Oct 2013
Commenté : David le 2 Oct 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 commentaires
Image Analyst
Image Analyst le 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 le 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.

Connectez-vous pour commenter.

Réponse acceptée

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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Birthdays dans Help Center et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by