1
$\begingroup$

I have a long analytical process where I have defined a stand in function Int[] to act as a placeholder for either NIntegrate[] or Integrate[]. This way I can do algebra on equations that involve integrals without having Mathematica try to actually do the integration. However, at the end when I place in some numbers and leave some parameters to be defied later I always get a warning messages. Below is a very simple example that demos the issue:

After the analytical work say I'm left with:

fun = 42 Int[Exp[-(x/a)^2] + b, {x, 0, 5}]

I now want to create a function with the parameters a and b

NewFun[a_, b_] := Evaluate[fun /. {Int -> NIntegrate}]

I get the warning: NIntegrate::inumr

Is there a better way to replace Int with NIntegrate then define my function? I realize I could just turn off the message but that feels like cheating.

$\endgroup$
1
$\begingroup$

You have to pass numeric-parameters a,b to function fun!

Try

fun[a_?NumericQ, b_?NumericQ] := 42 Int[Exp[-(x/a)^2] + b, {x, 0, 5}]
NewFun[a_, b_] := fun[a, b] /. {Int -> NIntegrate}
NewFun[1,1]
(*247.222*)
$\endgroup$
1
  • $\begingroup$ In my process I have to start with fun=stuff but I can do funaux[a_?NumericQ,b_?NumericQ]:=Evaluate[fun]; then use funaux. But when I look at your ?NewFun it still has the replacement to do once called. Is there not a way to get the replacement done in the definition of NewFun ? $\endgroup$ – c186282 Dec 17 '20 at 17:28
0
$\begingroup$

Inactivate and Activate

f[a_, b_] := Inactivate[Integrate[a x + b, {x, 0, 5}], Integrate]
f[3, 4] // Activate
$\endgroup$
0

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.