简体   繁体   English

使用scipy.integrate.simps进行集成

[英]Integrating using scipy.integrate.simps

I'm trying to learn about the scipy package and I came across something that I just cannot understand. 我正在尝试了解scipy包,我遇到了一些我无法理解的东西。

from scipy.integrate import simps
import numpy as np
def f1(x):
    ...:     return x**2
x = np.array([1,3,4])
y1 = f1(x)
I1 = integrate.simps(y1,x)
print(I1)
21.0

This corresponds exactly to 这完全对应于

1 4 x 2 dx = 21, ∫1 4×2 DX = 21,

what I don't get is the x = np.array([1, 3, 4]) line. 我没有得到的是x = np.array([1, 3, 4]) why do we need the 3 here? 为什么我们需要3? 1 and 4 are the limits of the integral so what is 3 then? 1和4是积分的极限,那么3是什么? can someone explain that to me please? 有人可以向我解释一下吗?

The documentation of scipy.integrate.simps says: scipy.integrate.simps文档说:

y : array_like

    Array to be integrated.

x : array_like, optional

    If given, the points at which y is sampled.

These are the points at which the function was sampled. 这些是对函数进行采样的点。 As you do not pass the function to be integrated directly to the algorithm, you have to provide sample points. 由于您没有将要直接集成的函数传递给算法,因此必须提供样本点。 The second array gives the x-location of the y-values y1 that you calculated in the previous line. 第二个数组给出了您在上一行中计算的y值y1的x位置。 Although some implementations of numerical integration methods take the integrand function directly, they will always create sample points like you provide here. 虽然数值积分方法的某些实现直接采用了整数函数,但它们总是会像您在此处提供的那样创建样本点。

So the array x is not the integration interval, although its max and min give the interval. 因此,数组x 不是积分间隔,​​尽管它的最大值和最小值给出了间隔。

In general for any numerical integration algorithm a higher number of sample points, distributed over the integration interval, will increase the accuracy of the numerical result and only 3 points will almost surely result in a very poor approximation. 通常,对于任何数值积分算法,在积分区间上分布的更多数量的采样点将增加数值结果的准确度,并且仅3个点几乎肯定会导致非常差的近似。

However in your example the integrand is a simple polynomial of order 2. Such are easy to integrate (analytically as well as numerically). 但是在您的示例中,被积函数是2阶的简单多项式。这样很容易集成(分析和数字)。 The algorithm you are using with scipy.integrate.simps is Simpson's rule , which is based on expanding the integrand up to order 2. Therefore this method is able to solve your sample integral exactly. 您使用scipy.integrate.simps的算法是Simpson的规则 ,它基于将被积函数扩展到2阶。因此,此方法能够精确地求解您的样本积分。

To fully define a second order polynomial you need to specify 3 coefficients and to be able to derive these the algorithm needs to know at least 3 points of the second order polynomial. 要完全定义二阶多项式,您需要指定3个系数,并且为了能够推导出这些系数,算法需要知道二阶多项式的至少3个点。 An additional fourth point however would not give any more information because the curve is already fully specified by three points. 然而,另外的第四点不会提供更多信息,因为曲线已经由三个点完全指定。 This is the reason, why in this example 3 points are sufficient to give the exact result. 这就是为什么在这个例子中为什么3分足以给出确切的结果。

If you do not provide the list x with sample location the result will be in general wrong, as the spacing will be assumed to be 1 between the individual y-values in y1 . 如果不为列表x提供样本位置,则结果通常是错误的,因为在y1各个y值之间的间距将被假定为1。 (see documentation link above) (见上面的文档链接)

Also as a side note using Python 2.7 with Numpy 1.6 and Scipy 0.10 the result of your code above is 20.75 , probably because the type of x is assumed to be integer. 另外作为使用Python 2.7与Numpy 1.6和Scipy 0.10的旁注,上面代码的结果是20.75 ,可能是因为x的类型被假定为整数。 Explicitly stating they are float with 明确说明他们是浮动的

x = np.array([1.0,3.0,4.0])

resolved this issue and the result is always exactly 21.0 . 解决了这个问题,结果总是正好是21.0 You can also see that the actual middle value doesn't matter as long as it is between 1.0 and 4.0 . 您还可以看到实际的中间值无关紧要,只要介于1.04.0之间即可。

它可能会将您的域划分为区间,并且其近似恰好是准确的,因为您的被积函数是低次多项式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM