Menu

Matlab Following Program Simulates Coin Toss Program Simulates Coin Toss P 05 Probability Q43864475

MATLAB:

  1. The following program simulates a cointoss:

% 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 thetime

fprintf(‘H’)

else % if it’s not Heads, it must be Tails

fprintf(‘T’)

end

fprintf(‘n’) % newline after the simulation

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

  2. Now create a for loop around everything except the firstand last lines of code. Have the loop perform Ntoss iterations, andstart out with Ntoss = 20. Run the program several times andobserve the results. Do you get exactly 10 Heads everytime?

  3. Now replace the fprintf statements with counters forHeads and Tails (remember to initialize them before the loop).Calculate and output the percentage of Heads after the loop iscompleted.  

After you verify that your program executessuccessfully, try running it repeatedly with Ntoss = 50. Thenchange Ntoss to 500 and run it several times again. Repeat withNtoss = 5000 and then 50000. What happens to the percentage ofHeads as Ntoss increases?

Expert Answer


Answer to MATLAB: The following program simulates a coin toss: % this program simulates a coin toss p = 0.5; % the probability of …

OR