Skip to content
Snippets Groups Projects
Commit ddb26a0a authored by cprutean's avatar cprutean
Browse files

Upload New File

parent be822f80
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:781271bc tags:
# $\Pi$ Example
%% Cell type:code id:5417046e tags:
``` python
""" Example of estimating Pi by Monte Carlo Simulation.
"""
import math
import random
import matplotlib.pyplot as plt
def main():
maxPoint = int(input("Number of points : "))
plotInterval = maxPoint/100
xData = []
yData = []
point = 0
inside = 0
while point < maxPoint:
point += 1
x = random.uniform(-1.0,1.0) # Random between -1 -> 1for both x and y
y = random.uniform(-1.0,1.0)
if x*x + y*y <= 1.0: # Test if inside unit circle
inside += 1
if point % plotInterval == 0: # Calcualte estimate for plot
estimate = 4.0*inside/point
xData.append(point)
yData.append(estimate)
print("Estimate for PI after " + str(maxPoint) + " is " + str(estimate))
plt.plot(xData,yData) # Plot the estimate
plt.plot([0,maxPoint],[math.pi,math.pi]) # Plot Pi
plt.ylim(3.0,3.3) # Limit graph range
plt.title("Plot of estimate of Pi again points")
plt.show()
main()
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment