A Tossing Coin Simulation Using Matlab




Ok, my lecturer has given the whole class a challenging task a week ago. It’s not something we’re not capable of, because of course we’re engineering student, what’s impossible? (As long as Google is in a healthy condition). It’s all about probability of getting a result from tossing a coin for BILLION TIMES. You wanna do it in an old style way? Go on. No one will ever stop you, but yourself, your hand, and also the arm are gotta have some hard times soon, flipping it for eternity.  
So I’m starting a research over Google for tossing a coin and deciding what kind of application/software/codes I need to do with this coin simulation.

The results are coming out like these:
www.random.org/coins
justflipacoin.com
www.shodor.org
www.virtualcointoss.com
Trust me these are helpful enough, but I’m just not that satisfied.

.....because I need something new, lol, no I mean, I’ve been flipped upside down by Matlab few days ago, and kinda get excited to involve this coin thingy with Matlab.
So I found some helpful results, but accidentally fell in love with few of them only ((FYI, I’m including the source at the bottom of my article, no need to worry!)).

The first program goes to…  *drumrolls*

Tossing Coin Simulation Using Matlab (For Trials Under 50000 Times With Graph)


Tbh, you can give any kind of amount you need, but this is not accurate tho’, it lacks in still many way if you’re inserting a really big amounts (it takes time for creating the graph), you will wait for forever, your pc will just stop from beating. But here’s the code, I insert 125 for the flipping numbers.

%for counting the probabilities
clc
y=[1 2];          %assigning value to y
a=0;              %assigning value to a
b=0;              %assigning value to b
k=125;      %number of trials
for i=1:k,        %start number for loop
ran=randi([0,1]); %random head and tails generating 0 for head 1 for tails
if ran==0         %if head happens
a=a+1;            %a should increase by 1
elseif ran==1     %else  if tails happens
b=b+1;            %b should increase by 1
end               %end of if
m=a/i;            %probability of total head/total outcomes
n=b/i;            %probability of total tails/total outcomes
x=[m n]           %putting probabities of head and tail in array

%for creating the graph
bar(y,x)             %Plotting bar graph y in x axis and x in y axis
xlabel('Head/tails') %x axis label
ylabel('probability')%y axis label
title(['No: of throws is ',num2str(i)]) %title with changing trials
set(gca, 'XTick',1:2, 'XTickLabel',{'H' 'T'}) %x axis change to H and T
axis equal              %making axis equal for animation
M(i) = getframe;        %getting frame at each for loop run

end                     %end of for loop


Here’s the column chart result of 125 times in flipping the coin:



It’s going to be such waste to mention all the 125 probabilities here, so I give the last 13 results, there we go:


The first-author said, it’s better to get the results when the K value is getting bigger which means you need to flip the coin for more than 50 times. It's a true after all! Because when the K value is gettin' higher, the probabilities number you'll have is getting more accurate, so it will likely to appear in the same number which it can prove the theory of probability for 2 coins, about 0,5.

But I assume that when you try for a higher number, I suggest not to create the graph based on the 'changing trials'. What is the reason? Ok lemme tell you tha your Matlab will run for forever because the process is going to do so unless it reached the number you input. So you just need to delete the graph part, or making the graph based on the final probability score between the head and tail part.

It's really worth a try, happy programming!
No comments

No comments :

Post a Comment