8
$\begingroup$

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.

$\endgroup$
2
  • 1
    $\begingroup$ Perhaps as a start you could differentiate the InterpolatingFunction to identify the intervals. $\endgroup$
    – IPoiler
    Feb 2, 2016 at 0:20
  • $\begingroup$ In this video we can learn to make a kinetic analysis of a mechanical arm applying polynomial interpolation youtu.be/v9p_2utyjgw $\endgroup$
    – LCarvalho
    Mar 21, 2018 at 12:06

2 Answers 2

5
$\begingroup$

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

$\endgroup$
3
$\begingroup$
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

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$ Feb 2, 2016 at 3:41

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