# Correct definition of a function to be passed to Nintegrate [duplicate]

If I type the following code

τMin0 = 0.1;
τMax0 = 2;
integrand[(τ_)?NumericQ] := {1000/Sqrt[τ^2 - τMin0^2], 0};
2*NIntegrate[Evaluate[integrand[τ][[1]]], {τ, τMin0, τMax0}]
2*NIntegrate[1000/Sqrt[τ^2 - τMin0^2], {τ, τMin0, τMax0}]


I obtain two different values for the same integrals

3.99
7376.51


The first integral is of course not well calculated (I have checked by comparing with the analytical solutions in arccos). I don't know why and what to do to fix it.

• The expression Evaluate[integrand[τ][[1]]] actually evaluates to τ. You need to Hold the expression inside NIntegrate to prevent premature application of Part: NIntegrate[Hold[integrand[τ] [[1]] ], {τ, τMin0, τMax0}]. See this and this. – MarcoB Aug 6 '15 at 0:03

## 1 Answer

Clear[integrand]


With your definition that uses an unnecessary list and then takes a part

integrand[t_?NumericQ] := {1000/Sqrt[t^2 - tMin0^2], 0};

integrand[t][[1]]


t

This occurs because integrand cannot evaluate with a symbolic input so the first part of the unevaluated expression is the function's argument.

Clear[integrand]

tMin0 = 1/10;
tMax0 = 2;
integrand[t_?NumericQ] := 1000/Sqrt[t^2 - tMin0^2];
2*NIntegrate[integrand[t], {t, tMin0, tMax0}]
2*NIntegrate[1000/Sqrt[t^2 - tMin0^2], {t, tMin0, tMax0}]


7376.51

7376.51

Comparing with the exact value

2*Integrate[1000/Sqrt[t^2 - tMin0^2], {t, tMin0, tMax0}]


2000 Log[20 + Sqrt[399]]

% // N


7376.51

• Of course the list is needed in what I want to do. This piece of code is a part of a much bigger one where the list is needed. I have just simplified it for the sake of clarity and to provide the minimum example. – lambertmular Aug 6 '15 at 9:33