The lagrangian interpolation or lagrangian
polynomials are perhaps the simplest way to exhibit the existence of a
polynomial for interpolation with unequally spaced data. Data where the x-value
is not equi-spaced often occurs as the result of experimental observation or
when historical data are examined.
The
lagrangian polynomial equation is made up of
‘n’ terms, each of which is a ‘n-1’ – ary degree where ‘n’ is the number
of set of data. It, in fact passes through each point used in its construction.
SOURCE CODE
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
double n,xx,mul;int i,j;
int x[50],y[50];
double sum=0;
cout<<"input the value of n ";
cin>>n;
cout<<"enter the value of x and y:";
for(i=0;i<n;i++)
{
cin>>x[i];
cin>>y[i];
}
cout<<"input point at
which value is unknown:";
cin>>xx;
for(i=0;i<n;i++)
{
mul=1;
for(j=0;j<n;j++)
{
if(i!=j)
mul=mul*(xx-x[i])/(x[i]-x[j]);
}
sum=sum+mul*y[i];
}
cout<<"result="<<sum;
}
It can also be implemented as following:
#include<iostream>
using namespace std;
int main(){
float *x,
*y, result = 0, a;
int size;
cout<<"Enter the number of data : ";
cin>>size;
x = new
float[size];
y = new
float[size];
cout<<"Enter the data (x y):"<<endl;
for (int i
= 0; i < size; i++)
cin>>x[i]>>y[i];
cout<<"Enter the desired x : ";
cin>>a;
for (int i
= 0; i < size; i++){
float
product = 1;
for
(int j = 0; j < size; j++){
if(j != i)
product *= ((a - x[j])/(x[i] - x[j]));
}
result
+= product*y[i];
}
cout<<"y("<<a<<") =
"<<result<<endl;
return 0;
}
No comments:
Post a Comment