Nov 18, 2008

Newton's Forward Interpolation Formula

#include<stdio.h>

int main()
{
int i,j,n,m;
float x[20],y[20],d[20][20],a,h,u,b,z=1.0,k=0.0;

printf("Enter the no.s of entries i.e n:\n");
scanf("%d",&n);

printf("Enter the value of x.\n");
for(i=0;i<=n-1;i++)
{
scanf("%f",&x[i]);
}

printf("Enter the value of y.\n");
for(i=0;i<=n-1;i++)
{
scanf("%f",&y[i]);
}

for(i=0;i<=n-1;i++)
{
d[i][0]=y[i+1]-y[i];
}

for(j=1;j<=n-2;j++)
{
for(i=0;i<=n-j-2;i++)
{
d[i][j]=d[i+1][j-1]-d[i][j-1];
}
}

for(i=0;i<=n-1;i++)
{
printf("%f %f",x[i],y[i]);
for(j=0;j<=n-i-2;j++)
{
printf(" %f",d[i][j]);
}
printf("\n");
}

printf("Enter the value of x for which y is required\n");
scanf("%f",&a);
for(i=0;a>x[i];i++)
{
m=i;
}

h=x[1]-x[0];
u=(a-x[m])/h;

for(i=0;i<=n-m-2;i++)
{
z*=(u-i)/(i+1);
k+=(z*d[m][i]);
}
b=y[m]+k;
printf("\n The value of y at %f = %f\n",a,b);
return 0;
}

1 comments:

licon said...
This comment has been removed by the author.