Created
April 24, 2020 05:14
-
-
Save jpcrespo/87131e15db25c4e40e63371a4be32e0e to your computer and use it in GitHub Desktop.
Calcula el valor de Pi con MC, usando un solo bucle for
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
from time import time | |
N = np.logspace(2,8,num=10,endpoint=True) | |
N = np.around(N).astype(int) | |
A=[] | |
pis=[] | |
tiempo_inicial = time() | |
for i in N: | |
a = np.random.random((i,2)) | |
z=np.sqrt(a[:,0]**2+a[:,1]**2) | |
a_sc1=z<=1 | |
a_sc=np.sum(a_sc1) | |
pi = 4*a_sc/i | |
pis.append(pi) | |
A.append(np.absolute(pi-np.pi)) | |
print(pis) | |
tiempo_final = time() | |
tiempo_ejecucion = tiempo_final - tiempo_inicial | |
print('El tiempo de ejecución del programa fue ',tiempo_ejecucion,' segundos') | |
#generamos la gráfica | |
plt.figure() | |
plt.plot(np.arange(1,11),A,'o-') | |
plt.xticks(range(1,N.size+1), N, rotation=20) | |
plt.xlabel('Número de pruebas') | |
plt.ylabel('Error abs Pi por MC') | |
plt.grid(True) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment