繁体   English   中英

如何使用Java中的牛顿多项式从给定的x和f(x)点生成插值函数?

[英]How to produce an interpolated function from a given x and f(x) points using Newton polynomial in Java?

这是我的代码:

Double[] x = {1.0, 2.0, 3.0};
Double[] fx = {1.0, 8.0, 27.0};
s = x.length;

Double[][] newton = new Double[(2*s)-1][s+1];
    for(int i=0, z=0;i<s;i++,z+=2){
        newton[z][0]=x[i];
        newton[z][1]=fx[i];
    }

int i=1, ii=2, j=2, ss=s;
for(int z=0;z<s-1;z++,j++,ss-=1,ii++){
        for(int y=0;y<ss-1;y++,i+=2){
            newton[i][j]=(newton[i+1][j-1]-newton[i-1][j-1])/(newton[i+(ii-1)][0]-newton[i-(ii-1)][0]);
        }
        i=ii;
    }
}

很抱歉提供难看的代码。 给定x点= {1,2,3}和f(x)= {1,2,3},上述代码将产生一个二维数组,如下所示:

1.0   1.0
            7.0
2.0   8.0         6.0
            19.0
3.0   27.0

这是一个划分的差异表。

然后,我要生成其插值多项式函数。 因此,在上面的示例中,使用牛顿多项式规则,输出函数应为1 + 7(x-1)+ 6(x-1)(x-2)= 6x ^ 2-11x + 6 我真的很坚持,任何人都可以帮助我如何产生这样的输出吗?

从我从牛顿插值中可以理解的,从除数和x位置,您想要牛顿多项式的系数,对吗? 这是应该做的一件事:

public class PolynomProduct {

    public static void main(String[] args) {
        double[] values = {1.0, 2.0, 3.0};
        double[] diffs = {1.0, 7.0, 6.0};

        // Initialize result array
        double[] result = new double[values.length];
        for (int i = 0; i < values.length; ++i) {
            result[i] = 0.0;
        }

        for (int i = 0; i < values.length; ++i) {
            // 'poly' has a degree 'i'. We use 'i - 1' because only terms
            // from 0 to 'i - 1' are used
            double[] poly = getPoly(values, i - 1);

            // Now add to result, do not forget to multiply by the divided
            // difference !
            for (int j = 0; j < poly.length; ++j) {
                result[j] += poly[j] * diffs[i];
            }
        }

        for (int i = 0; i < result.length; ++i) {
            System.out.println("Coef for x^" + i + " is: " + result[i]);
        }
    }

    public static double[] getPoly(double[] values, int i) {
        // Start poly: 1.0, neutral value for multiplication
        double[] coefs = {1.0};

        // Accumulate values of products
        for (int j = 0; j <= i; ++j) {
            // 'coefsLocal' represent polynom of 1st degree (x - values[j])
            double[] coefsLocal = {-values[j], 1.0};
            coefs = getPolyProduct(coefs, coefsLocal);
        }

        return coefs;
    }

    public static double[] getPolyProduct(double[] coefs1, double[] coefs2) {
        // Get lengths and degree
        int s1 = coefs1.length - 1;
        int s2 = coefs2.length - 1;
        int degree = s1 + s2;

        // Initialize polynom resulting from product, with null values
        double[] coefsProduct = new double[degree + 1];
        for (int k = 0; k <= degree; ++k) {
            coefsProduct[k] = 0.0;
        }

        // Compute products
        for (int i = 0; i <= s1; ++i)   {
            for (int j = 0; j <= s2; ++j)   {
                coefsProduct[i + j] += coefs1[i] * coefs2[j];
            }
        }
        return coefsProduct;
    }
}

它通过双精度数组表示多项式: {1.0, 3.0, -2.0} 1.0,3.0 {1.0, 3.0, -2.0}表示1 + 3x -2x^2 这简化了计算(尤其是多项式乘积)。

执行给出:

Coef for x^0 is: 6.0
Coef for x^1 is: -11.0
Coef for x^2 is: 6.0

这是您期望的多项式6 - 11x + 6x^2

暂无
暂无

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

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