3
$\begingroup$

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

F[2]

This returns the result

(0.2 + 0.8 x) x

When really, it should be

0.350667

(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

a1x$12776

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

$\endgroup$
2
$\begingroup$

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

fp[2]
(* 0.350667 *)
$\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.