Question
MATLAB: Suppose you repeatedly flip a coin and keep track of the number of times you get heads. How many flips will it take before
MATLAB: Suppose you repeatedly flip a coin and keep track of the number of times you get heads. How many flips will it take before you get twenty heads? thirty heads? one hundred heads? You probably have an idea of what the answers should be. In this problem, you will model flipping a coin using MATLAB's randperm function and determine the average number of coin flips experimentally.
a. Write a function named flipUntil that takes one parameter, numHeads, repeatedly flips a coin until a heads has been seen numHeads times, and returns the number of flips needed to get numHeads heads. You may assume that numHeads is a single non-negative integer.
The first line of your flipUntil function should be:
function numFlips = flipUntil(numHeads)
The flipUntil function must use a while loop.
b. If you run your flipUntil several times with the same value for the input parameter, you will find that you get different results each time. Suppose we consider one call to flipUntil to be a single trial.
Write a code fragment that asks the user for the number of heads and runs 2000 trials and then displays the average number of flips needed to get that number of heads. (Hint: you will need to use a 'for' loop.) You may assume that the user always enters a single non-negative integer when prompted for the number of heads.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started