I'm trying to implement some Interpolation functions to plot some values where the X value = Date.seconds and the Y value = double.
I'v been researching using the Apache Commons Math
lib to achieve this and I've found a method I think I might be able to use here
The method I'm trying to understand:
public double linearInterp(double[] x, double[] y, double xi) {
// return linear interpolation of (x,y) on xi
LinearInterpolator li = new LinearInterpolator();
PolynomialSplineFunction psf = li.interpolate(x, y);
double yi = psf.value(xi);
return yi;
}
I don't understand where I'm supposed to get the xi
value from?
What is xi what value do I pass into this method for xi, am I supposed to loop through my array of X
values and pass in the ith element of X
with the array of X
and Y
?
And when I want to plot this new data do I use the returned yi
along with the passed in xi
to plot?
The
interpolate
method expects arrays of pairs (here calledx
andy
) and returns a function (psf
) that fits these values as best as possible.This function is then used to interpolate the yi-value of a given xi-value (which is normally not contained in the x/y array used to define the function).
So you have pairs containing of x- and y-values defining a function and use this function to interpolate missing values.
See the userguide on Apache Commons (Chapter 4.4: Interpolation).
Example:
The green dots are known value pairs. These are defined by the
x
andy
value arrays.When calling
interpolate
, the returnedPolynomialSplineFunction
returns the function that is approximated using the known value pairs. The shape of the funtion is defined by the type ofUnivariateInterpolator
that is used. In the question example, aLinearInterpolator
is used. The drawing shows the function returned by a spline interpolator.The red dot symbolizes a value with an unknown y-value on the interpolated function (this would be the value with an x-value of
xi
in the question example).If you need to calculate more than one extra value, use a function like this
A calculation example:
Three known value pairs are given:
One value is unknown:
The
LinearInterpolator
interpolates the given values and generates a function with two piecewise, linear polynomials:The value of the interpolated
xi
(here: 70) is