Damn,
Here is a debug version that show the formula with expanded values, it might help us find the issue.
Note data file should not contain the heading ( #X Y
) line:
awk '
{ P[$1]=$2 ; m=$1>m?$1:m; x[NR]=$1 ; y[NR]=$2 }
END {
for(i=1;i<=m;i++) {
if (i in P) printf "%0.4f %0.4f\n", i, P;
else { printf "%0.4f %0.4f\n", i, \
(((i-x[2]) * (i-x[3])) / ((x[1]-x[2]) * (x[1]-x[3]))) * y[1] + \
(((i-x[1]) * (i-x[3])) / ((x[2]-x[1]) * (x[2]-x[3]))) * y[2] + \
(((i-x[1]) * (i-x[2])) / ((x[3]-x[1]) * (x[3]-x[1]))) * y[3] ;
printf " (((%f-%f) * (%f-%f)) / ((%f-%f) * (%f-%f))) * %f +\n",
i,x[2],i,x[3],x[1],x[2],x[1],x[3],y[1];
printf " (((%f-%f) * (%f-%f)) / ((%f-%f) * (%f-%f))) * %f +\n",
i,x[1],i,x[3],x[2],x[1],x[2],x[3],y[2];
printf " (((%f-%f) * (%f-%f)) / ((%f-%f) * (%f-%f))) * %f\n",
i,x[1],i,x[2],x[3],x[1],x[3],x[1],y[3];
printf " = %f + %f + %f\n",
(((i-x[2]) * (i-x[3])) / ((x[1]-x[2]) * (x[1]-x[3]))) * y[1],
(((i-x[1]) * (i-x[3])) / ((x[2]-x[1]) * (x[2]-x[3]))) * y[2],
(((i-x[1]) * (i-x[2])) / ((x[3]-x[1]) * (x[3]-x[1]))) * y[3] ;
}
}
}' infile
Exampe output for X=7.0:
7.0000 16.2130
(((7.000000-4.000000) * (7.000000-8.000000)) / ((1.000000-4.000000) * (1.000000-8.000000))) * 22.312500 +
(((7.000000-1.000000) * (7.000000-8.000000)) / ((4.000000-1.000000) * (4.000000-8.000000))) * 22.500000 +
(((7.000000-1.000000) * (7.000000-4.000000)) / ((8.000000-1.000000) * (8.000000-1.000000))) * 22.187500
= -3.187500 + 11.250000 + 8.150510