Say I have two lists called x
and y
, both which contain numbers (coordinates), say that:
x = [0, 1, 2, 3, 4, 4, 5]
y = [0, 1, 3, 3, 5, 6, 7]
I would need to calculate the area under the curve that is formed when combining these two lists to (x, y) coordinates. I don't really understand how to create a function that could calculate the area out of this information.
i.e.
def integrate(x, y):
""" x & y = lists, area_under_curve = float value of the area """
area_under_curve = 0
last_x = x[0]
last_y = y[0]
for cur_x, cur_y in list(zip(x, y))[1:]:
## some code here
return area_under_curve
As was mentioned, using the Trapezoid Rule the function is rather simple
def integrate(x, y):
sm = 0
for i in range(1, len(x)):
h = x[i] - x[i-1]
sm += h * (y[i-1] + y[i]) / 2
return sm
The theory behind is to do with the trapezoid area being calculated, with the use of interpolation.