Sunday, June 12, 2016

BISECTION METHOD


It is an ancient but effective method for finding a zero of f(x). It begins with two values for ‘x’ that bracket a root. It determines that they do in fact bracket a root because the function f(x) changes signs at these two x-values and if f(x) is continuous, there must be at least one root between the values.

          The bisection method then successively divides the initial interval in half, finds in which half the root(s) must lie, and repeat with the endpoints of the smaller interval. The test to see that f(x) does change sign between a points  a and  b is to see if 
f (a)*f (b) < 0.

The iteration formula for bisection method is:
          c = (a + b) / 2
Where f(a)*f(b) < 0

and f(x) is continuous at [a, b].


Program code:

#include<stdio.h>
#include<math.h>
#include<conio.h>
#define f(x) x*x*x-3*x*x+x+1
#define err 0.000001

void main()
{
//clrscr();
    float a,b,x;
    float fa,fb,fx;

    do{
    printf("Enter two initial guesses\n");
    scanf("%f%f",&a,&b);
    fa=f(a);
    fb=f(b);

    }while((fa*fb)>0);
  //  printf("\na    \tb   \tx\t       f(x)\t          f(a)\t       f(b)\n");

    do{
         x=(a+b)/2;
         fx=f(x);
         fa=f(a);
         fb=f(b);
         if((fb*fx)<0)
            {
              a=x;
            }                                                
            else
           b=x;
           // printf("%f  %f  %f  %f  %f  %f\n",a,b,x,fx,fa,fb);

     }while(fabs(a-b)>err);
     printf("Root is %f\n",x);
     getch();
}


Sample run:

Here we have assume the equation: x^3-3x^2+x+1=0

So the possible values of x are: 1,-0.414, and 2.414.
 so the root depends on our two guesses.
 The answer is obtained by determining the nearest guesses as shown below:





No comments:

Post a Comment