2018年12月23日 星期日

C C++ CODE for Newton's interpolation

C C++ CODE for Newton's interpolation

源自於
http://ganeshtiwaridotcomdotnp.blogspot.com/2009/12/c-c-code-for-newtons-interpolation.html

/***************** 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;
}


沒有留言:

張貼留言

WOKWI LED + MQTT Node-Red SQLite

WOKWI LED + MQTT Node-Red SQLite const char *mqtt_broker = "broker.mqtt-dashboard.com" ; const char *topic1 = "alex9ufo/e...