【问题标题】:How to find the area under the curve如何求曲线下面积
【发布时间】:2020-03-30 19:01:18
【问题描述】:

我是 Python 初学者。

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

如何求曲线下的两个区域,如图:

黑色区域(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)

【问题讨论】:

  • 什么定义(数学上)A 结束和 B 开始?
  • Y轴上应该是500标记

标签: python python-3.x numpy data-science area


【解决方案1】:

您可以使用辛普森规则或梯形规则来计算图形下的面积,给定一个定期间隔的 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

【讨论】:

  • OP 已经在使用trapz。我认为他在问如何在特定坐标上分割该区域。
  • 是的,我知道需要从 0 积分到 Y = 500 的值。但不清楚如何用值指向文件中的那个字符串。
  • 我知道 Y=500 它在 y_data.txt 文件中的 154 字符串。所以我需要从底部字符串整合到 154?
  • 是的,你应该给出序列,如果我没有从文档中读取是错误的。希望对您有所帮助。
猜你喜欢
  • 2018-10-28
  • 1970-01-01
  • 1970-01-01
  • 2021-01-02
  • 2012-01-29
  • 2018-07-04
  • 1970-01-01
  • 2011-06-24
相关资源
最近更新 更多