Newton’s Divided Difference Formula

Newton’s Divided Difference Formula

#include<stdio.h>
#include<conio.h>
int main (void)
{
	int i,j,n;
   float xp,fp,sum,pi,x[10],f[10],a[10],d[10][10];
   printf("Enter the number of data points: ");
   scanf("%d",&n);
   printf("Enter the value of x: ");
   for(i=1;i<=n;i++)
   	scanf("%f",&x[i]);
   printf("Enter the value of f(x): ");
   for(i=1;i<=n;i++)
   	scanf("%f",&f[i]);
   for(i=1;i<=n;i++)
   	d[i][1]=f[i];
   for(j=2;j<=n;j++)
   {
   	for(i=1;i<=n-j+1;i++)
      	d[i][j]=(d[i+1][j-1]-d[i][j-1])/(x[i+j-1]-x[i]);
   }
   for(j=1;j<=n;j++)
   	a[j]=d[1][j];
   printf("Input the value of xp: ");
   scanf("%f",&xp);
   sum=a[1];
   for(i=2;i<=n;i++)
   {
   	pi=1.0;
      for(j=1;j<=i-1;j++)
      	pi=pi*(xp-x[j]);
      sum=sum+a[i]*pi;
   }
   fp=sum;
   printf("\n\nNewton interpolation: \n");
   printf("at x = %f is %f ",xp,fp);
   getch();
}

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *