Main Page | Recent changes | View source | Page history

Printable version | Disclaimers | Privacy policy | Latest revision

Not logged in
Log in | Help
 

Monte-Carlo Method

Revision as of 18:57, 8 July 2008 by Nam Taf (Talk | contribs)

This is a small, simple example of utilising the Monte-Carlo method to calculate the value of pi. It's easy because it is only a 2-dimensional problem and thus is easy to visualise, and is trivial in its code implementation.

Picture the graph shown in the parent article - that of a quarter circle plotted on a unit x-y graph:

http://imagestore.ugbox.net/image/montecarlo_7dd5a4bb001953bd326f.jpg

Below is a small example piece of Matlab code that does the above simulation:

Code:

 for i = 1:1000
     x = rand;
     y = rand;
     if x^2+y^2 <=1
         tally = tally + 1;
     end
     count = count + 1;
 end
 area = tally/count;


This, when run on my computer, produced the result of 0.783. The area of a quarter circle works out to be π/4 if the radius is 1. π/4 = 0.7854, so you can see that this is quite close. More iterations than 1000 would produce a closer answer.


[Main Page]
OCAU News
OCAU Forums
PC Database

Main Page
Recent changes
Random page
All pages
Help

View source
Discuss this page
Page history
What links here
Related changes

Special pages