既然你只知道數據點,最好的選擇是使用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