home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
World of Ham Radio 1997
/
WOHR97_AmSoft_(1997-02-01).iso
/
antenna
/
ant_21
/
prog
/
mlinreg.bas
< prev
next >
Wrap
BASIC Source File
|
1997-02-01
|
5KB
|
160 lines
REM MLINREG.BAS
cls: PRINT : PRINT " This program finds the coefficients of a multiple variable linear"
PRINT " equation using the method of least squares. The equation is of the"
PRINT " following form:"
PRINT
PRINT " y = c + a x + a x + ... a x "
PRINT " 1 1 2 2 n n "
PRINT : PRINT
PRINT " where: y = dependent variable "
PRINT " c = constant "
PRINT " a , a ... a = coefficients of independent variables"
PRINT " 1 2 n "
PRINT " x , x ... x "
PRINT " 1 2 n "
PRINT : PRINT
PRINT " The constant and the coefficients are printed."
PRINT
PRINT " You must provide the x and y coordinates of known data points. Once"
PRINT " the equation has been found using the data you enter, you may predict"
PRINT " values of the dependent variables for given values of the independent"
PRINT " variables.": PRINT
INPUT " <hit any key to continue>", dummy
CLS : PRINT : PRINT " The dimension statement at line 30 limits the number of known data"
PRINT " points the equation may contain. You can change this limit according"
PRINT " to the following scheme:"
PRINT
PRINT " 30 DIM X(N+1), S(N+1), T(N+1), A(N+1, N+2)"
PRINT : PRINT " where N = the number of known data points"
PRINT
PRINT " This program is from SOME COMMON BASIC PROGRAMS, 3rd edition, by"
PRINT " Lon Poole and Mary Borchers, Osborne/McGraw-Hill, Berkeley CA, 1979."
PRINT " The authors state that the program was tested on a Wang 2200 and"
PRINT " the Commodore PET (ancient history!). Adapted where necessary to"
PRINT " Microsoft QuickBasic 4.5, and modified for more aesthetic and useful"
PRINT " output (with attention to curve-fitting for programmers), by"
PRINT " Orrin C. Winton WN1Z, 11/94 and 11/95. The plot routine came from"
PRINT " PROGRAMMING WITH BASIC 3rd ed., by Byron S. Gottfried, McGraw-Hill,"
PRINT " NY, 1986, extensively modified by O.C. Winton. The menu program is"
PRINT " from ADVANCED QuickBASIC by Ken Knecht, Scott, Foresman and Company,"
PRINT " Glenview, Illinois, 1989, and is modified only slightly."
PRINT
PRINT " multiple linear regression; no graph routines"
PRINT
REM set array limits to x(n+1), s(n+1), t(n+1), a(n+1, n+2)
30 DIM x(9), s(9), t(9), a(9, 10)
PRINT " number of known points: ";
INPUT n
PRINT " number of independent variables: ";
INPUT v
x(1) = 1
FOR i = 1 TO n
PRINT " point"; i
FOR j = 1 TO v
REM enter independent variables for each point
PRINT " variable"; j;
INPUT x(j + 1)
NEXT j
REM enter dependent variable for each point
PRINT " dependent variable";
INPUT x(v + 2)
REM populate a matrix to be used in curve fitting
FOR k = 1 TO v + 1
FOR l = 1 TO v + 2
a(k, l) = a(k, l) + x(k) * x(l)
s(k) = a(k, v + 2)
NEXT l
NEXT k
s(v + 2) = s(v + 2) + x(v + 2) ^ 2
NEXT i
REM fit curve by solving the system of
REM linear equations in matrix a()
FOR i = 2 TO v + 1
t(i) = a(1, i)
NEXT i
FOR i = 1 TO v + 1
j = i
1300 IF a(j, i) <> 0 THEN GOTO 1340
j = j + 1
IF j <= v + 1 THEN GOTO 1300
PRINT " no unique solution"
GOTO 1810
1340 FOR k = 1 TO v + 2
b = a(i, k)
a(i, k) = a(j, k)
a(j, k) = b
NEXT k
z = 1 / a(i, i)
FOR k = 1 TO v + 2
a(i, k) = z * a(i, k)
NEXT k
FOR j = 1 TO v + 1
IF j = i THEN 1490
z = -a(j, i)
FOR k = 1 TO v + 2
a(j, k) = a(j, k) + z * a(i, k)
NEXT k
1490 NEXT j
NEXT i
PRINT
PRINT " equation coefficients:"
PRINT " constant = "; a(1, v + 2)
FOR i = 2 TO v + 1
PRINT " variable("; i - 1; ") = "; a(i, v + 2)
NEXT i
p = 0
FOR i = 2 TO v + 1
p = p + a(i, v + 2) * (s(i) - t(i) * s(1) / n)
NEXT i
r = s(v + 2) - s(1) ^ 2 / n
z = r - p
l = n - v - 1
PRINT
i = p / r
PRINT " in equation form: y = ";
g = 1
FOR i = 2 TO v + 1
PRINT "("; a(i, v + 2); "* x"; g; ") + ";
g = g + 1
NEXT i
PRINT a(1, v + 2)
PRINT
PRINT " coefficient of determination (r^2) = "; i
PRINT " coefficient of multiple correlation = "; SQR(i)
IF l = 0 THEN GOTO g
PRINT " standard error of estimate = "; SQR(ABS(z / l)): GOTO h
g: PRINT " standard error of estimate = "
h: PRINT
REM estimate dependent variable from entered independent variables
INPUT " do you wish to do interpolation? enter 'y' for yes"; y$
IF y$ = "y" THEN
PRINT " enter -999 to quit interpolation"
GOTO 1710
ELSE GOTO 1810
END IF
1710 p = a(1, v + 2)
FOR j = 1 TO v
PRINT " variable "; j;
INPUT x
REM test for end of program
IF x = -999 THEN GOTO 1810
p = p + a(j + 1, v + 2) * x
NEXT j
PRINT " dependent variable = "; p
PRINT
GOTO 1710
1810 COLOR 2: INPUT "hit any key to end MULTIPLE LINEAR REGRESSION sub-program", dummy
RUN "menu"
END