【问题标题】:Integration with infinite limit in python与python中的无限限制集成
【发布时间】:2020-12-25 13:51:30
【问题描述】:

我在 (0,infinite) 限制内积分时遇到困难。我正在尝试做这个 Jupyter 笔记本(不知道这是否是相关信息)。代码如下:

import numpy as np
import matplotlib.pyplot as plt
import scipy as sc
import math
from scipy.integrate import quad

def integrand3(x,z,m,n):
    return ((np.cosh(x))**m)*((np.sinh(x))**n)*(np.exp(-z*np.cosh(x)))

def IgK0(z,m,n):
    IntK1 = quad(integrand3, 0, 1, args=(z,m,n))
    return IntK1

IgK0(1,1,1)

这给出了结果:

(0.19224739693489237, 2.1343748650136926e-15)

没关系。但是当我用无穷大替换上限时,我得到'nan'作为输出。见以下代码:

def IgKI(z,m,n):
    IntKI = quad(integrand3, 0, np.inf, args=(z,m,n))
    return IntKI

当我对(m,n)使用(0,0)值时,虽然有一些我不明白的错误,但至少我得到了一些答案。

IgKI(1,0,0)

<ipython-input-53-9b9d0956fa85>:2: RuntimeWarning: overflow encountered in cosh
  return ((np.cosh(x))**m)*((np.sinh(x))**n)*(np.exp(-z*np.cosh(x)))
<ipython-input-53-9b9d0956fa85>:2: RuntimeWarning: overflow encountered in sinh
  return ((np.cosh(x))**m)*((np.sinh(x))**n)*(np.exp(-z*np.cosh(x)))
(0.42102443824067737, 1.3628527263018718e-08)

但是当我对 (m,n) 使用任何其他值时,我会得到以下结果:

IgKI(1,0,1)

<ipython-input-53-9b9d0956fa85>:2: RuntimeWarning: overflow encountered in cosh
  return ((np.cosh(x))**m)*((np.sinh(x))**n)*(np.exp(-z*np.cosh(x)))
<ipython-input-53-9b9d0956fa85>:2: RuntimeWarning: overflow encountered in sinh
  return ((np.cosh(x))**m)*((np.sinh(x))**n)*(np.exp(-z*np.cosh(x)))
<ipython-input-53-9b9d0956fa85>:2: RuntimeWarning: invalid value encountered in double_scalars
  return ((np.cosh(x))**m)*((np.sinh(x))**n)*(np.exp(-z*np.cosh(x)))
<ipython-input-76-9bee7a5d6456>:2: IntegrationWarning: The occurrence of roundoff error is detected, which prevents 
  the requested tolerance from being achieved.  The error may be 
  underestimated.
  IntKI = quad(integrand3, 0, np.inf, args=(z,m,n))

(nan, nan)

那么,我做错了什么?

【问题讨论】:

  • 您为什么希望数值积分方法能够在无限极限下工作?这对我来说没有意义。即使存在解析解,在数值上你能做的最好的事情就是尝试几个大数,然后投影一个收敛。

标签: python numpy scipy integration infinity


【解决方案1】:

错误很明显 - 您遇到了溢出,一切都中断了。这是由于您的功能迅速分歧:

>>> [np.cosh(10**x) for x in range(5)]
__main__:1: RuntimeWarning: overflow encountered in cosh
[1.5430806348152437, 11013.232920103324, 1.3440585709080678e+43, inf, inf]

在 1000 处,Python 已经无法计算 cosh(例如)。事实上,将(1,1,1) 集成到 100 是可以的。由于这是数值积分,因此需要计算边界处的值(无穷大也将转换为边界,但您可以只用 1000 进行测试)。正如您从警告中看到的那样,这意味着函数的每个部分都是单独计算的,更不用说您将其乘以幂,然后取幂。

这个库不能满足你的需要。您可以尝试使用 sympy 或 Mathematica 等更专业的工具进行符号集成。

【讨论】:

  • 感谢您花时间回答。我之前在mathematica中尝试过。它在那里工作。也感谢您推荐 sympy。我明白你的意思。那我试试别的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-30
  • 1970-01-01
  • 1970-01-01
  • 2014-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多