- #1
Amy Elliott
- 1
- 0
New member warned about posting without the template
I am using the monte carlo method to calculate the integral of cos^2(x). This is done using N random numbers. In my case I am doing it for 10^i random numbers when i ranges from 1-6. I want to calculate the error on the integral for each value of i used.
I know for each value of N I want to take say 100 samples of the integral then using numpy calculate the average and standard deviation for my final result. I will need to use arrays to store the values in. I'm just not sure how to do this. This is my code so far. I know it's wrong in the middle where the correct code needs to go. Thanks in advance!
CODE:
# this is the integral from 0 to 2 of cos(x)^2
# using monte carlo integration
# the errors are also calculated
import numpy.random as rnd;
import numpy as np;
def integrate(n):
return np.sum(pow(np.cos(rnd.uniform(0,1,size=n)),2))/n
for i in range(1,8):
# creating and adding to arrays
measurement=[];# create an empty array or list
measurement.append('intergrate(n)');# add to it
np.average(measurement)
np.std(measurement)
ERROR
print(pow(10,i),":", integrate(pow(10,i)),":", ERROR)
I know for each value of N I want to take say 100 samples of the integral then using numpy calculate the average and standard deviation for my final result. I will need to use arrays to store the values in. I'm just not sure how to do this. This is my code so far. I know it's wrong in the middle where the correct code needs to go. Thanks in advance!
CODE:
# this is the integral from 0 to 2 of cos(x)^2
# using monte carlo integration
# the errors are also calculated
import numpy.random as rnd;
import numpy as np;
def integrate(n):
return np.sum(pow(np.cos(rnd.uniform(0,1,size=n)),2))/n
for i in range(1,8):
# creating and adding to arrays
measurement=[];# create an empty array or list
measurement.append('intergrate(n)');# add to it
np.average(measurement)
np.std(measurement)
ERROR
print(pow(10,i),":", integrate(pow(10,i)),":", ERROR)