Calculating Pi 𝞹 Values Using Python Programming | Python MatplotLib | How to Calculate Pi 𝞹 Values? Visualization of Pi 𝞹 Values | Python | Pi Day

by Python EveryDay in Teachers > University+

3646 Views, 12 Favorites, 0 Comments

Calculating Pi 𝞹 Values Using Python Programming | Python MatplotLib | How to Calculate Pi 𝞹 Values? Visualization of Pi 𝞹 Values | Python | Pi Day

Normal.gif

As we all know that the circle is just a polygon, with infinite sides. So, with this concept, we can calculate the value of pi 𝞹. With simple trigonometry. We will consider every object as a circle & polygon at the same time. And use the naming convention interchangeably so, don't get confused.

Supplies

Python IDE

Below are the libraries required:

  • Math
  • Maplotlib

These need to be imported as follows:

import math
import matplotlib.pyplot as plt

Circle Is a Polygon With Infinite Sides

Normal.gif

We can see in the above figure. As we go on increasing the number of sides of the polygon its shape starts to look like a circle.

When the number of sides of the polygon is equal to infinity it becomes a perfect circle.

Pi 𝞹 Vs. Sides

pi vs sides.png

In the graph shown above. It is a graph of the 𝞹 pi values calculated versus the number of sides of the polygon used to calculate that value. The more the number of sides the more accurate the value we get approximated to 3.141592653589793238.

Polygons With Diagonals

Digonal.gif

The above figure shows the diagonals of the polygon.

The number of sides of polygon = number of diagonals.

For a perfect circle, there will be infinite diagonals. And the length of the diagonal will be equal to the diameter of the circle.

So, we will consider the radius = digonal/2.

Decagon for Calculations

4.jpg

Let us consider this decagon for the explanation and calculation of the 𝞹 (pi) value.

It has 10 sides & 10 diagonals.

Angles (θ) of Polygon

6.jpg

Let us consider the angle between any two diagonals of the polygon as theta(θ).

Theta = 360 / no of sides

for decagon :

θ = 360/ 10

θ = 36°

Consider This Triangle

5.jpg

Let us consider this triangle for the calculation.

Triangles in Polygon

Triangle.gif

Every polygon has diagonals. So a triangle is formed between two diagonals and the side.

So, we can use this triangle-based method for any polygon to calculate the value of pi.

Triangle for Calculations

1.jpg

This is the triangle to be used for calculation. We have separated it out from polygon. For better visualization and easier calculations.

Specs of Triangle

2.jpg

There is an angle θ theta between two diagonals. The two sides of the triangle can be considered radii because it is equal to the diagonal/2 of the polygon. The other side of the triangle is equal to the edge of the polygon.

Calculating Theta Θ

3.jpg

The values we know are theta, radius, phi.

To find: base,side

let us consider raduis = 10

θ = 360 / no of sides


for decagon :

θ = 360 / 10

θ = 36°


radius = 10
hypo = radius
theta = 360/sides

Calculating Phi Φ

As per the figure shown. The phi value is equal to the half of theta value.

Φ = θ /2


for decagon:

Φ = 36 /2

Φ = 18°

phi = theta/2

Calculating Base of Triangle

This is a right-angled triangle.

Therefore base = radius x sin(Φ)



for decagon:

base = 10 x sin(18)

base = 3.09016994


base = hypo* math.sin(phi*math.pi/180)

Calculating Edge of Polygon

7.jpg

The side length of the polygon is equal to twice the base of the right-angled triangle.

side length = 2 x base


for decagon:

side length = 3.09016994 x 2

side length = 6.18033988

side_length = 2* base

Calculating Perimeter of Polygon

18.png

The circle is a polygon with infinite edges.

The perimeter of any polygon = no of sides x length of the side


for decagon:

perimeter = 10 x 6.18033988

perimeter = 61.8033988


#perimeter of polygon = circumference of circle
perimeter = side_length * sides

Calculating 𝞹 Pi Value

We are considering a circle as a polygon with infinite sides.

So, the perimeter of the polygon is equal to the circumference of the circle.


circumference of circle = 2 x 𝞹 x radius

2 x 𝞹 x radius = no of sided x length of side

𝞹 = (no of side x length of side)/(2 x radius)

That's how we calculated the value of 𝞹.


for decagon:

𝞹 = (10 x length of side)/(2 x 10)

𝞹 = (10 x 6.18033988)/(2 x 10)

𝞹 = 3.09016994


pi_calculated = perimeter /(2*radius)

Increasing Sides Reduces Error

pi vs sides.png
table.JPG

In the graph shown above. It is a graph of the 𝞹 pi values calculated versus the number of sides of the polygon used to calculate that value. The more the number of sides the more accurate the value we get approximated to 3.14159.

The table has the values of the number of sides of the polygon and the respective pi value calculated from that figure.

Conclusion

We have successfully calculated the 𝞹 value with the help of trigonometry and polygons.

And the most important part is we made this right before World Pi 𝞹 day 14 March.


Code to Calculate 𝞹 Pi

Below is the code in Python Programming language.

The function calculate_pi takes in the sides(Integer) as parameter and returns the calculated pi value.

def calculate_pi(sides):

  radius = 10

  theta = 360/sides

  hypo = radius

  phi = theta/2

  base = hypo* math.sin(phi*math.pi/180)

  side_length = 2* base

  #perimeter of polygon = circumference of circle
  perimeter = side_length * sides

  #circumference of circle = 2*pi*r
  pi_calculated = perimeter /(2*radius)

  return pi_calculated

Comparing Theoretical & Calculated Pi Value

value comparison.JPG

The above image compares the pi value stored in the math library and our calculated pi value.

With the polygon of 10,000 sides, we get the error of just 5.1677127910210174e-08.

Code to Draw Polygon

The below code is to make a polygon in the Python programming language.

It uses a library named MatplotLib to plot/draw.

It has a function draw_polygon which takes the number of sides of the polygon and draws a polygon of that number of sides in a new window.

def draw_polygon(sides):
  
  radius =10
  x,y=[],[]

  for i in range(sides):

    theta = i*360/sides 
    
    x.append(radius*math.cos(math.pi*theta/180))
    y.append(radius*math.sin(math.pi*theta/180))
  
  x.append(x[0])
  y.append(y[0])

  plt.plot(x,y)
  plt.scatter(0,0)