I need help with the following: I have a data file (columns are separated by the table "\ t"), for example, data.dat
# y1 y2 y3 y4 17.1685 21.6875 20.2393 26.3158
These are x values of 4 points for linear correspondence. Four values of y are constant: 0, 200, 400, 600 .
I can create a linear correspondence of pairs of points (x,y) : (x1,y1)=(17.1685,0), (x2,y2)=(21.6875,200), (x3,y3)=(20.2393,400), (x4,y4)=(26.3158,600) .
Now I would like to make a linear landing on three of these points paris, (x1,y1), (x2,y2), (x3,y3) and (x2,y2), (x3,y3), (x4,y4) and (x1,y1), (x3,y3), (x4,y4) and (x1,y1), (x2,y2), (x4,y4).
If I have three points with linear fit, I would like to know the x value of the extrapolated point from these three set points.
I have this awk code:
#!/usr/bin/awk -f BEGIN{ z[1] = 0; z[2] = 200; z[3] = 400; z[4] = 600; } { split($0,str,"\t"); n = 0.0; for(i=1; i<=NF; i++) { centr[i] = str[i]; n += 1.0; # printf("%d\t%f\t%.1f\t",i,centr[i],z[i]); } # print ""; if (n > 2) { lsq(n,z,centr); } } function lsq(n,x,y) { sx = 0.0 sy = 0.0 sxx = 0.0 syy = 0.0 sxy = 0.0 eps = 0.0 for (i=1;i<=n;i++) { sx += x[i] sy += y[i] sxx += x[i]*x[i] sxy += x[i]*y[i] syy += y[i]*y[i] } if ( (n==0) || ((n*sxx-sx*sx)==0) ) { next; } # print "number of data points = " n; a = (sxx*sy-sxy*sx)/(n*sxx-sx*sx) b = (n*sxy-sx*sy)/(n*sxx-sx*sx) for(i=1;i<=n;i++) { ycalc[i] = a+b*x[i] dy[i] = y[i]-ycalc[i] eps += dy[i]*dy[i] } print "# Intercept =\t"a" print "# Slope =\t"b" for (i=1;i<=n;i++) { printf("%8g %8g %8g \n",x[i],y[i],ycalc[i]) } } # function lsq()
So,
If we extrapolate to the place of 4th 0 17.1685 <--(x1,y1) 200 21.6875 <--(x2,y2) 400 20.2393 <--(x3,y3) 600 22.7692 <<< (x4 = 600,y1 = 22.7692) If we extrapolate to the place of 3th 0 17.1685 <--(x1,y1) 200 21.6875 <--(x2,y2) 400 23.6867 <<< (x3 = 400,y3 = 23.6867) 600 26.3158 <--(x4,y4) 0 17.1685 200 19.35266 <<< 400 20.2393 600 26.3158 0 18.1192 <<< 200 21.6875 400 20.2393 600 26.3158
My current output is as follows:
$> ./prog.awk data.dat