subroutine  aitken8(g8, x0, eps, lnit, rad, kod)
! Solutia ecuatiei X=G(X), cu extrapolarea Aitken.
! Parametri formali:
!	g: numele functiei g(x).
!	x0: aproximatia initiala.
!	eps: toleranta pentru corectie
!	lnit: - INTRARE: numarul maxim de iteratii; - IESIRE: numarul efectiv de iteratii.	
!	rad : solutia ecuatiei/ultima aproximatie.
!	kod : cod de intrare/iesire.
!		  INTRARE: cod de tiparire: =0: NU; #0: Da - tipareste iteratiile
!		  IESIRE: cod de terminare:
!			0:  solutia gasita cu toleranta "eps".
!			1:  depasirea nr.limita de iteratii
!		    2: proces stationar
!	       -1:  numitor nul in formula metodei.

implicit real(8) (a-h, o-z)
common p	!  parametru pt. functia G


real(8) num	! numitor
logical print_it
real(8) aa(0:lnit)	! Tablou pentru toate val. - pt. proces stationar
integer period8, per
integer width

ULP(x) =2.0**(exponent(x) -53)
eps_min =ULP(x0)
if(eps <eps_min) then
eps =eps_min
write(1,*) '* Corectat toleranta:', eps
endif

print_it =(kod /=0)
iwlnit =width(lnit)
	write(1, '(a, G24.15, a, 1pG10.3)') ' Aprox. initiala:',x0,';  Toleranta:',eps
    write(1, '(a, I<iwlnit>, /)') ' Numar limita de iteratii: ', lnit
    
	if(print_it) then
	write(1,'(1x, a, t14, a, t28, a)') 'Iteratia', 'x', 'dif'
	endif

aa =0
aa(0) =x0

iter =0

100	iter =iter+1

x1 =g8(x0)

iwiter =width(iter)

! Test-1 pentru iteratia obisnuita:
dif =x1 -x0
if (abs(dif) <= EPS) then
	kod =0;
	write(*, '(a, I<iwiter>, a, I<iwiter>, a, 1pg24.15)') ' * x(', iter, ') -x(', iter-1, ') =', dif

	write(1,'(/, a, I<iwiter>, a, 1pg24.15)') ' Iteratia ', iter,':', x1
	write(1, '(a, I<iwiter>, a, I<iwiter>, a, 1pg24.15)') ' * x(', iter, ') -x(', iter-1, ') =', dif

a =x1
goto 500
endif


x2 =g8(x1)

! Test-2:
dif =x2 -x1
if(abs(dif) <= EPS) then
	kod =0;
	write(*, '(a, I<iwiter>, a, I<iwiter>, a, 1pg14.7)') ' * x(', iter, ') -x(', iter-1, ') =', dif

	write(1, '(/, a, I<iwiter>, a, 1pg24.15)') ' Iteratia ', iter,':', x2
	write(1, '(a, I<iwiter>, a, I<iwiter>, a, 1pg24.15)') ' * x(', iter, ') -x(', iter-1, ') =', dif

a =x2
goto 500
endif

! AITKEN
num =(x2-x1) -(x1-x0)	!
! numitor nul
	if(num ==0) then
	write(1,*); write(1,*) 'x2-x1; x1-x0:', x2-x1, x1-x0;

	kod =-1; a =x2;
	goto 500
	endif

! calculeaza corectia si 'a'
cor =(x1-x0)**2/num
a =x0 -cor

	IF(print_it) THEN
	write(1,'(1x, I<iwiter>, t11, G24.15, t26, G24.15)') iter, a, dif
	ENDIF


! Test-3 pentru corectie:
dif =a -x0

if (abs(dif) <= EPS) then
kod =0
write(1,*) '* Diferenta curenta =', dif

goto 500
endif


! Test proces stationar:
600 aa(iter) =a; per =period8(iter, aa, i)

STAT_If: IF(per >0) THEN
	kod =2;
iwiter =width(iter)
iwi =width(i)
iwper =width(per)

	write(1, '(/, a, I<iwiter>, a1, \)') ' Iteratia ', iter,': '
	write(1, '(a, I<iwiter>, a, I<iwi>, a)') ' x(', iter, ') = x(', i, ')'
	write(1, '(a, I<iwper>)') ' ** Proces stationar de perioada = ', per

if(.not. Print_it) then
	write(1,*); write(1,198);
	do ii =i, iter
	write(1, '(I6, t14, G24.15)') ii, aa(ii)
	enddo
endif

goto 500
ENDIF STAT_If


198	FORMAT(' Iteratia', t18, 'x')

! Test numar de iteratii:
if(iter <lnit) then
x0 =a
goto 100

else	! iter=lnit
kod =1
endif

500 continue
lnit =iter
rad =a

END

