尝试将表达式与lambda集成时出现问题



我正在尝试集成一个具有实值和复数值的表达式,该表达式将其定义为lambda表达式。积分变量是kx,积分的最终解将在x和y维度上进行评估,但在我积分并尝试评估积分后,我得到以下错误:

File "/opt/tools/anaconda/2020.11/lib/python3.8/site-packages/scipy/integrate/quadpack.py", line 351, in quad
retval = _quad(func, a, b, args, full_output, epsabs, epsrel, limit,
File "/opt/tools/anaconda/2020.11/lib/python3.8/site-packages/scipy/integrate/quadpack.py", line 463, in _quad
return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
TypeError: <lambda>() missing 2 required positional arguments: 'x' and 'y'

这是我正在使用的代码:

import numpy as np
from scipy import integrate
import matplotlib.pyplot as plt

"Constants and parameters"
f = 8500            # Source frequency [Hz]
rho = 1.225         # Density of air [kg/m^3]
c0 = 343            # Speed of sound [m/s]
omega = 2*np.pi*f   # Angular velocity [rad/s]
k = omega/c0        # Wave number [rad/m]
Z = -426            # Impedance
"Domain parameters"
Lx = 0.1                        # Total x-dimension [m]
Ly = 0.1                        # Total y-dimension [m]
nx = 50                         # Number of points to discretize the domain in x
ny = int(nx/2)                  # Number of points to discretize the domain in y

integrandReal = lambda kx, x, y: np.real(((2*np.sqrt(k**2 - kx**2)*Z)/(np.sqrt(k**2 - kx**2)*Z + omega*rho))*((np.exp(1j*(kx*x + np.sqrt(k**2 - kx**2)*y)))/(np.sqrt(k^2 - kx^2))))
integrandImag = lambda kx, x, y: np.imag(((2*np.sqrt(k**2 - kx**2)*Z)/(np.sqrt(k**2 - kx**2)*Z + omega*rho))*((np.exp(1j*(kx*x + np.sqrt(k**2 - kx**2)*y)))/(np.sqrt(k**2 - kx**2))))
integral = lambda x, y: integrate.quad(integrandReal, -100*k, 100*k) + 1j*integrate.quad(integrandImag-100*k, 100*k)
G = integral(1,1)

如果有人能帮助我,我将不胜感激。

  • 您在最后一个lambda中忘记了逗号
  • 您的lambda有三个参数,quad在第一个参数上集成,您必须用args=(x,y)传递其他参数。示例中的集成限制是-100*k+100*k
  • 存在一些预期CCD_ 5的CCD_
  • quad返回一个具有整数值和积分误差的元组,因此您对输出的第一个元素感兴趣,可以使用[0]获得它
integrandReal = lambda kx, x, y: np.real(((2*np.sqrt(k**2 - kx**2)*Z)/(np.sqrt(k**2 - kx**2)*Z + omega*rho))*((np.exp(1j*(kx*x + np.sqrt(k**2 - kx**2)*y)))/(np.sqrt(k**2 - kx**2))))
integrandImag = lambda kx, x, y: np.imag(((2*np.sqrt(k**2 - kx**2)*Z)/(np.sqrt(k**2 - kx**2)*Z + omega*rho))*((np.exp(1j*(kx*x + np.sqrt(k**2 - kx**2)*y)))/(np.sqrt(k**2 - kx**2))))
integral = lambda x, y: integrate.quad(integrandReal, -100*k, 100*k, args=(1,1))[0] + 1j*integrate.quad(integrandImag,-100*k, 100*k, args=(1,1))[0]
G = integral(1,1)

最新更新