Although there are similar posts here Negative integral of a positive function , Positive integrand giving negative answer ,

my case is even more surprising. I have a very simple function

y[q_, w_] := Module[{res},
 res = If[w > q (2 - q), 0, w];
 If[w > Abs[q (2 - q)] && w < q (2 + q), res = 1 - 1/4 (q - w/q)^2];
 If[w < 0, res = 0];

It looks like this

g1 = Plot[y[0.25, w], {w, 0, 4}, PlotRange -> All]

enter image description here

Numerical and analytical integrals are equal

Integrate[y[0.25, w], {w, 0, 4}]
NIntegrate[y[0.25, w], {w, 0, 4}]

Now let us shift the function

g2 = Plot[y[0.25, w-2], {w, 0, 4}, PlotRange -> All]

enter image description here

and integrate again in the same limits

Integrate[y[0.25, w - 2], {w, 0, 4}]
NIntegrate[y[0.25, w - 2], {w, 0, 4}]

The result is now negative and not equal to the previous value! What's up?

In this particular case i am not so much interested in the origin of such peculiar result, but rather in the practical prescription on how to deal with it.

  • $\begingroup$ I think the result 0.0957031 is also wrong. Redefine your function as Clear[y];y[q_?NumericQ, w_?NumericQ] := ... and run both NIntegrate[y[0.25, w], {w, 0, 4}] and NIntegrate[y[0.25, w - 2], {w, 0, 4}]. $\endgroup$
    – march
    Jun 4, 2016 at 20:04
  • $\begingroup$ @march I see that I get the same result (0.124349) for the w and w-2 cases by forcing the arguments to be numeric, the question is why does that work? $\endgroup$ Jun 5, 2016 at 0:50
  • 1
    $\begingroup$ I think your function doesn't generally do what you intend. Try y[a,b]. It's strange to be using a module for this. Less strange, but more troublesome, is using If instead of Piecewise. $\endgroup$
    – John Doty
    Jun 5, 2016 at 2:16
  • 2
    $\begingroup$ Yes, what @JohnDoty said. Certainly Integrate is not going to work correctly, because y doesn't evaluate correctly symbolically, and Integrate manipulates symbolic expressions. NIntegrate is also failing probably because it does symbolic pre-processing before it does the numerical integration, and so the function doesn't evaluate correctly as an integrand. $\endgroup$
    – march
    Jun 5, 2016 at 5:35
  • $\begingroup$ @march Thank you for your suggestion. I have also explored another route by expressing everything in terms of the "UnitStep" function (this is in line suggestions of @ Jack Lavigne ). So I have solutions in both, numerical and analytical, ways. Should I delete this question as too simple? Can one, actually, learn anything from my question? $\endgroup$
    – yarchik
    Jun 5, 2016 at 13:55

1 Answer 1


There is some confusion using Plot. Compare the plot of the non-evaluated y in the question with the plot of the evaluated y:

Grid[{{Plot[y[0.25, w - 2], {w, 0, 4}, PlotRange -> All, 
    ImageSize -> Medium],
   Plot[Evaluate@y[0.25, w - 2], {w, 0, 4}, PlotRange -> All, 
    ImageSize -> Medium]}}]

enter image description here

We can see that going through the algorithmic steps of defining the value y with symbolic arguments produces an expression that would give different values over the domain of y if using numerical arguments.

In this particular case i am not so much interested in the origin of such peculiar result, but rather in the practical prescription on how to deal with it.

Since in the question both Integrate and NIntegrate are used let us redefine y with Piecewise.

res1 = Piecewise[{{0, w > q (2 - q)}}, w]
res2 = Piecewise[{{1 - 1/4 (q - w/q)^2, 
    w > Abs[q (2 - q)] && w < q (2 + q)}}, res1]
res3 = Piecewise[{{0, w < 0}}, res2]

enter image description here

With that definition we get positive results for the integrals in the question.

Block[{q = 0.25},
 NIntegrate[res3, {w, 0, 4}]

(* 0.124349 *)

Integrate[res3, {w, 0, 4}]

enter image description here

Using IntegrationMonitor

Using the option IntegrationMonitor can help in these kind of situations. In this case we clearly see that the integrant is not what it is expected:

enter image description here

  • $\begingroup$ I'd prolly run //PiecewiseExpand //Simplify on res3 before feeding it to the integration, if I were the OP. $\endgroup$ Jun 5, 2016 at 13:57
  • $\begingroup$ It is a good idea. I did not post the expanded version of res3 because that would be harder to evaluate for correctness of its definition. $\endgroup$ Jun 5, 2016 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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