ESTIMATION OF PI USING MONTE CARLO METHOD USING PYTHON
The value of pi is calculated using monte carlo method by taking a square of 1 unit and inscribing a circle in the square. The radius of circle is 0.5 units. Now the ratio of area of circle to the ratio of square multiplied by 4 gives us pi.
Python code:
import random
n=1000000
c_points=0 #points inside circle
s_points=0 #points inside square
for i in range(n):
x = random.uniform(0,1)
y = random.uniform(0,1)
d = x**2 + y**2
if d<=1 :
c_points +=1
s_points +=1
pi = 4*(c_points/s_points)
print("pi value is:", pi)
Conclusion: Higher the value of n, higher is the accuracy of value of pi.

Comments
Post a Comment