      SUBROUTINE TOL(X,N)
C
C     PURPOSE--THIS SUBROUTINE COMPUTES NORMAL AND
C              DISTRIBUTION-FREE TOLERANCE LIMITS 
C              FOR THE DATA IN THE INPUT VECTOR X.
C              15 NORMAL TOLERANCE LIMITS ARE COMPUTED; AND 
C              30 DISTRIBUTION-FREE TOLERANCE LIMITS ARE COMPUTED.
C     INPUT ARGUMENTS--X      = THE SINGLE PRECISION VECTOR OF
C                               (UNSORTED OR SORTED) OBSERVATIONS.
C                      N      = THE INTEGER NUMBER OF OBSERVATIONS
C                               IN THE VECTOR X.
C     OUTPUT--2 PAGES OF AUTOMATIC PRINTOUT--
C             1 PAGE GIVING NORMAL TOLERANCE LIMITS; AND
C             1 PAGE GIVING DISTRIBUTION-FREE TOLERANCE LIMITS.
C     PRINTING--YES.
C     RESTRICTIONS--THERE IS NO RESTRICTION ON THE MAXIMUM VALUE
C                   OF N FOR THIS SUBROUTINE.
C     OTHER DATAPAC   SUBROUTINES NEEDED--NONE.
C     FORTRAN LIBRARY SUBROUTINES NEEDED--SQRT.
C     MODE OF INTERNAL OPERATIONS--SINGLE PRECISION.
C     LANGUAGE--ANSI FORTRAN. 
C     REFERENCES--GARDINER AND HULL, TECHNOMETRICS, 1966, PAGES 115-122
C               --WILKS, ANNALS OF MATHEMATICAL STATISTICS, 1941, PAGE 92
C               --MOOD AND GRABLE, PAGES 416-417
C     WRITTEN BY--JAMES J. FILLIBEN
C                 STATISTICAL ENGINEERING LABORATORY (205.03)
C                 NATIONAL BUREAU OF STANDARDS
C                 WASHINGTON, D. C. 20234
C                 PHONE--301-921-2315
C     ORIGINAL VERSION--JUNE      1972. 
C     UPDATED         --NOVEMBER  1975. 
C
C---------------------------------------------------------------------
C
      DIMENSION X(1)
      DIMENSION PA(6),PC(6),Z1(3),A(6),B(6),C(6),RSMALL(5,6),USMALL(6,6)
      DIMENSION TMIN(3,6),TMAX(3,6)
      DIMENSION P(10),C1(10),C2(10),C3(10)
C
      DATA PA(1),PA(2),PA(3),PA(4),PA(5),PA(6)/50.,75.,90.,95.,99.,99.9/
      DATA PC(1),PC(2),PC(3)/90.,95.,99./
      DATA Z1(1),Z1(2),Z1(3)/-1.28155157,-1.64485363,-2.32634787/
      DATA A(1),A(2),A(3),A(4),A(5),A(6)/.6745,1.1504,1.6449,1.9600,2.57
     158,3.2905/
      DATA B(1),B(2),B(3),B(4),B(5),B(6)/.33734,.57335,.82140,.97910,1.2
     1889,1.64038/
      DATA C(1),C(2),C(3),C(4),C(5),C(6)/-0.15460,-0.02991,.22044,.40675
     1,.85514,1.42601/
      DATA RSMALL(1,1),RSMALL(1,2),RSMALL(1,3),RSMALL(1,4),RSMALL(1,5),
     1RSMALL(1,6)            /1.0505,1.6859,2.2844,2.6463,3.3266,4.0903/
      DATA RSMALL(2,1),RSMALL(2,2),RSMALL(2,3),RSMALL(2,4),RSMALL(2,5),
     1RSMALL(2,6)            /0.8557,1.4333,2.0078,2.3624,3.0368,3.7983/
      DATA RSMALL(3,1),RSMALL(3,2),RSMALL(3,3),RSMALL(3,4),RSMALL(3,5),
     1RSMALL(3,6)            /0.7929,1.3412,1.8979,2.2457,2.9128,3.6708/
      DATA RSMALL(4,1),RSMALL(4,2),RSMALL(4,3),RSMALL(4,4),RSMALL(4,5),
     1RSMALL(4,6)            /0.7622,1.2940,1.8388,2.1815,2.8422,3.5965/
      DATA RSMALL(5,1),RSMALL(5,2),RSMALL(5,3),RSMALL(5,4),RSMALL(5,5),
     1RSMALL(5,6)            /0.7442,1.2654,1.8019,2.1408,2.7963,3.5472/
      DATA USMALL(1,1),USMALL(1,2),USMALL(1,3)/0.,0.,0./
      DATA USMALL(2,1),USMALL(2,2),USMALL(2,3)/7.9579,15.9472,79.7863/
      DATA USMALL(3,1),USMALL(3,2),USMALL(3,3)/3.0808,4.4154,9.9749/
      DATA USMALL(4,1),USMALL(4,2),USMALL(4,3)/2.2658,2.9200,5.1113/
      DATA USMALL(5,1),USMALL(5,2),USMALL(5,3)/1.9393,2.3724,3.6692/
      DATA USMALL(6,1),USMALL(6,2),USMALL(6,3)/1.7621,2.0893,3.0034/
      DATA P(1),P(2),P(3),P(4),P(5),P(6),P(7),P(8),P(9),P(10)
     1/50.,75.,90.,95.,97.5,99.,99.5,99.9,99.95,99.99/
C
      IPR=6
C
C     CHECK THE INPUT ARGUMENTS FOR ERRORS
C
      IF(N.LT.1)GOTO50
      IF(N.EQ.1)GOTO55
      HOLD=X(1)
      DO60I=2,N
      IF(X(I).NE.HOLD)GOTO90
   60 CONTINUE
      WRITE(IPR, 9)HOLD
      RETURN
   50 WRITE(IPR,15) 
      WRITE(IPR,47)N
      RETURN
   55 WRITE(IPR,18) 
      RETURN
   90 CONTINUE
    9 FORMAT(1H ,109H***** NON-FATAL DIAGNOSTIC--THE FIRST  INPUT ARGUME
     1NT (A VECTOR) TO THE TOL    SUBROUTINE HAS ALL ELEMENTS = ,E15.8,6
     1H *****)
   15 FORMAT(1H , 91H***** FATAL ERROR--THE SECOND INPUT ARGUMENT TO THE
     1 TOL    SUBROUTINE IS NON-POSITIVE *****)
   18 FORMAT(1H ,100H***** NON-FATAL DIAGNOSTIC--THE SECOND INPUT ARGUME
     1NT TO THE TOL    SUBROUTINE HAS THE VALUE 1 *****)
   47 FORMAT(1H , 35H***** THE VALUE OF THE ARGUMENT IS ,I8   ,6H *****)
C
C-----START POINT-----------------------------------------------------
C
      AN=N
C
C     COMPUTE NORMAL TOLERANCE LIMITS
C
C     COMPUTE THE SAMPLE MEAN 
C
      XBAR=0.0
      DO100I=1,N
      XBAR=XBAR+X(I)
  100 CONTINUE
      XBAR=XBAR/AN
C
C     COMPUTE THE SAMPLE STANDARD DEVIATION
C
      VAR=0.0
      DO200I=1,N
      VAR=VAR+(X(I)-XBAR)**2
  200 CONTINUE
      VAR=VAR/(AN-1.0)
      SD=SQRT(VAR)
C
C     COMPUTE THE NORMAL TOLERANCE LIMITS
C
      DO 300 I=1,3
      Z=Z1(I)
      F=N-1
      IF(N.LE.6)U=USMALL(N,I) 
      IF(N.LE.6)GOTO390
      D1=1.0+Z*SQRT(2.0)/SQRT(F)
      D2=2.0*(Z**2-1.0)/(3.0*F)
      D3=(Z**3-7.0*Z)/(9.0*SQRT(2.0)*F**1.5)
      D4=(6.0*Z**4+14.0*Z**2-32.0)/(405.0*F**2.0) 
      D5=(9.0*Z**5+256.0*Z**3-433.0*Z)/(4860.0*SQRT(2.0)*F**2.5)
      D6=(12.0*Z**6-243.0*Z**4-923.0*Z**2+1472.0)/(25515.0*F**3.0)
      D7=(3753.0*Z**7+4353.0*Z**5-289517.0*Z**3-289717.0*Z)/(9185400.0*S
     1QRT(2.0)*F**3.5)
      UNIV=D1+D2+D3-D4+D5+D6-D7
      U=1.0/UNIV
      U=SQRT(U)
  390 DO 400 J=1,6
      R=A(J)+(B(J)/(C(J)+AN)) 
      IF(N.LE.5)R=RSMALL(N,J) 
      AK=R*U
      TMIN(I,J)=XBAR-AK*SD
      TMAX(I,J)=XBAR+AK*SD
  400 CONTINUE
  300 CONTINUE
C
C     WRITE OUT THE NORMAL TOLERANCE LIMITS
C
      WRITE(IPR,998)
      WRITE(IPR,605)N
      WRITE(IPR,999)
      WRITE(IPR,609)
      WRITE(IPR,610)
      WRITE(IPR,999)
      WRITE(IPR,615)XBAR,SD
      WRITE(IPR,999)
      WRITE(IPR,999)
      DO 600 I=1,3
      DO 700 J=1,6
      WRITE(IPR,620)PC(I),PA(J),TMIN(I,J),TMAX(I,J)
  700 CONTINUE
      WRITE(IPR,999)
  600 CONTINUE
C
C
C
C
C     COMPUTE DISTRIBUTION-FREE TOLERANCE LIMITS
C
      K=N/2
      NUMSEC=3
      IF(K.LT.NUMSEC)NUMSEC=K 
C
C     DETERMINE THE SMALLEST 3 AND LARGEST 3 OBSERVATIONS
C
      LOCMIN=1
      XMIN=X(1)
      DO800I=1,N
      IF(X(I).LE.XMIN)LOCMIN=I
      IF(X(I).LE.XMIN)XMIN=X(I)
  800 CONTINUE
      LOCMAX=1
      XMAX=X(1)
      DO850I=1,N
      IF(X(I).GE.XMAX)LOCMAX=I
      IF(X(I).GE.XMAX)XMAX=X(I)
  850 CONTINUE
      DO900I=1,N
      IF(I.NE.LOCMIN)GOTO910
  900 CONTINUE
  910 LOCMN2=I
      XMIN2=X(I)
      DO950I=1,N
      IF(I.EQ.LOCMIN)GOTO950
      IF(X(I).LE.XMIN2)LOCMN2=I
      IF(X(I).LE.XMIN2)XMIN2=X(I)
  950 CONTINUE
      DO1000I=1,N
      IF(I.NE.LOCMAX)GOTO1010 
 1000 CONTINUE
 1010 LOCMX2=I
      XMAX2=X(I)
      DO1050I=1,N
      IF(I.EQ.LOCMAX)GOTO1050 
      IF(X(I).GE.XMAX2)LOCMX2=I
      IF(X(I).GE.XMAX2)XMAX2=X(I)
 1050 CONTINUE
      DO1100I=1,N
      IF(I.NE.LOCMIN.AND.I.NE.LOCMN2)GOTO1110
 1100 CONTINUE
 1110 LOCMN3=I
      XMIN3=X(I)
      DO1150I=1,N
      IF(I.EQ.LOCMIN.OR.I.EQ.LOCMN2)GOTO1150
      IF(X(I).LE.XMIN3)LOCMN3=I
      IF(X(I).LE.XMIN3)XMIN3=X(I)
 1150 CONTINUE
      DO1200I=1,N
      IF(I.NE.LOCMAX.AND.I.NE.LOCMX2)GOTO1210
 1200 CONTINUE
 1210 LOCMX3=I
      XMAX3=X(I)
      DO1250I=1,N
      IF(I.EQ.LOCMAX.OR.I.EQ.LOCMX2)GOTO1250
      IF(X(I).GE.XMAX3)LOCMX3=I
      IF(X(I).GE.XMAX3)XMAX3=X(I)
 1250 CONTINUE
      AN1=AN-1.0
      AN2=AN-2.0
      AN3=AN-3.0
      AN4=AN-4.0
      AN5=AN-5.0
      AN6=AN-6.0
      DO1600I=1,10
      D=P(I)/100.0
      C1(I)=(D**AN1)*(-AN   +AN1*D)
      C1(I)=1.0-C1(I)
      Q=1.0-D
      T=Q*AN
      C1(I)=1.0+AN1*Q
      C1(I)=1.0-(D**AN1)*C1(I)
      C1(I)=C1(I)*100.0
      IF(NUMSEC.EQ.1)GOTO1600 
      A0=6.0*D*D*D
      A1=2.0-7.0*D+11.0*D*D
      A2=-3.0+6.0*D 
      A3=1.0
      C2(I)=A0+A1*T+A2*T*T+A3*T*T*T
      C2(I)=1.0-(D**AN3)*C2(I)/6.0
      C2(I)=C2(I)*100.0
      IF(NUMSEC.EQ.2)GOTO1600 
      A0=120.0*D*D*D*D*D
      A1=24.0-126.0*D+274.0*D*D-326.0*D*D*D+274.0*D*D*D*D
      A2=-50.0+205.0*D-320.0*D*D+225.0*D*D*D
      A3=35.0-100.0*D+85.0*D*D
      A4=-10.0+15.0*D
      A5=1.0D0
      C3(I)=A0+A1*T+A2*T*T+A3*T*T*T+A4*T*T*T*T+A5*T*T*T*T*T 
      C3(I)=1.0-(D**AN5)*C3(I)/120.0
      C3(I)=C3(I)*100.0
 1600 CONTINUE
C
C     WRITE OUT THE DISTRIBUTION-FREE TOLERANCE LIMITS
C
      WRITE(IPR,998)
      WRITE(IPR,205)N
      WRITE(IPR,999)
      WRITE(IPR,209)
      WRITE(IPR,210)
      WRITE(IPR,999)
      WRITE(IPR,999)
      IF(NUMSEC.EQ.1)GOTO1850 
      IF(NUMSEC.EQ.2)GOTO1750 
      DO1700I=1,10
      WRITE(IPR,217)C3(I),P(I),XMIN3,XMAX3
 1700 CONTINUE
      WRITE(IPR,999)
 1750 DO1800I=1,10
      WRITE(IPR,216)C2(I),P(I),XMIN2,XMAX2
 1800 CONTINUE
      WRITE(IPR,999)
 1850 DO1900I=1,10
      WRITE(IPR,215)C1(I),P(I),XMIN,XMAX
 1900 CONTINUE
C
  605 FORMAT(1H ,45H             NORMAL TOLERANCE LIMITS FOR THE ,I6,13H
     1 OBSERVATIONS)
  609 FORMAT(1H ,49H             REFERENCE--CRC HANDBOOK, PAGES 32-35)
  610 FORMAT(1H ,77H             REFERENCE--GARDINER AND HULL, TECHNOMET
     1RICS, 1966, PAGES 115-122)
  615 FORMAT(1H ,27H             SAMPLE MEAN = ,E15.8 ,37H         SAMPL
     1E STANDARD DEVIATION = ,E15.8 )
  620 FORMAT(1H ,7HWE ARE ,F6.2,24H PERCENT CONFIDENT THAT ,F5.2,49H PER
     1CENT OF THE POPULATION IS BETWEEN XBAR-K*S = ,E12.5,16H AND XBAR+K
     1*S = ,E12.5)
  205 FORMAT(1H ,55H            DISTRIBUTION-FREE TOLERANCE LIMITS FOR T
     1HE ,I6,13H OBSERVATIONS)
  209 FORMAT(1H ,51H            REFERENCE--WILKS, ANNALS, 1941, PAGE 92)
  210 FORMAT(1H ,53H            REFERENCE--MOOD AND GRABLE, PAGES 416-41
     17)
  215 FORMAT(1H ,7HWE ARE ,F6.2,24H PERCENT CONFIDENT THAT ,F5.2,45H PER
     1CENT OF THE POPULATION IS BETWEEN XMIN = ,F8.3,14H AND XMAX   = ,F
     18.3)
  216 FORMAT(1H ,7HWE ARE ,F6.2,24H PERCENT CONFIDENT THAT ,F5.2,45H PER
     1CENT OF THE POPULATION IS BETWEEN X2   = ,F8.3,14H AND X(N-1) = ,F
     18.3)
  217 FORMAT(1H ,7HWE ARE ,F6.2,24H PERCENT CONFIDENT THAT ,F5.2,45H PER
     1CENT OF THE POPULATION IS BETWEEN X3   = ,F8.3,14H AND X(N-2) = ,F
     18.3)
  998 FORMAT(1H1)
  999 FORMAT(1H )
C
      RETURN
      END 
