Newtons Backward interpolation
// write a c program to implement backward interpolation method
#include <stdio.h>
int fact(int x){
if(x == 0 ){
return 1;
}else{
return x*fact(x-1);
}
}
float calcp(float p,int x){
int i;
float sum = 1;
for(i = 0;i<x;i++){
sum *= (p+i);
}
return sum;
}
int main() {
float xn[100],y[100][10],p,xi,sum = 0;
int x,i,j,xt,xne;
printf("Enter how many x's are there: ");
scanf("%d",&x);
xt = x;
for(i = 0;i<x;i++){
printf("\nEnter the X%d : ",i);
scanf("%f",&xn[i]);
}
for(i = 0;i<x;i++){
printf("\nEnter the f(%.4f): ",xn[i]);
scanf("%f",&y[0][i]);
}
xt--;
for(i = 1;i<x;i++){
for(j = 0;j<xt;j++){
y[i][j] = y[i-1][j+1] - y[i-1][j];
}
xt--;
}
xt = x - 1;
printf("Enter value to be interpolated: ");
scanf("%f",&xi);
printf("Enter the position of the nearest value from down:");
scanf("%d",&xne);
p = (xi - xn[(x-(xne-1))-1])/(xn[1]-xn[0]);
sum = y[0][(x-(xne-1))-1];
for(i = 1;i<x;i++){
sum += (calcp(p,i) * y[i][((x-i)-(xne-1))-1])/(fact(i)) ;
}
printf("Interpolated value = %f",sum);
return 0;
}
Tags:
SY