by Dinesh Thakur Category: Function and Pointer

Given a function f(x) which is continuous in the interval [a, b] and satisfying the property hat f(a)*f(b) < 0, there exists a root of the function f(x) in the interval [a, b]. The bisection algorithm works as follows where t: and 8 are small values specified by the user.

• Compute the midpoint, c, of the interval [a, b] as c= (a+b )/2.

• Compute f(c). If f(c) < t:, stop and print c as the required root.

• If f(a)*f(c) < 0, replace the search interval with [a, c]. The root will be found in this smaller Intervals Else, replace the search interval with [b, c].

• If the size of the search interval< 8, then stop and print c as root.

#include <stdlib.h>

#include <math.h>
#include <stdio.h>
/* function prototypes */
int bisection(double(*func)(double),
double xa, double xb, double *xroot,
double eps, double delta, int maxiter);
double foobar(double x);
double myfunc(double x);
double foobar(double x)
{
   return (exp(x)-2.0);
}
   double myfunc(double x)
   {
     return x*x - 3.0;
   }
    int bisection(double(*yfunc)(double),
     double xa, double xb, double *xroot,
      double eps, double delta, int maxiter)
     {
                 double xmid,fmid,fa,fb;
                 int iter;
                 fa= yfunc(xa);
                 fb = yfunc(xb);
              /*check whether the required precondition for bisection is satisfied*/
              if(fa*fb > 0.0)
              {
                   printf("ERROR: f'a=fb> 0.0, Bisection wi 11 not work!");
                   exit(EXIT_FAILURE);
              }
       /* check if xa or xb is a root */
             if(fabs(fa)<eps)
             {
                 *xroot = xa;
                 return 2;
             }
                        else if (fabs(fb)<eps)
                        {
                           *xroot = xa;
                           return 2;
                        }
              /* else begin bisection search for a root in the interval [xa,xb]*/
       for(iter=0;iter<maxiter;iter++)
       {
               xmid = (xa+xb)/2.0;
               fmid = yfunc(xmid);
               *xroot = xmid;
                if(fabs(fmid)<eps)
               {
                 return 1; /*absolute convergence achieved*/
               }
                        if(fabs(xa-xb)<delta)
                        {
                            return 2;/* relative convergence achieved */
                            /*shrink the search interval*/
                        }
               if(fb*fmid<0.0)
                 {
                   xa = xmid;
                   fa=fmid;
                 }
                          else
                           {
                            xb = xmid;
                            fb=fmid;
             }
     }
                  /*failed to find root within maxiter iterations*/
 return 0;
}
      int main()
      {
                double xroot;
                int flag;
                clrscr();
                flag=bisection(foobar,0.0,4.0,&xroot,1.e-16,1.e-6,500);
                printf("root =%lg, foobar(root)=%lg\n",xroot,foobar(xroot));
                printf("return value is %d\n\n",flag);
                flag=bisection(sin,1.0,4.0,&xroot,1.e-16,1.e-6,500);
                printf("root =%lg, sin(root)=%lg\n",xroot,sin(xroot));
                printf("return value is %d\n\n",flag);
                flag=bisection(myfunc,1.0,4.0,&xroot,1.e-16,1.e-16,500);
                printf("root =%lg, myfunc(root)=%lg\n" ,xroot,myfunc(xroot));
                printf("return value is %d\n\n",flag);
                getch();
              return 0;
      }

C Program to find out root using Bisection

Popular Article