Example-
Find the vaiue of f(153)for the given table.y=f(x)
x y=f(x)
150 12.247152 12.329
154 12.410
156 12.490
C++ implementation of Lagrange's interpolation-
#include iostream .h
#include stdio.h
#include conio.h
#include math.h
void main()
{
float x[10],y[10],temp=1,f[10],sum,p;
int i,n,j,k=0,c;
clrscr();
cout<<" how many record you will enter:";
cin>>n;
for(i=0;i>x[i];
cout<<"enter the value of y["<>y[i];
}
cout<<"enter the x for finding f(x):";
cin>>p;
for(i=0;in;i++)
{
cout<<"enter the value of x["<<i<<"]";
cin>>x[i];
cout<<"enter the value of y["<<i<<"]";
cin>>y[i];
}
cout<<"enter the x for finding f(x):";
cin>>p;
for(i=0;i<n;i++)
{
temp=1;
k=i;
for(j=0;j<n;j++)
{
if(k==j)
continue;
else
temp=temp*((p-x[j])/(x[k]-x[j]));
f[i]=y[i]*temp;
}
for(i=0;i<n;i++)
sum=sum+f[i];
cout<<"the value of f["<<p<<"]="<<sum;
getch();
}
OUTPUT-
how many record you will enter:4
enter the value of x[0]=150
enter the value of y[0]=12.247
enter the value of x[1]=152
enter the value of y[1]=12.329
enter the value of x[2]=154
enter the value of y[2]=12.410
enter the value of x[3]=156
enter the value of y[3]=12.490
enter the x for finding f(x)=153
the value of f[153]=12.369625
Comments :
Post a Comment