Hi everyone!
I need to integrate a function in a 4D region (x1,y1,x2,y2), which explodes whenever x1=x2&&y1=y2. The code is as following:
{Lx, Ly} = {1/50, 1/50};
f1[x_, y_, n_, m_] := Cos[n*Pi*(Lx + x)/(2*Lx)]*Sin[m*Pi*(Ly + y)/(2*Ly)];
f2[x_, y_, n_, m_] := Sin[n*Pi*(Lx + x)/(2*Lx)]*Cos[m*Pi*(Ly + y)/(2*Ly)]*m*Lx/(n*Ly);
dist[x1_, y1_, x2_, y2_] := Sqrt[(x1 - x2)^2 + (y1 - y2)^2];
int[f_, n_, m_, i_, j_] := int[f, n, m, i, j] =
NIntegrate[f[x1, y1, n, m]*f[x2, y2, i, j]/dist[x1, y1, x2, y2],
{y1, -Lx, Lx}, {x1, -Lx, Lx}, {y2, -Ly, Ly}, {x2, -Lx, Lx}]
Then, I want to evaluate int
using either f1
or f2
, for positive integer (n,m,i,j)
, for instance using AbsoluteTiming[int[f1,1,2,1,2]]
. I may need to compute these integrals thousands of times, for the different possible combinations of (n,m,i,j)
- in the most complex cases, each of them may reach up to 14, for instance. So I must be able to calculate the integral as fast as possible.
However, the method is simply not working so well, for some. When I compute, for instance: int[f1,4,1,1,1]
, the computation simply lasts for ever and I receive some scary error messages:
NIntegrate::errprec: Catastrophic loss of precision in the global error estimate due to insufficient WorkingPrecision or divergent integral.
I have tried multiple integration strategies and methods, but the thing that seems to work best is to simply define the integration boundaries using {y1,-Lx,Lx},{x1,-Lx,Lx},{y2,-Ly,y1,Ly},{x2,-Lx,x1,Lx}
, taking advantage of the integration order to define the singularity locations.
The integration method "DuffyCoordinates" performed quite fast, but I didn't know whether I was correctly using the "Corners" option, e.g.:
int2[f_,n_,m_,i_,j_] := NIntegrate[f[x1, y1, n, m]*f[x2, y2, i, j]/dist[x1, y1, x2, y2],
{y1, -Lx, Lx}, {x1, -Lx, Lx}, {y2, -Ly, Ly}, {x2, -Lx, Lx},Method -> {"DuffyCoordinates","Corners" -> {0, 0, 1, 1}}]
... But it also gets stuck for (n,m,i,j)=(4,1,1,1)
. So I don't know what else to do:
- Am I doing something wrong with this couple of integration strategies, or missing something obvious?
- I suspect much of the problems are due to the integral actually evaluating to 0, maybe because the functions are somehow odd (whatever that means in 4D?), and I'm integrating them in a symmetric interval. But I don't know how I could define my function
int
in a way that it would check whether the integrand is odd in this 4D symmetrical integration volume.