Subscribe Us

Responsive Advertisement

Advertisement

7. Newton’s Backward Interpolation

 

7. Newton’s Backward Interpolation

ujjal roy



#include<bits/stdc++.h>

using namespace std;

int fact(int n)

{

    int r;

    r=1;

    for(int i=2;i<=n;i++)r*=i;

    return r;

}

double val(double v,double i)

{

    double d;


   d=v;

    for(int j=1;j<i;j++)

    {

        d*=(v+j);

    }

    return d;

}

int main()

{

   cout<<"Enter number : ";

   int n,i,j;

   cin>>n;

   double x[n],y[n][n];

   for(i=0;i<n;i++)

   {

       cout<<"Enter X and Y : ";

       cin>>x[i];

       cin>>y[i][0];

   }

   //cout<<y[0][0]<<endl;

  //difference table

  for(j=1;j<n;j++)

  {

      for(i=n-1;i>=j;i--)

      {

          y[i][j]=y[i][j-1]-y[i-1][j-1];

      }

  }

cout<<"Enter value : ";

double value;

cin>>value;

double p;

p=(value-x[n-1])/(x[1]-x[0]);

double sum;

sum=y[n-1][0];

for(i=1;i<n;i++)

{

    double  r;

    r=val(p,i);

    r/=(double)fact(i);

    r*=y[n-1][i];

    sum+=r;

}

cout<<sum<<endl;


    return 0;

}


Post a Comment

0 Comments