2082 2

BIT203 · 2082 · 10 marks

Write an algorithm to compute the value of interpolation using Newton’s divided difference method.Write a program to compute the value of interpolation using Newton’s divided difference method.[5+5]

Write an algorithm to compute the value of interpolation using Newton’s divided difference method.Write a program to compute the value of interpolation using Newton’s divided difference method.[5+5]

Numerical MethodsChapter 3: Interpolation and ApproximationNewton's divided difference method

Quick answer

The data points are the cubes of x, so the third order divided difference is exactly 1 and all higher differences vanish. The interpolating polynomial reproduces $y = x^3$, and indeed $2.5^3 = 15.625$, which confirms the program.

Newton's Divided Difference Interpolation

(a) Algorithm

Concept

Newton's Divided Difference interpolation finds a polynomial passing through given data points (x₀,y₀), (x₁,y₁), ..., (xₙ,yₙ) and estimates the value at any point x.

Divided Difference Formula

The interpolating polynomial is:

f(x) = f[x₀] + (x-x₀)f[x₀,x₁] + (x-x₀)(x-x₁)f[x₀,x₁,x₂] + ...

Where divided differences are defined as:

  • Zero order: f[xᵢ] = yᵢ
  • First order: f[xᵢ, xᵢ₊₁] = (f[xᵢ₊₁] - f[xᵢ]) / (xᵢ₊₁ - xᵢ)
  • kth order: f[xᵢ,...,xᵢ₊ₖ] = (f[xᵢ₊₁,...,xᵢ₊ₖ] - f[xᵢ,...,xᵢ₊ₖ₋₁]) / (xᵢ₊ₖ - xᵢ)

Algorithm

Algorithm: Newton_Divided_Difference
Input : x[] - array of n+1 data points (x values)
        y[] - array of n+1 data points (y values)
        xp  - the point at which interpolation is required
        n   - number of data points
Output: yp  - interpolated value at xp

Step 1: START

Step 2: Read n, x[0..n-1], y[0..n-1], xp

Step 3: Declare a 2D array dd[n][n]
        (dd = divided difference table)

Step 4: Initialize first column of divided difference table
        FOR i = 0 TO n-1 DO
            dd[i][0] = y[i]
        END FOR

Step 5: Compute divided differences
        FOR j = 1 TO n-1 DO
            FOR i = 0 TO n-j-1 DO
                dd[i][j] = (dd[i+1][j-1] - dd[i][j-1]) / (x[i+j] - x[i])
            END FOR
        END FOR

Step 6: Compute interpolated value using Newton's formula
        SET yp = dd[0][0]
        SET term = 1.0

        FOR k = 1 TO n-1 DO
            term = term * (xp - x[k-1])
            yp = yp + dd[0][k] * term
        END FOR

Step 7: Print yp (interpolated value at xp)

Step 8: STOP

(b) C Program

#include <stdio.h>

#define MAX 20

int main()
{
    float x[MAX], y[MAX], dd[MAX][MAX];
    float xp, yp, term;
    int n, i, j, k;

    /* Input number of data points */
    printf("Enter the number of data points: ");
    scanf("%d", &n);

    /* Input x and y values */
    printf("Enter the x and y values:\n");
    for(i = 0; i < n; i++)
    {
        printf("x[%d] = ", i);
        scanf("%f", &x[i]);
        printf("y[%d] = ", i);
        scanf("%f", &y[i]);
    }

    /* Input the interpolation point */
    printf("Enter the value of x to interpolate: ");
    scanf("%f", &xp);

    /* Step 1: Initialize first column with y values */
    for(i = 0; i < n; i++)
    {
        dd[i][0] = y[i];
    }

    /* Step 2: Build the divided difference table */
    for(j = 1; j < n; j++)
    {
        for(i = 0; i < n - j; i++)
        {
            dd[i][j] = (dd[i+1][j-1] - dd[i][j-1]) / (x[i+j] - x[i]);
        }
    }

    /* Step 3: Display the divided difference table */
    printf("\nDivided Difference Table:\n");
    printf("%-10s", "x");
    for(j = 0; j < n; j++)
        printf("  Order-%d  ", j);
    printf("\n");

    for(i = 0; i < n; i++)
    {
        printf("%-10.4f", x[i]);
        for(j = 0; j < n - i; j++)
            printf("  %-8.4f", dd[i][j]);
        printf("\n");
    }

    /* Step 4: Compute interpolated value */
    yp   = dd[0][0];
    term = 1.0;

    for(k = 1; k < n; k++)
    {
        term = term * (xp - x[k-1]);
        yp   = yp + dd[0][k] * term;
    }

    /* Output result */
    printf("\nInterpolated value at x = %.4f is y = %.4f\n", xp, yp);

    return 0;
}

Sample Output

Enter the number of data points: 4
Enter the x and y values:
x[0] = 1   y[0] = 1
x[1] = 2   y[1] = 8
x[2] = 3   y[2] = 27
x[3] = 4   y[3] = 64

Enter the value of x to interpolate: 2.5

Divided Difference Table:
x           Order-0    Order-1    Order-2    Order-3  
1.0000      1.0000     7.0000     6.0000     1.0000   
2.0000      8.0000     19.0000    9.0000   
3.0000      27.0000    37.0000  
4.0000      64.0000  

Interpolated value at x = 2.5000 is y = 15.6250

Conclusion

Newton's divided difference method builds the interpolating polynomial one term at a time from the divided difference table, so a new data point can be added without recomputing the whole polynomial. Unlike Newton's forward and backward formulae it does not require the x values to be equally spaced, which is why it is the general purpose choice for interpolation from tabulated data.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.