I'm trying to perform resolve this exercise:
using the Monte Carlo Method "Box Method".
The code that i implemented is this:
#UNIDIMENSIONAL INTEGRATION
import numpy as np #library for numerical calculations
import matplotlib.pyplot as plt #library for plotting purposes
from scipy import random #needed for generate random number
from sympy import symbols, integrate, exp #needed for integrate function
from scipy.stats import norm #needed for gaussian fit
#*******************************************************************************
def f(x,n): #definition of the function to integrate
return x**n
#*******************************************************************************
for i in range(1, 6): #for cycle over the period
x = symbols('x') #needed for the integration
print("The exact mathematical value of the integral with dimension N", i, "is:", integrate(f(x,i),(x, 0,1)).evalf(2), "\n")
print("************************************************************************* \n")
#*******************************************************************************
N = 10**3 #number of point generated, statistics
for j in range(1,6): #for loop over the D dimensions
ans = 0 #variable ans
list_ans = [] #list to store all the values for plotting.
n_below_curve = 0 #variable n_below_curve
for k in range(N):
for i in range(N):
x0 = np.random.uniform(0,1)
y0 = np.random.uniform(0,j)
if (y0 <= f(x0,j)):
n_below_curve += 1
ans = (n_below_curve/N) * (1*j)
list_ans.append(ans)
print("\nThe distribution of the results of integral with dimension N", j, ".\n")
_, bins, _ = plt.hist(plt_vals, int(np.sqrt(N)), density=True) #sintex to create a histogram from a dataset x with n bins
#and store an array specifying the bin ranges in the variable bins.
mu, sigma = norm.fit(plt_vals) #get the mean and standard deviation of data
best_fit_line = norm.pdf(bins, mu, sigma) #get a line of best fit for the data
print("\n")
print("The distribution of the results of integral with dimension N", n, ".\n")
print("The mean of the distribution is ", mu, ". The sigma of the distribution is", sigma, ".\n")
plt.hist(plt_vals, bins, ec="black", density=True) #compute and draw the histogram of x with n bins
plt.plot(bins, best_fit_line) #plot y versus x as lines and/or markers
plt.grid() #configure the grid lines
plt.xlabel("Results of integral") #set the label for the x-axis
plt.ylabel("N") #set the label for the y-axis
plt.title("Distribution of results of integral") #set a title for the histogram
plt.show() #display all open figures
#*******************************************************************************
But the outputs give 5 distribution with only one bin not empty...
Why this happened?
The code without the distribution part works perfectly, so I suppose that the problem arise when I try to build the histograms...
Thanks in advance.

