This is my first time here. Apologies, I'm quite new to python. I'm trying to use a monte carlo method to compute an integral in 8D as part of my uni coursework. However, when I run the program, I get the error described in the title on line when i define x0[i]. It seems to only allow i to run up to 2, but I want to investigate the error as N is varied so I need to be able to use N>2. Any help would be appreciated
Jake
a=0 #Defining Limits of Integration
b=np.pi/8
N=4 #Number of random numbers generated in each dimension
for i in range(N):
x0rand[i]=((np.pi)/8)*np.random.uniform(0,1) #Generates N random numbers 8D between 0 and pi/8
x1rand[i]=((np.pi/8))*np.random.uniform(0,1)
x2rand[i]=((np.pi/8))*np.random.uniform(0,1)
x3rand[i]=((np.pi/8))*np.random.uniform(0,1)
x4rand[i]=((np.pi/8))*np.random.uniform(0,1)
x5rand[i]=((np.pi/8))*np.random.uniform(0,1)
x6rand[i]=((np.pi/8))*np.random.uniform(0,1)
x7rand[i]=((np.pi/8))*np.random.uniform(0,1)
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)
IndexError Traceback (most recent call last)
<ipython-input-141-1a3483405a4d> in <module>()
4
5 for i in range(N):
----> 6 x0rand[i]=((np.pi)/8)*np.random.uniform(0,1) #Generates N random numbers 8D between 0 and pi/8
7 x1rand[i]=((np.pi/8))*np.random.uniform(0,1)
8 x2rand[i]=((np.pi/8))*np.random.uniform(0,1)
IndexError: index 2 is out of bounds for axis 0 with size 2