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 >
BASIC Source File  |  1997-02-01  |  5KB  |  160 lines

  1.      REM   MLINREG.BAS
  2.          cls: PRINT : PRINT " This program finds the coefficients of a multiple variable linear"
  3.      PRINT " equation using the method of least squares.  The equation is of the"
  4.      PRINT " following form:"
  5.      PRINT
  6.      PRINT "          y   =   c  +  a  x   +   a  x   +   ...   a  x     "
  7.      PRINT "                         1  1       2  2             n  n    "
  8.      PRINT : PRINT
  9.      PRINT "   where: y  =  dependent variable                           "
  10.      PRINT "          c  =  constant                                     "
  11.      PRINT "          a  ,  a  ...  a   =   coefficients of independent variables"
  12.      PRINT "           1     2       n                                   "
  13.      PRINT "                                x  ,  x   ...  x             "
  14.      PRINT "                                 1     2        n            "
  15.      PRINT : PRINT
  16.      PRINT " The constant and the coefficients are printed."
  17.      PRINT
  18.      PRINT " You must provide the x and y coordinates of known data points.  Once"
  19.      PRINT " the equation has been found using the data you enter, you may predict"
  20.      PRINT " values of the dependent variables for given values of the independent"
  21.      PRINT " variables.": PRINT
  22.      INPUT " <hit any key to continue>", dummy
  23.      CLS : PRINT : PRINT " The dimension statement at line 30 limits the number of known data"
  24.      PRINT " points the equation may contain.  You can change this limit according"
  25.      PRINT " to the following scheme:"
  26.      PRINT
  27.      PRINT "       30 DIM X(N+1), S(N+1), T(N+1), A(N+1, N+2)"
  28.      PRINT : PRINT " where N  =  the number of known data points"
  29.      PRINT
  30.      PRINT " This program is from SOME COMMON BASIC PROGRAMS, 3rd edition, by"
  31.      PRINT " Lon Poole and Mary Borchers, Osborne/McGraw-Hill, Berkeley CA, 1979."
  32.      PRINT " The authors state that the program was tested on a Wang 2200 and"
  33.      PRINT " the Commodore PET (ancient history!).  Adapted where necessary to"
  34.      PRINT " Microsoft QuickBasic 4.5, and modified for more aesthetic and useful"
  35.      PRINT " output (with attention to curve-fitting for programmers), by"
  36.      PRINT " Orrin C. Winton WN1Z, 11/94 and 11/95.  The plot routine came from"
  37.      PRINT " PROGRAMMING WITH BASIC 3rd ed., by Byron S. Gottfried, McGraw-Hill,"
  38.      PRINT " NY, 1986, extensively modified by O.C. Winton.  The menu program is"
  39.      PRINT " from ADVANCED QuickBASIC by Ken Knecht, Scott, Foresman and Company,"
  40.      PRINT " Glenview, Illinois, 1989, and is modified only slightly."
  41.      PRINT
  42.      PRINT " multiple linear regression; no graph routines"
  43.      PRINT
  44.  
  45.      REM   set array limits to x(n+1), s(n+1), t(n+1), a(n+1, n+2)
  46. 30       DIM x(9), s(9), t(9), a(9, 10)
  47.      PRINT " number of known points:  ";
  48.      INPUT n
  49.      PRINT " number of independent variables:  ";
  50.      INPUT v
  51.      x(1) = 1
  52.      FOR i = 1 TO n
  53.          PRINT " point"; i
  54.          FOR j = 1 TO v
  55.          REM   enter independent variables for each point
  56.              PRINT "  variable"; j;
  57.              INPUT x(j + 1)
  58.          NEXT j
  59.          REM   enter dependent variable for each point
  60.          PRINT "  dependent variable";
  61.          INPUT x(v + 2)
  62.          REM   populate a matrix to be used in curve fitting
  63.          FOR k = 1 TO v + 1
  64.              FOR l = 1 TO v + 2
  65.                  a(k, l) = a(k, l) + x(k) * x(l)
  66.                  s(k) = a(k, v + 2)
  67.              NEXT l
  68.          NEXT k
  69.          s(v + 2) = s(v + 2) + x(v + 2) ^ 2
  70.      NEXT i
  71.  
  72.      REM   fit curve by solving the system of
  73.      REM   linear equations in matrix a()
  74.      FOR i = 2 TO v + 1
  75.          t(i) = a(1, i)
  76.      NEXT i
  77.      FOR i = 1 TO v + 1
  78.          j = i
  79. 1300     IF a(j, i) <> 0 THEN GOTO 1340
  80.          j = j + 1
  81.          IF j <= v + 1 THEN GOTO 1300
  82.          PRINT " no unique solution"
  83.          GOTO 1810
  84. 1340     FOR k = 1 TO v + 2
  85.              b = a(i, k)
  86.              a(i, k) = a(j, k)
  87.              a(j, k) = b
  88.          NEXT k
  89.          z = 1 / a(i, i)
  90.          FOR k = 1 TO v + 2
  91.              a(i, k) = z * a(i, k)
  92.          NEXT k
  93.          FOR j = 1 TO v + 1
  94.              IF j = i THEN 1490
  95.              z = -a(j, i)
  96.              FOR k = 1 TO v + 2
  97.                  a(j, k) = a(j, k) + z * a(i, k)
  98.              NEXT k
  99. 1490     NEXT j
  100.      NEXT i
  101.  
  102.      PRINT
  103.      PRINT " equation coefficients:"
  104.      PRINT "     constant = "; a(1, v + 2)
  105.      FOR i = 2 TO v + 1
  106.          PRINT " variable("; i - 1; ") = "; a(i, v + 2)
  107.      NEXT i
  108.      p = 0
  109.      FOR i = 2 TO v + 1
  110.          p = p + a(i, v + 2) * (s(i) - t(i) * s(1) / n)
  111.      NEXT i
  112.      r = s(v + 2) - s(1) ^ 2 / n
  113.      z = r - p
  114.      l = n - v - 1
  115.      PRINT
  116.      i = p / r
  117.  
  118.      PRINT " in equation form:   y  =  ";
  119.      g = 1
  120.      FOR i = 2 TO v + 1
  121.          PRINT "("; a(i, v + 2); "* x"; g; ")  +  ";
  122.          g = g + 1
  123.      NEXT i
  124.      PRINT a(1, v + 2)
  125.      PRINT
  126.  
  127.      PRINT " coefficient of determination (r^2)  = "; i
  128.      PRINT " coefficient of multiple correlation = "; SQR(i)
  129.      IF l = 0 THEN GOTO g
  130.      PRINT " standard error of estimate          = "; SQR(ABS(z / l)): GOTO h
  131. g:       PRINT " standard error of estimate          = "
  132. h:       PRINT
  133.  
  134.      REM   estimate dependent variable from entered independent variables
  135.      INPUT " do you wish to do interpolation?  enter 'y' for yes"; y$
  136.      IF y$ = "y" THEN
  137.           PRINT " enter -999 to quit interpolation"
  138.           GOTO 1710
  139.      ELSE GOTO 1810
  140.      END IF
  141.  
  142. 1710     p = a(1, v + 2)
  143.      FOR j = 1 TO v
  144.          PRINT " variable "; j;
  145.          INPUT x
  146.          REM   test for end of program
  147.          IF x = -999 THEN GOTO 1810
  148.          p = p + a(j + 1, v + 2) * x
  149.      NEXT j
  150.      PRINT " dependent variable = "; p
  151.      PRINT
  152.      GOTO 1710
  153.  
  154. 1810 COLOR 2: INPUT "hit any key to end MULTIPLE LINEAR REGRESSION sub-program", dummy
  155.      RUN "menu"
  156.      END
  157.  
  158.  
  159.  
  160.