I'm having an issue integrating numerically with Mathematica. This is a very simple MWE of a much larger calculation, in which I have no option to integrate analytically. I'd like to understand how I can get better results just by numerically integrating.

The setup:

Consider the one-dimensional Gaussian integral, $\int_{-\infty}^\infty e^{-x^2}=\sqrt{\pi}$. I can evaluate it very easily:

both Integrate[Exp[-x^2], {x, -Infinity, Infinity}] and NIntegrate[Exp[-x^2], {x, -Infinity, Infinity}] return the correct result.

The problem:

In my current situation, I have a squared parameter added to the exponent, which shouldn't change the behavior of the integral:

Integrate[Exp[-(x+a^2)^2], {x, -Infinity, Infinity}] still returns $\sqrt{\pi}$.

However, the numerical integration is very sensitive to the value of a. It works for small values of the parameter, but NIntegrate[Exp[-(x+a^2)^2] /. a -> 100, {x, -Infinity, Infinity}] returns 0 and an error. Changing the integration method to LocalAdaptive gets rid of the error but still returns 0 instead of the expected $\sqrt{\pi}\simeq1.77$, even with very high PrecisionGoal or AccuracyGoal. Same goes for most other integration methods I tried, including for example Adaptive(Quasi)MonteCarlo.

I get something nonzero with Method->{"MonteCarloRule","Points"->10^7}, but anything higher than that just crashes my kernel and the calculation is still very far from the expected value of the integral.

Is there any integration strategy to avoid this problem and get an accurate result?


1 Answer 1


NIntegrateneeds some guidance. Splitting the integration range near x==-a^2 gives the correct numerical result

With[{a = 100, 
  b = 10}, {NIntegrate[Exp[-(x + a^2)^2], {x, -Infinity, -a^2 - b}, 
   PrecisionGoal -> 5, AccuracyGoal -> 10], 
  NIntegrate[Exp[-(x + a^2)^2], {x,  -a^2 - b, -a^2 + b}, 
   PrecisionGoal -> 5, AccuracyGoal -> 10 ],
  NIntegrate[Exp[-(x + a^2)^2], {x,  - a^2 + b, Infinity}, 
   PrecisionGoal -> 5, AccuracyGoal -> 10]}]
(*{1.85087*10^-45, 1.77245, 1.85087*10^-45}*)

Hope it helps!

  • $\begingroup$ This helps a lot! A follow up question, since this is not my exact problem but a rough MWE. What is your reasoning behind this approach? Since this is a Gaussian, it's reasonable that most of the information is found around a point, but why is NIntegrate unable to calculate the whole thing? I'm just asking to try to transfer this solution to my actual problem! $\endgroup$
    – Sotiris
    Nov 30, 2022 at 11:00
  • 1
    $\begingroup$ Have a look at the integrand. Only near the mean x=-a^2 it takes nonvanishing values $\endgroup$ Nov 30, 2022 at 11:22
  • 3
    $\begingroup$ (upvote) Also can let NIntegrate know where the problematic point is by explicitly placing it on the path of integration. In[252]:= With[{a = 100}, NIntegrate[Exp[-(x + a^2)^2], {x, -Infinity, -a^2, Infinity}]] Out[252]= 1.77245 $\endgroup$ Nov 30, 2022 at 15:11
  • 1
    $\begingroup$ @Sotiris Given a bounded interval of integration, NIntegrate can sample the value of the function over the entire interval, but when you give it an unbounded interval, NIntegrate must assume that the function eventually goes to zero, and so it searches for where the function takes nonzero values, and integrates until the point where the function becomes vanishingly small (or maybe estimates the integral over the tail based on a rate of decay). It is reasonable for NIntegrate to start looking for nonzero values near $x=0$, but if they are all very small, it will assume the... $\endgroup$
    – Plutoro
    Nov 30, 2022 at 20:28
  • 1
    $\begingroup$ ... whole function is zero because it cannot look for nonzero values forever. By giving it an indication of where to look for nonzero values by splitting the integral or defining a path of integration, it will do the expected thing. As an aside, my wife once had the same problem with a function that was nonzero only near the origin and also in an interval very far from the origin. The integrator only integrated the function near the origin, assuming the function was zero everywhere else. I suggested essentially the same solution as @UlrichNeumann $\endgroup$
    – Plutoro
    Nov 30, 2022 at 20:31

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.