# 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 FORWARD 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[1])/(x[2]-x[1]);
// generating table
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];
// printing table
printf("-------------------------------------------------------------------\n");
printf("X F(x) ");
for(i=1;i
printf("\n-----------------------------------------------------------------\n");
for(i=1;i<=n;i++)
{
printf("%4.2f ",x[i]);
for(j=1;j<=n-i+1;j++)
printf("%6.2f ",fx[i][j]);
printf("\n");
}
printf("-------------------------------------------------------------------\n");
//calculting interpolated value
sum=fx[1][1]+fx[1][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[1][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 :-
FORWARD INTERPOLATION
------------------------------------------------------------------------------
Enter number of data points : 6
Enter data values set by set
.20 1.65
.22 1.66
.24 1.68
.26 1.69
.28 1.70
.30 1.71
Enter value of X at which interpolation is required : .23
------------------------------------------------------------------------------
X F(x) y1 y2 y3 y4 y5
-----------------------------------------------------------------------------
0.20 1.65 0.01 0.01 -0.02 0.03 -0.04
0.22 1.66 0.02 -0.01 0.01 -0.01
0.24 1.68 0.01 -0.00 0.00
0.26 1.69 0.01 0.00
0.28 1.70 0.01
0.30 1.71
------------------------------------------------------------------------------
Value of U := 1.50
Interpolation value := 1.6712
No comments:
Post a Comment