# Integrating a functional of an InterpolatingFunction

It is straightforward to Integrate an InterpolatingFunction. However, even for a simple functional of an InterpolatingFunction, Integrate returns unevaluated.

ifun1 =
First[
u /.
NDSolve[
{u''[t] + u[t] == 0, u == 0, u' == 1},
u,
{t, 0, π}
]
]

Integrate[ifun1[t], {t, 0, π}]
Integrate[2*ifun1[t], {t, 0, π}]


The former Integrate returns a Real, the latter, the expression unevaluated.

How can the second integral be evaluated symbolically? (In my non-MWE, symbolically is important because the solution to my PDE has two arguments, over only one of which I would like to integrate, so NIntegrate will not work.) The solution should apply to more-complicated functionals, too (e.g. 2*t*ifun1[t]). If a solution applies only to functionals that are restricted to a certain family, such as "linear," it would still be helpful.

• Use NIntegrate – ciao May 23 '14 at 7:48
• @rasher, I want to avoid this because my non-MWE is a PDE solution with two arguments and I want to integrate over only one of them. NIntegrate can't do that, in my experience (because the other argument is non-numerical). (updated my question) – Rico Picone May 23 '14 at 7:50
• You can use FunctionInterpolation[2*ifun1[t], {t, 0, π}] to get the resulting InterpolatingFunction. – b.gates.you.know.what May 23 '14 at 8:31
• I am not sure why it does not work. I think Integrate does not know if c*ifun1[t] will converge. Even if c is constant. Only when c=1 it will do it. – Nasser May 23 '14 at 8:35

## 1 Answer

You should know that your first Integrate is numerically evaluated (so Mathematica calls NIntegrate on your interpolation function anyway). On the second I don't know why Mathematica doesn't call NIntegrate immediately but explicitly using NIntegrate yields a result.

In any case, this is building on your comment that you have a function with two arguments and you want to integrate over one and get a result as a function of the other. In my opinion b.gatessucks is spot on in that you need to use FunctionInterpolation.

Here's a random set of points:

points = MapThread[{#1[], #1[], #1[] + #2} &, {Flatten[
Array[{Sequence@##, Sin[Times@##]} &, {20, 20}], 1],
RandomReal[{-1, 1}, 400]}];


and here's an interpolation of the points:

f = Interpolation[points];


Now you can do whatever you want in the integrand over one of the arguments. FunctionInterpolation will evaluate the integral numerically at some points of your specified domain and create a function :

g = FunctionInterpolation[

Integrate[2*f[t, y] - f[t, y]^2 + t Cos[f[t, y]], {t, 0, π}]
,
{y, 1., 20.}
]


that behaves like any interpolating function. But note that "symbolically" and Interpolation are quite incompatible notions.

• Just wanted to let you know that I'm in the process of testing this for my case. I'll report back asap. – Rico Picone May 23 '14 at 23:18
• This works great, thanks! I ended up doing the FunctionInterpolation before the integration, which seemed to be more straight-forward to implement when writing function. I don't know that there are any significant advantages to switching the order, though (your order works just fine). – Rico Picone May 24 '14 at 6:02
• Hey, glad it worked! It is pretty much the same idea when you call ParametricNDSolve. Mathematica solves for certain values in the range of the parameter and creates a function interpolation of the solutino w.r.t. the parameter – gpap May 26 '14 at 8:37
• @gpap, although your approach certainly is useful, it can reduce the accuracy of the answer substantially. – bbgodfrey Nov 29 '14 at 19:46
• @bbgodfrey yeah, I agree - but part of that (in the specific problem at least) would be due to how the original interpolation function is calculated which you'd have some control over. I don't see how else one could do this unless one created a set of points and then interpolated...? – gpap Nov 29 '14 at 23:31