[英]use scipy.integrate.simps or similar to integrate three vectors
I want to approximate a function that I do not have an actual analytical expression for. 我想近似一个我没有实际解析表达式的函数。 I know that I want to compute this integral: integral a * b * c dx
. 我知道我想计算该积分: integral a * b * c dx
。 Pretend that I get the a
, b
, and c
are from observed data. 假设我从观察到的数据中得到a
, b
和c
。 How can I evaluate this integral? 我如何评估这个积分? Can scipy
do this? scipy
可以scipy
做吗? Is scipy.integrate.simps
the right approach? 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?'
With your setup, the correct way to integrate is either 通过您的设置,正确的集成方式是
simps(a*b*c, x) # function values, argument values
or 要么
simps(a*b*c, dx=dx) # function values, uniform spacing between x-values
Both yield the same result. 两者产生相同的结果。 Yes, simps
is a very good choice for integrating sampled data. 是的, simps
是集成采样数据的很好选择。 Most of the time it is more accurate than trapz
. 在大多数情况下,它比trapz
更准确。
If the data comes from a smooth function (even though you don't know the function), and you can somehow make the number of points to be 1 more than a power of 2, then Romberg integration will be even better. 如果数据来自平滑函数(即使您不知道该函数),并且可以以某种方式使点数比2的幂大1,则Romberg集成会更好。 I compared trapz
vs simps
vs quad
in this post . 我在这篇文章中比较了trapz
, simps
和quad
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.