将数组与scipy集成在一起。

时间:2018-07-12 14:43:29

标签: python-2.7 scipy

我正在尝试集成数据数组,但是有界限。因此,我计划使用simps(scipy.integrate.simps)。因为simps本身不支持边界,所以我决定只将要集成的数据供其使用。但这会导致奇怪的结果,其结果是预期结果的两倍。

我在做什么错,或者我想念什么,或者误会了?

# -*- coding: utf-8 -*-
from scipy import integrate
from scipy import interpolate
import numpy as np
import matplotlib.pyplot as plt

# my data
x = np.linspace(-10, 10, 30)
y = x**2

# but I only want to integrate from 3 to 5
f = interpolate.interp1d(x, y)
x_selection = np.linspace(3, 5, 10)
y_selection = f(x_selection)

# quad returns the expected result
print 'quad', integrate.quad(f, 3, 5), '<- the expected value (includig error estimation)'
# but simps returns an uexpected result, when using the selected data
print 'simps', integrate.simps(x_selection, y_selection), '<- twice as big'
print 'trapz', integrate.trapz(x_selection, y_selection), '<- also twice as big'

plt.plot(x, y, marker='.')
plt.fill_between(x, y, 0, alpha=0.5)

plt.plot(x_selection, y_selection, marker='.')
plt.fill_between(x_selection, y_selection, 0, alpha=0.5)
plt.show()

Windows7,python2.7,scipy1.0.0

1 个答案:

答案 0 :(得分:0)

simps()和trapz()的参数顺序错误。

  

您已经翻转了调用参数; simps和trapz首先期望y维度,然后是x维度(根据文档)。更正此问题后,应获得类似的结果。请注意,您的示例函数接受了一个平凡的解析反导数,它的评估成本要低得多。

– N. Wouda