	SUBROUTINE SOLVE(X,Y,V,NUMB,XM,XB,VM,VB)
C***********************************************************************
C
C  SUBROUTINE SOLVES MX+B=Y BY LEAST SQUARES
C
C  FORMAL PARAMETERS ARE:
C      X=ARRAY OF Xi VALUES
C      Y=ARRAY OF Yi VALUES
C      V=ARRAY OF VARIANCES OF Yi
C      NUMB=NUMBER OF Xi VALUES
C      XM=CALCULATED SLOPE(=M)
C      XB=CALCULATED INTERCEPT(=B OF MX+B=Y)
C      VM=CALCULATED VARIANCE OF XM
C      VB=CALCULATED VARIANCE OF XB
C
C***********************************************************************
	COMMON/IO/ IRD,ITYP,IPRT,ISCRT,ICOBRG,LIMPRT,IDBUG
	DIMENSION X(1),Y(1),V(1)
	DIMENSION XTX(2,2),XTXINV(2,2),XTY(2,1)
C
C-----ZERO ARRAYS FOR SUMMING
C
	DO 110 I=1,2
	DO 100 J=1,2
100	XTX(I,J)=0.0
110	XTY(I,1)=0.0
C
C-----FORM MATRIX PRODUCT  (Xi 1)TR * (Xi 1)
C
	DO 120 I=1,NUMB
	XTX(1,1)=XTX(1,1)+X(I)*X(I)/V(I)
	XTX(2,2) = XTX(2,2) +1.0/V(I)
120	XTX(1,2)=XTX(1,2)+X(I)*1/V(I)
C-----MATRIX XTX IS SYMMETRIC
	XTX(2,1)=XTX(1,2)
C
C-----CALCULATE THE DETERMINANT
C
	DET=XTX(1,1)*XTX(2,2)-XTX(1,2)*XTX(2,1)
C
C-----CALCULATE THE INVERSE
C
	XTXINV(1,1)=XTX(2,2)/DET
	XTXINV(1,2)=-XTX(2,1)/DET
	XTXINV(2,1)=-XTX(1,2)/DET
	XTXINV(2,2)= XTX(1,1)/DET
C
C-----CALCULATE THE OBSERVATION MATRIX  (Xi 1)TR*Yi
C
	DO 140 I=1,NUMB
	XTY(1,1)=XTY(1,1)+X(I)*Y(I)/V(I)
	XTY(2,1)=XTY(2,1)+ 1 *Y(I)/V(I)
140	CONTINUE
C
C-----CALCULATE THE SLOPE (XM) AND INTERCEPT (XB)
C
	XM=XTXINV(1,1)*XTY(1,1)+XTXINV(1,2)*XTY(2,1)
	XB=XTXINV(2,1)*XTY(1,1)+XTXINV(2,2)*XTY(2,1)
C
C-----TEST IF NUMB=2. i.e. AN EXACT SOLUTION
C
	VM=0.0
	VB=0.0
	IF(NUMB.EQ.2) RETURN
C
C-----C ALCULATE THE 'UNBIASED ESTIMATE OF SIGMA**2'
C
	SUM=0.0
	DO 210 I=1,NUMB
	R=Y(I)-XM*X(I)-XB
	SUM=SUM+R*R/V(I)
210	CONTINUE
	SUM=SUM/(NUMB-2)
C
C-----CALCULATE THE ESTIMATED ERROR IN SLOPE AND INTERCEPT
C
	VM=SUM*XTXINV(1,1)
	VB=SUM*XTXINV(2,2)
C
C----TEST IF HIGHLY CORRELATED
C
	SUM=SUM*XTXINV(1,2)
	IF(SUM.LT.0.25) RETURN
	WRITE(IPRT,220)
220	FORMAT(/' Caution - the slope and intercept are highly'
     *' correlated')
	RETURN
	END
