scipy.integrate.trapz 和不连续函数 [英] scipy.integrate.trapz and discontinuous functions
问题描述
函数 scipy.integrate.trapz
使用 Newton-Cotes 1 阶公式,如 scipy 文档中所述.然而,在推导这个公式时,通常假设
The function scipy.integrate.trapz
uses Newton-Cotes formula of order 1 as it said in the scipy documentation. However, in the derivation of this formula it is usually assumed that
- 被积函数是一个连续函数,并且
- 已知被积函数值的点是不同的.
然而,我试图逼近函数 f:[0,2] --> 的积分;[0,2]
,由 f(x) = 0 定义,如果 x <1 else 2
通过调用
However, I tried to approximate the integral of the function f:[0,2] --> [0,2]
, defined by f(x) = 0 if x < 1 else 2
by calling
scipy.integrate.trapz([0, 0, 2, 2], [0, 1, 1, 2])
并获得正确的结果(2.0
).在上层调用中,
and obtained the right result (2.0
). In the upper call,
- 被积函数不是连续函数,并且
- 已知被积函数值的点不明显.
这个hack"能否以示例中提供的方式安全地使用?
Can this "hack" be safely used in the way presented in the example?
(对于每个不连续点x
,在点列表中插入两次x
,将被积函数的左右极限插入到值列表.)
(For each point of discontinuity x
, insert x
twice in the list of points, and insert the left and right limit of the integrand into the corresponding places in the list of values.)
推荐答案
在 trapz
中重复 x 值的效果与在该值处拆分积分区间并应用 trapz
分别到每个部分:
The effect of repeating an x-value in trapz
is the same as splitting the interval of integration at that value, and applying trapz
separately to each part:
from scipy.integrate import trapz
trapz([0, 0, 2, 2], [0, 1, 1, 2])
trapz([0, 0], [0, 1]) + trapz([2, 2], [1, 2])
两个结果是一样的.事实上,对像您这样的分段函数进行积分的最佳方法是在不连续点处分割积分区间,因为这样您就可以对两个连续函数进行积分.你的方法是正确的.
Both results are the same. And indeed, the best way to integrate a piecewise function like yours is to split the interval of integration at the discontinuity, because then you are integrating two continuous functions. Your approach is correct.
从某种意义上说,您的hack"是不必要的.梯形规则收敛于任何黎曼可积函数的正确积分值,涵盖所有分段连续函数等.教科书只给出连续函数的证明是教科书作者的选择,希望有一个更简单的证明.可以证明一个更一般的定理.
In some sense, your "hack" isn't necessary. The trapezoidal rule converges to the correct value of the integral for any Riemann-integrable function, which covers all piecewise continuous functions and more. That a textbook presents its proof for continuous functions only is the choice of the textbook's author, wishing to have a simpler proof. A more general theorem could be proved instead.
也就是说,对于实践中出现的任何函数,只要 x 值形成积分区间的足够细的划分,梯形规则的输出将接近其积分.例如,如果您选取足够多的点,均匀间隔的点将始终有效.
That is, for any function arising in practice, the output of the trapezoidal rule will be close to its integral provided that the x-values form a sufficiently fine partition of the interval of integration. For example, uniformly spaced points will always work if you take sufficiently many.
然而,在实践中人们关心的是收敛速度,当函数具有不连续性时,收敛速度明显更差.从这个实用的角度来看,将每个不连续点包括两次,以 y 值作为左右界限,显着提高了梯形规则的准确性.
However, in practice one is concerned about the speed of convergence, which is noticeably worse when the function has a discontinuity. From this practical point of view, including each point of discontinuity twice, with y-values being the left and right limits, is a significant boost for the accuracy of trapezoidal rule.
这篇关于scipy.integrate.trapz 和不连续函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!