# polfit.f

          SUBROUTINE POLFIT (N, X, Y, W, MAXDEG, NDEG, EPS, R, IERR, A)
    C***BEGIN PROLOGUE  POLFIT
    C***PURPOSE  Fit discrete data in a least squares sense by polynomials
    C            in one variable.
    C***LIBRARY   SLATEC
    C***CATEGORY  K1A1A2
    C***TYPE      SINGLE PRECISION (POLFIT-S, DPOLFT-D)
    C***KEYWORDS  CURVE FITTING, DATA FITTING, LEAST SQUARES, POLYNOMIAL FIT
    C***AUTHOR  Shampine, L. F., (SNLA)
    C           Davenport, S. M., (SNLA)
    C           Huddleston, R. E., (SNLL)
    C***DESCRIPTION
    C
    C     Abstract
    C
    C     Given a collection of points X(I) and a set of values Y(I) which
    C     correspond to some function or measurement at each of the X(I),
    C     subroutine  POLFIT  computes the weighted least-squares polynomial
    C     fits of all degrees up to some degree either specified by the user
    C     or determined by the routine.  The fits thus obtained are in
    C     orthogonal polynomial form.  Subroutine  PVALUE  may then be
    C     called to evaluate the fitted polynomials and any of their
    C     derivatives at any point.  The subroutine  PCOEF  may be used to
    C     express the polynomial fits as powers of (X-C) for any specified
    C     point C.
    C
    C     The parameters for  POLFIT  are
    C
    C     Input --
    C         N -      the number of data points.  The arrays X, Y and W
    C                  must be dimensioned at least  N  (N .GE. 1).
    C         X -      array of values of the independent variable.  These
    C                  values may appear in any order and need not all be
    C                  distinct.
    C         Y -      array of corresponding function values.
    C         W -      array of positive values to be used as weights.  If
    C                  W(1) is negative,  POLFIT  will set all the weights
    C                  to 1.0, which means unweighted least squares error
    C                  will be minimized.  To minimize relative error, the
    C                  user should set the weights to:  W(I) = 1.0/Y(I)**2,
    C                  I = 1,...,N .
    C         MAXDEG - maximum degree to be allowed for polynomial fit.
    C                  MAXDEG  may be any non-negative integer less than  N.
    C                  Note -- MAXDEG  cannot be equal to  N-1  when a
    C                  statistical test is to be used for degree selection,
    C                  i.e., when input value of  EPS  is negative.
    C         EPS -    specifies the criterion to be used in determining
    C                  the degree of fit to be computed.
    C                  (1)  If  EPS  is input negative,  POLFIT  chooses the
    C                       degree based on a statistical F test of
    C                       significance.  One of three possible
    C                       significance levels will be used:  .01, .05 or
    C                       .10.  If  EPS=-1.0 , the routine will
    C                       automatically select one of these levels based
    C                       on the number of data points and the maximum
    C                       degree to be considered.  If  EPS  is input as
    C                       -.01, -.05, or -.10, a significance level of
    C                       .01, .05, or .10, respectively, will be used.
    C                  (2)  If  EPS  is set to 0.,  POLFIT  computes the
    C                       polynomials of degrees 0 through  MAXDEG .
    C                  (3)  If  EPS  is input positive,  EPS  is the RMS
    C                       error tolerance which must be satisfied by the
    C                       fitted polynomial.  POLFIT  will increase the
    C                       degree of fit until this criterion is met or
    C                       until the maximum degree is reached.
    C
    C     Output --
    C         NDEG -   degree of the highest degree fit computed.
    C         EPS -    RMS error of the polynomial of degree  NDEG .
    C         R -      vector of dimension at least NDEG containing values
    C                  of the fit of degree  NDEG  at each of the  X(I) .
    C                  Except when the statistical test is used, these
    C                  values are more accurate than results from subroutine
    C                  PVALUE  normally are.
    C         IERR -   error flag with the following possible values.
    C             1 -- indicates normal execution, i.e., either
    C                  (1)  the input value of  EPS  was negative, and the
    C                       computed polynomial fit of degree  NDEG
    C                       satisfies the specified F test, or
    C                  (2)  the input value of  EPS  was 0., and the fits of
    C                       all degrees up to  MAXDEG  are complete, or
    C                  (3)  the input value of  EPS  was positive, and the
    C                       polynomial of degree  NDEG  satisfies the RMS
    C                       error requirement.
    C             2 -- invalid input parameter.  At least one of the input
    C                  parameters has an illegal value and must be corrected
    C                  before  POLFIT  can proceed.  Valid input results
    C                  when the following restrictions are observed
    C                       N .GE. 1
    C                       0 .LE. MAXDEG .LE. N-1  for  EPS .GE. 0.
    C                       0 .LE. MAXDEG .LE. N-2  for  EPS .LT. 0.
    C                       W(1)=-1.0  or  W(I) .GT. 0., I=1,...,N .
    C             3 -- cannot satisfy the RMS error requirement with a
    C                  polynomial of degree no greater than  MAXDEG .  Best
    C                  fit found is of degree  MAXDEG .
    C             4 -- cannot satisfy the test for significance using
    C                  current value of  MAXDEG .  Statistically, the
    C                  best fit found is of order  NORD .  (In this case,
    C                  NDEG will have one of the values:  MAXDEG-2,
    C                  MAXDEG-1, or MAXDEG).  Using a higher value of
    C                  MAXDEG  may result in passing the test.
    C         A -      work and output array having at least 3N+3MAXDEG+3
    C                  locations
    C
    C     Note - POLFIT  calculates all fits of degrees up to and including
    C            NDEG .  Any or all of these fits can be evaluated or
    C            expressed as powers of (X-C) using  PVALUE  and  PCOEF
    C            after just one call to  POLFIT .
    C
    C***REFERENCES  L. F. Shampine, S. M. Davenport and R. E. Huddleston,
    C                 Curve fitting by polynomials in one variable, Report
    C                 SLA-74-0270, Sandia Laboratories, June 1974.
    C***ROUTINES CALLED  PVALUE, XERMSG
    C***REVISION HISTORY  (YYMMDD)
    C   740601  DATE WRITTEN
    C   890531  Changed all specific intrinsics to generic.  (WRB)
    C   890531  REVISION DATE from Version 3.2
    C   891214  Prologue converted to Version 4.0 format.  (BAB)
    C   900315  CALLs to XERROR changed to CALLs to XERMSG.  (THJ)
    C   920501  Reformatted the REFERENCES section.  (WRB)
    C   920527  Corrected erroneous statements in DESCRIPTION.  (WRB)
    C***END PROLOGUE  POLFIT