Thursday, October 6, 2016

Bisection Method

Consider finding the root of  f(x) = Cos[x] – x . Let εstep = 0.01 ,  εabs = 0.001 and start with the interval [1.0 ,  0.5] .


Program Bisection_method
    Implicit none
    Real :: a , b , f , c , error
    
Write( * , * ) " Enter numbers between which the root is to be found : "   !(Can be change)
      10 read ( * , * ) a , b
 Write ( * , 61 ) " Input error value : "    !(Can be change)
       61 Format ( / , / , a18 )
        Read ( * , * ) error
     
  15 If ( f ( a ) * f ( b ) . lt . 0 )then
            c = ( a+b ) / 2.0
         else
  Write ( * , 62 ) " There is no zeros in this interval. Try with  another value of a & b "
       62 Format  ( / , / , a65 )
           go to 10
        Endif
        if ( f (a) * f (c) . lt . 0) then
            b = c
           else
          a = c
        Endif
       if ( abs (b-a) . gt . error ) goto 15
       write ( * , 63 ) " The approximate root of the function is : " , c
           63 Format ( / , / , a42 , f12.9 )
        end

    function f (x)
            implicit none
            real : : f , x
            f = cos (x) - x   !(Can be change)
    end function


Input ::




Output : :


No comments:

Post a Comment