! From: "COMPUTATIONAL PHYSICS, 2nd Ed" ! by RH Landau, MJ Paez, and CC Bordeianu ! Copyright Wiley - VCH, 2007. ! Electronic Materials copyright: R Landau, Oregon State Univ, 2007; ! MJ Paez, Univ Antioquia, 2007; and CC Bordeianu, Univ Bucharest, 2007. ! Supported by the US National Science Foundation ! ! Newton_cd.f90: Newton-Raphson root finder, central diff derivative ! Program Newton_cd Implicit none Integer :: it, imax = 10 ! Maximum number of iterations permitted Real*8 :: x, dx = 1e - 2, eps = 1e - 6, f1, df, F ! x guess, must be close to root x = 2. Do it = 0, imax f1 = F(x) ! Compute Function value write(*, *) it, x, f1 ! Central difference derivative df = ( F(x + dx/2) - F(x - dx/2) )/dx dx = - f1/df x = x + dx ! New guess ! Check for convergence If ( abs(F(x)) <= eps ) then write(*, *) eps Stop Endif End Do End Program Newton_cd Function F(x) Implicit none Real*8 :: x, F F = 2*cos(x) - x End