C++ : lagrangue's interpolation


#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
int i,j,n;
float mult,sum=0,x[10],f[10],a;
cout<<"Enter the number of sample points : ";cin>>n;
for(i=0;i<n;i++)
{
        cout<<"( x["<<i<<"],f["<<i<<"] ) : ";
cin>>x[i]>>f[i];
}

cout<<"\nEnter the value of x : ";
cin>>a;

for(i=0;i<=n;i++)
{
mult=1;
for(j=0;j<=n;j++)
{
if(j!=1)
mult=mult*((a-x[j])/(x[i]-x[j]));
}
sum=sum+mult*f[i];
}
cout<<"\nThe value of f(x) = "<<sum;
getch();
return(0);
}

No comments:

Post a Comment