%N wpfit %F $MIR/src/subs/wpfit.for
%D Weighted least squares polynomial fit.
%P rjs
%: polynomials
%B
      SUBROUTINE WPFIT(ND,NP,X,Y,W,A,RNORM,PHI,PHIX,IERR)

      IMPLICIT NONE
      INTEGER ND,NP,IERR
      REAL X(NP),Y(NP),W(NP),A(ND+1),PHI(2,ND+1),PHIX(4,NP),RNORM

     Weighted least squares polynomial fit.

  Input:
    ND         Poly order.
    NP         Number of points.
    X,Y        X and Y values.
    W          Weights.
  Scratch:
    PHI        At least of size 2*(nd + 1)
    PHIX       At least of size 4*NP
  Output:
    IERR       0 on success. 1 if there is a dimension problem. 3 is there
               is a negative weight.
    RNORM      Weighted error, sqrt(Sum W(i)*(y(i)-poly)**2)
    A          Poly coefficients.
                 poly = a(1) + a(2)*x + a(3)*x**2 ...

  Algorithm:
    The abcissas, X(i) are mapped into the interval [-1,1]. Then the
    Forsythe procedure is used.
