7. Write a program to demonstrate Newton-Forward Interpolation formula.
/*Program to execute Newton Forward 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;
print("Enter the value of n: ");
scanf("%d", &n);
printf("Enter values for x and y:\n"); /*Input data */
for (i = 0; i <= n - 1; i++) /* Calculate First difference*/
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 frist difference */
diff[i][1] = arry[i + 1] - arry[i];
for (j - 2; j < 5; j++) /* calculate other difference */
for (i - 0; i <= n - j; i++)
diff[i][j] = diff[i + 1][i + 1] - diff[i][j - 1];
i = 0;
while (arry[i + 1] < x) /*search for x0,y0 */
i++;
p = (x - arry[i + 1]) / h;
y = arry[i];
for (k = 1; k < 5; k++) /* apply interpolation formula */
{
num *= p - k + 1;
den *= k;
y += (num / den) * diff[i][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: 218
Value of y = 15.754879, when x = 218.000000"