-2
$\begingroup$

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}]

and the answer is 42.4266

but when I integrate over smaller intervals

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

and the answer is 209.479.

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

$\endgroup$
7
  • 4
    $\begingroup$ In both cases NIntegrate gives error messages, you didn't mention. $\endgroup$ Jan 14, 2021 at 9:31
  • 3
    $\begingroup$ Try it with {y, -200, x, 200}. $\endgroup$
    – Michael E2
    Jan 14, 2021 at 11:24
  • 2
    $\begingroup$ 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. $\endgroup$
    – Bob Hanlon
    Jan 14, 2021 at 15:22
  • 1
    $\begingroup$ 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 $\endgroup$
    – Akku14
    Jan 15, 2021 at 4:52
  • 1
    $\begingroup$ {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. $\endgroup$
    – Michael E2
    Feb 27, 2021 at 0:03

3 Answers 3

5
$\begingroup$

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 *)
$\endgroup$
4
  • $\begingroup$ thank you very much. I hope it works for more complicated integrals which I have not analytic form of them. $\endgroup$
    – Arian
    Jan 14, 2021 at 12:26
  • $\begingroup$ 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 $\endgroup$ Jan 14, 2021 at 12:34
  • $\begingroup$ @Arian: Would you feel like accepting the answer? Thanks! $\endgroup$ Jan 18, 2021 at 13:33
  • $\begingroup$ Although I got negative votes for my (elementary) question, but your answer was helpful for me. Thank you $\endgroup$
    – Arian
    Jan 18, 2021 at 20:54
0
$\begingroup$

First look at the function you want to integrate

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

enter image description here

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

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – Arian
    Jan 14, 2021 at 10:09
  • 1
    $\begingroup$ @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). $\endgroup$ Jan 14, 2021 at 10:30
0
$\begingroup$

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}]

enter image description here

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – Arian
    Jan 14, 2021 at 10:44
  • 2
    $\begingroup$ If your real integrand is also a function of x-y, then maybe another parametrization like:z=x-y may help. $\endgroup$ Jan 14, 2021 at 11:01

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