【问题标题】:Performing a double integral over a matrix of limits对极限矩阵执行二重积分
【发布时间】:2014-02-14 17:10:41
【问题描述】:

我最近一直在学习如何在 python 中执行双积分。这就是我正在使用的:

myint = dblquad(lambda xa,xb: np.exp(-(xa-xb)**2),-np.inf,x1,lambda x: -np.inf, lambda x: x2)

出于测试目的,我选择了 x1 和 x2 来表示 5 和 10。这似乎可行。

但实际上,我的 x1 = [1,2,3,4,5] 和 x2 = [5,6,7,8,9] 我希望对 x1 和x2 即矩阵。我猜我可以用 2 个 for 循环来做到这一点,但我认为可能有更好的方法。

所以我的问题是 - 我如何在限制矩阵上执行双重积分。

谢谢。


编辑:

我收到以下警告:

UserWarning: The maximum number of subdivisions (50) has been achieved.
If increasing the limit yields no improvement it is advised to analyze 
the integrand in order to determine the difficulties.  If the position of a 
local difficulty can be determined (singularity, discontinuity) one will 
probably gain from splitting up the interval and calling the integrator 
on the subranges.  Perhaps a special-purpose integrator should be used.

这是否意味着它不会收敛?我不太明白这个消息。

当我绘图时:

y = exp(-(x-5)^2)

例如,它看起来就像一条高斯曲线,所以在上面积分没有问题,对吧?是双积分的问题吗?

谢谢。


编辑:

啊,我明白了。感谢 Raman Shah,我现在明白问题所在了。

【问题讨论】:

  • 你确定,这个积分收敛了吗?
  • Stefan 的评论很重要,使用x1,x2 = (5,10) 会发出积分器无法收敛的警告。考虑增加细分的数量。
  • 如果你只集成一个高斯,你应该研究特殊的函数(例如误差函数)来实现它的功能。这可能会更快,更准确。编辑:我认为你有一些分歧,因为 xa = xb 的被积函数是 1 一直回到 -infinity。

标签: python numpy integral


【解决方案1】:

使用 itertools,您可以创建一个限制迭代器来遍历。这本质上是一个双循环,但为了更可扩展,因为您可以使用 itertools.product 获得任意数量的输入,并且您不会一次存储所有限制:

import numpy as np
from scipy.integrate import dblquad
import itertools

f = lambda xa,xb: np.exp(-(xa-xb)**2)
intg = lambda (x1,x2): dblquad(f,-np.inf,x1,
                               lambda x:-np.inf, 
                               lambda x:x2)


X1 = np.arange(1,6)
X2 = np.arange(5,10)
for limit in itertools.product(X1,X2):
    print limit, intg(limit)

如果您需要更快的速度,您可以查看multiprocessing 模块进行并行计算,因为每个进程都是独立的。

【讨论】:

    【解决方案2】:

    为什么不使用 python 的 zip 函数将您希望作为双积分输入的每个元组中的值准确地输入,然后使用 map/apply 对这些离散对进行操作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-27
      • 2015-07-10
      • 2012-07-28
      • 1970-01-01
      • 1970-01-01
      • 2015-03-30
      • 2012-12-03
      相关资源
      最近更新 更多