Java 上的插值和导数

Interpolation and derivative on Java

我正在尝试制作一个项目 涉及从原始数据及其导数计算插值。 我有两个数组,如下所示:

A = { 1, 2, 3, 4, ... , n }
B = { 0.23, 0.43, 0.24, 0.19, ... , n }

我想要一个函数来描述以下数组,所以我使用 apache-common-math 库来插入将描述函数的多项式,其中:F(A[i]) = B[i] . 之后,我想计算这个函数的导数,以便能够找到极值(max/min)。

出于某种原因,我在微分部分遇到了问题。

目前正在使用:

        DividedDifferenceInterpolator devider = new DividedDifferenceInterpolator();
        PolynomialFunctionNewtonForm polynom = devider.interpolate(xArray,  
        yArray);

现在我有了多项式,它是代表我之前数组的函数。 我该如何计算它的导数..?

谢谢。

通过使用拉格朗日多项式而不是使用库,您可以很容易地找到多项式的导数。

您可以编写一个简单的函数(从 apache Commons 实现 UnivariateFunction)。参考@Krokop 在 this page.

上的等式

那你可以使用commons math given here中的求根库,看看页面上给出的"typical usage"。

我不确定是否有更简单的方法。

您可以通过从牛顿形式多项式中提取系数,从中创建一个 PolynomialFunction 然后使用该函数的 derivative 方法来获得导数。这是一个例子:

double[] x = {0, 1, 2, 3, 4, 5};
double[] y = new double[6];
for (int i = 0; i < 6; i++) {
    y[i] = 1 + 2 * x[i] + 3 * x[i] * x[i];
}
DividedDifferenceInterpolator divider = new DividedDifferenceInterpolator();
PolynomialFunctionNewtonForm polynom = divider.interpolate(x, y);
double[] coefficients = polynom.getCoefficients();
System.out.println(Arrays.toString(coefficients));
PolynomialFunction derivative =
 (PolynomialFunction) new PolynomialFunction(coefficients).derivative();
System.out.println(Arrays.toString(derivative.getCoefficients()));

上面的代码从多项式 y = 1 + 2x + 3x^2 生成点。输出应该是

[1.0, 2.0, 3.0, 0.0, 0.0, 0.0]
[2.0, 6.0]