If I define
f[x_]:=If[x<0,-1,1]
and then integrate,say
Integrate[f[x],{x,-3,7}]
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?
If
, construct the function usingPiecewise
. $\endgroup$Piecewise
is better, but an alternative isIntegrate[PiecewiseExpand@f[x], {x, -3, 7}]
, which converts the function toPiecewise
. $\endgroup$