Tuesday, July 2, 2013

C PROGRAM FOR NEWTON FORWARD DIFFERENCE FORMULA FOR INTERPOLATION

/*PROGRAM FOR NEWTON FORWARD DIFFERENCE FORMULA FOR INTERPOLATION */

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main() {
int i,n,j,fact=1;
float x[20],y[20][20],h,d=1,p,a,f;
clrscr();
printf("Enter The Value of n:");
scanf("%d",&n);
printf("\nEnter the elements of x:");
for(i=1;i<=n;i++)
scanf("%f", &x[i]);
printf("\nEnter The Elements of y:");
for(i=1;i<=n;i++)
scanf("%f",&y[i][1]);
h=x[2]-x[1];
printf("Enter x for which y is to be calculated:");
scanf("%f",&f);
p = (f-x[1])/h;
a=y[1][1];
for(j=2;j<=n;j++) {
for(i=1;i<=(n-j)+1;i++)
y[i][j]= y[i+1][j-1] - y[i][j-1];
}
printf("The Table is :\n\n");
for(i=1;i<=n;i++)
    {
printf("\t%.2f",x[i]);
for(j=1;j<=(n-i)+1;j++)
   printf("\t%.2f",y[i][j]);
printf("\n");
    }
for(j=2;j<=n;j++) {
for(i=1;i<j;i++)
fact=fact*i;

d = d*(p-(j-1));
a = a + (y[1][j]*d)/fact;
fact=1;
}
printf("\n\nFor x=%f The Value of y is %f",f,a);
getch();
}

/* OUTPUT:-
Enter The Value of n:4

Enter the elements of x:0
1
2
3

Enter The Elements of y:1
0
1
10
Enter x for which y is to be calculated:4
The Table is :

        0.00    1.00    -1.00   2.00    6.00
        1.00    0.00    1.00    8.00
        2.00    1.00    9.00
        3.00    10.00


For x=4.000000 The Value of y is 10.000000  */

1 comment: