!     *********************
!     INITIATION of PROGRAM
!     *********************
      program LSCF
c	Least Square Curve Fitting

!     ***********************
!     DEFINITION of VARIABLES
!     ***********************
      implicit none

	integer size, N
	parameter (size=100, N=11)

c	dimension x(100), f(100)
c	dimension x(size), f(size)
      real*8 x(size), sum1, sum2, sum3, sum4, A, B, x_kout, f_x
	 
      integer i, j(size), f(size)

!	************************
!	DEFINITION of FORMATTING
!	************************

!	Format of the 'read' files 
5	format(i4,x,f10.5,x,i4)

!	Format of the 'write' files 
15    format(f10.5,x,f10.5)
25    format('A',x,'B')


!	**************
!	OPEN DATA FILE
!	**************
!	Open the READ files
      open(10,file='LSCF_Data.txt')  !Opens the x, f datasets

!	Open the WRITE/PRINT files
	open(20,file='AB.txt')

	write(20,25)

!	**************
!	READ DATA FILE
!	**************
!	Read the available LSCF_Data file containing x=lnz, f=u values 
      do i=1,N
	 read(10,*) j(i), x(i), f(i)
	enddo

!	Inititalize/Nullize the summators for iterative process
	sum1=0.
	sum2=0.
	sum3=0.
	sum4=0.

	do i=1,N
	 sum1=sum1+x(i)       !Ó[x(i)]
	 sum2=sum2+f(i)		  !Ó[f(i)]
	 sum3=sum3+x(i)**2	  !Ó[x(i)^2]
	 sum4=sum4+x(i)*f(i)  !Ó[x(i)*f(i)]
	enddo

	A=(N*sum4-sum1*sum2)/(N*sum3-sum1**2)
	B=(sum3*sum2-sum1*sum4)/(N*sum3-sum1**2)

!	write(20,25)    !Done already before READ
	write(20,15) A, B
	write(*,*) 'A=', A, 'B=', B
	pause    
	
	x_kout=1.252762968
	f_x=A*x_kout+B
	write(*,*) 'f_x=', f_x
	pause

      close(10)
	close(20)
	      
      stop
      
      end