1
$\begingroup$

Imagine that I have two conditions con1 and con2 over the two-dimensional domain $x,y\in[-2,2]$. I want to compute the area of the union of these two conditions i.e. the region $(con1 )\cup(con2)$. I can use the two methods given below

con1 = ImplicitRegion[x + y < 1 && x - y < 1, {{x, -2, 2}, {y, -2, 2}}];
con2 = ImplicitRegion[x + y > 0 && x - y > 0, {{x, -2, 2}, {y, -2, 2}}];

method1 =Integrate[1, {x, y} \[Element] RegionUnion[con1, con2]]
(*23/2*)
con1 = (NIntegrate[
     Boole[  x + y < 1 && x - y < 1], {x, -2, 2}, {y, -2, 2}, 
     Method -> "MonteCarlo", MaxRecursion -> 20, 
     PrecisionGoal -> 10] // FullSimplify);
con2 = (NIntegrate[
     Boole[  x + y > 0 && x - y > 0], {x, -2, 2}, {y, -2, 2}, 
     Method -> "MonteCarlo", MaxRecursion -> 20, 
     PrecisionGoal -> 10] // FullSimplify);

method2 = con1 + con2
(*11.9218*)

My question: The first method gives an exact answer which is $23/2=11.5$. How can I modify the precision of the second code (I have to use the second code since the first code does not work for the real conditions I have) so that I can get the most accurate/reliable numerical result which should be very close to $11.5$?

$\endgroup$
4
  • 1
    $\begingroup$ Have you considered that the regions overlap?: RegionPlot[{x + y < 1 && x - y < 1, x + y > 0 && x - y > 0}, {x, -2, 2}, {y, -2, 2}]. Otherwise, if you get rid of all the options to NIntegrate, then the NIntegrate integrals are computed highly accurately. $\endgroup$
    – Michael E2
    May 19 at 23:57
  • 2
    $\begingroup$ Try NIntegrate[ Boole[(x + y < 1 && x - y < 1) || (x + y > 0 && x - y > 0)], {x, -2, 2}, {y, -2, 2}] for an equivalent formulation of Integrate over the union. $\endgroup$
    – Michael E2
    May 19 at 23:58
  • $\begingroup$ @MichaelE2 Thanks; then, do you mean that NIntegrate without any precision options (such as MaxRecursion , PrecisionGoal and Method) gives the highest accurate result? $\endgroup$
    – Martha97
    May 20 at 8:03
  • 1
    $\begingroup$ I meant it gives a very highly accurate result. The error of the NIntegrate in my comment is 2 ulp (one bit in the next to last place), a relative error of about $3\times10^{-16}$. The error of the NIntegrate in my answer is zero, which is more accurate, obviously. (Side remark: the Monte Carlo method is used when only a few digits of accuracy and nothing else works. It is a last resort.) $\endgroup$
    – Michael E2
    May 20 at 15:48

1 Answer 1

3
$\begingroup$

This gives the exact result:

NIntegrate[
 Boole[(x + y < 1 && x - y < 1) || (x + y > 0 && x - y > 0)],
 {x, -2, 2}, {y, -2, 2},
 Exclusions -> {x + y == 1, x - y == 0}]

(*  11.5`  *)

I'd say the original question was a simple mistake, but using the Exclusions option is not obvious and yields the most accurate answer possible in this case.

$\endgroup$

Your Answer

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