使用 scipy.integrate.simps 或类似的方法来整合三个向量

use scipy.integrate.simps or similar to integrate three vectors

我想逼近一个我没有实际解析表达式的函数。我知道我想计算这个积分:integral a * b * c dx。假设我得到的 abc 来自观察数据。我如何评估这个积分? scipy能做到吗? scipy.integrate.simps 是正确的方法吗?

import numpy as np
from scipy.integrate import simps

a = np.random.random(10)
b = np.random.uniform(0, 10, 10)
c = np.random.normal(2, .8, 10)
x = np.linspace(0, 1, 10)
dx = x[1] - x[0]

print 'Is the integral of a * b * dx is ', simps(a * b, c, dx), ', ', simps(b * a, c, dx), ',', simps(a, b * c, dx), ', ', simps(a, c * b, dx), ', or something else?'

根据您的设置,正确的集成方式是

simps(a*b*c, x)   # function values, argument values

simps(a*b*c, dx=dx)   # function values, uniform spacing between x-values

两者产生相同的结果。是的,simps是整合采样数据的一个很好的选择。大多数时候它比 trapz 更准确。

如果数据来自平滑函数(即使你不知道这个函数),并且你可以通过某种方式使点数为 1 大于 2 的幂,那么 Romberg integration will be even better. I compared trapz vs simps vs quad in this post .