To find corresponding f(x) value of given x value using a set of given data by Lagrange's interpolation method
/* To find corresponding f(x) value of given x value using a set of given data by Lagrange's interpolation method */
#include<stdio.h>
int main()
{
float x[20],f[20],xn,fxn=0,p[30];
int n,i,j;
printf("Enter the no of data:");
scanf("%d",&n);
printf("Enter the values of xi:\n");
for(i=0;i<n;i++)
{
printf("x[%d]=",i);
scanf("%f",&x[i]);
}
printf("Enter the values of fi:\n");
for(i=0;i<n;i++)
{
printf("f[%d]=",i);
scanf("%f",&f[i]);
}
printf("Enter the values of xn:");
scanf("%f",&xn);
for(i=0;i<n;i++)
{
p[i]=1;
for(j=0;j<n;j++)
{
if(i==j)
{
continue;
}
else
{
p[i]=p[i]*((xn-x[j])/(x[i]-x[j]));
}
}
}
for(i=0;i<n;i++)
{
fxn=fxn+(p[i]*f[i]);
}
printf("Result is %f\n",fxn);
}
#include<stdio.h>
int main()
{
float x[20],f[20],xn,fxn=0,p[30];
int n,i,j;
printf("Enter the no of data:");
scanf("%d",&n);
printf("Enter the values of xi:\n");
for(i=0;i<n;i++)
{
printf("x[%d]=",i);
scanf("%f",&x[i]);
}
printf("Enter the values of fi:\n");
for(i=0;i<n;i++)
{
printf("f[%d]=",i);
scanf("%f",&f[i]);
}
printf("Enter the values of xn:");
scanf("%f",&xn);
for(i=0;i<n;i++)
{
p[i]=1;
for(j=0;j<n;j++)
{
if(i==j)
{
continue;
}
else
{
p[i]=p[i]*((xn-x[j])/(x[i]-x[j]));
}
}
}
for(i=0;i<n;i++)
{
fxn=fxn+(p[i]*f[i]);
}
printf("Result is %f\n",fxn);
}
Comments
Post a Comment