I'm trying to use Monte Carlo integration to approximate the region under a given graph to calculate its area. For this to work, I need the calculated y_min and y_max to be accurate. So as an example I'll use the graph of sin(x)
from 0 to pi. To find y_min and y_max I have the following function:
def y_range(f, x_min, x_max, n=100):
# Step size
h = float((x_max - x_min)) / n
# Calculate y for n points between x_min and x_max
y = [f(x * h) for x in range(0, n + 1)]
# Get minimum and maximum y
y_max = max(y)
y_min = min(y)
return y_min, y_max
Printing y_min and y_max gives:
y_max = 1.0
y_min = -3.21624529935e-16
I know y_min should equal 0.0, so how do I rectify this inaccuracy?