Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If I define


and then integrate,say


I get what I expect, namely 4.

But if I do the same thing with a different function

f[x_] := If[x < -1, 1/(2x^2), If[x > 1, 1/(2x^2), 0]]

and if I then integrate

Integrate[f[x], {x, -3, 7}]

I do not, as expected, get 16/21; instead I just get an expression consisting of an integral sign with -3 and 7 at the limits, followed by the definition of f just as I typed it above (with all the If statements, etc) and then a dx.

I can of course break the integral into two parts, either of which Mathematica handles perfectly well. But how do I get it to evaluate this expression without my manual intervention?

share|improve this question
Instead of using If, construct the function using Piecewise. – Szabolcs Mar 5 '14 at 1:52
Using Piecewise is better, but an alternative is Integrate[PiecewiseExpand@f[x], {x, -3, 7}], which converts the function to Piecewise. – Michael E2 Mar 5 '14 at 1:58
@Szabolcs: I hadn't known about Piecewise, and apparently the very old version of Mathematica that I use ( doesn't know about it either. Of course I shouldn't expect that others will tailor their answers to my ancient software, so I do thank you for the suggestion, though it doesn't seem to work for me. – WillO Mar 5 '14 at 3:09

In Mathematica 9.0 the second integral evaluates to 16/21...

share|improve this answer
Thanks for this info. Maybe it's time to upgrade. – WillO Mar 5 '14 at 6:54

You can use Piecewise to define your piecewise functions. For example your second example could be defined as follows:

f[x_] := Piecewise[{{1/(2 x^2), Abs[x] > 1}, {0, True}}]


Integrate[f[x], {x, -3, 7}]

yields 16/21

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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