5
$\begingroup$

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] == 0, u'[0] == 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.

$\endgroup$
4
  • $\begingroup$ Use NIntegrate $\endgroup$
    – ciao
    Commented May 23, 2014 at 7:48
  • $\begingroup$ @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) $\endgroup$ Commented May 23, 2014 at 7:50
  • 1
    $\begingroup$ You can use FunctionInterpolation[2*ifun1[t], {t, 0, π}] to get the resulting InterpolatingFunction. $\endgroup$ Commented May 23, 2014 at 8:31
  • $\begingroup$ 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. $\endgroup$
    – Nasser
    Commented May 23, 2014 at 8:35

1 Answer 1

5
$\begingroup$

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]], #1[[3]] + #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.

$\endgroup$
5
  • $\begingroup$ Just wanted to let you know that I'm in the process of testing this for my case. I'll report back asap. $\endgroup$ Commented May 23, 2014 at 23:18
  • $\begingroup$ 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). $\endgroup$ Commented May 24, 2014 at 6:02
  • $\begingroup$ 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 $\endgroup$
    – gpap
    Commented May 26, 2014 at 8:37
  • $\begingroup$ @gpap, although your approach certainly is useful, it can reduce the accuracy of the answer substantially. $\endgroup$
    – bbgodfrey
    Commented Nov 29, 2014 at 19:46
  • $\begingroup$ @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...? $\endgroup$
    – gpap
    Commented Nov 29, 2014 at 23:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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