2
$\begingroup$

I have an list of data which I interpolate such that it returns the value 0 in case I give it values outside of its domain:

g = Interpolation[Transpose[{{1, 2, 3, 4, 5}, {1, 2, 4, 1, 4}}], 
  "ExtrapolationHandler" -> {0 &, "WarningMessage" -> False}]

Which works fine:

g[0] and g[100] both return 0

However, if I want to integrate this function, I get an error message:

NIntegrate[g[x], {x, 0, 100}]

InterpolatingFunction::dmvali: The integration endpoint 0 in dimension 1 lies outside the range of data in the interpolating function. Extrapolation will be used. InterpolatingFunction::dmvali: The integration endpoint 100 in dimension 1 lies outside the range of data in the interpolating function. Extrapolation will be used.

and the output is some large (incorrect) number.

Is this a bug?

$\endgroup$
  • 1
    $\begingroup$ The behavior is certainly incorrect, but recall that "ExtrapolationHandler" is undocumented functionality, so this will make Support less likely to look at it. As a workaround, just multiply your InterpolatingFunction[] with a Boole[] factor: Integrate[Boole[Between[x, First[g["Domain"]]]] g[x], {x, 0, 100}]. $\endgroup$ – J. M.'s discontentment May 19 at 10:15
3
$\begingroup$

I do not know if it is a bug, or by design NIntegrate does not support these options or does not "look" for them.

Notice they are shown in red color as well. Which means they are not officially known

Mathematica graphics

But you could always do it explicitly

g = Interpolation[Transpose[{{1, 2, 3, 4, 5}, {1, 2, 4, 1, 4}}]];

With[{left = g["Domain"][[1, 1]], right = g["Domain"][[1, 2]]},
 NIntegrate[If[left <= x <= right, g[x], 0], {x, 1, 100}]
 ]

(*8.33333*)

Compare to

 NIntegrate[g[x], {x, 1, 5}] // N
 (*8.33333*)

And compare to

NIntegrate[g[x], {x, 1, 100}] // N
(*3.9804774791666664*^7*)
| improve this answer | |
$\endgroup$

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.