c c Time-stamp: <2000-06-15 14:10:58 baron> c program solve_quad implicit none real*8 :: x,y,a,b,c,d,e real*8 :: dydx,deltax real*8, parameter :: tol1=1.d-6,tol2=1.d-3,eps=0.05d0 integer :: icount integer, parameter :: imax=500 c c get input values c c sample equation, 1 root c give a,b,c,d,e c -5 5 .1 2 -10 c give initial guess c 5 c y = -0.668071594918551170E-03 x = 1.26559757663514860 c c print *,"y = a*x**4 + b*x**3 + c*x**2 + d*x + e = 0" c print *,"give a,b,c,d,e" read(*,*)a,b,c,d print *," give initial guess" read(*,*)x c c icount = 0 newton_loop: do icount = icount + 1 y = a*x**4 + b*x**3 + c*x**2 + d*x + e dydx = 4*a*x**3 + 3*b*x**2 + 2*c*x + d deltax = -y/dydx deltax = sign(min(abs(deltax),abs(eps*x)),deltax) c c check convergence c if(abs(y) .le. tol1) exit newton_loop if(abs(deltax/x) .le. tol2) exit newton_loop c x = x + deltax if(icount .gt. imax) exit newton_loop end do newton_loop if(icount .gt. imax) then print *,"failed to converge in ",imax," iterations" endif print *,"y = ",y," x = ",x end program solve_quad