计算给定一组坐标的曲线下面积,而需求该函数;
包括复合梯形(numpy.trapz)和辛普森(scipy.integrate.simps)规则。
这是一个简单的例子。在trapz和simps中,参数dx = 5表示数据沿x轴的间距为5个单位。
from __future__ import print_function
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)
输出
area = 452.5
area = 460.0