Lagrange interpolation formula in c++
Solution in C++:
///******Bismillahir-Rahmanir-Rahim******///
///AH Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
#include<bits/stdc++.h>
using namespace std;
int main()
{
double x[1000], y[1000], xp,findpoint, yp=0,p,fxsum;
int i,j,n;
cout<<"Enter number of data: ";
cin>>n;
cout<<"Enter data:"<< endl;
for(i=1;i<=n;i++)
{
cout<<"x["<< i<<"] = ";
cin>>x[i];
cout<<"y["<< i<<"] = ";
cin>>y[i];
}
cout<<"Enter interpolation point: ";
cin>>findpoint;
// Lagrange Interpolation implement
for(i=1;i<=n;i++)
{
p=1;
for(j=1;j<=n;j++)
{
if(i!=j)
{
p = p* (findpoint - x[j])/(x[i] - x[j]);
}
}
fxsum=fxsum + p * y[i];
}
cout<< endl<<" Interpolated value at "<< findpoint<< " is "<< fxsum;
return 0;
}
No comments