#include <stdio.h>
#define n 7
double x[10],y[10];
double lag(double xx)
{
double l,s=0;
int i,k;
for(i=0;i<n;i++)
{
l=1;
for(k=0;k<n;k++)
if(k-i)
l=l*(xx-x[k])/(x[i]-x[k]);
s=s+l*y[i];
}
return(s);
}
void main()
{
FILE *f;
int i;
f=fopen("lag2.txt","r");
for(i=0;i<n;i++)
fscanf(f,"%lf",&x[i]);
for(i=0;i<n;i++)
fscanf(f,"%lf",&y[i]);
fclose(f);
printf("%lf\n",lag(1.8));
printf("%lf\n",lag(6.15));
}
lag2.txt
1 2 3 4 5 6 7
0.368 0.135 0.050 0.018 0.007 0.002 0.001