C C++ CODE for Newton's interpolation

Working C C++  Source code program for Newton's interpolation
/***************** Newtons interpolation **************/
#include<iostream.h>
#include<conio.h>
int main()
{
    int n,i,j;
    float x[10],f[10],a,sum=0,mult;
    clrscr();
    cout<<"No of sample points ? ";
    cin>>n;
    cout<<"\nAll x with corresponding f(x) ";
    for(i=0;i<n;i++)
    cin>>x[i]>>f[i];
    cout<<"\nEnter x for calculation ";
    cin>>a;

    for(j=0;j<n-1;j++)
    {
        for(i=n-1;i>j;i--)
            f[i]=(f[i]-f[i-1])/(x[i]-x[i-j-1]);
    }
    for(i=n-1;i>=0;i--)
    {
        mult=1;
        for(j=0;j<i;j++)
            mult*=(a-x[j]);
            
        mult*=f[j];
        sum+=mult;
    }
    cout<<"The result is: "<<sum;
    getch();
    return 0;
}



1 comment :

  1. can help me please?

    to create a code for newton,
    like this code, but can visible the polynomial first, then the result

    i had try to modif this code, but still cannot get the code to visible the polynomial.

    thank's
    hansen

    ReplyDelete

Your Comment and Question will help to make this blog better...