# Simplifying a numerical integral involving a variable integration limit

I have a one-variable function basically consisting of an integral whose limits depend on the variable —I mean, something like this:

$$F(x)=\int_0^{L(x)}{f(x,t)}dt$$

with

$$L(x)=5x$$   and   $$f(x,t)=t+x^2$$,

for instance (actually, the expressions for $$L$$ and $$f$$ are much bigger and more complex).

So, what I want is to define such a function in Mathematica, as a single independent function that doesn't call or refer to any other function.

I tried something like this:

integrand[t_, x_] = t + x^2
limit[x_] = 5*x
myfunc[x_] = NIntegrate[integrand[t, x], {t, 0, limit[x]}]


and I get this:

Out[108]= t + x^2

Out[109]= 5 x

During evaluation of In[108]:= NIntegrate::nlim: t = 5. x is not a valid limit of integration.

During evaluation of In[108]:= NIntegrate::nlim: t = 5. x is not a valid limit of integration.

Out[110]= NIntegrate[integrand[t, x], {t, 0, limit[x]}]

while I was expecting something like

NIntegrate[t + x^2, {t, 0, 5*x}]

In case it was not clear enough, I was just looking for a way to create a new function (myfunc) out from other previous expressions (integrand and limit), in such a way that everything is contained by the new function (I mean, without references to other functions).
• integrand[t_, x_] := t + x^2; limit[x_] := 5*x; myfunc[x_] := NIntegrate[integrand[t, x], {t, 0, limit[x]}]; Aug 19, 2021 at 17:06
• @flinty This is not what I am asking. If I do that, and then I write myfunc[x], I do not get NIntegrate[t + x^2, {t, 0, 5*x}] (what I want) but NIntegrate[integrand[t, x], {t, 0, limit[x]}]. Aug 19, 2021 at 17:16
• What you are asking for won't work because NIntegrate must take numerical limits. You must Hold it somehow then: integrand[t_, x_] = t + x^2; limit[x_] = 5*x; myfunc[x_] := HoldFirst[NIntegrate[integrand[t, x], Evaluate@{t, 0, limit[x]}]] Aug 19, 2021 at 17:37
• @flinty: The HoldFirst doesn't seem to be necessary. The code runs fine without it on my machine, and if it's there the output is (for example) myfunc[1] = HoldFirst[17.5]. Aug 19, 2021 at 18:17
• @MichaelSeifert yeah it runs fine for numbers, but try with a symbol e.g myfunc[x] - you will get a message NIntegrate::nlim: t = 5. x is not a valid limit of integration. - which is why you need the HoldFirst - adding it returns HoldFirst[NIntegrate[integrand[t, x], {t, 0, 5 x}]] instead, which has the 5 x in the limit and is closer to what OP wants. Aug 21, 2021 at 10:22