8

I am using Interpolation to get an InterpolatingFunction that fits a set of points. For example,

points = {{6, 14}, {2, 0.15}, {...}, {...}, ...};
func = Interpolation[points];

which returns an InterpolatingFunction. I understand that Interpolate does a piecewise polynomial interpolation to fit the data. My question is, how do I get the explicit definition of this piecewise function from the InterpolatingFunction object? I want to do this with an arbitrary interpolation order, not specifically the maximum data points. For example, I want to see the form:

func = {{x^2 + 2, x < 2}, {1 - x^3 + 3, x >= 2}, ...}

I see something like this at the bottom of the InterpolatingFunction documentation page as linked above, but the writer seemed to already know the locations at which the InterpolatingFunction was split between the piecewise fits.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
aquirdturtle
  • 200
  • 4
  • 1
    Perhaps as a start you could differentiate the InterpolatingFunction to identify the intervals. – IPoiler Feb 02 '16 at 00:20
  • In this video we can learn to make a kinetic analysis of a mechanical arm applying polynomial interpolation https://youtu.be/v9p_2utyjgw – LCarvalho Mar 21 '18 at 12:06

2 Answers2

5

I do not know how to extract that information from an InterpolatingFunction object, but perhaps you could make your own Piecewise function using InterpolatingPolynomial:

pts = RandomReal[{-10, 10}, {10, 2}];
piecewise = Piecewise[
  {InterpolatingPolynomial[#, x], #[[1, 1]] <= x < #[[2, 1]]} & /@ 
   Partition[SortBy[First][pts], 2, 1]
  ]

Mathematica graphics

Here is a plot of the Interpolation result and of the Piecewise function together (the built-in interpolation is offset by three vertical units for clarity):

Plot[{
  piecewise,
  Interpolation[pts, InterpolationOrder -> 1][x] + 3
  },
 Evaluate@Flatten@{x, MinMax[pts[[All, 1]]]},
 PlotStyle -> {Red, Blue}
]

Mathematica graphics

MarcoB
  • 67,153
  • 18
  • 91
  • 189
3
SeedRandom[1];

pts = RandomReal[{-10, 10}, {10, 2}];

Clear[if, ip]

If the InterpolationOrder is set to Length[pts] - 1 then the InterpolatingFunction is the InterpolatingPolynomial

if[x_] = Interpolation[pts, InterpolationOrder -> Length[pts] - 1][x];

ip[x_] = InterpolatingPolynomial[pts, x] // HornerForm

(*  -18.7844 + 
 x (6.55948 + 
    x (11.9763 + 
       x (-0.292684 + 
          x (-1.04441 + 
             x (0.030186 + 
                x (0.0317666 + 
                   x (-0.00161601 + (-0.000321526 + 0.0000231103 x) x)))))))  *)

Plot[{if[x], ip[x]},
 {x, Min[pts[[All, 1]]], Max[pts[[All, 1]]]},
 PlotStyle -> {Dashed, DotDashed},
 PlotLegends -> "Expressions",
 Epilog -> {Red, PointSize[Large], Point[pts]}]

enter image description here

Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198
  • This is potentially useful so thanks, but I'd like to be able to get the function for an arbitrary interpolation order, not just specifically
        Length[pts] - 1
    
    

    For example, if the order was 3, I'd like to see all of the cubic function definitions and what intervals they'd apply on.

    – aquirdturtle Feb 02 '16 at 03:41