Tuesday, 27 March 2012

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();
}

No comments:

Post a Comment