4
$\begingroup$

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][[1]]
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?

$\endgroup$
2
  • 1
    $\begingroup$ 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. $\endgroup$
    – Michael E2
    Dec 8 '20 at 17:35
  • $\begingroup$ 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%. $\endgroup$ Dec 8 '20 at 17:42
4
$\begingroup$

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}]

Mathematica graphics

$\endgroup$
3
  • $\begingroup$ Insightful. I still think OP deserves my “brute force” answer. $\endgroup$ Dec 9 '20 at 1:49
  • 1
    $\begingroup$ @AntonAntonov Certainly, and Reduce will fail sometimes. Both approaches are worth keeping in mind. $\endgroup$
    – Michael E2
    Dec 9 '20 at 2:25
  • $\begingroup$ 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. :) $\endgroup$ Dec 9 '20 at 2:54
5
$\begingroup$

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.)

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.