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

时间:2011-04-05 10:25:11

标签: java math interpolation newtons-method

这是我的代码:

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 points = {1,2,3}和f(x)= {1,8,27},上面的代码将生成一个二维数组,如下所示:

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 。我真的坚持这个,任何人都可以帮助我如何产生这样的输出吗?

1 个答案:

答案 0 :(得分:0)

根据我从newtonian interpolation可以理解的,从你的分歧差异和你的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 + 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