! Main pt. NEWTON
! Ecuatia f(x) =0
! f =f(x) si f1 =f'(x) = calculate in subrutina "SUB"

Program Mnew

external SUB

common p	! parametru pt. f(x)
logical exist_p

character(80) text(-2:1), w*1
character(260) fname0, fname
integer width

character(132) f_text(2), f_source
logical new_rad, new_p, new_tol


text(-2)=' ** F-derivat <1E-10:'
text(0) =' * Toleranta EPS s-a atins:'
text(1) =' ** Numarulul limita de iteratii atins:'
text(-1)=' ** Divergenta'

10 format(a, \)

f_source ='sub.f90'
call F_Expr(f_source, f_text)
write(*, '(1x, a)') trim(f_text(1))
write(*, '(1x, a)') trim(f_text(2))

print 10, ' Nume fisier rezultate (Fara extensie): '
read *, fname0
fname =trim(fname0)//'.rez'
open(1, file =fname, form ='formatted', status ='unknown')

print 10,' Functia f(x) include parametrul "p" (Y / char) ?: '
read '(a)',w
exist_p =(w =='Y' .or. w =='y')

! Write header:
write(1, '(a)') ' Metoda NEWTON'

write(1, '(1x, a)') trim(f_text(1))
write(1, '(1x, a)') trim(f_text(2))


nrad =1
new_p =.false.
new_tol =.true.

100 if(exist_p) then
print 10,' p=? : '
read*,p
endif

200 continue
if(new_p) then
print '(a, \)',' Alt "eps" si "nlim" ? (Y/ char): '
read '(a)',w
new_tol =w=='Y' .or. w =='y'
endif

if(new_tol) then
print 10,' Introdu eps, nlim : '
read *, eps, nlim0
endif

210 print 10,' Introdu x0 si cod tiparire (0/#0; >=2 tiparire si corectii): '
read *, x00, kod0

iwn =width(nrad); iwnlim0 =width(nlim0);
write(1, '(/, a, I<iwn>, a1)') ' Radacina nr.', nrad

if(exist_p) then
write(1,*) 'p:', p
endif

write(1,*) 'Aproximatia initiala:', x00
write(1, '(a, 1pG10.3, a, I<iwnlim0>, /)') ' EPS:', EPS, '; NLIM: ', nlim0


x0 =x00; nlim=nlim0; kod=kod0

n_div =0
call NEWTON(SUB, x0, eps, nlim, rad, kod, n_div)

call sub(rad, f, f1)

! 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

if(kod /=2) then
print '(/a)', trim(text(kod))
write(1, '(/, a)'), trim(text(kod))
endif

if(kod ==0) then
write(1,*)
!write(1,*) ' Radacina =', rad,'; f(Radacina) =', f
write(1, '(a,1pg14.7, a,1pg14.7 )') ' Radacina =', rad,'; f(Radacina) = ', f
write(*,*) ' Radacina =', rad,'; f(Radacina) = ', f

else
write(1,*)
!write(1,*) ' Aproximatia curenta: x =', rad,'; f(x) =', f
write(1,'(a,1pg14.7, a,1pg14.7 )') ' Aproximatia curenta: x =', rad,'; f(x) =', f
write(*,*) ' Aproximatia curenta: x =', rad,'; f(x) =', f
endif

iwnlim =width(nlim)
write(1, '(a, I<iwnlim>)') ' Numar de iteratii = ', nlim

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

new_rad =w=='Y' .or. w =='y'
new_p =.false.

IF(new_rad) then
nrad =nrad+1

	if(exist_p) then
	print '(a, \)',' Alt "p" ? (Y/ char): '
	read '(a)',w
	new_p =w=='Y' .or. w =='y'

	if(new_p) goto 100
	endif


print '(a, \)',' Alt "eps" si "nlim" ? (Y/ char): '
read '(a)',w
new_tol =w=='Y' .or. w =='y'

if(new_tol) then
goto 200
else
goto 210
endif

ENDIF

close(1)

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

END program Mnew

