/* Backward Interpolation */
# include <stdio.h<
# include <conio.h<
# define MAX 20
void main()
{
int i,j,n;
float x[MAX],fx[MAX][MAX],u,xp,sum,p;
clrscr();
printf("\t\t\t BACKWARD INTERPOLATION\n");
printf("-------------------------------------------------------------------\n");
printf("Enter number of data points : ");
scanf("%d",&n);
// reading independant & dependant value
printf("Enter data values set by set \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);
u=(xp-x[n])/(x[2]-x[1]);
// generating table
for(i=2;i<=n;i++)
for(j=i;j<=n;j++)
fx[j][i]=fx[j][i-1]-fx[j-1][i-1];
// printing table
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 ",x[i]);
for(j=1;j<=i;j++)
printf("%6.2f ",fx[i][j]);
printf("\n");
}
printf("-----------------------------------------------------------------\n");
// calculting interpolated value
sum=fx[n][1]+fx[n][2]*u;
for(i=2;i<n;i++)
{
p=u;
for(j=1;j<i;j++)
p=p*(u+j);
p=p/fact(i);
sum=sum+p*fx[n][i+1];
}
printf("Value of U := %5.2f\n",u);
printf("Interpolation value := %4.4f\n",sum);
getch();
}
int fact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
f=f*i;
return f;
}
OUTPUT
BACKKWARD INTERPOLATION
------------------------------------------------------------------------------
Enter number of data points : 7
Enter data values set by set
3 13
4 21
5 31
6 43
7 57
8 73
9 91
Enter value of X at which interpolation is required : 10
------------------------------------------------------------------------------
X F(x) y1 y2 y3 y4 y5 y6
-----------------------------------------------------------------------------
3.00 13.00
4.00 21.00 8.00
5.00 31.00 10.00 2.00
6.00 43.00 12.00 2.00 0.00
7.00 57.00 14.00 2.00 0.00 0.00
8.00 73.00 16.00 2.00 0.00 0.00 0.00
9.00 91.00 18.00 2.00 0.00 0.00 0.00 0.00
------------------------------------------------------------------------------
Value of U := 1.00
Interpolation value := 111.0000
No comments:
Post a Comment