c to compile this, first type "f77 bisect.f" then to run it, type "a.out" program bisect real x_left,x_right,x_test real f_left,f_right,f_test,tol,err_prev integer ict c define the initial left-hand and right-hand endpoints of the interval c we're going to bisect. x_left = -10.0 x_right = 10.0 c find the values of the function at the endpoints f_left = f(x_left) f_right =f(x_right) c make sure that the problem even makes sense: if(f_left*f_right.gt.0.d0) then write(6,*)'We do not know if there is a zero in between.' stop end if 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 f_test close to zero?" f_test = 2*tol do while((abs(f_test).gt.tol).and.(ict.lt.100)) c find the values of the function at the endpoints f_left = f(x_left) f_right =f(x_right) c define the midpoint x_test = (x_left+x_right)/2.0 c find the value of the function at the midpoint f_test = f(x_test) if (f_left*f_test.lt.0.0) then c we happen to know that the solution is at x=1, so we use this c information to define the error err_prev = x_test + 1.0 x_right = x_test else if(f_test*f_right.gt.0.0) stop err_prev = x_test + 1.0 x_left = x_test end if write(3,*)' step = ',ict,' a_i = ',x_left,' b_i = ' & ,x_right, & ' b_i-a_i =', x_right-x_left write(6,*)ict,x_right-x_left write(4,*)' step=',ict,' root =',x_test,' f(root) =', & f_test c write(4,*)'step ',ict,' root ',x_test,' f(root) ',f_test, c & ' (present error)/(previous error) ',(x_test+1)/err_prev 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