PROGRAM LSQUAR ************************************************************************ * Program to find the equation of the least-squares line for a set * * of data points. Identifiers used are: * * X, Y : (X,Y) is the observed data point * * ENDATA : end-of-data flag (parameter) * * COUNT : number of data points * * SUMX : sum of the Xs * * SUMX2 : sum of the squares of the Xs * * SUMY : sum of the Ys * * SUMXY : sum of the products X*Y * * XMEAN : mean of the Xs * * YMEAN : mean of the Ys * * SLOPE : slope of least-squares line * * YINT : y-intercept of the line * * * * Input: A collection of data points * * Output: The equation of the least-squares line * ************************************************************************ INTEGER COUNT REAL X, Y, ENDATA, SUMX, SUMX2, SUMY, SUMXY, XMEAN, YMEAN, + SLOPE, YINT PARAMETER (ENDATA = -999.0) * Initialize counter and the sums to 0 and read first data point COUNT = 0 SUMX = 0 SUMX2 = 0 SUMY = 0 SUMXY = 0 PRINT *, 'TO STOP, ENTER', ENDATA, ' FOR COORDINATES OF POINT.' PRINT *, 'ENTER POINT:' READ *, X, Y * While there is more data, calculate the necessary sums * and read the next data point (X, Y) 10 IF ((X .NE. ENDATA) .AND. (Y .NE. ENDATA)) THEN COUNT = COUNT + 1 SUMX = SUMX + X SUMX2 = SUMX2 + X ** 2 SUMY = SUMY + Y SUMXY = SUMXY + X * Y PRINT *, 'ENTER NEXT POINT:' READ *, X, Y GO TO 10 END IF * Find equation of least-squares line XMEAN = SUMX / COUNT YMEAN = SUMY / COUNT SLOPE = (SUMXY - SUMX * YMEAN) / (SUMX2 - SUMX * XMEAN) YINT = YMEAN - SLOPE * XMEAN PRINT * PRINT *, 'EQUATION OF LEAST-SQUARES LINE IS Y = MX + B, WHERE' PRINT *, 'SLOPE = M = ', SLOPE PRINT *, 'Y-INTERCEPT = B =', YINT END