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

时间:2018-01-27 21:45:11

标签: python scipy calculus

我想近似一个我没有实际解析表达式的函数。我知道我想计算这个积分: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?'

1 个答案:

答案 0 :(得分:0)

使用您的设置,正确的集成方式是

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

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

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

如果数据来自平滑函数(即使你不知道函数),并且你可以某种方式使得点的数量比2的幂大1,那么Romberg integration将是甚至更好。我在this post中比较了trapzsimpsquad的对比。

相关问题