[Main Page] Main Page | Recent changes | View source | Page history

Printable version | Disclaimers | Privacy policy | Latest revision

Not logged in
Log in | Help
 

Monte-Carlo Method

(Difference between revisions)

 
m
Line 1: Line 1:
Currently under construction. Come back later.  
+
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:
 +
 
 +
<center>http://imagestore.ugbox.net/image/montecarlo_7dd5a4bb001953bd326f.jpg</center>
 +
 
 +
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.

Revision as of 18:57, 8 July 2008

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.