# Plot3D and NIntegrate issues

f[x_, y_] := 2*x - y

Plot3D[f[x, y], {x, -1*Sqrt[4 - y^2], Sqrt[4 - y^2]}, {y, -2, 2}]

NIntegrate[f[x, y], {x, -1*Sqrt[4 - y^2], Sqrt[4 - y^2]}, {y, -2, 2}]


I get a

Plot3D::plln: Limiting Value -Sqrt[4-y^2] in {x,-Sqrt[4-y^2],Sqrt[4-y^2]} is not a machine-sized real number.

And a

NIntegrate::nlim:x=-Sqrt[4-y^2] is not a valid limit of integration.

I get these same errors with the limits of x: 3*y, 3 and 2-y, 2*y-1 (in different problems).

What is causing this? Is this a bug?

• Reverse the limits Plot3D[f[x, y], {y, -2, 2}, {x, -1*Sqrt[4 - y^2], Sqrt[4 - y^2]}] Oct 14, 2014 at 0:26
• The integral can be done symbolically: Integrate[f[x, y], {y, -2, 2}, {x, -1*Sqrt[4 - y^2], Sqrt[4 - y^2]}] gives 0 Oct 14, 2014 at 4:23
• Indeed, Integrate[2*x - y, {x, -1*Sqrt[4 - y^2], Sqrt[4 - y^2]}, {y, -2, 2}] gives 0 , while NIntegrate with the same content returns an error meaasge on my machine Win7 Mma10.0. It looks like a bug for me. Oct 14, 2014 at 10:33
• Why would it be a bug? The iterated integral takes the outermost range first, and while Integrate can handle a symbolic y, NIntegrate (and Plot3D) cannot. May 26, 2015 at 19:28
• not a bug, but it is a deficiency in the Plot3D docs that fail to mention that order matters if the range of one variable depends on the value of another. May 26, 2015 at 20:17

While belisarius's comment answers the question, an arguably better way to achieve these is to use regions. For example, the plot is less choppy and there is less rounding error when integrating (for this example at least).

(* without regions *)
f[x_, y_] := 2*x - y

(* choppy plot *)
Plot3D[f[x, y], {y, -2, 2}, {x, -1*Sqrt[4 - y^2], Sqrt[4 - y^2]}] (* messages thrown *)
NIntegrate[f[x, y], {y, -2, 2}, {x, -1*Sqrt[4 - y^2], Sqrt[4 - y^2]}]

During evaluation of NIntegrate::slwcon: Numerical integration converging too slowly; suspect one of the following: singularity, value of the integration is 0, highly oscillatory integrand, or WorkingPrecision too small. >>

During evaluation of NIntegrate::eincr: The global error of the strategy GlobalAdaptive has increased more than 2000 times. The global error is expected to decrease monotonically after a number of integrand evaluations. Suspect one of the following: the working precision is insufficient for the specified precision goal; the integrand is highly oscillatory or it is not a (piecewise) smooth function; or the true value of the integral is 0. Increasing the value of the GlobalAdaptive option MaxErrorIncreases might lead to a convergent numerical integration. NIntegrate obtained -1.16877*10^-16 and 7.359246377230905*^-16 for the integral and error estimates. >>

-1.16877*10^-16

(* with regions *)
f[x_, y_] := 2*x - y

(* less choppy *)
Plot3D[f[x, y], {x, y} ∈ Disk[{0, 0}, 2]] (* no rounding error *)
NIntegrate[f[x, y], {x, y} ∈ Disk[{0, 0}, 2]]

0.

• …tho, since the region of interest is a disk, an appropriate change of coordinates is likely to yield a more expedient and efficient solution. May 27, 2015 at 6:33

Just to illustrate versatility of Mathematica:

Plot3D[2 x - y, {x, -2, 2}, {y, -2, 2},
RegionFunction -> Function[{x, y, z}, x^2 + y^2 < 4]]
f = TransformedField["Cartesian" -> "Polar",
2 x - y, {x, y} -> {r, t}];
j = Simplify[Det[Outer[D[#1, #2] &, {r Cos[t], r Sin[t]}, {r, t}]]];
Integrate[f j, {r, 0, 2}, {t, 0, 2 Pi}]


where the plot uses RegionFunction, f transforms plane into polar coordinates, j` is Jacobian.