Can anyone help me do this on MATLAB Please do not change th
Can anyone help me do this on MATLAB? Please do not change the variable names as they have to be the same. Thank you very much.
4. (10 points) The birthday problem is as follows: given a group of n people in a room, what is the probability that two or more of them have the same birthday? It is possible to determine the answer to this question via simulation. Write a function called calcBirthdayProbability that takes as input n and calculates the probability that two or more of the n people will have the same birthday. To do this, the function should create an array of size n and generate n birthdays in the range l to 365 randomly. It should then check to see if any ofthe n birthdays are identical. The function should perform this experiment 106 times and calculate the fraction of time during which two or more people had the same birthday The function is called as follows: probability caloBirthday Probability (numPeople) Examples of correct function behavior follow: num People 10; prob calcBirthday Probability numPeople) prob 0.1175 num People 20 prob calcBirthday Probability numPeople) prob 0.4117 prob calcBirthday Probability numPeople) prob 0.4114 prob calcBirthday Probability numPeople) prob 0.4113 Note that different runs of the program with the same value for numPeople yield slightly different probability results. This is expected since we are performing experiments with randomly generated values during each run of the program. The Cody tests will be designed to account for this type of variation in the results.Solution
i am copying the MATLAB code below which solves the problem, it takes some time to run since it has to run 10^6 iterations (around 1-2 minutes). you can change the loop value from 10^6 to any value in the code.You can just open matlab file and copy the code directly and call the function \"calcBirthdayProbability()\"
function prob = calcBirthdayProbability(numPeople)
matches=0;
loop=1000000;
if numPeople > 1
for a = 1: 1: loop
birthdayMatch=false;
%generate random birthdays
randomBirthdays = randi(365,numPeople,1);
for idx1=1:1:numPeople
for idx2=idx1+1:1:numPeople
if(randomBirthdays(idx1)==randomBirthdays(idx2))
%check same birthday
birthdayMatch = true;
end
end
end
if(birthdayMatch==true)
matches += 1;
end
end
end
prob = matches/loop;
end
