Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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  *)
share|improve this answer
    
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? –  Santi Mar 4 at 13:04
    
@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. –  m_goldberg Mar 4 at 13:43
    
Ok thanks, it definitely worked! But what is exactly that third argument? –  Santi Mar 4 at 16:12
1  
@Santi Evaluation of xifun["Grid"] // Differences reveals that the third argument is just the sampling step. –  Alexey Popkov Mar 4 at 16:34
1  
@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. –  rasher Mar 4 at 21:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.