0

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.

3
  • Did you mean integral = lambda x, y: integrate.quad(integrandReal, -100*k, 100*k, args=(x,y)) + 1j*integrate.quad(integrandImag-100*k, 100*k, args=(x,y)). Commented Mar 2, 2022 at 16:59
  • In the last lambda (integral) you use the first two lambdas (integrandReal and integrandImag -- great names btw) but without any arguments. Commented Mar 2, 2022 at 16:59
  • Thanks to both of you! Commented Mar 2, 2022 at 18:00

1 Answer 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)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Bob for pointing out those errors. I also got another correction in another post and it is due to the complex nature of the integral. To cope with it it is better to use from numpy.lib.scimath import sqrt instead of the common sqrt function of Numpy since it can deal with complex values when the argument in the sqrt is negative. Just in case it can help someone!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.