# Problem with numerical integration.(Is numerical integration reliable?) [closed]

I just want to use numerical integration, and I can not trust the results and it seems something goes wrong. the problem is as follow : I have a totally positive two variable function and from elementary high school mathematics, integration over bigger intervals must be greater than smaller intervals, but NIntegration can not realize this fact. for example :

NIntegrate[Exp[-(x - y)^2], {x, -200, 200}, {y, -200, 200}]


but when I integrate over smaller intervals

NIntegrate[Exp[-(x - y)^2], {x, -100, 100}, {y, -100, 100}]


How can I trust to the mathematica, and what to do to fix the problem. thanks

• In both cases NIntegrate gives error messages, you didn't mention. Jan 14, 2021 at 9:31
• Try it with {y, -200, x, 200}. Jan 14, 2021 at 11:24
• You can never "trust" any calculation done using machine precision since there is no attempt to track and control precision. At a minimum, use arbitrary-precision by specifying a WorkingPrecision. Even then, you need to proceed with caution. Jan 14, 2021 at 15:22
• It is always a good practice to first plot the integrand and look where the major contributions to the integral are. Then you can force NIntegrate to divide integration area into some with high contribution and some with low like @MichealE2 proposed or even better like NIntegrate[ Exp[-(x - y)^2], {x, -200, 200}, {y, -200, x - 3, x + 3, 200}]  to get 707.967  very close to the analytical 707.982  Jan 15, 2021 at 4:52
• {y, -200, x, 200} means the integration will be broken up at y == x, where the Exp has a maximum Exp[-(x-y)^2] == 1. Akku14’s breaking up the region just before and after the maximum is more reliable in general. The trouble is with error estimation. When the function is nearly flat almost everywhere, then discrete sampling will probably lead to a very small error estimate unless some sample points land in a region where the function varies rapidly. By dividing the region into subregions where the function changes a lot, the error estimates will be greater and NIntegrate will work harder. Feb 27, 2021 at 0:03

Changing the numerical integration strategy from the default "GlobalAdaptive" to "LocalAdaptive" gives the expected behavior (and results much closer to the expected ones):

NIntegrate[Exp[-(x - y)^2], {x, -100, 100}, {y, -100, 100}, Method -> "LocalAdaptive"]
(* 352.491 *)

NIntegrate[Exp[-(x - y)^2], {x, -200, 200}, {y, -200, 200}, Method -> "LocalAdaptive"]
(* 707.982 *)

• thank you very much. I hope it works for more complicated integrals which I have not analytic form of them. Jan 14, 2021 at 12:26
• You are welcome! It should work; look out however for warnings from Mathematica, that are in general quite reliable in signalling pathologies of your integral Jan 14, 2021 at 12:34
• @Arian: Would you feel like accepting the answer? Thanks! Jan 18, 2021 at 13:33
• Although I got negative votes for my (elementary) question, but your answer was helpful for me. Thank you Jan 18, 2021 at 20:54

First look at the function you want to integrate

Plot3D[Exp[-(x - y)^2], {x, -5, 5}, {y, -5, 5}, AxesLabel -> {x, y}]


The plot shows integral must depend on the range of integration! That's why you cannot expect unique solution.

• integral obviously depend on range of integration, my question was that why integration over bigger interval is smaller than smaller interval. as is shown in figure, our function is positive everywhere. It was a toy example for my original calculation and for complicated cases I don't know what to do. Jan 14, 2021 at 10:09
• @Adrian The NIntegrate- messages gave a hint, you shouldn't trust the results! Try to increase workingprecision (and decrease your integration range, numerical Exp[-100.^2]  isn't easy to calculate). Jan 14, 2021 at 10:30

This integral can be don analytically. Then you do not have to worry about precision. And you see that the exponential term can be safely neglected.

Integrate[Exp[-(x - y)^2], {x, -200, 200}, {y, -200, 200}]


• yeah, it as a gaussian like integral, it was just a toy example to show my problem. My original integral is so complicated than that, and there is no analytical solution. Jan 14, 2021 at 10:44
• If your real integrand is also a function of x-y, then maybe another parametrization like:z=x-y may help. Jan 14, 2021 at 11:01