Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中使用牛顿多项式从给定的x和f(x)点生成插值函数?_Java_Math_Interpolation_Newtons Method - Fatal编程技术网

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

如何在Java中使用牛顿多项式从给定的x和f(x)点生成插值函数?,java,math,interpolation,newtons-method,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

这是我的密码:

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;
    }
}
这是一个分差表


然后,我想生成它的插值多项式函数。因此,在上面的例子中,使用牛顿多项式规则,输出函数应该是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;
    }
}

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

多项式插值的关键点是你永远不会计算多项式的系数。每次需要值时,都必须计算除法差分算法。原因是1)其复杂性非常小(O(d^2),d为插值次数,通常小于10),2)多项式的系数通常较大且具有交替符号,因此对其进行评估会导致灾难性的对消错误。如果你想一劳永逸地计算高次多项式,看看切比雪夫插值。即使性能不是问题,我建议你在这里使用
double
而不是
double
。我也建议你考虑如何使代码更清晰,而不是使用一个字母变量。@亚历山大:谢谢,但我想要的是使用牛顿的方法。这就是为什么我把它作为一个评论。如果你在做家庭作业,那么好吧,如果你打算插入一些真实的东西,不要这样做。
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;
    }
}
Coef for x^0 is: 6.0
Coef for x^1 is: -11.0
Coef for x^2 is: 6.0