# Function providing input and integration limits for NIntegrate

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


etc?

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.

• 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. Apr 30 at 12:48
• 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. May 1 at 12:10
• 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! 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}]]


Then

test[1, 4]

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

% // N

(*  1.25331  *)


Further,

test[2, 4]

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

% // N

0.626657


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!

• 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! May 3 at 6:03