All Programs are Written and Compiled in Dev C++. So, it may generate some error in case of other compilers and may need some modifications in program. Download Dev C++

Wednesday 8 April 2015

Interpolation using Newton's Backward Interpolation Formula

#include<iostream>
#define N 4
using namespace std;
int main()
{
float ax[10],ay[10],dtab[10][N+1],x,h,p,y;
int i,j,k,n;
cout<<"Enter Value of n: ";
cin>>n;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
dtab[i][j]=0.0;

cout<<"Enter Value for X and Y respectively\n";
cout<<"X   Y\n";
for(i=0;i<n;i++)
cin>>ax[i]>>ay[i];
cout<<"Enter the value of X for which Y is Required: ";
cin>>x;
h=ax[1]-ax[0];

for(i=0;i<=n-1;i++)
dtab[i][1]=ay[i+1]-ay[i];
for(j=2;j<=N;j++)
{
for(i=0;i<n-j;i++)
dtab[i][j]=dtab[i+1][j-1]-dtab[i][j-1];
}
    for(i=0;i<n&&x>ax[i];i++);
    i--;
    p=(x-ax[i])/h;
    y=ay[i];
    float nr=1.0,dr=1.0;
    for (k=1;k<=N;k++)
    {
        nr*=p+k-1;
        dr*=k;
        y+=(nr/dr)*dtab[--i][k];
    }
    cout<<"\nFor X="<<x<<"\t"<<"Corresponding Y="<<y;
}

No comments:

Post a Comment