c to compile this, first type "f77 newton.f" then to run it, type "a.out" program newton real x_guess,f_guess,df_guess,f_prev,x_prev,x_temp integer ict c how close we want to get to the solution: tol = 1.e-8 c the counter for the iteration loop: ict = 0 c what we're going to test: "is test close to zero?" f_guess = 2*tol c first guess for root c x_guess = -5.0 write(6,*)'what is your first guess?' read(5,*)x_guess c to get started, we take a newton step f_guess = f(x_guess) fx_guess = fx(x_guess) x_temp = x_guess - f_guess/fx_guess write(4,*)ict,'x_0 = ',x_guess,'f(x_0) =',f_guess write(6,*)'step = ',ict, ' root ',x_guess c now update: x_prev = x_guess f_prev = f_guess x_guess = x_temp ict = ict + 1 c now that we have values for x_guess, x_prev, and f_prev, we can start c the secant method do while((abs(f_guess).gt.tol).and.(ict.lt.100)) f_guess = f(x_guess) write(4,*)ict,'x_0 = ',x_guess,'f(x_0) =',f_guess write(6,*)'step = ',ict, ' root ',x_guess x_temp = x_guess - f_guess*(x_guess-x_prev)/(f_guess-f_prev) c save present values of f_guess and x_guess as "past" x_prev = x_guess f_prev = f_guess c save new value of x as present x_guess = x_temp c update the iteration counter ict = ict+1 end do stop end ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc c define the function whose zero we're looking for: function f(x) real f,x f = (x+1.0)*(x-1.0)**2 return end ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc c define the derivative of the function whose zero we're looking for: function fx(x) real fx,x fx = (x-1.0)**2 + (x+1.0)*2*(x-1.0) return end