Program NEWT_SYS  ! main pt. Newton_Sys
!* REAL(8)
implicit real(8) (a-h, o-z)

real(8), allocatable :: x(:), f8(:)
external fcn8, jfcn8
character*255 nume, fname
character*1 w
character*80 text(-2:2)
integer width
character(6) str_n

character(132), allocatable :: f8_text(:)
character(132) f_source
character(2) i_char


text(-2)=' ** Matrice jacobian singulara'
text(-1)=' ** Divergenta'
text(0) =' * XTOL atins'
text(1) =' ** LNIT depasit'
text(2) =' ** Proces stationar'

print '(a, \)', ' Introdu numarul de ecuatii: '
read *, n

allocate(f8_text(n))
f8_text =''
! Initialize with "f(i) = ?":
do i=1,n
iwi =width(i)
write(i_char, '(I<iwi>)') i
f8_text(i) ='f('//trim(i_char)//') = ?'
enddo

! Open "f_source" and read:
f_source ='fcn8.f90'
call F_Expr_Sys(f_source, n, f8_text)

do i=1,n
write(*, '(1x, a)') trim(f8_text(i))
enddo

print '(/, a, \)', ' Nume fisier de iesire (fara extensie): '
read(*,'(a)') nume
fname =trim(nume)//'.rez'
open(1, file= fname, status ='unknown')

allocate (x(n), f8(n))

write(1, '(a)') ' Metoda NEWTON'

do i=1,n
if(i==1) write(1,*)

write(1, '(1x, a)') trim(f8_text(i))
enddo

deallocate(f8_text)

nrad =1

iwn =width(n)
write(str_n, '(I<iwn>)') n


100 print '(a, \)',' Introdu aproximatia initiala x(1:'//trim(str_n)//'): '

read *,x
print '(a, \)',' Introdu xtol, lnit: '
read *, xtol, lnit
print '(a, \)',' Cod de tiparire=? (0/#0): '
read *, kod

iwnrad =width(nrad); iwlnit =width(lnit);
write(1, '(/, a, I<iwnrad>, a1)') ' Radacina nr.', nrad

write(1,*) 'Aproximatia initiala:', x
!write(1,*) ' lnit, xtol:', lnit, xtol
write(1, '(a, 1pG10.3, a, I<iwlnit>, /)') ' xtol:', xtol, '; lnit: ', lnit

!call Newton_Sys8(fcn8, jfcn8, n, x, lnit, xtol, f8, kod, n_div)
call Newton_Sys8(n, x, lnit, xtol, f8, kod, n_div)

! Explore divergence:
IF(n_div /=0) THEN
iwn_div =width(n_div)
write(1,'(/, a, I<iwn_div>)') '  * Nr. de divergente locale: ', n_div

ENDIF

write(1,'(/, a)') trim(text(kod))
write(*,'(/, a)') trim(text(kod))

iwlnit =width(lnit)
if(kod ==0) then
write(1,*) ' Solutia X: ', x
write(1,*) ' F(X):      ', f8
write(1, '(a, I<iwlnit>)') '  Numar de iteratii = ', lnit

else
write(1, '(a, I<iwlnit>)') '  Iteratia numarul: ', lnit
write(1,*) ' Aproximatia curenta X: ', x
write(1,*) ' F(X): ', f
endif



print'(/, a, \)',' Alta radacina ? (Y, char): '
read'(a)',w

if(w =='Y' .or. w =='y') then
nrad =nrad+1
goto 100
endif

print '(/, a, a, /)', ' * Fisier de rezultate: ', trim(fname)

close(1)

END program NEWT_SYS

