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

发布于 2025-01-11 01:37:07 字数 1799 浏览 0 评论 0原文

我正在尝试集成一个具有实数和复数值的表达式,将其定义为 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)

如果有人可以帮助我。

I am trying to integrate an expression that has real and complex values defining it as a lambda expression. The integration variable is kx and the resulting solution of the integral will be evaluated in x and y dimensions, but after I integrate and try to evaluate the integral I get the following error:

  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'

This is the code I am using:

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)

I will be grateful if someone can help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肥爪爪 2025-01-18 01:37:07

一些事情

  • 您忘记了最后一个 lambda 中的逗号,
  • 您的 lambda 有三个参数,四元对第一个参数进行积分,您必须使用 args=(x,y) 传递其他参数。您的示例中的集成限制为 -100*k+100*k
  • 有一些 ^ 是预期的 **
  • 四边形返回一个带有积分值和积分误差的元组,因此您对输出的第一个元素感兴趣,可以使用 [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)

A few things

  • You forgot a comma in the last lambda
  • your lambda has three arguments, quad integrates over the first argument, you have to pass the other arguments with args=(x,y). The limits of integration in your example are -100*k to +100*k.
  • there were some ^ where ** was expected.
  • The quad returns a tupple with integral value and integral error, so you are interested in the first element of the output, you can get it with the [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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文