Answer using the numerical integration was provided here https://mathematica.stackexchange.com/a/276353/88922

I want to NIntegrate and plot a highly oscillatory function, but get errors which I don't know how to resolve. Also, the integration converges very slowly.

I have a function

$\mathrm{eA} = 0.0579484\cdot \int dk_x \, dk_y \, dk_z \frac{\cos[-3.2 \cdot k_x] \cos[-0.999957 \cdot t \cdot k_z]}{0.000086\cdot k_z^2 + k_x^2 +k_y^2}$

which I want to integrate numerically and plot over $t$. I write

eA[t_] := (0.0579484*NIntegrate[(Cos[-3.2*kx]*Cos[-0.999957*t*kz])/(0.000086*kz^2+kx^2+ky^2),{kx,-60 Pi,60 Pi},{ky,-60 Pi,60 Pi},{kz,-60 Pi,60 Pi}])

where I don't integrate from $-\infty$ to $\infty$ because I find $-60\pi$ to $60\pi$ to be a sufficient boundary.

When I plot it

Plot[eA[t], {t, -0.2, 0.2}, PlotRange -> All]

I get

enter image description here

which for some reason is the exact same result as when I plotted it for the $-14 \pi$ to $14 \pi$. Also, this result doesn't match a reference one.

The errors that I get are

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.

NIntegrate::eincr: The global error of the strategy GlobalAdaptive has increased more than 2000 times. The global error is expected to decrease monotonically after a number of integrand evaluations. Suspect one of the following: the working precision is insufficient for the specified precision goal; the integrand is highly oscillatory or it is not a (piecewise) smooth function; or the true value of the integral is 0. Increasing the value of the GlobalAdaptive option MaxErrorIncreases might lead to a convergent numerical integration. NIntegrate obtained 110.14634155042681 and 0.001241352644141132 for the integral and error estimates.

A known singularity is when $k_x = k_y = k_z = 0$, but I'm not sure how to exclude it.

The reference plot is for

eAref[t_]:=1.14386/Sqrt[0.000880621 + 0.999914 t^2]

and gives

enter image description here



The solution https://mathematica.stackexchange.com/a/275768/88922 worked and I acquired the result that I needed for which I thank the author very much.

However, I encountered some problems when I modified the integral by multiplying the numerator by $\cos(k_y \cdot R_y)$

f1[u_,Rx_,Rz_]=Assuming[Rx\[Element]Reals&&Rx!=0&&Rz\[Element]Reals&&u>0,Integrate[(Cos[kx Rx] Cos[ky Ry]Cos[kz Rz])/(kx^2+ky^2+u*kz^2),{kz,-\[Infinity],\[Infinity]},{ky,-\[Infinity],\[Infinity]},{kx,-\[Infinity],\[Infinity]}]]

which resulted in

enter image description here

The program integrated over only $dk_x$ and left the rest untouched.

Also, when I delete the $\cos(k_y \cdot R_y)$, go back to the previous form

f1[u_,Rx_,Rz_]=Assuming[Rx\[Element]Reals&&Rx!=0&&Rz\[Element]Reals&&u>0,Integrate[(Cos[kx Rx]Cos[kz Rz])/(kx^2+ky^2+u*kz^2),{kz,-\[Infinity],\[Infinity]},{ky,-\[Infinity],\[Infinity]},{kx,-\[Infinity],\[Infinity]}]]

and run it I also get

enter image description here

instead of the original result. The calculation then takes much more time this way.

The program works well only if I run freshly copied formula provided by the answer's author. Any ideas why?

  • $\begingroup$ Please post v. $\endgroup$
    – cvgmt
    Commented Nov 8, 2022 at 12:38
  • $\begingroup$ Edited to replace $v$ with a number, sorry $\endgroup$
    – Patrycja
    Commented Nov 8, 2022 at 12:55
  • $\begingroup$ Note that $\int dk_x dk_y dk_z \frac{e^{-i(k_x x + k_y y + k_z z)}}{k_x^2 + k_y^2 + k_z^2} = \frac{2\pi^2}{\sqrt{x^2+y^2+z^2}}$ is a well-known Fourier integral, see this or this. You can reduce your integral to this case by scaling $k_z$ appropriately. All these integral do not actually converge absolutely, the problem being large $k_x,k_y,k_z$, not small $k_x,k_y,k_z$. $\endgroup$
    – user293787
    Commented Nov 9, 2022 at 3:59
  • $\begingroup$ Thank you for your input and valuable information. However, I want to keep this integral in as general form as I can because I'll be modifying it later on for different cases $\endgroup$
    – Patrycja
    Commented Nov 9, 2022 at 8:57

1 Answer 1


Analytic integration is much more stable than numerical integration, especially when infinite and/or multi-dimensional integrals are concerned:

f[u_, x_, z_] = Assuming[x \[Element] Reals && x != 0 && z \[Element] Reals && u > 0,
    Integrate[(Cos[-kx x] Cos[-kz z])/(kx^2 + ky^2 + u*kz^2),
        {kz, -∞, ∞}, {kx, -∞, ∞}, {ky, -∞, ∞}]]
(*    (2 π^2)/Sqrt[u x^2 + z^2]    *)

eA[t_] = 0.0579484*f[0.000086, 3.2, 0.999957 t]
(*    1.14386/Sqrt[0.00088064 + 0.999914 t^2]    *)
  • $\begingroup$ The problem with the analytic integration is that it takes significantly longer to compute than with numerical integration even for small boundaries. I'm pretty sure I need to stick with NIntegrate $\endgroup$
    – Patrycja
    Commented Nov 8, 2022 at 13:23
  • 6
    $\begingroup$ The analytic integration is done once-and-for-all (hence an immediate assignment was used) and every future evaluation of f or eA is instantaneous. Numerical integration must be done for every value of the parameters anew (hence you used a delayed assignment). So what you are saying is only true if you evaluate your eA once; but as you are evaluating it multiple times, analytic integration is much faster. $\endgroup$
    – Roman
    Commented Nov 8, 2022 at 13:36
  • $\begingroup$ Thank you very much for your input, the provided solution worked. If by any chance you have time to look at the post again, where I added a problem appearing after applying the solution, it would be much appreciated. Either way, thanks :) $\endgroup$
    – Patrycja
    Commented Nov 9, 2022 at 9:51
  • $\begingroup$ @Patrycja you interchanged the order of integration; if you go back to the order I suggest, it works. Try to integrate step by step (one dimension at a time) and fiddle with assumptions until it works. $\endgroup$
    – Roman
    Commented Nov 9, 2022 at 10:56

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.