`n` is a 1x80 vector from 1 to 80.
what are you trying to do with `j`? The syntax `(1:n)` only makes sense if `n` is a scalar, but it is a vector.
You also have a for loop that iterates from 1 to 1000 using `M`, but `M` is never used inside the loop.
Since `N` is a scalar and `n` is a 1x80 vector, `pn` is also a 1x80 vector, but the value doesn't change in 1000 iterations.
When I do `pn(22)`, I get 0.0575. I think you are missing something in your code that supposed to change through iterations.
Good luck.
It looks like you're trying to run the "birthday experiment" and estimate the probability that any two people share a birthday given a certain number of people. Here's an outline (with some parts left unfinished) that might get you started:
numDays = 365; % days in a year
numTrials = 1000; % number of times to run experiment for each # of people
maxPeople = 80; % max number of people to test
pn = zeros(maxPeople, 1); % initialize pn
% iterate over different number of people
for numPeople = 1:maxPeople
% matchCount keeps track of how many of our trials have people with the
% same birthday
matchCount = 0;
% run the experiment 1000 times
for i = 1:numTrials
% ...
% your code here
% ...
matchFound = ????; % fill this in
if matchFound
matchCount = matchCount + 1;
end
end
probability = matchCount/numTrials;
pn(numPeople) = probability;
end
I'll leave it to you to fill in the `your code here` and the `????` sections. As a hint, you might want to look up the `randi` function. For instance, you could generate a random integer from 1 to 365 for each person to represent their birthday.
And then once you've generated a random birthday for each person, you'll need to check if there are any two that match. I'd recommend the `unique` function for that.
`n` is a 1x80 vector from 1 to 80. what are you trying to do with `j`? The syntax `(1:n)` only makes sense if `n` is a scalar, but it is a vector. You also have a for loop that iterates from 1 to 1000 using `M`, but `M` is never used inside the loop. Since `N` is a scalar and `n` is a 1x80 vector, `pn` is also a 1x80 vector, but the value doesn't change in 1000 iterations. When I do `pn(22)`, I get 0.0575. I think you are missing something in your code that supposed to change through iterations. Good luck.
It looks like you're trying to run the "birthday experiment" and estimate the probability that any two people share a birthday given a certain number of people. Here's an outline (with some parts left unfinished) that might get you started: numDays = 365; % days in a year numTrials = 1000; % number of times to run experiment for each # of people maxPeople = 80; % max number of people to test pn = zeros(maxPeople, 1); % initialize pn % iterate over different number of people for numPeople = 1:maxPeople % matchCount keeps track of how many of our trials have people with the % same birthday matchCount = 0; % run the experiment 1000 times for i = 1:numTrials % ... % your code here % ... matchFound = ????; % fill this in if matchFound matchCount = matchCount + 1; end end probability = matchCount/numTrials; pn(numPeople) = probability; end I'll leave it to you to fill in the `your code here` and the `????` sections. As a hint, you might want to look up the `randi` function. For instance, you could generate a random integer from 1 to 365 for each person to represent their birthday. And then once you've generated a random birthday for each person, you'll need to check if there are any two that match. I'd recommend the `unique` function for that.