-
Notifications
You must be signed in to change notification settings - Fork 1
/
integration of sine.py
56 lines (49 loc) · 1.55 KB
/
integration of sine.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
import numpy as np
import random
import math
import matplotlib.pyplot as plt
points_insidecircle=0 #number of points lying downside the square curve
points_total=0 #number of points lying upside the square curve
#empty arrays for storing the values of the points up or down the square curve
x_inside=[]
y_inside=[]
x_outside=[]
y_outside=[]
pi=[]
darts=[]
a=2
b=5
for i in range(500000):
x_coordinate=random.uniform(0,6.28)
y_coordinate=random.uniform(-1,1)
d=math.sin(x_coordinate)
if y_coordinate>=0:
if d>=y_coordinate:
points_insidecircle+=1
x_inside.append(x_coordinate)
y_inside.append(y_coordinate)
else:
x_outside.append(x_coordinate)
y_outside.append(y_coordinate)
else:
if d<=y_coordinate:
points_insidecircle+=1
x_inside.append(x_coordinate)
y_inside.append(y_coordinate)
else:
x_outside.append(x_coordinate)
y_outside.append(y_coordinate)
points_total+=1 #increments number of points downside the curve
value_of_pi_1=3.14*(points_insidecircle/points_total)
pi.append(value_of_pi_1)
darts_number=i
darts.append(darts_number)
plt.scatter(x_inside,y_inside,s=1)
plt.scatter(x_outside,y_outside,s=1,alpha=.4)
plt.savefig('p11.pdf')
plt.show()
value_of_pi=6.28*(points_insidecircle/points_total)
print("Integration is : ","%.2f" %value_of_pi)
plt.plot(darts,pi)
plt.savefig('p12.pdf')
plt.show()