      SUBROUTINE SOLVE2(M, N, M1, A, B, W, N1, IPIVOT, QR, D,
     * ETA, FAIL, NUMIT, DIGITX,
     * X, RES, WRES, Y1, Y2, Y, F, G, MM, MMPNN)
      INTEGER IPIVOT(N)
      REAL A(MM,N), B(1), C, D(1), F(1), G(1),
     * QR(MMPNN,N), RES(1), W(M), WRES(1), X(1), Y(1), Y1(1), Y2(1)
      REAL DIGITX, DXNORM, ETA, ETA2, RDR1, RDR2, RDX1, RDX2, RNR,
     * RNX, XNORM
      DOUBLE PRECISION SUM
      LOGICAL FAIL
      NUMIT = 0
      KZ = 0
      ETA2 = ETA*ETA
      MP1 = M + 1
      MPN = M + N
      N1P1 = N1 + 1
      DO 10 I=1,M
        F(I) = B(I)*W(I)
        G(I) = 0.0
        WRES(I) = 0.0
        RES(I) = 0.0
        Y1(I) = 0.0
        IF (W(I).EQ.0.0) KZ = KZ + 1
   10 CONTINUE
      DO 20 NS=1,N
        J = M + NS
        F(J) = 0.0
        G(J) = 0.0
        X(NS) = 0.0
        Y2(NS) = 0.0
   20 CONTINUE
      K = 0
      RDX2 = 0.0
      RDR2 = 0.0
C BEGIN K-TH ITERATION STEP.
   30 IF (K.LT.2) GO TO 40
      IF (((64.*RDX2.LT.RDX1) .AND. (RDX2.GT.ETA2*RNX)) .OR.
     * ((64.*RDR2.LT.RDR1) .AND. (RDR2.GT.ETA2*RNR))) GO TO 40
      GO TO 270
   40 RDX1 = RDX2
      RDR1 = RDR2
      RDX2 = 0.0
      RDR2 = 0.0
      IF (K.EQ.0) GO TO 160
C NEW RESIDUALS.
      DO 50 I=1,M
        WRES(I) = WRES(I) + F(I)*W(I)
        IF (W(I).EQ.0.0) GO TO 50
        RES(I) = RES(I) + F(I)/W(I)
        Y1(I) = Y1(I) + G(I)
   50 CONTINUE
      DO 100 NS=1,N
        J = M + NS
        NP = IPIVOT(NS)
        X(NP) = X(NP) + F(J)
        Y2(NP) = Y2(NP) + G(J)
        SUM = -DBLE(X(NP))
        DO 60 L=1,M
          SUM = SUM + DBLE(A(L,NP))*DBLE(Y1(L))
   60   CONTINUE
        G(J) = -SUM
        IF (NS.GT.N1) GO TO 70
        GO TO 80
   70   F(J) = 0.0
        GO TO 100
   80   SUM = 0.0
        DO 90 L=1,M
          SUM = SUM + DBLE(A(L,NP))*DBLE(WRES(L))
   90   CONTINUE
        F(J) = -SUM
  100 CONTINUE
      DO 130 I=1,M
        SUM = 0.0
        IF (I.GT.M1) SUM = DBLE(RES(I))
        DO 110 L=1,N
          SUM = SUM + DBLE(A(I,L))*DBLE(X(L))
  110   CONTINUE
        SUM = SUM - DBLE(B(I))
        F(I) = -SUM
        F(I) = F(I)*W(I)
        IF (W(I).EQ.0.0) RES(I) = DBLE(RES(I)) - SUM
        SUM = 0.0
        IF (I.GT.M1) SUM = DBLE(Y1(I))
        DO 120 L=1,N
          SUM = SUM + DBLE(A(I,L))*DBLE(Y2(L))
  120   CONTINUE
        G(I) = -SUM
  130 CONTINUE
      IF (N1P1.GT.N) GO TO 160
      DO 150 I=N1P1,N
        NS = N + N1P1 - I
        J = M + NS
        SUM = 0.0
        DO 140 L=1,J
          SUM = SUM + DBLE(QR(L,NS))*DBLE(G(L))
  140   CONTINUE
        G(J) = SUM
  150 CONTINUE
C END NEW RESIDUALS.
C
  160 CALL SOLVE3(F, M1, M, N1, QR, D, Y, MMPNN)
C
      IF (N1P1.GT.N) GO TO 200
      DO 190 NS=N1P1,N
        J = M + NS
        SUM = DBLE(G(J))
        DO 170 L=MP1,J
          SUM = SUM + DBLE(QR(L,NS))*DBLE(F(L))
  170   CONTINUE
        C = SUM
        C = C/D(NS)
        DO 180 I=1,J
          F(I) = F(I) - C*QR(I,NS)
  180   CONTINUE
  190 CONTINUE
  200 DO 210 J=MP1,MPN
        G(J) = 0.0
        IF (J.LE.M+N1) G(J) = G(J) + F(J)
  210 CONTINUE
C
      CALL SOLVE3(G, M1, M, N1, QR, D, Y, MMPNN)
C
      DO 220 I=1,M
        RDR2 = RDR2 + F(I)*F(I)
  220 CONTINUE
      DO 230 I=MP1,MPN
        RDX2 = RDX2 + F(I)*F(I)
  230 CONTINUE
      IF (K.NE.0) GO TO 240
      RNR = RDR2
      RNX = RDX2
  240 IF (K.NE.1) GO TO 260
      XNORM = SQRT(RNX)
      DXNORM = SQRT(RDX2)
      IF (XNORM.NE.0.0) GO TO 250
      DIGITX = -ALOG10(ETA)
      GO TO 260
  250 DIGITX = -ALOG10(AMAX1(DXNORM/XNORM,ETA))
C END K-TH ITERATION STEP.
  260 NUMIT = K
      K = K + 1
      GO TO 30
  270 IF ((M1+KZ.EQ.M) .AND. (RDX2.GT.4.*ETA2*RNX)) GO TO 280
      IF ((RDR2.GT.4.*ETA2*RNR) .AND.
     * (RDX2.GT.4.*ETA2*RNX)) GO TO 280
      FAIL = .FALSE.
      RETURN
  280 FAIL = .TRUE.
      RETURN
      END
