Tuesday, July 2, 2013

C PROGRAM FOR NEWTON BACKWARD DIFFERENCE FORMULA FOR INTERPOLATION

/*PROGRAM FOR NEWTON BACKWARD 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[n])/h;
a=y[n][1];
for(j=2;j<=n;j++) {
for(i=n;i>=1;i--)
y[i][j]= y[i][j-1] - y[i-1][j-1];
}
printf("The Table is :\n\n");
for(i=1;i<=n;i++)
    {
printf("\t%.2f",x[i]);
for(j=1;j<=i;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-2));
a = a + (y[n][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:5

Enter the elements of x:.1
.2
.3
.4
.5

Enter The Elements of y:.09
.19
.29
.38
.47
Enter x for which y is to be calculated:.15
The Table is :

        0.10    0.09
        0.20    0.19    0.10
        0.30    0.29    0.10    0.00
        0.40    0.38    0.09    -0.01   -0.01
        0.50    0.47    0.09    0.00    0.01    0.02


For x=0.150000 The Value of y is 0.138594       */

No comments:

Post a Comment