为了账号安全,请及时绑定邮箱和手机立即绑定

python 中无限极限的集成

python 中无限极限的集成

波斯汪 2023-07-18 10:24:39
我在(0,无限)限制内积分时遇到困难。我正在尝试做这个 Jupyter 笔记本(不知道这是否是相关信息)。代码如下:import numpy as npimport matplotlib.pyplot as pltimport scipy as scimport mathfrom scipy.integrate import quaddef 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 IntK1IgK0(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当我使用 (0,0) 值作为 (m,n) 时,尽管存在一些我不明白的错误,但至少我得到了一些答案。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)那么,我做错了什么?
查看完整描述

1 回答

?
繁星淼淼

TA贡献1775条经验 获得超11个赞

错误很明显——你遇到了溢出,一切都崩溃了。这是由于您的功能很快就出现了分歧:


>>> [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)。


查看完整回答
反对 回复 2023-07-18
  • 1 回答
  • 0 关注
  • 87 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信