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

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