I've been trying to compute the integral of (10*6)*sin(x0+x1+x2+x3+x4+x5+x6+x7)dx0dx1dx2dx3dx4dx5dx6dx7 using Monte-Carlo methods as part of my computing coursework at uni. I've managed to get the code to work after some trying, but it doesn't seem to give the correct answer. I've been given a reference analytical result of 537.187.... but my code converges to about 360
I'm not sure what else to try to resolve it. Is it something to do with my random numbers, could they be correlated by defining them seperately? Should I use a single 8 dimensional generator? Or do I need to use a more stratified sampler?
Apologies if it's something small, I'm quite new to Python!
UPDATE: I'm sorry I forgot to say what limits I was using. I'm integrating from 0 to pi/8 in each dimension
a=0 #Defining Limits of Integration
b=(np.pi)/8
N=20000 #Number of random numbers generated in each dimension
x0rand, x1rand, x2rand, x3rand, x4rand, x5rand, x6rand, x7rand = [[0]*N]*8
for i in range(N):
x0rand[i]= np.random.uniform(a,b) #Generates N random numbers 8D between 0 and pi/8
x1rand[i]= np.random.uniform(a,b)
x2rand[i]= np.random.uniform(a,b)
x3rand[i]= np.random.uniform(a,b)
x4rand[i]= np.random.uniform(a,b)
x5rand[i]= np.random.uniform(a,b)
x6rand[i]= np.random.uniform(a,b)
x7rand[i]= np.random.uniform(a,b)
def func(x0,x1,x2,x3,x4,x5,x6,x7): #Defining the integrand
return (10**6)*np.sin(x0+x1+x2+x3+x4+x5+x6+x7)
integral = 0.0
for i in range(N):
integral += func(x0rand[i],x1rand[i],x2rand[i],x3rand[i],x4rand[i],x5rand[i],x6rand[i],x7rand[i])
answer=(((b-a)**8)/N)*integral
print ('The Integral is:', answer)