I want to compute the integral of the following integrand
((0.000245587 + 0.0000651908 I) E^(-I kx)
kx ky (kz^2 - 4 \[Pi]^2))/((kx^2 + ky^2) kz) - ((4.00033*10^-17 -
3.51178*10^-18 I) E^(-I kx) (kx^3 kz + kx ky^2 kz + kx^2 kz^2 +
4 ky^2 \[Pi]^2))/((kx^2 + ky^2) kz)
Where $k = 2 \pi$.
For this I have the following code:
SetAttributes[withTransforms, HoldAll];
withTransforms[code_] :=
Block[{kx = k*Sin[a]*Cos[b],
ky = k*Sin[a]*Sin[b], kz = k*Cos[a]},
code]
And the symbolic integration:
IntoS[int_] :=
withTransforms[
Integrate[
int*k^2*Sin[a]*Cos[a], {a, 0, Pi/4}, {b, 0, 2*Pi}]]
And finally, the numerical:
IntoN[int_] :=
withTransforms[
NIntegrate[
int*k^2*Sin[a]*Cos[a], {a, 0, Pi/4}, {b, 0, 2*Pi}]]
Now, IntoS
, the symbolic integration doesn't give a result within 5 minutes (after which I have aborted the evaluation) on my core 2 duo. I need to compute many of these integrals for a plot and if it takes several minutes already to evaluate one point that won't be very useful.
Furthermore, IntoN
throws
NIntegrate::slwcon: Numerical integration converging too slowly; suspect one of the following: singularity, value of the integration is 0, highly oscillatory integrand, or WorkingPrecision too small.
at me. How do I integrate this function?
Edit Scaling coefficients does not really work. I always get that some coefficients are quite large and others very small. This way, the integration never works. I can delete the "small" terms manually but that it not really automatic.
Edit 2: Mathematica is unable to compute the following integral numerically:
(E^(I kx) kx ky (-1 + kz^2))/(Sqrt[2] (kx^2 + ky^2) kz)
Symbolically this integral is $0$. However, I do need the numerical computation as the symbolic computation can take quite long and I need to plot the end-result. This is a special case of the term
(E^(I (kx x + ky y + kz z)) kx ky (-1 + kz^2))/(Sqrt[2] (kx^2 +
ky^2) kz)
And I want to plot in $x$ (with $y, z = 0$ for example) and this integral is sometimes impossible to compute symbolically.
Edit 3: I have "fixed" it by writing my own numerical method:
Trapezoidal2D[f_, {x_, a0_, b0_}, {y_, c0_, d0_}, m0_] :=
Module[{h = (b0 - a0)/m0, k = (d0 - c0)/m0, a = N[a0], b = N[b0],
c = N[c0], d = N[d0], i, j, m = m0, X, Y},
Subscript[X, k_] = a + i h;
Subscript[Y, j_] = c + j k;
Return[
1/4 h k (Function[{x, y}, f][a, c] + Function[{x, y}, f][b, d] +
Function[{x, y}, f][a, d] + Function[{x, y}, f][b, d] +
2 Sum[Function[{x, y}, f][Subscript[X, i], c], {i, 1,
m - 1}] +
2 Sum[Function[{x, y}, f][Subscript[X, i], d], {i, 1,
m - 1}] +
2 Sum[Function[{x, y}, f][a, Subscript[Y, j]], {j, 1,
m - 1}] +
2 Sum[Function[{x, y}, f][b, Subscript[Y, j]], {j, 1,
m - 1}] +
4 Sum[Function[{x, y}, f][Subscript[X, i], Subscript[Y,
j]], {i, 1, m - 1}, {j, 1, m - 1}])];];
withTransforms[int]
before passing to(N)Integrate
... otherwise, it doesn't make sense to call that on the output ofNIntegrate
, which is just a number. It could possibly change how your integral behaves, but certainly will change your answer. $\endgroup$IntoN
it gives for the same integrand the same result for the unmodifiedIntoS
. I'm not sure what you mean with "change your answer". $\endgroup$k
is supposed to be (I changedNIntegrate
tonintegrate
to see what would the integrand would be, and ak
is left in there). I also tried settingk=1
and plotting the real and imaginary parts of the integrand and see no obvious problems; but perhaps I am looking at the wrong integrand. $\endgroup$Plot3D[Im[withTransforms[intgnd]],{a, 0, Pi/4},{b, 0, 2*Pi}]
(hereintgnd
is the first expression in your question), and see no problem (also, no problem is evident with its real part). $\endgroup$