Error from integration using mpmath quad
Hello all,
I am trying to solve an integral with one variable kx
and then I need the result from that integral to be evaluated in a point defined by x,y
coordinates.
The code I am using is the following:
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], [x], [y]) + 1j*quad(integrandImag, [-100*k, 100*k], [x], [y])
G = integral(0,0)
My problem is that for any value of x
and y
that I use I get:
mpc(real='0.0', imag='0.0')
I appreciate any help to solve this!
Welcome to Ask Sage! Thank you for your question.
The problem may reside in the third and fourth arguments of
quad(integrandImag, [-100*k, 100*k], [x], [y])
. As far as I understand it,mpmath.quad
expects a function as its first argument and integration intervals (possibly subdivided) as arguments after the first ; the intervals are lists of values. The documentation defines what is done for lists of length superior to 2, but not for length one intervals.It is possible that
mpmath
tries ti integrate on the interval[x x]
, which is of null measure ; since your function is finite, this integral is 0."L'enfer est pavé de bonnes intentions." ("Hell is paved with good intents")...
You may try to pass a call to a two-argument functions
x, y
returning a one-argumentxk
function as your first argument...It makes sense what you say. I was trying to apply something similar to how
scipy
works. The problem ofscipy
is that the integration methods it uses don't let me achieve good numerical precision, butmpmath
evidently works differently. I will try what you suggest. Thanks!Mixing
numpy
,scimath
andmpmath
seems to be unsupported. Why numpy ?My mistake! I was just playing before with other expressions and I left
numpy
in the code.