# Two different results of NIntegrate in a unit cube

The problem to solve is as follows. In a unit cube $$\left(x,y,z\right) = (0,1)^2$$ find a volume of a shape produced by following inequlity: $$2 x^2 + y (1 + z) + x (z-y-2)<0$$

Eventually, I need an exact result. However, my approach seems to be wrong for some reason I don't see. If I write

NIntegrate[If[2 x^2 + y (1 + z) + x (-2 - y + z) < 0, 1, 0], {x, 0, 1}, {y, 0, 1}, {z, 0, 1}]


I get $$0.205431$$. On the other hand, if I solve for $$z$$ in the inequality, i.e.

$$z < \frac{(1-x) (2 x-y)}{x+y}$$

and treat the problem as a double integral, i.e.

NIntegrate[If[0 < ((1 - x) (2 x - y))/(x + y) < 1, ((1 - x) (2 x - y))/(x + y), 0], {x, 0, 1}, {y, 0, 1}]


I get the result $$0.181523$$.

Why these results are different? I would be very greatful.

• How is it possible? Since the coefficient by $z$ is $(x+y)$, which is allways positive in the unit square so we can divide by it in the inequality? Nov 12, 2020 at 12:07
• Your first sentence is very confusing! What is "unit cube $(x,y,z)=(0,1)^2$". Nov 12, 2020 at 18:58

Better use Boole instead of If here:

NIntegrate[Boole[2 x^2 + y (1 + z) + x (-2 - y + z) < 0], {x, 0, 1}, {y, 0,1}, {z, 0,1}]
(*0.205431*)


Beneath the inequality z< … don't forget to require 0<z<1! This gives

NIntegrate[Boole[0 < z < 1], {x, 0, 1}, {y, 0, 1}, {z,0, -(((-1 + x) (2 x - y))/(x +y))}]
(*0.205431*)


The reduction of the second integral in your question to a double integral is unclear.

By using a region you can abstract the details of the integration away:

R = ImplicitRegion[2 x^2 + y (1 + z) + x (-2 - y + z) < 0,
{{x, 0, 1}, {y, 0, 1}, {z, 0, 1}}];

RegionMeasure[R, 3] // FullSimplify
(*    3/2 + Log[(81 Sqrt)/512]    *)

% // N
(*    0.205431    *)


If you solve $$z$$ by $$x$$ and $$y$$,then the integral should separate into three parts.

Reduce[2 x^2 + y (1 + z) + x (-2 - y + z) < 0 && 0 <= x <= 1 &&
0 <= y <= 1 && 0 <= z <= 1, z]

(0 < x <= 1/
2 && ((0 <= y < (-x + 2 x^2)/(-2 + x) &&
0 <= z <= 1) || (y == (-x + 2 x^2)/(-2 + x) &&
0 <= z < 1) || ((-x + 2 x^2)/(-2 + x) < y < 2 x &&
0 <= z < (2 x - 2 x^2 - y + x y)/(x + y)))) || (1/2 < x < 1 &&
0 <= y <= 1 && 0 <= z < (2 x - 2 x^2 - y + x y)/(x + y))


So we need to integral the three parts respectively.

{NIntegrate[(-x + 2 x^2)/(-2 + x), {x, 0, 1/2}, {z, 0, 1}],
NIntegrate[(2 x - 2 x^2 - y + x y)/(
x + y), {x, 0, 1/2}, {y, (-x + 2 x^2)/(-2 + x), 2 x}],
NIntegrate[(2 x - 2 x^2 - y + x y)/(x + y), {x, 1/2, 1}, {y, 0, 1}]}
% // Total

{0.0239076, 0.0788078, 0.102715}
0.205431