How to Find Roots of a Continuous Function using the Bisection Method

The bisection method is useful when you want to find a root of a continuous function in a specified range. It works on the Intermediate Value Theorem which says that if a continuous function changes sign over an interval, there is at least one root of the function in that interval. Here is how to find a root using this method.
  •  Assign end points for the 'root hunt'. Let's call them 'a' ad 'b'. The end points should be such that the value of the function, f at both points should be opposite in sign.i.e. f(a), f(b)<0. This is because we will search for a point within this limit that has zero value for the function or a point where the function crosses the x-axis of the Cartesian Plane. If the end points have the same sign, we can use the GO TO statement to read new values for a and b.
  • We choose the mid-point of this interval. Now this mid-point, let's call it 'c', will have some value for the function, namely f(c). Evidently, f(c) will have a positive or negative sign unless it has the value zero in which case it is the root.
  • If f(c) has a sign opposite to f(a) or f(b), we will continue searching the root between f(c) and that particular endpoint. Obviously, it cannot have a sign opposite to both as per our choice of a and b. Depending on which end has the opposite sign, either a will take the  value of c or c will take the value of b.
  • We continue this till we get the desired degree of accuracy in the value of the root (upto a certain number of decimal points).
Pros and Cons: This  method is useful to find a  value of a root where other methods fail. It is a slow method but has a convergence factor of 1 and is thus considered very reliable.

Now for the FORTRAN program, 

  !To find the root of a function using bisection method

program rootf
    implicit none
    real a,b,c,f,root

    10 write (*,*) "Give the range within which you wish to find a single root?"
    read (*,*) a,b
    write(*,*) "The end points are"
    write (*,*) "a= ",a,"b= ",b

    if (f(a)*f(b)>0) then !To make sure the end points have opposite signs
        write (*,*) "The function has same sign at both end points. Please, try again."
        go to 10
        else
        write (*,*) "Finding roots in the range ",a," to ",b
   
        !To start computation
        do   
             if (abs(f(a))<0.00001) then
                root=a
                exit
            end if
   
            if (abs(f(b))<0.00001) then
                root=b
                exit
            end if
     
            c=(a+b)/2.0 !To check the value of the function at the mid-point. This will anyway be checked again when the value of c is replaced the next time the loop runs
            if (abs(f(c))<0.00001) then
                root=c
                exit
            end if
           
            if (f(a)*f(c)<0) then !To move end points closer to the root
                  b=c
                else
                 a=c 
            end if
           
            if (abs(b-a)<0.00001) then ! To exit the loop on getting the answer upto a certain number of decimal places
                 root=a
                    exit
            end if
        end do
    end if

    write (*,*) "The root of x**2-x-5 in the range ",a," to ",b," is ",root !To display the result
   
end program rootf

    !Subprogram to define the function, f
    function f(x)
        implicit none
        real f,x

        f=x**2-x-5 !You can change the function as per choice. Just make sure it is continuous

    end function f


 Sample Output for the Bisection Method

Confirming the answer with Gnuplot. Notice how the intervals get smaller while 'hunting' for the root. Clearly, one point (x,f(x)) remains above the x-axis and the other below it, till we find a root (x,0). Also note that the other root which is outside the range [0,9] remains untouched.

No comments:

Post a Comment