home *** CD-ROM | disk | FTP | other *** search
/ World of Ham Radio 1997 / WOHR97_AmSoft_(1997-02-01).iso / antenna / ant_21 / prog / geomreg.bas < prev    next >
BASIC Source File  |  1997-02-01  |  6KB  |  177 lines

  1.     REM   GEOMREG.BAS
  2.  
  3.         cls: PRINT : PRINT " This program fits a geometric curve to a set of coordinates using"
  4.     PRINT " the method of least squares.  The equation, coefficient of determination,"
  5.     PRINT " coefficient of correlation and standard error of estimate are printed."
  6.     PRINT
  7.     PRINT " From SOME COMMON BASIC PROGRAMS, 3rd edition, by Lon Poole and Mary"
  8.     PRINT " Borchers, Osborne/McGraw-Hill, Berkeley CA, 1979.  Authors state that"
  9.     PRINT " the programs were tested on the Wang 2200 and Commodore PET (ancient"
  10.     PRINT " history!).  Adapted where necessary to Microsoft QuickBasic 4.5, and"
  11.     PRINT " modified for more aesthetic and useful output (esp. as an aid in curve-"
  12.     PRINT " fitting for programmers), and compiled, by Orrin C. Winton WN1Z,"
  13.     PRINT " 11/94 and 11/95.  The plot routine came from PROGRAMMING WITH BASIC 3rd ed.,"
  14.     PRINT " by Byron S. Gottfried, McGraw-Hill, NY, 1986, extensively modified by"
  15.     PRINT " O.C. Winton.  The menu program is from ADVANCED QuickBASIC by Ken Knecht,"
  16.     PRINT " Scott, Foresman and Company, Glenview, Illinois, 1989, and is modified"
  17.     PRINT " only slightly."
  18.     PRINT
  19.     PRINT " You must provide the x and y coordinates of known data points.  Once"
  20.     PRINT " the curve has been fitted you may predict values of y for given values"
  21.     PRINT " of x."
  22.     PRINT
  23.     PRINT " geometric regression"
  24.     PRINT
  25.     PRINT " number of known points:  ";
  26.     INPUT n
  27.     DIM x(30)
  28.     DIM y(30)
  29.     DIM o(30)
  30.     DIM p(30)
  31.     j = 0
  32.     k = 0
  33.     l = 0
  34.     m = 0
  35.     r2 = 0
  36.     REM   enter coordinates of data points
  37.     FOR i = 1 TO n
  38.           PRINT " x,y of point "; i;
  39.           INPUT x(i), y(i)
  40.           REM   accumulate intermediate values
  41.           IF x(i) = 0 GOTO skipx
  42. rety:     IF y(i) = 0 GOTO skipy
  43.           y1 = LOG(y(i))
  44.           IF x(i) = 0 GOTO logskipx
  45.           x1 = LOG(x(i)): GOTO logskipx
  46.  
  47. skipx:    x1 = 0
  48.           GOTO rety
  49.  
  50. skipy:    y1 = 0
  51.           GOTO logskipx
  52.  
  53. logskipx: j = j + x1
  54.           k = k + y1
  55.           l = l + x1 ^ 2
  56.           m = m + y1 ^ 2
  57.           r2 = r2 + x1 * y1
  58.     NEXT i
  59.  
  60.     REM   calculate and print coefficients of equation
  61.     b = (n * r2 - k * j) / (n * l - j ^ 2)
  62.     a = (k - b * j) / n
  63.     PRINT
  64.     PRINT " f(x) = "; EXP(a); "* (x ^"; b; ")"
  65.     PRINT
  66.  
  67.     REM   calculate regression analysis
  68.     j = b * (r2 - j * k / n)
  69.     m = m - k ^ 2 / n
  70.     k = m - j
  71.     PRINT
  72.     r2 = j / m
  73.     PRINT " coefficient of determination (r^2)  =  "; r2
  74.     PRINT " coefficient of correlation          =  "; SQR(r2)
  75.     IF (n - 2) <= 0 OR k < 0 THEN GOTO g
  76.     PRINT " standard error of estimate          =  "; SQR(k / (n - 2)): GOTO h
  77. g:  PRINT " standard error of estimate          =  "
  78. h:  PRINT
  79.  
  80.     REM   estimate y-coordinate from entered x-coordinate
  81.     INPUT " do you wish to do interpolation?  enter 'y' for yes"; y$
  82.     IF y$ = "y" THEN
  83.         PRINT " enter -999 to quit interpolation"
  84.         GOTO a
  85.     ELSE GOTO b
  86.     END IF
  87.  
  88. a:      PRINT " x = ";
  89.     INPUT x
  90.     IF x = -999 THEN GOTO b
  91.     PRINT " y = "; EXP(a) * x ^ b
  92.     PRINT
  93.     GOTO a
  94. b:  REM
  95.  
  96.  
  97. 2115 ' ******* Prepare to plot regression equation y-values against
  98. 2116 '         user-input x values.  Not user-input y-values.
  99. 2117 '
  100. 2120 FOR i = 1 TO n
  101. 2122     o(i) = x(i)
  102. 2124 NEXT i
  103.  
  104. 2126 FOR i = 1 TO n
  105. 2128     p(i) = EXP(a) * (o(i) ^ b)
  106. 2130 NEXT i
  107.  
  108. 2270 ' ******* Find Largest and Smallest X and Y (the user-input x,y values)
  109. 2280 '
  110. 2290 XMAX = -100000!: YMAX = -100000!: XMIN = 100000!: YMIN = 100000!
  111. 2300 FOR i = 1 TO n
  112. 2310    IF x(i) > XMAX THEN XMAX = x(i)
  113. 2320    IF y(i) > YMAX THEN YMAX = y(i)
  114. 2330    IF x(i) < XMIN THEN XMIN = x(i)
  115. 2340    IF y(i) < YMIN THEN YMIN = y(i)
  116. 2350 NEXT i
  117. 2360
  118. 2370 ' ******* Scale the Xs and Ys
  119. 2380 '
  120. 2390 FOR i = 1 TO n
  121. 2400     x(i) = 29 + INT(280 * (x(i) - XMIN) / (XMAX - XMIN))
  122. 2410     y(i) = 189 - INT(150 * (y(i) - YMIN) / (YMAX - YMIN))
  123. 2420 NEXT i
  124.  
  125. 2470 ' ******* Plot the Graphical Display of user-input x,y values
  126. 2480 '
  127. 2490 SCREEN 9
  128. 2500 LINE (19, 29)-(19, 199): LINE -(319, 199)
  129. 2510 FOR IY = 59 TO 179 STEP 20: LINE (19, IY)-(23, IY): NEXT IY
  130. 2520 FOR IX = 39 TO 299 STEP 20: LINE (IX, 199)-(IX, 195): NEXT IX
  131. 2530 '
  132. 2540 FOR i = 1 TO n
  133. 2550     PSET (x(i), y(i)): LINE (x(i), y(i) - 2)-(x(i) - 4, y(i) + 2)
  134. 2560     LINE -(x(i) + 4, y(i) + 2): LINE -(x(i), y(i) - 2)
  135. 2570 NEXT i
  136. 2580 '
  137. 2590 FOR i = 1 TO n - 1
  138. 2600     LINE (x(i), y(i))-(x(i + 1), y(i + 1))
  139. 2610 NEXT i
  140.  
  141. 2770 ' ******* Find Largest and Smallest o and p
  142. 2780 '
  143. 2790 OMAX = -100000!: PMAX = -100000!: OMIN = 100000!: PMIN = 100000!
  144. 2800 FOR i = 1 TO n
  145. 2810    IF o(i) > OMAX THEN OMAX = o(i)
  146. 2820    IF p(i) > PMAX THEN PMAX = p(i)
  147. 2830    IF o(i) < OMIN THEN OMIN = o(i)
  148. 2840    IF p(i) < PMIN THEN PMIN = p(i)
  149. 2850 NEXT i
  150. 2860
  151. 2870 ' ******* Scale the Os and Ps
  152. 2880 '
  153. 2890 FOR i = 1 TO n
  154. 2900     o(i) = 29 + INT(280 * (o(i) - OMIN) / (OMAX - OMIN))
  155. 2910     p(i) = 189 - INT(150 * (p(i) - PMIN) / (PMAX - PMIN))
  156. 2920 NEXT i
  157.  
  158. 3000 ' ******* Plot the Regression Equation y-values against
  159. 3002 '         user-input x values.  Not user-input y-values.
  160. 3010 '
  161. 3015 COLOR 4
  162. 3020 FOR i = 1 TO n - 1
  163. 3030     LINE (o(i), p(i))-(o(i + 1), p(i + 1))
  164. 3040 NEXT i
  165. 3045 COLOR 7
  166. 3050 PRINT " f(x) = "; EXP(a); "* (x ^"; b; ")"
  167. 3060 PRINT : PRINT : PRINT : PRINT : PRINT : PRINT : PRINT : PRINT : PRINT : PRINT : PRINT : PRINT : PRINT : PRINT : PRINT : PRINT
  168. 3064 PRINT " White curve w/markers:  your x,y inputs."
  169. 3065 COLOR 4
  170. 3066 PRINT " Red curve is eqn's y based on your x-inputs."
  171. 3068 PRINT " Eqn's fit is close to perfect if red line overwrites white line."
  172. 3069 COLOR 2
  173. 3070 INPUT " hit any key to end GEOMETRIC REGRESSION sub-program", dummy
  174. 3080 RUN "menu"
  175. 3099 END
  176.  
  177.