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:

  1. 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

  1. Type the program above into an m-file, and then run it several times. Does it appear to generate random results?

  2. 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?

  3. 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!