Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PERFORM ONLY IN MATLAB Suppose you repeatedly flip a coin and keep track of the

ID: 3806043 • Letter: P

Question

PERFORM ONLY IN MATLAB

Suppose you repeatedly flip a coin and keep track of the number of times you get heads. How many coin flips will it take before you get thirty heads? You probably have an idea of what the answer should be. In this problem, you will model flipping a coin using MATLAB's ran perm function and determine the number of coin flips experimentally. Write a code fragment that repeatedly flips a coin until heads has been seen thirty times. Your code should then display the number of coin flips needed to get thirty heads. To receive full credit, you must use a while loop (and may not use a for loop or any break statements). If you run your code from part 2.a several times. you will find that you get different results each time. Suppose we consider what was done in part 2.a to be a single trial. Using your code from part 2.a. write a code fragment that runs 2000 trials and then displays the average number of coin flips needed to get thirty heads.

Explanation / Answer

%matlab code part 2a

count = 0;
coinsFlipped = 0;
while count <= 30
coin = randi(2);
% 2 is assumed as tail and 1 as head
if coin == 1
count = count + 1;
end
coinsFlipped = coinsFlipped + 1;
end

fprintf('Number of coins flipped to get 30 heads: %d ',coinsFlipped);
%output: Number of coins flipped to get 30 heads: 79





%matlab code part 2b

totalCoinsFlipped = 0;
for i=1:2000
count = 0;
coinsFlipped = 0;
while count <= 30
coin = randi(2);
% 2 is assumed as tail and 1 as head
if coin == 1
count = count + 1;
end
coinsFlipped = coinsFlipped + 1;
end
totalCoinsFlipped = totalCoinsFlipped + coinsFlipped;
end
average = totalCoinsFlipped/2000;
fprintf('Average Number of coins flipped to get 30 heads: %d ',average);
%output: Average Number of coins flipped to get 30 heads: 61.9895