我正试图将振荡表达式与mpmath.quad
积分。积分只有一个变量,但我需要在二维空间中对其进行求值。我需要对定义平面的x
和y
中的一组配位的kx
积分的结果函数进行评估。这就是为什么我将integral(x,y)
评估为2D阵列的单个点中的测试。
这是我正在使用的代码:
import numpy as np
from numpy.lib.scimath import sqrt
from mpmath import *
import matplotlib.pyplot as plt
mp.dps = 15
f = 8500
rho = 1.225
c0 = 343
Omega = 2*np.pi*f
k = Omega/c0
Z = -426
integrandReal = lambda kx, x, y: np.real(((2*sqrt(k**2 - kx**2)*Z)/(sqrt(k**2 - kx**2)*Z + Omega*rho))*((np.exp(1j*(kx*x + sqrt(k**2 - kx**2)*y)))/(sqrt(k**2 - kx**2))))
integrandImag = lambda kx, x, y: np.imag(((2*sqrt(k**2 - kx**2)*Z)/(sqrt(k**2 - kx**2)*Z + Omega*rho))*((np.exp(1j*(kx*x + sqrt(k**2 - kx**2)*y)))/(sqrt(k**2 - kx**2))))
integral = lambda x, y: quad(integrandReal, [-100*k, 100*k], maxdegree=10)[0] + 1j*quad(integrandImag, [-100*k, 100*k], maxdegree=10)[0]
G = integral(0.1,0.1)
当我试图评估任意点的积分时,我会得到以下错误:
Traceback (most recent call last):
File "<ipython-input-87-901e6e5dc630>", line 1, in <module>
integral(1,1)
File "/d/dfg/Documents/ImpedanceModel/GreenImpedance.py", line 55, in <lambda>
integral = lambda x, y: quad(integrandReal, [-100*k, 100*k], maxdegree=10)[0] + 1j*quad(integrandImag, [-100*k, 100*k], maxdegree=10)[0]
File "/opt/tools/anaconda/2020.11/lib/python3.8/site-packages/mpmath/calculus/quadrature.py", line 742, in quad
v, err = rule.summation(f, points[0], prec, epsilon, m, verbose)
File "/opt/tools/anaconda/2020.11/lib/python3.8/site-packages/mpmath/calculus/quadrature.py", line 232, in summation
results.append(self.sum_next(f, nodes, degree, prec, results, verbose))
File "/opt/tools/anaconda/2020.11/lib/python3.8/site-packages/mpmath/calculus/quadrature.py", line 304, in sum_next
S += self.ctx.fdot((w,f(x)) for (x,w) in nodes)
File "/opt/tools/anaconda/2020.11/lib/python3.8/site-packages/mpmath/ctx_mp_python.py", line 944, in fdot
for a, b in A:
File "/opt/tools/anaconda/2020.11/lib/python3.8/site-packages/mpmath/calculus/quadrature.py", line 304, in <genexpr>
S += self.ctx.fdot((w,f(x)) for (x,w) in nodes)
TypeError: <lambda>() missing 2 required positional arguments: 'x' and 'y'
我认为这个错误是因为我没有定义集成中的参数,因为它必须使用scipy.integrate.kquad来完成(正如你在我最后的一篇文章中看到的(,但对于mpmath,我不知道是什么导致了这个错误。
提前感谢您的帮助!
您需要将x
和y
传递给您的Lambda(integrandReal
和integrandImag
(。此外,当您尝试为quad
返回的mpc
对象添加下标时,您的示例还有另一个问题。我删除它是为了得到一个结果,它向您展示了如何通过调用传递x
和y
,但我不能向您保证数字结果是正确的:
integral = lambda x, y: quad(integrandReal, [-100*k, 100*k], [x], [y], maxdegree=10) + 1j*quad(integrandImag, [-100*k, 100*k], [x], [y], maxdegree=10)
G = integral(0.1,0.1)
# Returns: mpc(real='0.0', imag='0.0')