Friday, 10 May 2013

Monte Carlo Integration 1

Imagine calculating the area of a circle using a bag of marbles. One way to do this would be to enclose the circle inside a square of known area, say $1$, so that it was just touching the sides and throw the marbles in. The probability that a marble lands inside the circle is proportional to the area of the circle divided by the area of the square, this number is: $\pi (\frac{1}{2})^2/1 = \frac{\pi}{4}$. Assuming you don't spill any marbles, this means that the proportion that lie in the circle compared to the total number is given by the same ratio, $\pi/4$. The animation below illustrates this and in the process calculates $\pi$ (for this and the next animation, click the box to start or stop).

If you want to use this to calculate $\pi$ to any accuracy you will have to wait a long time. We'll talk about a better method of finding areas, or for doing any integral you can think of, in the next post. Until then you can enjoy this even less efficient calculation of $\pi$: a rose is a curve with the parametric equation, $r = acos(k \theta)$. If $k$ is an odd integer the rose has $k$ petals and for $k$ even there are $2 k$. The area of these roses is $\pi/2$ or $\pi/4$ for $k$ even or odd respectively. To see this let's calculate the area for $k$ even. Put $k = 2n$, the area $A$ is, \[A = \frac{1}{2} \int_0^{2 \pi} r^2 d \theta = \frac{1}{2} \int_0^{2 \pi} a^2 cos(2n \theta) d \theta = \] \[ \int_0^{2 \pi} \frac{a^2}{4}\left(1 + \cos(2n \theta)\right) d \theta = \frac{a^2}{4}\left( \theta - \frac{sin(2n\theta)}{2n}\right)\bigg|_0^{2 \pi} = \frac{a^2 \pi}{2}. \] Using the same method we had above for the circle, we can calculate the ratio of hits landing inside the petals to the total number, which will give us the integral and hence $\pi$.

Enter k > 1 value in the box:

(Bonus): Rose curves look very cool when k is a rational number $k = n/d$, try it! To stop browsers from crashing $n_{max} = k_{max} = 30$

Enter n value:

Enter d value:

No comments:

Post a Comment