	subroutine  aitken_G(g, x0, eps, lnit, rad, kod)
!*  AITKEN direct cu functia G
!*	Calculeaza solutia ecuatiei X=G(X), folosind extrapolarea Aitken.
!*	La radacini multiple - kod#0: afiseaza 'lambda ~g'(rad)' si 'r=1/(1-lambda)'
!       Parametri formali:
!		g   : numele functiei g(x).
!		x0  : aproximatia initiala.
!		eps : toleranta pentru corectia in formula metodei.
!		lnit: - INTRARE:numarul maxim de iteratii.
!			  - IESIRE :numarul efectiv de iteratii.	
!		rad : solutia ecuatiei/ultima aproximatie.
!		kod : cod de intrare/iesire.
!			  - INTRARE: 
!			   0 -NU tipareste iteratiile; 
!			  #0 -tipareste iteratiile
!			  - IESIRE: cod de terminare:
!				0  solutia gasita cu toleranta 'eps'.
!				1  depasirea nr.limita de iteratii
!		        2  proces STATIONAR: a2 =a0
!		       -1  numitor nul in formula metodei.

common p	!  parametru pt. functia G
external g
real(4) num, num1	! numitor
logical print_it
real(4) lambda
integer width

real aa(0:lnit)	! Tablou pentru toate val. - pt. proces stationar
integer period, per


print_it =(kod /=0)  ! cod de tiparire

	write(1, '(a, G15.7, a, 1pG10.3, /)') ' Aprox. initiala:',x0,';  Toleranta:',eps

	IF(print_it) THEN
	print '(1x, a, t14, a, t28, a, t46, a)',   'Iteratia', 'a', 'lambda', 'r'
	write(1,'(1x, a, t14, a, t28, a, t46, a)') 'Iteratia', 'a', 'lambda', 'r'
	ENDIF

aa =0.
aa(0) =x0

iter =0
a0 =x0

100	iter=iter+1

! Test-1 pentru iteratia obisnuita cu "g":
dif =g(a0) -a0
if (abs(dif) <= EPS) then
	kod =0;	rad =a0;
	goto 500
endif

! Numitor in G:
num =g(g(a0)) -2*g(a0) +a0

! Test numitor nul:
eps_num =2E-7
if(abs(num) <= eps_num) then
num1 =g(a0)  -a0

	if(abs(num1) <= eps_num) then
!	kod =0
	write(1,*) 'numitor:', num
	write(1,*) 'numarator:', num1

	write(*,*) 'numitor:', num
	write(*,*) 'numarator:', num1

	else
	write(1,*) 'numitor:', num
	write(*,*) 'numitor:', num
	endif

kod =-1
rad =a0
goto 500
endif

! calculeaza corectia
	cor = -(g(a0)-a0)**2/num
	a =a0 +cor

dif =a-a0

iwiter =width(iter)
		IF(print_it) THEN
	if(iter <2) then
	print '(1x, I<iwiter>, t11, G15.7)', iter, a
	write(1,'(1x, I<iwiter>, t11, G15.7)') iter, a

	else
!!	lambda =(a(iter)-a(iter-1))/(a(iter-1)-a(iter-2))
		lambda =dif/dif0
		if(lambda <1) then
		r =1./(1.-lambda)
		print '(1x, I<iwiter>, t11, G15.7, t26, G15.7, t44, G10.3)', iter, a, lambda, r
		write(1,'(1x, I<iwiter>, t11, G15.7, t26, G15.7, t44, G10.3)') iter, a, lambda, r

		else
		print '(1x, I<iwiter>, t11, G15.7, t26, G15.7, t46, a2)', iter, a, lambda, '*'
		write(1,'(1x, I<iwiter>, t11, G15.7, t26, G15.7, t46, a2)') iter, a, lambda, '*'
		endif
	endif
		ENDIF

! Test corectie:
	if (abs(dif) <=EPS) then
	kod=0; rad=a;
	print*,' ** Diferenta curenta =',dif
		IF(print_it) write(1,*)' ** Diferenta curenta =', dif
	goto 500
	endif

! Test proces stationar:
aa(iter) =a
per = period(iter, aa, i)

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

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

write(1,*) 'dif:',dif

	print '(/, a, I<iwiter>, a, I<iwi>, a)', ' a(', iter, ') = a(', i, ')'
	print '(a, I<iwper>)', ' ** Proces stationar de perioada ', 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, G16.7)') ii, aa(ii)
	enddo
endif

kod =2; rad =a;
goto 500
ENDIF STAT_If


! Test numar de iteratii:
	if(iter <lnit) then
	a0 =a
	dif0 =dif	! corectia anterioara - pentru 'lambda'
	goto 100

	else	! iter=lnit
	kod =1; rad =a;
	return
	endif

500 lnit =iter

END
