I am trying to define some custom function that evaluates the numeric integral of some complicated function. The problem is that I would like to include as an input the integration limits. A MWE follows

f[x_,y_]:= Exp[-2 x^2 y^2]
test[x_?NumericQ,IntL_]:= NIntegrate[f[x, y],IntL]
myIntL1= {y,-4 x^2,4x^2};
myIntL2= {y,-4 x^4,4x^4};

Then, if I evaluate for instance test[3,myIntL1] I get a problem concerning an invalid limit of integration.

Is there a clever way to fix this without defining several functions including the integration limits, such as

    test1[x_?NumericQ,IntL_]:= NIntegrate[f[x, y],{y,-4 x^2,4x^2}]
    test2[x_?NumericQ,IntL_]:= NIntegrate[f[x, y],{y,-4 x^4,4x^4}]


Of course, here everything looks simple, but in my case all the functions are rather long; some even purely numeric ones. Since I have multiple choices for the integration limits, it would be more practical to avoid defining test1[x_], test2[x_], ..., testN[x_]-

Thanks in advance, Pablo

Edited: Something that would work would be setting

f[x_,y_]:= Exp[-2 x^2 y^2]
myIntL1[x_,y_]:= {y,-4 x^2,4x^2};
myIntL2[x_,y_]:= {y,-4 x^4,4x^4};
test[x_?NumericQ,IntL_]:= NIntegrate[f[x, y],Evaluate[IntL[x,y]]]

Then, f[number,MyIntL1(2,...)], works. Still, it would be interesting to know if there is an alternative shorter and more elegant fixing.

  • 1
    $\begingroup$ The function is actually numeric (including Interpolate[]), so Integrate[] is not viable. I thought that, perhaps, using Hold[] somewhere might cause to evaluate the integration limits before and then replacing for x, but it didn't. Surprisingly, the kind of construction outlined above worked once, but then stopped. $\endgroup$
    – pablo
    Apr 30 at 12:48
  • $\begingroup$ The strategy used by Table and its ilk is something like this: test[x0_?NumericQ, IntL_] := Block[{x = x0}, NIntegrate[f[x, y], IntL]]. However, Table requires that the variable x be specified in its arguments. $\endgroup$
    – Michael E2
    May 1 at 12:10
  • $\begingroup$ Thanks @MichaelE2!! That's exactly what I was looking for. I just tested in the (more complicated) function I had to integrate and worked prefectly! $\endgroup$
    – pablo
    May 3 at 6:04

Finally, the suggestion from Michael E2 worked perfectly. The solution was to use Block[] as follows:

test[x0_?NumericQ, IntL_] := Block[{x = x0}, NIntegrate[f[x, y], IntL]]

This allowed for a simple implementation finally.

Thanks a lot! Pablo


Maybe, like this

test1[x_, g_] := NIntegrate[f[#, y], {y, -g[#], g[#]}] &[x];

test1[2, 4 #^4 &]
(* 0.626657 *)

One can also approach like this:

test[x_, n_] := 
 With[{x1 = x}, Integrate[Exp[-2 x1^2 y^2], {y, -4 x1^n, 4 x1^n}]]


test[1, 4]

(*  Sqrt[\[Pi]/2] Erf[4 Sqrt[2]]  *)

% // N

(*  1.25331  *)


test[2, 4]

1/2 Sqrt[\[Pi]/2] Erf[128 Sqrt[2]]

% // N


However, I do not find any difference between, say, test[2, 2] and test[2, 4]:

(test[2, 2] - test[2, 4]) // N[#, 100] &

N::meprec: Internal precision limit $MaxExtraPrecision = 50.` reached while evaluating 1/2 Sqrt[\[Pi]/2] Erf[32 Sqrt[2]]-1/2 Sqrt[\[Pi]/2] Erf[128 Sqrt[2]].

(* 0.*10^-150  *)

Have fun!

  • $\begingroup$ Thanks a lot! Actually, the first option seems quite interesting, but for the (more complicated) function I have to use, turns out to be not very useful in the end. Of course, this was not apparent from the MWE above. The comment by Michael E2 is however perfect. Thanks anyway! $\endgroup$
    – pablo
    May 3 at 6:03

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.