PI ON a PI - Pi Value Estimation on a Raspberry Pi

by nilutpolkashyap in Circuits > Raspberry Pi

387 Views, 0 Favorites, 0 Comments

PI ON a PI - Pi Value Estimation on a Raspberry Pi

IMG_20210316_103923.jpg

What is Pi?
Pi is the famous circle number approximately given by 3.14159… It is the area of the unit circle (i.e. the circle with radius 1) or half the perimeter of the unit circle.

Monte Carlo Simulations can be thought of as computational algorithms
that enable us to model probabilities that are difficult to calculate. This is done by using repeated random sampling to our advantage. With random sampling, the data we are using has no bias so we can simulate situations where the probabilities we obtain are close to the actual value. The more random data we use, the closer we get to the true value.

Supplies

Parts Required -

  1. Raspberry Pi 3/4/Zero/Zero W
  2. HDMI Cable
  3. HDMI Monitor
  4. Power Cable

Software Required -

  1. Python 3.x
  2. Turtle Module
  3. Matplotlib Module

Inspiration

IMG_20210316_103923.jpg

One method to estimate the value of π (3.141592…) is by using a Monte
Carlo method
. This methods consists of drawing on a canvas a square with an inner circle. We then generate a large number of random points within the square and count how many fall in the enclosed circle.

A Monte Carlo method is a technique that uses random numbers and probability to solve complex problems. The Monte Carlo simulation, or probability simulation, is a technique used to understand the impact of risk and uncertainty in financial sectors, project management, costs, and other forecasting machine learning models.

This is a project I made for Pi Day. I estimated the value of Pi, the mathematical variable using Monte Carlo Simulation on a Raspberry Pi 4.

Mathematics Behind the Code

IMG_20210314_170407.jpg

The area of the circle is πr^2,
The area of the square is width^2 = (2r)^2 = 4r^2.

If we divide the area of the circle, by the area of the square we get π/4.

The same ratio can be used between the number of points within the square and the number of points within the circle.

Hence we can use the following formula to estimate Pi:

π ≈ 4 x (number of points in the circle / total number of points)

Code - Monte Carlo Simulation

IMG_20210316_104341.jpg

To estimate the value of PI, we need the area of the square and the area of the circle. To find these areas, we will randomly place dots on the surface and count the dots that fall inside the circle and dots that fall inside the square. Such will give us an estimated amount of their areas. Therefore instead of using the actual areas, we will use the count of dots to use as areas.

Results & Conclusion

PI ON A PI - Instructables Pi Day Speed Challenge Project
IMG_20210316_105951.jpg

We can see that after 5000 iterations, we can get the approximate value
of PI.

Also, notice that the error in estimation also decreased exponentially as the number of iterations increased.