Tuesday, October 21, 2014

LAGRANGES INTERPOLATION FORMULA

#include<iostream.h>
#include<conio.h>
main()
{
    double xx;
    double x[20],y[20];
    int n,i;
    cout<<"\nEnter the number of terms=";
    cin>>n;

    for(i=0;i<n;i++)
    {
        cout<<"\nx"<<i<<"=";
        cin>>x[i];
    }
    for(i=0;i<n;i++)
    {
        cout<<"\ny"<<i<<"=";
        cin>>y[i];
    }
    clrscr();
   cout<<"\t\t<<== LAGRANGES INTERPOLATION FORMULA==>>";
    cout<<"\nx\ty\n";
    for(i=0;i<n;i++)
    {
        cout<<x[i]<<"\t"<<y[i];
        cout<<"\n";
    }
    cout<<"\nEnter the value of x=";
    cin>>xx;
    double d[20],s;
    int j,k;
    e200:i=0;
    e300:i++;
    if(xx>x[i]) goto e300;
    i--;
    for(j=0;j<n+1;j++) d[j]=1.0;
    s=0.0;
    for(k=0;k<n+1;k++)
    {
        for(j=0;j<n+1;j++)
        {
            if(j==k) goto e400;
            d[k]=d[k]*(xx-x[j+i])/(x[i+k]-x[j+i]);
            e400:;
        }
        s=s+d[k]*y[i+k];
    }

    cout<<"\nThe result is="<<s;
    return 0;
}

No comments:

Post a Comment