! Metoda pct. fix pt. sistem neliniar - iterare cu matr.const. A
! Cu actualizare F(w0) dupa 3 iteratii !
! Cu jacobianul calculat numeric: (f(x+delta,y)-f(x,y))/delta

subroutine FIX_SYS(n, x, xtol, delta, lnit, f, kod)

real x(n), f(n), x0(n), f0(n)
real A(n,n), b(n), w(n)

logical print_it, xtol_met
real norm$
real prag/1E-6/		! pt. elim


print_it = kod /=0

iter=0
xtol_met=.false.

190 format(/, a, t14, a, t<n*16+13>, a)

199 format(i6, t10, <n>G16.7, t<n*16+10>, <n>(1pG16.7), :, <n>(1pG20.7))

	if(print_it)then
	print 190, 'Iteratia', 'X', 'F(X)'
	write (1,190) 'Iteratia', 'X', 'F(X)'
	endif

! Calculeaza A=F(w0) - jacobianul lui 'f';
! introdu valorile X in X0 :
90 x0 =x

! introdu valorile f(X0) in f0 :
call fcns(n, x0, f0)

! Calculeaza jacobianul lui f in X0:
! Variaza pe rand, cate un x: x(1) = x0(1)+d; x(k#1) =x0(1); etc.
! Calculeaza A pe coloane: 1; 2; etc. 
jcol_do: do j =1,n
!x =x0
x(j) =x0(j) +delta

! Calculeaza f =f(... x0(j-1), x0(j) +delta, x0(j+1)...)
call fcns(n, x, f)

! Coloana "j" din A:
A(1:n,j) =(f(1:n) -f0(1:n))/delta

! restore x(j)
x(j) =x0(j)
enddo jcol_do


! Initializ. pt. ELIM:
nb =1
kodPrint =0

!  Iteratie:
100 iter=iter+1
call fcns(n, x, f)

!  Termenul liber
b =-f(:)

! rezolva A*d_iter= -f(w_iter):
call ELIM (n, nb, A, b, prag, kod, kodPrint)

	if(kod ==-1) then
	lnit =iter

	RETURN
	endif

w =b
x =x +w

	if(print_it)then
	call fcns(n, x, f)
	print 199, iter, x, f
	write(1,199) iter, x, f		!, w
	endif

!Test xtol:
wnorm =norm$(n,w)
if(wnorm <= xtol) then
xtol_met =.true.
kod =0
endif

IF(xtol_met) THEN
print*; print*,' Solutia:',x;
if(.not. print_it) call fcns(n, x, f)
print*,' Functia:',f
print*,' Nr. iteratii:',iter

	if(print_it) then
	print*,' Norma corectiei:', wnorm
	write(1,*) ' Norma corectiei:', wnorm
	endif
lnit =iter
return

ELSEIF(iter > lnit)THEN
print*; print*,' LNIT depasit !';
print*,' Ultima aproximatie - X: ',x
if(.not. print_it) call fcns(n, x, f)
print*,' F(X):',f

	if(print_it) then
	print*,' Norma corectiei:', wnorm
	write(1,*) ' Norma corectiei:', wnorm
	endif
kod =1
return

ELSE
   if(mod(iter,3).eq.0) then
   goto 90
   else
   goto 100
   endif
ENDIF

END

