Given the function (simplified example)

g[En_, k1_] := En^2 *k1

I have to plug it inside another expression, where En is integrated, but k1 remains as a free variable. The only way I found to do that is

fN[function_, k_] :=  NIntegrate[function[En, k]*Exp[-theta*function[En, k]], {theta,0,Pi/2}, {En,1,3}]
fN[g, 0.1]
(*0.921158 *)

But in fact I have many functions like g that need to undergo the same procedure, some with one, two or three k as argument,

g2[En_, k1_, k2_] := En^2 *k1/k2 
g3[En_, k1_, k2_, k3_] := En^2 *k1*k2*k3

Can the function fN be generalised to admit a function with an arbitrary number of arguments k ?


1 Answer 1


You can just use your definition, replacing the k_ with k__, i.e.

fN[function_, k__] :=  NIntegrate[function[En, k]*Exp[-theta*function[En, k]], {theta,0,Pi/2}, {En,1,3}]

That way all parameters you pass to fN will be passed to your function in NIntegrate.

  • $\begingroup$ Thank you, that's a really nice feature I never needed until now $\endgroup$
    – Albercoc
    Jan 23, 2021 at 20:03

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.