gpt4 book ai didi

python - 如何求曲线下面积

转载 作者:太空宇宙 更新时间:2023-11-03 20:01:33 25 4
gpt4 key购买 nike

我是一名 Python 初学者。

曲线 (x, y) 有两个数据文件:https://drive.google.com/open?id=1ZB39G3SmtamjVjmLzkC2JefloZ9iShpO

如何求曲线下的两个面积,如图:enter image description here

黑色区域(A)和红色区域(B)

我只知道如何找到总面积:

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)

最佳答案

您可以使用辛普森规则或梯形规则来计算给定定期间隔的 y 值表的图形下的面积。

from scipy.integrate import simps
from numpy import trapz

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

关于python - 如何求曲线下面积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59202053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com