我有两个表格数据数组,x 和 y,但我不知道生成数据的函数。我希望能够计算数据在 x 轴上任意点生成的直线的积分。
与其对数据插值一个分段函数,然后尝试对其进行积分,这让我遇到了麻烦,有什么我可以使用的东西可以通过评估数组来简单地提供积分吗?
在搜索解决方案时,我看到了对 iPython 和 Pandas 的引用,但我无法找到这些包中有助于完成此任务的部分。
如果没有简单集成阵列的方法,您能否就处理此任务的最佳方法提供一些建议?
最佳答案
Scipy has some nice tools to perform numerical integration.
例如,您可以使用 scipy.integrate.simpson
执行辛普森法则,您可以通过以下方式传递给它:
scipy.integrate.simps(y, x=None, dx=1, axis=-1, even='avg')
Parameters :
y : array_like Array to be integrated.
x : array_like, optional If given, the points at which y is sampled.
dx : int, optional Spacing of integration points along axis of y. Only used when x is None. Default is 1.
axis : int, optional Axis along which to integrate. Default is the last axis.
even : {‘avg’, ‘first’, ‘str’}, optional
‘avg’ : Average two results:1) use the first N-2 intervals with a trapezoidal rule on the last interval and 2) use the last N-2 intervals with a trapezoidal rule on the first interval.
‘first’ : Use Simpson’s rule for the first N-2 intervals with a trapezoidal rule on the last interval.
‘last’ : Use Simpson’s rule for the last N-2 intervals with a trapezoidal rule on the first interval.
因此您可以使用您的两个数组进行数值积分。
关于python - 如何在 Python 中集成两个一维数据数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17602076/