Question
MATLAB: The following program simulates a coin toss: % this program simulates a coin toss p = 0.5; % the probability of heads is 0.5
MATLAB:
-
The following program simulates a coin toss:
% this program simulates a coin toss
p = 0.5; % the probability of heads is 0.5
r = rand; % generate a random number from 0 to 1
if r < p % this should happen about 50% of the time
fprintf(H)
else % if its not Heads, it must be Tails
fprintf(T)
end
fprintf( ) % newline after the simulation
-
Type the program above into an m-file, and then run it several times. Does it appear to generate random results?
-
Now create a for loop around everything except the first and last lines of code. Have the loop perform Ntoss iterations, and start out with Ntoss = 20. Run the program several times and observe the results. Do you get exactly 10 Heads every time?
-
Now replace the fprintf statements with counters for Heads and Tails (remember to initialize them before the loop). Calculate and output the percentage of Heads after the loop is completed.
After you verify that your program executes successfully, try running it repeatedly with Ntoss = 50. Then change Ntoss to 500 and run it several times again. Repeat with Ntoss = 5000 and then 50000. What happens to the percentage of Heads as Ntoss increases?
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