/* Newton Divided difference formula */
# include <stdio.h>
# include <conio.h>
# define MAX 20
void main()
{
 int i,j,n;
 float x[MAX],fx[MAX][MAX],xp,u,sum=0;
 printf("Newton Divided Difference formula \n");
 printf("Enter No. of data points : ");
 scanf("%d",&n);
 printf("Enter values of X and f(x) set by set each at once \n");
 for(i=1;i<=n;i++)
  scanf("%f%f",&x[i],&fx[i][1]);
 printf("Enter value of X at which interpolation is required : ");
 scanf("%f",&xp);
 for(i=2;i<=n;i++)
  for(j=1;j<=n-i+1;j++)
   fx[j][i]=(fx[j+1][i-1]-fx[j][i-1])/(x[j+i-1]-x[j]);
 printf("-------------------------------------------------------------------\n");
 printf("X       F(x)         ");
 for(i=1;i<n;i++)
  printf("y%d         ",i);
 printf("\n-----------------------------------------------------------------\n");
 for(i=1;i<=n;i++)
 {
  printf("%4.2f\t ",x[i]);
  for(j=1;j<=n-i+1;j++)
   printf("%4.2f       ",fx[i][j]);
  printf("\n");
 }
 printf("\n-----------------------------------------------------------------\n");
 sum=fx[1][1];
 for(i=2;i<=n;i++)
 {
  u=1;
  for(j=1;j<i;j++)
   u=u*(xp-x[j]);
  u=u*fx[1][i];
  sum=sum+u;
 }
 printf("For %3.2f Interpolated value = %4.2f",xp,sum);
 getch();
}
OUTPUT
Newton Divided Difference formula
Enter No. of data points : 5
Enter values of X and f(x) set by set each at once
-1 3
0 -6
3 39
6 822
7 1611
Enter value of X at which interpolation is required : 4
------------------------------------------------------------------------------
X        F(x)         y1         y2         y3         y4
-----------------------------------------------------------------------------
-1.00    3.00        -9.00       6.00       5.00       1.00
0.00     -6.00       15.00       41.00      13.00
3.00     39.00       261.00      132.00
6.00     822.00      789.00
7.00     1611.00
-----------------------------------------------------------------------------
For 4.00 Interpolated value = 138.00
No comments:
Post a Comment