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

发布于 2024-10-30 22:15:52 字数 849 浏览 1 评论 0原文

这是我的代码:

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

Here's my code:

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

Sorry for the ugly code. Given x points = {1, 2, 3} and f(x) = {1, 8, 27}, the above code would produce a 2-dimension array like this:

1.0   1.0
            7.0
2.0   8.0         6.0
            19.0
3.0   27.0

which is a divided difference table.

Then, I want to generate its interpolating polynomial function. Thus, with above example, using Newton's polynomial rule, the output function should be 1 + 7(x-1) + 6(x-1)(x-2) = 6x^2-11x+6. I'm really stuck on this, can anyone help me how to produce an output like that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉殇 2024-11-06 22:15:52

根据我从牛顿插值中可以理解的,根据你的差异和你想要的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

From what I could understand from newtonian interpolation, from your divided differences and your x-positions you want the coefficients of the Newton polynom, right ?
Here is a little thing that should do the trick:

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

It represents polynoms by arrays of doubles: {1.0, 3.0, -2.0} represents 1 + 3x -2x^2. This eases computations (notably polynoms product).

The execution gives:

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

which is the polynom 6 - 11x + 6x^2 that you expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文