This commit was generated by cvs2svn to compensate for changes in r2,
[u/mrichter/AliRoot.git] / MINICERN / mathlib / gen / e / lfit.F
1 *
2 * $Id$
3 *
4 * $Log$
5 * Revision 1.1.1.1  1996/04/01 15:02:28  mclareni
6 * Mathlib gen
7 *
8 *
9 #include "gen/pilot.h"
10 #if defined(CERNLIB_FORTRAN)||!defined(CERNLIB_CDC)
11       SUBROUTINE LFIT(X,Y,L,KEY,A,B,E)
12 C
13 C     TO FIT A STRAIGHT LINE    Y=A*X+B    TO L POINTS WITH ERROR E
14 C     SEE MENZEL , FORMULAS OF PHYSICS P.116
15 C     POINTS WITH Y=0 ARE IGNOERD IF KEY=0
16 C     L IS NO. OF POINTS
17 C
18       DIMENSION X(1),Y(1)
19 C
20 C     CALCULATE SUMS
21       IF(L-2) 25,1,1
22     1 COUNT=0.0
23       SUMX=0.0
24       SUMY=0.0
25       SUMXY=0.0
26       SUMXX=0.0
27       SUMYY=0.0
28       DO 10 J=1,L
29       IF(Y(J).EQ.0..AND.KEY.EQ.0) GO TO 10
30       SUMX=SUMX+X(J)
31       SUMY=SUMY+Y(J)
32       COUNT=COUNT+1.0
33    10 CONTINUE
34       IF(COUNT.LE.1.) GO TO 25
35       YMED=SUMY/COUNT
36       XMED=SUMX/COUNT
37       DO 20 J=1,L
38       IF(Y(J).EQ.0..AND.KEY.EQ.0) GO TO 20
39       SCARTX=X(J)-XMED
40       SCARTY=Y(J)-YMED
41       SUMXY=SUMXY+SCARTX   *SCARTY
42       SUMXX=SUMXX+SCARTX   *SCARTX
43       SUMYY=SUMYY+SCARTY   *SCARTY
44    20 CONTINUE
45 C
46 C     FIT PARAMETERS
47       IF(SUMXX.EQ.0.) GO TO 25
48       A=SUMXY/SUMXX
49       B=YMED-A*XMED
50       IF(COUNT.LT.3.) GO TO 101
51       E=(SUMYY-SUMXY*A          )/(COUNT-2.0)
52       GO TO 100
53 C
54 C     ISUFFICIENT POINTS
55    25 A=0.0
56       B=0.0
57   101 E=0.0
58   100 RETURN
59       END
60 #endif