使用Apache Commons Math插值函数 [英] Interpolate function using Apache Commons Math

查看:155
本文介绍了使用Apache Commons Math插值函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一些插值函数以绘制一些X值= Date.seconds,Y值= double的值.

I'm trying to implement some Interpolation functions to plot some values where the X value = Date.seconds and the Y value = double.

我一直在研究使用Apache Commons Math lib来实现这一目标,我发现了一种我认为我可以使用

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

我想了解的方法:

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

我不知道应该从哪里获取xi值?

I don't understand where I'm supposed to get the xi value from?

xi是什么?我应将该值传递给xi的此方法吗,我是否应该遍历我的X值数组,并通过XX的第i个元素>?

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?

当我想绘制这些新数据时,我是否使用返回的yi以及传入的xi进行绘制?

And when I want to plot this new data do I use the returned yi along with the passed in xi to plot?

推荐答案

interpolate方法需要成对的数组(此处称为xy)并返回适合这些值的函数(psf)尽可能的好.

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.

然后使用此函数对给定xi值的yi值进行插值(通常不包含在用于定义该函数的x/y数组中).

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).

因此,您具有包含x和y值的对,它们定义了一个函数,并使用此函数对缺失的值进行插值.

So you have pairs containing of x- and y-values defining a function and use this function to interpolate missing values.

有关Apache Commons的信息,请参见用户指南(第4.4章:插值).

See the userguide on Apache Commons (Chapter 4.4: Interpolation).

示例:

绿点是已知值对.这些由xy值数组定义.

The green dots are known value pairs. These are defined by the x and y value arrays.

调用interpolate时,返回的PolynomialSplineFunction返回使用已知值对近似的函数.功能的形状由 UnivariateInterpolator .在问题示例中,使用LinearInterpolator.该图显示了样条插值器返回的函数.

When calling interpolate, the returned PolynomialSplineFunction returns the function that is approximated using the known value pairs. The shape of the funtion is defined by the type of UnivariateInterpolator that is used. In the question example, a LinearInterpolator is used. The drawing shows the function returned by a spline interpolator.

红点表示插值函数上的y值未知的值(在示例中为x值xi的值).

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

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

给出了三个已知值对:

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

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

一个值未知:

(70,?)

(70, ?)

LinearInterpolator内插给定值并生成具有两个分段线性多项式的函数:

The LinearInterpolator interpolates the given values and generates a function with two piecewise, linear polynomials:

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

插值的xi的值(此处为70)为

The value of the interpolated xi (here: 70) is

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

这篇关于使用Apache Commons Math插值函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆