scipy“积分可能发散,或缓慢收敛”

7xllpg7q  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(81)
R = 0.05; l = 0.1;
q0 = 200; 
alpha = 17.64;
lambda1 = 67.9;

def integral2(x, z, r):
  s = 0;
  if z > 0:
    s = 1
  elif z == 0:
    s = 0.5
  else: 
    s = 0
  B = ((lambda1*math.tan(x) - alpha) * math.exp(2 * math.tan(x) * l)) - ((lambda1*math.tan(x) + alpha) * math.exp((-2) * math.tan(x) * l))
  BZ = ((math.cosh(z+l) * ((lambda1 * math.tan(x) * math.cosh(math.tan(x)*l)) - (alpha * math.sinh(math.tan(x)*l)))) / B) - ((math.sinh(math.tan(x)*z) * s) / math.tan(x))
  result = ((mpmath.j0(r * math.tan(x)) * mpmath.j1(R * math.tan(x))) / (math.pow(math.cos(x),2) * math.pow(math.tan(x),2))) * BZ
  return result

r = 0
resArr = [];
rArr = [];
while r < 1:
  r += 0.01
  r = math.ceil(r*100)/100;
  res, _ = scipy.integrate.quad(integral2, 0, (np.pi / 2) - 0.02741184711388415, args=(l/2, r))
  resArr.append(res*R*q0/lambda1)
  rArr.append(r)

字符串
你好!我想知道为什么我会得到“积分可能发散,或者缓慢收敛。”在这种情况下。有人能帮我吗?
谢谢你,谢谢

8tntrjer

8tntrjer1#

添加所需的导入后,我们可以绘制您的被积函数:

import numpy as np
import mpmath
import math
import matplotlib.pyplot as plt
import scipy.integrate

R = 0.05; l = 0.1;
q0 = 200; 
alpha = 17.64;
lambda1 = 67.9;

def integral2(x, z, r):
  s = 0;
  if z > 0:
    s = 1
  elif z == 0:
    s = 0.5
  else: 
    s = 0
  B = ((lambda1*math.tan(x) - alpha) * math.exp(2 * math.tan(x) * l)) - ((lambda1*math.tan(x) + alpha) * math.exp((-2) * math.tan(x) * l))
  BZ = ((math.cosh(z+l) * ((lambda1 * math.tan(x) * math.cosh(math.tan(x)*l)) - (alpha * math.sinh(math.tan(x)*l)))) / B) - ((math.sinh(math.tan(x)*z) * s) / math.tan(x))
  result = ((mpmath.j0(r * math.tan(x)) * mpmath.j1(R * math.tan(x))) / (math.pow(math.cos(x),2) * math.pow(math.tan(x),2))) * BZ
  return result

r = 0.01
x = np.linspace(1e-6, (np.pi / 2) - 0.02741184711388415, 300)
y = [integral2(xi, l/2, r) for xi in x]
plt.plot(x, y)

字符串
x=0处,有一个奇点。
x1c 0d1x的数据
如果我们放大一点,我们可以看到在x=0.85周围似乎有另一个奇点。



即使它们是可积的,它们对数值积分器来说也是一个挑战。这就是为什么你会得到警告:quad知道它有问题,它让你知道结果可能不可靠。对于scipy.integrate.quad来说,要想处理中间区间奇点,你需要使用points参数来告诉它奇点发生在哪里。不过,我决定走另一条路来进一步研究。
一些求积方案可以处理积分区间端点处的一些奇点。mpmath实现的Tanh-Sinh规则就是这样一种方案,由于您已经使用了mpmath(尽管不必要-scipy.specialj0j1),我想看看mpmath.quad是否可以处理x=0处的奇点。

from mpmath import mp
mp.dps = 50

R = mp.mpf(0.05)
l = mp.mpf(0.1)
q0 = mp.mpf(200.)
alpha = mp.mpf(17.64)
lambda1 = mp.mpf(67.9)

def integrand(x, z, r):
  s = mp.mpf(0.);
  if z > 0:
    s = mp.mpf(1.)
  elif z == 0:
    s = mp.mpf(0.5)

  B = ((lambda1*mp.tan(x) - alpha) * mp.exp(2 * mp.tan(x) * l)) - ((lambda1*mp.tan(x) + alpha) * mp.exp((-2) * mp.tan(x) * l))
  BZ = ((mp.cosh(z+l) * ((lambda1 * mp.tan(x) * mp.cosh(mp.tan(x)*l)) - (alpha * mp.sinh(mp.tan(x)*l)))) / B) - ((mp.sinh(mp.tan(x)*z) * s) / mp.tan(x))
  result = ((mp.j0(r * mp.tan(x)) * mp.j1(R * mp.tan(x))) / (mp.cos(x)**2 * mp.tan(x)**2)) * BZ
  return result

r = mp.mpf(0.01)
l = mp.mpf(l)
# integrate just from 0 to 0.8
res = mp.quad(lambda x: integrand(x, l/2, r), (0, 0.8), method='tanh-sinh')
print(res)


当计算精度加倍时,结果会加倍,所以我不认为它会收敛到正确的答案,你的积分可能不是有限的。

相关问题