!===========================================================
!Newton Raphson of function of one variable
!===========================================================
program NewtonRaphson
 implicit none
 real(8), parameter :: rho  = 8.0D0
 real(8), parameter :: eps  = 1D-12
 integer, parameter :: NMAX = 1000
 real(8) :: x0, x1, err, g, gp
 integer :: i
 real(8) :: xin=0.1, xfi=7, dx=0.0001


 do while (xin <= xfi)
    x0 = xin
    do i=1,NMAX
!value of function g(x):
       g   = x0*tan(x0)-sqrt(rho*rho-x0*x0)
!value of the derivative g'(x):
       gp  = x0/sqrt(rho*rho-x0*x0)+x0/(cos(x0)**2)+tan(x0)
       x1  = x0 - g/gp
       err = ABS(x1-x0)
       if(err .lt. eps) exit
       x0 = x1
    enddo
    if(err .lt. eps) print *,xin,i,x1,err
    xin = xin + dx
 enddo
end program NewtonRaphson


