开发者

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

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜