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.

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.

share|improve this question
    
Use NIntegrate –  rasher May 23 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 at 7:50
1  
You can use FunctionInterpolation[2*ifun1[t], {t, 0, π}] to get the resulting InterpolatingFunction. –  b.gatessucks May 23 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 at 8:35

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
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 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 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 at 8:37

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.