//Program for newton forward interpolation formula
#include #include #include #include #define MAXN 100 #define ORDER 4 void main() { float ax[MAXN],ay[MAXN],diff[MAXN+1][ORDER+1]; float nr=1.0,dr=1.0,x,p,h,yp; int i,n,j,k; textcolor(GREEN); clrscr(); cout<<"enter the value of n : "; cin>>n; cout<<"\nenter the values in form x,y\n"; for(i=0;i<=n;i++) cin>>ax[i]>>ay[i]; cout<<"\nenter the value of x for which y is wanted : "; cin>>x; h=ax[1]-ax[0]; for(i=0;i<=n-1;i++) diff[i][1]=ay[i+1]-ay[i]; for(j=2;j<=ORDER;j++) for(i=0;i<=n;i++) diff[i][j]=diff[i+1][j-1]-diff[i][j-1]; i=0; while(!(ax[i]>x)) i++; i--; p=(x-ax[i])/h; yp=ay[i]; for(k=1;k<=ORDER;k++) { nr*=p-k+1; dr*=k; yp+=(nr/dr)*diff[i][k]; } cout<<"\n when x = "<textcolor(YELLOW); cout<<"\n\n\a";
cout<<"\n\n";
getch(); }
output: enter the value of n : 4 enter the values in form x,y 80 5026 85 5674 90 6362 95 7088 100 7854 enter the value of x for which y is wanted : 83 when x = 83, y = 5.41e+03
|
No responses found. Be the first to respond and make money from revenue sharing program.
|