Can anyone help me solve this coin toss problem. I am a beginner
Show older comments
function cointoss
a=round(rand(1,5000));
b=ones(1,5000);
c=cumsum(a);
d=cumsum(b)
e=a./b
plot(1:5000,e(1,5000),'b')
hold on
plot(1:5000,0.5,'r')
That is the body of my program.
The problem is that computer round my e matrix. Thus, it did not reveal the experimental probability of getting a 'head' after a certain number of trials. How could I solve this problem?
Thanks a lot
Accepted Answer
More Answers (2)
Image Analyst
on 18 Dec 2012
Well, close. But try it like this:
clear
clc;
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
numberOfTosses = 500;
tossSequence = randi(2, 1, numberOfTosses) - 1;
c = cumsum(tossSequence);
d = 1 : numberOfTosses;
headsProbability = c ./ d;
plot(d, headsProbability, 'b', 'LineWidth', 2);
grid on;
fontSize = 20;
xlabel('Number of Tosses', 'FontSize', fontSize);
ylabel('Probability of heads', 'FontSize', fontSize);
title('Coin Toss Monte Carlo Experiment', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
2 Comments
DI
on 18 Dec 2012
Image Analyst
on 18 Dec 2012
You must have a really old version of MATLAB. It's been in there for a while. No it's not a typo - I just copied and pasted a fully functional script.
Categories
Find more on Common Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!