2
$\begingroup$

In working with interpolating functions and integration, I noticed a lot of differences between NIntegrate and Integrate in the case of a function applied to an interpolating function. In order to provide a simple example, look at this output:

ifun = Interpolation[ {{0, 0}, {1, 1}, {2, 3}, {3, 4}, {4, 3}, {5, 0}}]
NIntegrate[x*ifun[x], {x, 0, 5}]
32.5694
xifun = FunctionInterpolation[x*ifun[x], {x, 0, 5}]

FunctionInterpolation::precbd: Requested precision [Infinity] is not a machine-sized real number between \$MinPrecision and \$MaxPrecision. >>

out = Integrate[xifun[x], {x, 0, 5}];
N[out]
32.591

The difference is at the second decimal place, or of the order of 0.06%. For more complicated functions (lots of data values and Bessel functions, I get errors of more than 100%).

I would like to know the reason of the error. In my real-life example I only get correct answers using the NIntegrate method.

I know there is this other question: How to integrate functions of linearly interpolated data?

But they don't use FunctionInterpolation and I think that there should be a simple explanation for this difference. My data is also not linearly-interpolated.

$\endgroup$
3
$\begingroup$

Give this a whirl:

ifun = Interpolation[{{0, 0}, {1, 1}, {2, 3}, {3, 4}, {4, 3}, {5, 0}}];
out1 = NIntegrate[x*ifun[x], {x, 0, 5}];

xifun = FunctionInterpolation[x*ifun[x], {x, 0, 5, .01}];
out2 = Integrate[xifun[x], {x, 0, 5}];

Row[{out1, N[out2]}, "    "]

(*  32.5694    32.5694  *)
$\endgroup$
  • $\begingroup$ This third argument appears with color red in my Mathematica 9 version. That means that it shouldn't go there. I also didn't find it in the documentation. I tried it anyway and it indeed works for the more complicated case! What is it, the precision? $\endgroup$ – Santi Mar 4 '14 at 13:04
  • $\begingroup$ @Santi. The syntax highlighter isn't infalible. It sometimes rejects a form that works -- maybe because the the function involved has been recently changed and the syntax editor didn't get updated with the change. I've encountered this with functions other than FunctionInterpolation. Go with what works. $\endgroup$ – m_goldberg Mar 4 '14 at 13:43
  • 1
    $\begingroup$ @Santi Evaluation of xifun["Grid"] // Differences reveals that the third argument is just the sampling step. $\endgroup$ – Alexey Popkov Mar 4 '14 at 16:34
  • 1
    $\begingroup$ @Santi: Sorry for delay responding, needed sleep! As Alexy said, it is sample step - I discovered this some time ago, figured it was common knowledge, sorry for not explicitly defining it and causing confusion. $\endgroup$ – ciao Mar 4 '14 at 21:16
  • 1
    $\begingroup$ FunctionInterpolation::precbd error will be surpressed if one changes the range to be machine precision number i.e. {x,0,5} to be {x,0.,5} (@Santi). Very odd. $\endgroup$ – luyuwuli Mar 22 '16 at 11:58

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.