Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating the area under a curve given a set of coordinates, without knowing the function

I have one list of 100 numbers as height for Y axis, and as length for X axis: 1 to 100 with a constant step of 5. I need to calculate the Area that it is included by the curve of the (x,y) points, and the X axis, using rectangles and Scipy. Do I have to find the function of this curve? or not? ... almost all the examples I have read are about a specific equation for the Y axis. In my case there is no equation, just data from a list. The classic solution is to add or the Y points and multiple by the step X distance... using Scipy any idea?

Please, can anyone recommend any book which focusing on numerical (finite elementary) methods, using Scipy and Numpy? ...

like image 462
user1640255 Avatar asked Nov 10 '12 07:11

user1640255


People also ask

How do you find the area under a curve manually?

The area under a curve between two points is found out by doing a definite integral between the two points. To find the area under the curve y = f(x) between x = a & x = b, integrate y = f(x) between the limits of a and b. This area can be calculated using integration with given limits.


1 Answers

The numpy and scipy libraries include the composite trapezoidal (numpy.trapz) and Simpson's (scipy.integrate.simps) rules.

Here's a simple example. In both trapz and simps, the argument dx=5 indicates that the spacing of the data along the x axis is 5 units.

import numpy as np from scipy.integrate import simps from numpy import trapz   # The y values.  A numpy array is used here, # but a python list could also be used. y = np.array([5, 20, 4, 18, 19, 18, 7, 4])  # Compute the area using the composite trapezoidal rule. area = trapz(y, dx=5) print("area =", area)  # Compute the area using the composite Simpson's rule. area = simps(y, dx=5) print("area =", area) 

Output:

area = 452.5 area = 460.0 
like image 159
Warren Weckesser Avatar answered Sep 27 '22 21:09

Warren Weckesser