既然你只知道数据点,最好的选择是使用trapz
(基于你知道的数据点的梯形逼近积分)。
您很可能不想将您的数据集转换为函数,并且您不需要使用trapz
。
所以,如果我理解正确的话,你想要做这样的事情:
from numpy import *
# x-coordinates for data points
x = array([0, 0.4, 1.6, 1.9, 2, 4, 5, 9, 10])
# some random data: 3 whatever data sets (sharing the same x-coordinates)
y = zeros([len(x), 3])
y[:,0] = 123
y[:,1] = 1 + x
y[:,2] = cos(x/5.)
print y
# compute approximations for integral(dataset, x=0..10) for datasets i=0,1,2
yi = trapz(y, x[:,newaxis], axis=0)
# what happens here: x must be an array of the same shape as y
# newaxis tells numpy to add a new "virtual" axis to x, in effect saying that the
# x-coordinates are the same for each data set
# approximations of the integrals based the datasets
# (here we also know the exact values, so print them too)
print yi[0], 123*10
print yi[1], 10 + 10*10/2.
print yi[2], sin(10./5.)*5.
我可能失去了一些东西,而是一个整体的仅仅是区域中的“曲线”下,这样你就可以只添加了值在每一列中。 – 2011-01-11 19:53:38