subroutine LAG(x01, tol, niter, i_rad, z, kod_print)
use work

!real(4) coef(0:n)
!real(4) coef_1(0:n-1), coef_2(0:n-2)

complex x01, z
complex sH, cor
complex den_plus, den_minus, den
complex polval, polval_0, polval_1, polval_2

complex aa(0:niter), x1	! Tablou pentru toate val. - pt. proces stationar
integer period, per
logical is_real
integer width

nlim =niter
niter =0

z =x01
cor =1.
aa(0) =x01

do_1: do i=1, nlim
niter =niter+1

polval_0 =polval(n, coef, z)
polval_1 =polval(n-1, coef_1, z)
polval_2 =polval(n-2, coef_2, z)

!H =(n-1)*((n-1)*polval_1*polval_1 -n*polval*polval_2)
sH =csqrt( (n-1)*((n-1)*polval_1*polval_1 -n*polval_0*polval_2) )

den_plus  =polval_1 +sH
den_minus =polval_1 -sH

if(abs(den_plus) > abs(den_minus)) then
den =den_plus
else
den =den_minus
endif

cor =n*polval_0/den
z =z -cor

if(abs(cor) <=tol) then
!if(kod_print /=0) write(1,*) 'cor =', cor, niter
exit do_1
endif

! Test proces stationar:
aa(niter) =z		! pt. stationar
per =period(niter, aa, ii)

IF(per >0) then
iwiter =width(niter)
iwi =width(ii)
iwper =width(per)

if(is_real(z)) then
	write(*, '(/, a, 1pG14.7, \)') ' Radacina', real(z)
	write(1, '(/, a, 1pG14.7, \)') ' Radacina', real(z)

else
	write(*, '(/, a, (1pG14.7, 1x, 1pG14.7), \)') ' Radacina', z
	write(1, '(/, a, (1pG14.7, 1x, 1pG14.7), \)') ' Radacina', z
endif

	print '(/, a, I<iwiter>, a, I<iwi>, a)', ' z(', niter, ') = z(', ii, ')'
	print '(a, I<iwper>,/)', ' ** Proces stationar de perioada ', per

	write(1, '(/, a, I<iwiter>, a, \)') ' Iteratia ', niter,': '
	write(1, '(a, I<iwiter>, a, I<iwi>, a)') ' z(', niter, ') = z(', ii, ')'
	write(1, '(a, I<iwper>)') ' ** Proces stationar de perioada = ', per

!ier =2
goto 500
ENDIF

enddo do_1

500 continue

if(kod_print /=0) write(1, '(I2, a, 1pg14.6:, 1x, I5)') i_rad, ': |cor| =', abs(cor), niter

end
