/* Interpolation by Newton Forward and Backward formulae */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,op;
float x[10],y[10],xx,yy,d[3][10],u,t;
clrscr();
printf("\nNo. of pairs of points");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("%d th value of x and wanted\t",i+1);
scanf("%f%f",&x[i],&y[i]);
}
for(i=0;i<3 && i<n-1;i++)
for(j=0;j<n-i-1;j++)
if(i==0)
d[i][j]=y[j+1]-y[j];
else
d[i][j]=d[i-1][j+1]- d[i-1][j];
while(1)
{
printf(" Give option: Press 1 for Forward: 2 for Backward:3 for Exit\n ");
scanf("%d",&op);
if(op==3) break;
printf("Give value of x for which y to be interpolated");
scanf("%f",&xx);
switch(op)
{
case 1: u=(xx-x[0])/(x[0]);yy=y[0];t=1;
for(i=0;i<3 && i<n-1;i++)
{
t*=(u-i)/(i+1);
yy+=t*d[i][0];
}
break;
case 2: u=(xx-x[n-1])/(x[i]-x[0]);yy=y[n-1];t=1;
for(i=0;i<3&&i<n-1;i++)
{
t*=(u+i)/(i+1);
yy+=t*d[i][n-i-2];
}
}
printf("x=%f\t y=%f\n",xx,yy);
getch();
}
}
No comments:
Post a Comment