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$?
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 toNIntegrate
, then theNIntegrate
integrals are computed highly accurately. $\endgroup$NIntegrate[ Boole[(x + y < 1 && x - y < 1) || (x + y > 0 && x - y > 0)], {x, -2, 2}, {y, -2, 2}]
for an equivalent formulation ofIntegrate
over the union. $\endgroup$NIntegrate
without any precision options (such asMaxRecursion
,PrecisionGoal
andMethod
) gives the highest accurate result? $\endgroup$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 theNIntegrate
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$