# Problem with NIntegrate over a piecewise function

The following code seems to generate wrong results

q2 = 1/y^0.9;
G2 = 1.1 x^0.045;
b2bar = x /. Solve[G2 == 1, x][]
ff = FullSimplify[PiecewiseExpand[D[Min[G2, 1], x]*x*q2]]
NIntegrate[ff, {y, 0, 1}, {x, y, b2bar}]
NIntegrate[ff, {y, 0, 1}, {x, y, 1}]


Since ff=0 for x>=b2bar, one would expect NIntegrate[ff, {y, 0, 1}, {x, y, b2bar}] and NIntegrate[ff, {y, 0, 1}, {x, y, 1}] to generate the same results. But NIntegrate[ff, {y, 0, 1}, {x, y, b2bar}] gives 0.038246 and NIntegrate[ff, {y, 0, 1}, {x, y, 1}] gives 0.022375.

Is there an error with NIntegrate when integrating piecewise functions? How can I avoid this?

• It's not identifying the singularity in the second integral. Adding MinRecursion -> 1 helps. If no one else answers first, I'll try to come back to this. Dec 8 '20 at 17:35
• Voting to close as "described in the documentation", since NIntegrate's messages hint to ways of dealing with the described problem. But I do not feel strongly about it, 65%. Dec 8 '20 at 17:42

As I mentioned in a comment, NIntegrate does solve the condition 1.1 x^0.045 < 1 for the singularity at x == b2bar and this causes a problem with the integration, which is itself an issue. But that issue can be avoided by reducing the condition to something NIntegrate can handle. If we throw in the domain restriction 0 <= x <= 1 && 0 <= y <= 1 from the integral, or just the x component 0 <= x <= 1, then Reduce will solve the condition.

ff = PiecewiseExpand[D[Min[G2, 1], x]*x*q2,
Method -> {"ConditionSimplifier" -> (Quiet[
Reduce[# && 0 <= x <= 1, x, Reals], Reduce::ratnz] &)}]
NIntegrate[ff, {y, 0, 1}, {x, y, 1}] • Insightful. I still think OP deserves my “brute force” answer. Dec 9 '20 at 1:49
• @AntonAntonov Certainly, and Reduce will fail sometimes. Both approaches are worth keeping in mind. Dec 9 '20 at 2:25
• I would like to clarify. I meant that OP “deserves” my less insightful answer because of the way OP suspected that NIntegrate cannot deal with piecewise integrants. :) Dec 9 '20 at 2:54

If your second numerical integration uses appropriate options nearly the same results are obtained.

(Those options can be figured out from the messages NIntegrate issues while evaluating your second integral.)

One such way is:

NIntegrate[ff, {y, 0, 1}, {x, y, 1},
Method -> {"GlobalAdaptive", "MaxErrorIncreases" -> 10^6},
MaxRecursion -> 1000]


Here is another:

NIntegrate[ff, {y, 0, 1}, {x, y, 1}, MinRecursion -> 3]


(See res3 and res4 in the attached screenshot.) 