BACKWARD DIFFERENCE PROGRAM IN C
#include<stdio.h> #include<conio.h> void main() { float x[10],y[10],d[10][10]; float a,u,sum=0,prod; int i,j,n,k; clrscr(); printf("\n enter the no of points"); scanf("%d",&n); printf("\nenter the value for interpolation"); scanf("%f",&a); printf("\n enter the value for x"); for(i=1;i<=n;i++) { scanf("%f",&x[i]); } printf("\n enter the value for y"); for(i=1;i<=n;i++) { scanf("%f",&y[i]); } if ((a<x[1])||(a>x[n])) { printf("\n values lies outside the range"); exit(); } i=1; while(a>x[i]) { i++; } k=i; printf("\n k=%d",k); for(j=1;j<=n;j++) { for(i=j+1;i<=n;i++) { if(j==1) { d[i][j]=y[i]-y[i-1]; printf("\n d[i][j]=%f",d[i][j]); } else d[i][j]=d[i][j-1]-d[i-1][j-1]; printf("\n d[i][j]=%f",d[i][j]); }
} u=(a-x[k])/(x[k]-x[k-1]); printf("\n u=%f",u); sum=y[k]; printf("\n sum=%f",sum); for(i=1;i<=(k-1);i++) { prod=1.0; for(j=0;j<=i-1;j++) { prod=prod*(u+j); } sum=sum+d[k][i]*prod/fact(i); } printf("\n interpolated value=%f",sum); getch(); }
int fact(int m) { int f=1,i; for(i=2;i<=m;i++) f=f*i; return(f); }
0 comments:
Post a Comment