-
Notifications
You must be signed in to change notification settings - Fork 0
/
monte_carlo.py
60 lines (50 loc) · 1.7 KB
/
monte_carlo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
Aproximation of PI value with the method monte carlo
"""
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
figure = plt.figure(figsize=(8,8))
axes1 = figure.add_subplot(1, 1, 1)
axes1.set_title("Aproximation of PI value with the method monte carlo")
axes1.set_xlim(0, 1.5)
axes1.set_ylim(0, 1.5)
# Draw square
x = [1, 1, 1, 0]
y = [0, 1, 1, 1]
axes1.plot(x, y, color='k', linestyle='-', linewidth=3)
# Draw quarter circle
circle1 = plt.Circle((0, 0), 1, color='k', fill=False, linewidth=3)
axes1.add_artist(circle1)
trials = 0
inside_circle = 0
x_outside, y_outside, x_inside, y_inside = [],[], [], []
sc_outside = axes1.scatter(x_outside, y_outside)
sc_outside.set_color('r')
sc_inside = axes1.scatter(x_inside, y_inside)
sc_inside.set_color('b')
text1 = plt.text(1, 1.4, "trials = 0")
text2 = plt.text(1, 1.3, "inside circle = 0")
text3 = plt.text(1, 1.2, "value of PI = 0")
# plot the points and update the pi value
def animate(i):
global trials, inside_circle
trials = trials + 1
x = np.random.rand(1)
y = np.random.rand(1)
if x**2 + y**2 <= 1:
x_inside.append(x)
y_inside.append(y)
sc_inside.set_offsets(np.c_[x_inside, y_inside])
inside_circle = inside_circle + 1
else:
x_outside.append(x)
y_outside.append(y)
sc_outside.set_offsets(np.c_[x_outside, y_outside])
text1.set_text("trials = %d" % trials)
text2.set_text("inside circle = %d" % inside_circle)
pi = 4.0*inside_circle/trials
text3.set_text("pi = %f" % pi)
# Call methode animate with animation for see the value of PI in the time
ani = animation.FuncAnimation(figure, animate, frames=50, interval=1, repeat=True)
plt.show()