I'm trying to evaluate an integral (numerically would be good enough), but since the integrand contains the same expression several times, I'd like to speed it up by defining a local variable that locally evaluates that expression. Specifically, I write the following

F[σ_] := Integrate[
   Module[{a1x = x, a2x = x^σ},
      If[.2 a1x >= a2x, a1x, a2x]
   {x, 0, 1}


This returns the result

(0.2 + 0.8 x) x

When really, it should be


(After all, F is simply integrating $x$ over $[0,.2]$ and $x^2$ over $(.2,1]$.

Why is that? And how can it be fixed?

Edit: In this specific case, NIntegrate gives me the desired result, albeit with a bunch of warnings. In my actual example however, it just runs without end... and then outputs the result with local variables of the type


So I need to be able to somehow force Mathematica to evaluate the a1x expression FIRST each time it tries to figure out an integrand...


1 Answer 1


Use Piecewise to define a function that is used as the argument to the integral.

p[x_, σ_] := Piecewise[
   {x, x <= 0.2},
   {x^σ, x > 0.2}

Now we will use the funciton p as the function to be integrated

fp[σ_] := Integrate[p[x, σ], {x, 0, 1}]

(* 0.350667 *)

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.