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

Upload New File

parent 8ac42741
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:1e2beded tags:
# $\Pi$ Example
%% Cell type:code id:016a7406 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