Search code examples
javaapache-commons-mathextrapolation

Extrapolation OutOfRangeException Apache Commons Math


I'm trying to implement an Extrapolation function using the Apache Commons Math lib and the PolynomialSplineFunction & LinearInterpolator functions.

 public double[] linearInterp(double[] x, double[] y, double[] xi) {
   LinearInterpolator li = new LinearInterpolator(); // or other interpolator
   PolynomialSplineFunction psf = li.interpolate(x, y);

   double[] yi = new double[xi.length];
   for (int i = 0; i < xi.length; i++) {
       yi[i] = psf.value(xi[i]);
   }
   return yi;
}

x = [0, 60, 120,180,240];

y = [196, 232, 250, 157, 300];

xi = [300, 360, 420];

the problem is that if I use a value outside of the range of x I get a OutOfRangeException is there any way to Extrapolate using this method? how do I get around this error.


Solution

  • If you look at the documentation PolynomialSimlineFunction Doucumentaition you can read for the value method "OutOfRangeException - if v is outside of the domain of the spline function (smaller than the smallest knot point or larger than the largest knot point)."

    This is quite reasonable since the interpolation out of the knots is not an aproximation of your funcition. What your are attempting simply doesn't make sense mathematically speaking.