8. Write a program to demonstrate Newton-Backward Interpolation formula.
/* program to execute Newton Backward interpolation */
#include <stdio.h>
#include < conio.h>
#define max 50
void main()
{
float arrx[max + 1], arry[max + 1], diff[max + 1][5], num = 1.0, den = 1.0;
float x, p, h, y;
int i, j, k, n;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Enter the value of x and y: \n"); /* input data */
for (i = 0; i <= n; i++)
scanf("%f %f", &arrx[i], &arry[i]);
printf("nEnter value of x for which value of y is needed: ");
scanf("%f", &x);
h = arrx[1] - arrx[0];
for (i = 0; i <= n - 1; i++) /* calculate first difference */
diff[i][1] = arry[i + 1] - arry[i];
for (j = 2; j < 5; j++)
for (i = 0; i <= n - j; i++) /* calculate other difference */
diff[i][j] = diff[i + 1][j - 1] - diff[i][j - 1];
i = n;
p = (x - arrx[i]) / h; /* search for x0 , y0*/
y = arry[i];
for (k = 1; k < 5; k++) /* Apply Interpolation formula */
{
num *= (p + k - 1);
den *= k;
y += (num / den) * diff[i - k][k];
}
printf("Value of y = %f, when x = %f ", y, x);
getch();
}
Output
Enter the value of n: 6
Enter values of x and y:
100 10.63
150 13.03
200 15.04
250 16.81
300 18.42
350 19.90
400 21.27
Enter value of x for which value of y is needed: 410
Value of y = 21.531857, when x = 410.000000