'**************************************************** '* Linear Least Squares Demonstration Program * '* ------------------------------------------------ * '* Reference: BASIC Scientific Subroutines, Vol. II * '* By F.R. Ruckdeschel, BYTE/McGRAWW-HILL, 1981 * '* [BIBLI 01]. * '* ------------------------------------------------ * '* SAMPLE RUN: * '* * '* This program calculates a linear * '* least squares fit to a given data set. * '* * '* INSTRUCTIONS * '* ------------ * '* * '* The number of data coordinates provided must be * '* greater than two. Otherwise, a divide by zero * '* error may result. * '* * '* Number of data points : 4 * '* * '* There are two input options: * '* 1. input coordinate pairs * '* 2. first input the independant variable values, * '* then input dependant ones. * '* Your choice (1 or 2): 1 * '* * '* ? 1 1 * '* ? 2 2 * '* ? 3 3 * '* ? 5 5.01 * '* * '* Fitted equation is: * '* Y = -4.573E-003 + 1.002571 X * '* * '* Standard deviation of fit: 2.9E-003 * '* * '**************************************************** defint i-n defdbl a-h,o-z 10 CLS PRINT PRINT " This program calculates a linear " PRINT " least squares fit to a given data set." PRINT PRINT " INSTRUCTIONS" PRINT " ------------" PRINT PRINT " The number of data coordinates provided" PRINT " must be greater than two. Otherwise, a" PRINT " divide by zero error may result." PRINT INPUT " Number of data points : ", n IF n < 3 THEN GOTO 10 DIM x(n + 1), y(n + 1) 20 PRINT PRINT " There are two input options:" PRINT " 1. input coordinate pairs" PRINT " 2. first input the independant variable values, then input dependant ones." INPUT " Your choice (1 or 2) : ", iz PRINT IF iz < 1 THEN GOTO 20 IF iz > 2 THEN GOTO 20 IF iz = 1 THEN GOTO 30 FOR m = 0 TO n - 1 PRINT m + 1; : INPUT x(m) NEXT PRINT FOR m = 0 TO n - 1 PRINT m + 1; : INPUT y(m) NEXT GOTO 50 30 FOR m = 0 TO n - 1 PRINT m + 1; : INPUT x(m), y(m) NEXT 'Call linear least squares subroutine 50 GOSUB 1000 PRINT PRINT " Fitted equation is:" PRINT " Y = "; INT(1000000 * a) / 1000000; " "; IF b > 0 THEN PRINT "+"; PRINT INT(1000000 * b) / 1000000; "X" PRINT PRINT " Standard deviation of fit: "; INT(10000 * d) / 10000 PRINT END '*************************************************** '* Linear least squares subroutine * '* ----------------------------------------------- * '* The input data set is X(m), Y(m). The number of * '* data points is n (n must be > 2). The returned * '* parameters are: a,b, coefficients of equation * '* Y = a + b X, and d, standard deviation of fit. * '*************************************************** 1000 a1 = 0: a2 = 0: b0 = 0: b1 = 0 FOR m = 0 TO n - 1 a1 = a1 + x(m) a2 = a2 + x(m) * x(m) b0 = b0 + y(m) b1 = b1 + y(m) * x(m) NEXT m% a1 = a1 / n: a2 = a2 / n: b0 = b0 / n: b1 = b1 / n d = a1 * a1 - a2 a = a1 * b1 - a2 * b0: a = a / d b = a1 * b0 - b1: b = b / d 'Evaluation of standard deviation d (unbiased estimate) d = 0 FOR m = 0 TO n - 1 d1 = y(m) - a - b * x(m) d = d + d1 * d1 NEXT d = SQR(d / (n - 2)) RETURN 'End of file Lstsqr1.bas