Friday, 9 December 2011

NEWTON DIVIDED DIFFERENCE INTERPOLATION PROGRAM IN C

#include<stdio.h>
#include<conio.h>
void main()
{
float x[10],y[10],d[10][10];
float a,u,sum=0,prod;
int i,j,n,k;
clrscr();
printf("\n enter the no of points");
scanf("%d",&n);
printf("\nenter the value for interpolation");
scanf("%f",&a);
printf("\n enter the value for x");
for(i=1;i<=n;i++)
{
scanf("%f",&x[i]);
}
printf("\n enter the value for y");
for(i=1;i<=n;i++)
{
scanf("%f",&y[i]);
}
/*for(i=1;i<=n;i++)
{
printf("\n x=%f",x[i]);
printf("\n y=%f",y[i]);
} */
if((a<x[1])||(a>x[n]))
{
printf("\n values lies outside the range");
}
//i=1;
/*while(a>x[i])
//{
i++;
}
k=i-1;
printf("\n k=%d",k);*/
for(j=1;j<=n-1;j++)
{
for(i=1;i<=(n-j);i++)
{
if(j==1)
{
 d[i][j]=((y[i+1]-y[i])/(x[i+1]-x[i]));
 printf("\n 1d[i][j]=%f",d[i][j]);
}
else
 d[i][j]=((d[i+1][j-1]-d[i][j-1])/(x[i+j]-x[i]));
  printf("\n d[i][j]=%f",d[i][j]);
}}
sum=y[i];
printf("\n sum=%f",sum);
for(i=1;i<=n;i++)
{
 prod=1.0;
 for(j=0;j<=i-1;j++)
// prod=prod*(u-j);
prod=prod*(a-x[i]);
 sum=sum+d[i][j]*prod;
 }
 printf("\n interpolated value=%f",sum);
 getch();
 }

No comments:

Post a Comment