'******************************************************* '* Least Squares of order 1 or 2 Demonstration Program * '* --------------------------------------------------- * '* Reference: BASIC Scientific Subroutines, Vol. II * '* By F.R. Ruckdeschel, BYTE/McGRAWW-HILL, 1981 [1]. * '* --------------------------------------------------- * '* FIRST SAMPLE RUN: * '* * '* This program calculates a linear or parabolic least * '* squares fit to a given data set. * '* * '* INSTRUCTIONS * '* ------------ * '* * '* The number of data coordinates provided must be * '* greater than three. Otherwise, a divide by zero * '* error may result. * '* * '* Order of fit (1 or 2): 1 * '* 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 * '* * '* SECOND SAMPLE RUN: * '* * '* Order of fit (1 or 2): 2 * '* 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 4 * '* ? 3 9 * '* ? 5 24.95 * '* * '* Fitted equation is: * '* Y = -1.7728E-002 + 2.2045E-002 X + 0.994318 X^2 * '* * '* Standard deviation of fit: 4.7E-003 * '* * '******************************************************* DEFINT I-N DEFDBL A-H, O-Z 10 CLS PRINT PRINT "LEAST SQUARES CURVE FIT ROUTINE" PRINT PRINT "This program calculates a linear or parabolic least" PRINT "squares fit to a given data set" PRINT PRINT "INSTRUCTIONS" PRINT "------------" PRINT PRINT " The number of data coordinates provided must be greater" PRINT " than three. Otherwise, a divide by zero error may result." PRINT INPUT " Order of fit (1 or 2): ", iorder IF iorder < 1 THEN iorder = 1 IF iorder > 2 THEN iorder = 2 INPUT " Number of data points: ", n IF n < 4 THEN GOTO 10 DIM x(n + 1), y(n + 1) 20 PRINT PRINT " There are two input options:" PRINT " 1. input coordinate pairs (Example: 1,2.5)" 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 'read data from screen (option 2) FOR m = 0 TO n - 1 PRINT m + 1; : INPUT x(m) NEXT m PRINT FOR m = 0 TO n - 1 PRINT m + 1; : INPUT y(m) NEXT m GOTO 50 'read data from screen (option 1) 30 FOR m = 0 TO n - 1 PRINT m + 1; : INPUT x(m), y(m) NEXT m 'Call linear or parabolic least squares subroutine 50 IF iorder = 1 THEN gosub 1000 IF iorder = 2 THEN gosub 2000 'Write results to screen taking care of zero, one or minus one values PRINT PRINT " FITTED EQUATION IS:" PRINT " Y = "; IF a <> 0 THEN PRINT INT(1000000 * a) / 1000000; " "; IF b <> 0 THEN IF b > 0 AND a <> 0 THEN PRINT "+"; IF ABS(b) <> 1 THEN PRINT INT(1000000 * b) / 1000000; "X"; ELSEIF b > 0 THEN PRINT "X" ELSE PRINT "-X" END IF END IF IF iorder > 1 THEN IF c <> 0 THEN IF c > 0 AND a <> 0 AND b <> 0 THEN PRINT " +"; IF ABS(c) <> 1 THEN PRINT INT(1000000 * c) / 1000000; "X^2" ELSEIF c > 0 THEN PRINT "X^2" ELSE PRINT "-X^2" END IF END IF END IF PRINT PRINT " STANDARD DEVIATION OF FIT: "; INT(10000 * d) / 10000 END 'End lstsqr2b.bas '**************************************** '* Linear Least Squares Subroutine * '* ------------------------------------ * '* In: integer n = number of points * '* n values x(i), y(i) * '* Out: coefficients a,b of fit (a+b*x) * '* standard deviation d * '**************************************** 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 estim%ate) d = 0 FOR m = 0 TO n - 1 d1 = y(m) - a - b * x(m) d = d + d1 * d1 NEXT m d = SQR(d / (n - 2)) RETURN '************************************************ '* Parabolic least squares subroutine * '* -------------------------------------------- * '* In: integer n = number of points * '* n values x(i), y(i) * '* Out: coefficients a,b,c of fit (a+b*x+c*x^2) * ' standard deviation d shared with main * '************************************************ 2000 a0 = 1: a1 = 0: a2 = 0: a3 = 0: a4 = 0 b0 = 0: b1 = 0: b2 = 0 FOR m = 0 TO n - 1 a1 = a1 + x(m) a2 = a2 + x(m) * x(m) a3 = a3 + x(m) * x(m) * x(m) a4 = a4 + x(m) * x(m) * x(m) * x(m) b0 = b0 + y(m) b1 = b1 + y(m) * x(m) b2 = b2 + y(m) * x(m) * x(m) NEXT m a1 = a1 / n: a2 = a2 / n: a3 = a3 / n: a4 = a4 / n b0 = b0 / n: b1 = b1 / n: b2 = b2 / n d = a0 * (a2 * a4 - a3 * a3) - a1 * (a1 * a4 - a2 * a3) + a2 * (a1 * a3 - a2 * a2) a = b0 * (a2 * a4 - a3 * a3) + b1 * (a2 * a3 - a1 * a4) + b2 * (a1 * a3 - a2 * a2) a = a / d b = b0 * (a2 * a3 - a1 * a4) + b1 * (a0 * a4 - a2 * a2) + b2 * (a1 * a2 - a0 * a3) b = b / d c = b0 * (a1 * a3 - a2 * a2) + b1 * (a2 * a1 - a0 * a3) + b2 * (a0 * a2 - a1 * a1) c = c / d 'Evaluation of standard deviation d d = 0 FOR m = 0 TO n - 1 d1 = y(m) - a - b * x(m) - c * x(m) * x(m) d = d + d1 * d1 NEXT m d = SQR(d / (n - 3)) RETURN 'End of file lstsqr2b.bas 