简体   繁体   中英

How to find the area under the curve

I am a beginner Python user.

There are two data files for the curve (x, y): https://drive.google.com/open?id=1ZB39G3SmtamjVjmLzkC2JefloZ9iShpO

How to find two areas under the curve, as shown in Figure: 在此处输入图片说明

Black area (A) and red area (B)

I`m only know to how find total area:

from scipy.integrate import trapz

with open('./x_data.txt', 'rt') as f:
    x_file = f.read()

with open('./y_data.txt', 'rt') as f:
    y_file = f.read()

xlist = []
for line in x_file.split('\n'):
    if line: 
        xlist.append(float(line.strip()))
ylist = []
for line in y_file.split('\n'):
    if line: 
        ylist.append(float(line.strip()))

if len(xlist) != len(ylist):
    print(len(xlist), len(ylist))
    raise Exception('X and Y have different length')

xData = np.array(xlist)
yData = np.array(ylist)

area = trapz(y = yData, x = xData)
print("area =", area)

You can use Simpsons rule or the Trapezium rule to calculate the area under a graph given a table of y-values at a regular interval.

from scipy.integrate import simps
from numpy import trapz

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM