      INCLUDE 'misciofi.for'
      INCLUDE 'stdhdr.for'
      REAL xdata(0 : maxv), ydata(0 : maxv),indvar(0 : maxr)
      REAL depvar(0 : maxr), coef(0 : maxc), coefsig(0 : maxc)
      INTEGER numobs, i,  order
      INTEGER quit,errr, count, maxX, maxY, L
      REAL xval, randomnum
      REAL yest(0 : maxr), resid(0 : maxr)
      REAL rsq, r, see
      CHARACTER * 80   xstr, coefstr, coefsigstr, tempstr
      CHARACTER ch, scancode

      numobs = 24
      !set up dependent / independent variables}
      DO i = 0, numobs - 1
        CALL Random(RandomNum)
        xval = REAL(i)/10.0 -2
        indvar(i) = xval
        depvar(i) = 10 +xval*200.0 + (xval**2) * 30.0 + (xval**3) * 40.0
      END DO

      CALL InitSEGraphics( -1, 'C:\FOR\LIB\*.FON')
      CALL SelectColor(15)
      CALL GetMaxCoords(maxX, maxY)
      CALL DefGraphWindow(maxX / 3, 1, maxX, maxY / 2, 3)
      CALL DefGraphWindow(maxX / 3, maxY / 2, maxX, maxY, 4)
      CALL DefGraphWindow(0, 0, NINT(maxX / 3.25), maxY, 5)
      CALL SetWin2PlotRatio(3, .21, .14, .05, .2)
      CALL SetWin2PlotRatio(4, .25, .14, .05, .23)
      quit = 1
      DO WHILE ( quit .GT. 0)
        CALL SetCurrentWindow(5)
        CALL ClearWindow
        CALL BorderCurrentWindow(1)
        CALL SelectColor(15)
        CALL LabelGraphWindow(30.0,900.0,'Input Order of Curve',0,0)
        CALL LabelGraphWindow(30.0,830.0,'  must be 4 or less: ',0,0)
        CALL ReadKey(ch,scancode)
        order = ICHAR(ch)-48
        CALL LabelGraphWindow(30.0, 760.0, 'Curvefit Equation = ', 0, 0)
        CALL LabelGraphWindow(30.0, 690.0, 'Coefficient   Error ', 0, 0)
        DO i = 0, numobs - 1
          xdata(i) = indvar(i)
          ydata(i) = depvar(i)
        END DO
        IF (order .LE. 4 .AND. order .GT. 0) THEN
          CALL PolyCurveFit(indvar, depvar, numobs, order,
     +        coef, yest, resid, see, coefsig, rsq, r, errr)
          CALL LabelGraphWindow(30.0, 620.0, 'Y =    ', 0, 0)
          count = 550
          CALL SelectColor(15)
          DO i = 0, order
            xstr = '                                         '
            CALL RealToString(REAL(i), 0, 1, xstr)
            tempstr = 'X^                                    '
            l = len_trim(tempstr) + 1
            tempstr(l:l) = xstr(1:1)
            coefstr = '                                      '
            coefsi gstr = '                                   '
            CALL LabelGraphWindow(30.0, REAL(count), tempstr, 0, 0)
            CALL RealToString(coef(i), 1, 6, coefstr)
            CALL LabelGraphWindow(550.0, REAL(count), coefstr, 2, 0)
            CALL RealToString(coefsig(i), 1, 6, coefsigstr)
            CALL LabelGraphWindow(825.0, REAL(count), coefsigstr, 2, 0)
            count = count - 70
          END DO
          CALL SetTextJustifyXX(0, 0)
          xstr = '                                 '
          CALL RealToString(r, 3, 6, xstr)
          tempstr = 'R Value ='
          CALL LabelGraphWindow(30.0,REAL(count), tempstr, 0, 0)
          CALL LabelGraphWindow(500.0,REAL(count), xstr, 0, 0)
          count = count - 50

          xstr = 'R Squared Value = '
          CALL LabelGraphWindow(30.0, REAL(count), xstr, 0, 0)
          count = count - 50
          CALL RealToString(rsq, 3, 5, xstr)
          CALL LabelGraphWindow(500.0, REAL(count), xstr, 0, 0)
          count = count - 50

          CALL RealToString(see, 3, 5, xstr)
          tempstr = 'SEE = '
          CALL LabelGraphWindow(30.0, REAL(count), tempstr, 0, 0)
          CALL LabelGraphWindow(500.0, REAL(count), xstr, 0, 0)
          count = count - 50
          CALL SetLineStyleXX(0,  1)
         CALL SetCurrentWindow(3)
         CALL SetAxesType(0, 0)
         CALL ClearWindow
         CALL BorderCurrentWindow(15)
         CALL AutoAxes(xdata, ydata, numobs, 1)
         CALL LinePlotData(xdata, ydata, numobs, 1, 0)
         CALL SelectColor(15)
         CALL TitleWindow(
     +     'ORIGINAL DATA - SOLID,  FITTED DATA - DOTTED')
         CALL TitleXAxis('INDEPENDENT VARIABLE')
         CALL TitleYAxis('DEP. VAR.')
         DO i = 0, numobs - 1
           ydata(i) = yest(i)
         END DO
         CALL LinePlotData(xdata, ydata, numobs, 15, 3)
         CALL SetLineStyleXX(1,  1)
         CALL DrawGrid(10)
         CALL SetLineStyleXX(0,  1)
         CALL SetCurrentWindow(4)
         CALL SetAxesType(0, 0)
         CALL ClearWindow
         CALL BorderCurrentWindow(15)

         DO i = 0, numobs - 1
          ydata(i) = resid(i)
         END DO
        CALL AutoAxes(xdata, ydata, numobs, 1)
        CALL BarGraphData(xdata, ydata, numobs, .05, 1, 2)
        CALL SelectColor(15)
        CALL TitleWindow('CURVE FIT ERROR ANALYSIS')
        CALL TitleXAxis('INDEPENDENT VARIABLE')
        CALL TitleYAxis('RESIDUALS')
        CALL SetLineStyleXX(1, 1)
        CALL DrawGridY(10)

        CALL SetCurrentWindow(5)
        CALL LabelGraphWindow(30.0, REAL(count),
     +         'MORE <1-Y or 0-N> ?', 0, 0)
        CALL ReadKey(ch,scancode)
        quit = ICHAR(ch)-48
        CALL RealToString(REAL(order), 0,1, Tempstr)
        CALL LabelGraphWindow(650.0, REAL(count), tempstr, 0, 0)
       END IF
      END DO
      CALL CloseSEGraphics
      END

