4

i'm trying to integrate an Interpolating function, as simple as this

z = {{-3, 0}, {-2, 1}, {-1, 2}, {0, 3}, {1, 4}, {2, 5}, {3, 6}};

f = Interpolation[z, Method -> "Spline"];

after of that, i would like integrate using "Integrate" command, but i don't know why an error message appears

F = Integrate[f[x], {x, -3, 3}]

InterpolatingFunction::unsops: The operation is not supported for InterpolatingFunction[{{-3,3}},{5,39,0,{7},{4},0,0,0,0,Automatic,{}, {},False},{{<<1>>}},{BSplineFunction[{{-3.,3.}},<>],{}},{Automatic}] that was created with Method -> "Spline".`

someone can help, please.

Carl Woll
  • 130,679
  • 6
  • 243
  • 355

2 Answers2

3

The most expedient method is to use NIntegrate[]:

f = Interpolation[pts = {{-3, 0}, {-2, 1}, {-1, 2}, {0, 3}, {1, 4}, {2, 5}, {3, 6}},
                  Method -> "Spline"];

NIntegrate[f[x], {x, -3, 3}]
   18.

Otherwise, use the methods of this answer to obtain a form that can be handled by Integrate[] (which will yield an exact answer if the points are exact):

m = First[f["InterpolationOrder"]]; n = Length[pts]; {xa, ya} = Transpose[pts];

knots = Join[ConstantArray[xa[[1]], m + 1], 
             If[m + 2 <= n, MovingAverage[ArrayPad[xa, -1], m], {}], 
             ConstantArray[xa[[-1]], m + 1]];
cp = LinearSolve[Outer[BSplineBasis[{m, knots}, #2, #1] &, xa,
                       Range[0, Length[pts] - 1]], ya];

Integrate[cp.Table[BSplineBasis[{m, knots}, j - 1, x], {j, n}], {x, -3, 3}]
   18
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
  • Hi, thanks for answering, i'm new with Mathematica, and i still don't understand why is not possible integrate directly with Integrate command a function made by the spline method, Is because the software versión (my versión is 11.0)? or because is necessary a data treatment before the integration procedure?. Thanks J.M. – RaillBeBack Mar 18 '19 at 19:32
3

I would use NDSolveValue to integrate your interpolating function. Your interpolating function:

z = {{-3,0},{-2,1},{-1,2},{0,3},{1,4},{2,5},{3,6}};
f = Interpolation[z, Method->"Spline"];

Using NDSolveValue:

NDSolveValue[{g'[x] == f[x], g[-3] == 0}, g[3], {x, -3, 3}]

18.

which is the same as JM's answer. You could also use this approach to perform an indefinite integration:

int = NDSolveValue[{g'[x] == f[x], g[-3] == 0}, g, {x, -3, 3}]

enter image description here

For example, here is a plot of the integral:

Plot[int[x], {x, -3, 3}]

enter image description here

Carl Woll
  • 130,679
  • 6
  • 243
  • 355