In [1]:
import math
import matplotlib.pyplot as plt
import numpy as np
def randuX(n):
a = 1995
b = 9513
m = 999983
#print "Used parameters: "
#print "a = "+str(a)+" b = "+str(b)+" m = "+str(m)
lastXn = 0
randList = []
#print "Random numbers generated: "
for i in range(n):
Xn = float((a*lastXn + b)% m)
#print Xn
lastXn = Xn
random = float(Xn/m)
#print random
randList.append(random)
return randList
def randuY(n):
a = 12859
b = 965139
m = 3474749660383
#print "Used parameters: "
#print "a = "+str(a)+" b = "+str(b)+" m = "+str(m)
lastXn = 0
randList = []
#print "Random numbers generated: "
for i in range(n):
Xn = float((a*lastXn + b)% m)
#print Xn
lastXn = Xn
random = float(Xn/m)
#print random
randList.append(random)
return randList
def calcularArea(r):
randX = randuX(10000)
randY = randuY(10000)
InXList = []#Points inside the circle
OutXList = [] #Points outside the circle
InYList = []#Points inside the circle
OutYList = [] #Points outside the circle
area = 0
for i in range(len(randX)):
X = (randX[i]*2*r-r)
Y = (randY[i]*2*r-r)
if(r**2 >=(X**2+Y**2)):
area = area+1
InXList.append(X)
InYList.append(Y)
else:
OutXList.append(X)
OutYList.append(Y)
print "Numero de puntos: "+ str(area)
print "Area experimental: "+str(area)
area = float(area)/float(len(randX))*((2*r)**2)
print "Area experimental: "+str(area)
areaTeorica = math.pi*((r)**2)
print "Area teorica: "+str(areaTeorica)
graficar(r,InXList,OutXList, InYList, OutYList,areaTeorica,area)
def graficar(r, InX, OutX, InY, OutY,aT,aE):
print"Pintelo"
plt.suptitle("Area experimental: "+str(aE)+" Area Teorica: "+str(aT)+" Diferencia: "+str(abs(aT-aE)))
plt.subplot(2,2,1).set_title("N/8")
x = np.linspace(0, 2*np.pi, 100) # This will be something like the 'x' data for the functions we are going to use to draw the circle.
plt.plot(r*np.cos(x), r*np.sin(x),'k-')
plt.axis('equal')
plt.axis([-r, r, -r, r])
for i in range(len(InX)/8):
plt.plot(InX[i], InY[i], 'o', color='blue')
for i in range(len(OutX)/8):
plt.plot(OutX[i], OutY[i], 'o', color='red')
plt.subplot(2,2,2).set_title("N/4")
plt.plot(r*np.cos(x), r*np.sin(x),'k-')
plt.axis('equal')
plt.axis([-r, r, -r, r])
for i in range(len(InX)/4):
plt.plot(InX[i], InY[i], 'o', color='blue')
for i in range(len(OutX)/4):
plt.plot(OutX[i], OutY[i], 'o', color='red')
plt.subplot(2,2,3).set_title("N/2")
plt.plot(r*np.cos(x), r*np.sin(x),'k-')
plt.axis('equal')
plt.axis([-r, r, -r, r])
for i in range(len(InX)/2):
plt.plot(InX[i], InY[i], 'o', color='blue')
for i in range(len(OutX)/2):
plt.plot(OutX[i], OutY[i], 'o', color='red')
plt.subplot(2,2,4).set_title("N")
plt.plot(r*np.cos(x), r*np.sin(x),'k-')
plt.axis('equal')
plt.axis([-r, r, -r, r])
for i in range(len(InX)):
plt.plot(InX[i], InY[i], 'o', color='blue')
for i in range(len(OutX)):
plt.plot(OutX[i], OutY[i], 'o', color='red')
#Let's draw the circle
plt.show()
def main():
calcularArea(15)
main()
In [ ]:
In [ ]: