简体   繁体   English

使用Apache Commons Math插值函数

[英]Interpolate function using Apache Commons Math

I'm trying to implement some Interpolation functions to plot some values where the X value = Date.seconds and the Y value = double. 我正在尝试实现一些插值函数来绘制一些值,其中X值= Date.seconds,Y值= 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 我一直在研究使用Apache Commons Math lib来实现这个目标,我发现了一个我认为我可以在这里使用的方法

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? 我不明白我应该从哪里得到xi值?

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 ? 什么是我传玺什么价值这个方法十一,我在通过我的阵列应该循环X值和传递的第i个元素X与阵列XY

And when I want to plot this new data do I use the returned yi along with the passed in xi to plot? 当我想绘制这个新数据时,我是否使用返回的yi以及传入的xi来绘制?

The interpolate method expects arrays of pairs (here called x and y ) and returns a function ( psf ) that fits these values as best as possible. interpolate方法需要对的数组(此处称为xy )并返回一个尽可能符合这些值的函数( psf )。

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). 然后,该函数用于内插给定xi值的yi值(通常不包含在用于定义函数的x / y数组中)。

So you have pairs containing of x- and y-values defining a function and use this function to interpolate missing values. 因此,您有包含定义函数的x值和y值的对,并使用此函数来插入缺失值。

See the userguide on Apache Commons (Chapter 4.4: Interpolation). userguide在Apache下议院(第4.4章:插值)。


Example: 例:

一个严重绘制的样条插值示例

The green dots are known value pairs. 绿点是已知的值对。 These are defined by the x and y value arrays. 这些由xy值数组定义。

When calling interpolate , the returned PolynomialSplineFunction returns the function that is approximated using the known value pairs. 调用interpolate ,返回的PolynomialSplineFunction返回使用已知值对近似的函数。 The shape of the funtion is defined by the type of UnivariateInterpolator that is used. 函数的形状由所使用的UnivariateInterpolator的类型定义。 In the question example, a LinearInterpolator is used. 在问题示例中,使用了LinearInterpolator 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). 红点表示插值函数上具有未知y值的值(这将是问题示例中x值为xi的值)。

If you need to calculate more than one extra value, use a function like this 如果您需要计算多个额外值,请使用这样的函数

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;
}

A calculation example: 一个计算示例:

public class Interpolate {

    public static void main(String[] args) {
        double[] x = { 0, 50, 100 };
        double[] y = { 0, 50, 200 };

        LinearInterpolator interp = new LinearInterpolator();
        PolynomialSplineFunction f = interp.interpolate(x, y);

        System.out.println("Piecewise functions:");
        Arrays.stream(f.getPolynomials()).forEach(System.out::println);

        double value = f.value(70);
        System.out.println("y for xi = 70: " + value);
    }
}

Three known value pairs are given: 给出了三个已知值对:

(0, 0) (0,0)
(50, 50) (50,50)
(100, 200) (100,200)

One value is unknown: 一个值是未知的:

(70, ?) (70,?)

The LinearInterpolator interpolates the given values and generates a function with two piecewise, linear polynomials: LinearInterpolator插入给定值并生成具有两个分段线性多项式的函数:

y = x             (for x values < 50)
y = 50 + 3 * x    (for x-values >= 50)

The value of the interpolated xi (here: 70) is 插值xi (此处:70)的值为

y = 50 + 3 * (70 - 50) = 110

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM