I am developing a simulation of the integration of montecarlo (dx) and I find myself wondering which is the best way to determine the exact number of samples (N) in the Monte Carlo method to approximate the solution to a definite integral.
This is a simple code of implementation:
import math
import random
class Montecarlo:
def __init__ (self):
print ("Inicializa..")
def fy(self, ri, a, b):
res = math.pow((b-a)*ri+a, 2.0)+math.sqrt((b-a)*ri+a)
return res
def integral (self, a, b, N):
suma = 0.0
ri = 0.0
for i in range (N):
ri = random.random()
suma+=self.fy(ri,a,b)
res=((b-a)/N)*suma
return res
if __name__ == "__main__":
monte = Montecarlo()
res = monte.integral(10.0,27.0,N)
print("Res: ", res)
- Where N must be a value that allows to approximate the real result of the integral