Here is the code..
/*BACKWARD INTERPOLATION METHOD*/
#include <stdio.h>
#define m 10
void main()
{
int n,i,j;
float mx[m],my[m],x,sum,fun,p,diff[m][m];
printf("Enter the number of terms:\n");
scanf("%d",&n);
printf("\n\Enter the %d values of x:\n",n);
for(i=0;i<n;i++)
scanf("%f",&mx[i]);
printf("\n Enter the %d values of y:\n",n);
for(i=0;i<n;i++)
scanf("%f",&my[i]);
printf("\nEnter x for which you want the value of y:\n");
scanf("%f",&x);
for(i=0;i<n-1;i++)
{
diff[i][1]=my[i+1]-my[i];
}
for(j=2;j<n;j++)
{
for(i=0;i<n-j;i++)
{
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
}}
fun=1;
p=(x-mx[n-1])/(mx[1]-mx[0]);
sum=my[n-1];
i=1;
for(j=1;j<=4;j++)
{
i=i*j;
fun=(fun*(p+(j-1)))/i;
sum=sum+fun*diff[n-1][j];
}
printf("\nwhen x=%6.4f,y=%6.8f",x,sum);
printf("\n Press Enter to Exit");
}
For Turbo C users.
/*BACKWARD INTERPOLATION METHOD*/
#include <stdio.h>
#include <conio.h>
#define m 10
void main()
{
int n,i,j;
float mx[m],my[m],x,sum,fun,p,diff[m][m];
clrscr();
printf("Enter the number of terms:\n");
scanf("%d",&n);
printf("\n\Enter the %d values of x:\n",n);
for(i=0;i<n;i++)
scanf("%f",&mx[i]);
printf("\n Enter the %d values of y:\n",n);
for(i=0;i<n;i++)
scanf("%f",&my[i]);
printf("\nEnter x for which you want the value of y:\n");
scanf("%f",&x);
for(i=0;i<n-1;i++)
{
diff[i][1]=my[i+1]-my[i];
}
for(j=2;j<n;j++)
{
for(i=0;i<n-j;i++)
{
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
}}
fun=1;
p=(x-mx[n-1])/(mx[1]-mx[0]);
sum=my[n-1];
i=1;
for(j=1;j<=4;j++)
{
i=i*j;
fun=(fun*(p+(j-1)))/i;
sum=sum+fun*diff[n-1][j];
}
printf("\nwhen x=%6.4f,y=%6.8f",x,sum);
printf("\n Press Enter to Exit");
getch();
}
No comments:
Post a Comment