Friday, May 31, 2013

Implementing Bisection Method[Numerical Methods]

The method is applicable when we wish to solve the equation f(x) = 0 for the real variable x, where f is a continuous function defined on an interval [ab] and f(a) and f(b) have opposite signs. In this case a and b are said to bracket a root since, by the intermediate value theorem, the f must have at least one root in the interval (a, b).
At each step the method divides the interval in two by computing the midpoint c = (a+b) / 2 of the interval and the value of the function f(c) at that point. Unless c is itself a root (which is very unlikely, but possible) there are now two possibilities: either f(a) and f(c) have opposite signs and bracket a root, or f(c) and f(b) have opposite signs and bracket a root. The method selects the subinterval that is a bracket as a new interval to be used in the next step. In this way the interval that contains a zero of f is reduced in width by 50% at each step. The process is continued until the interval is sufficiently small.
Explicitly, if f(a) and f(c) are opposite signs, then the method sets c as the new value for b, and if f(b) and f(c) are opposite signs then the method sets c as the new a. (If f(c)=0 then c may be taken as the solution and the process stops.) In both cases, the new f(a) and f(b) have opposite signs, so the method is applicable to this smaller interval.[4]

C code implementing bisection method for the equation x*x*x-2*x-5
#include
#include
double f(double x)
{
    return (x*x*x-2*x-5);
}
int main()
{
    double a,b,x;
    scanf("%lf %lf",&a,&b);
    if((f(a)>=0 && f(b)>=0) || (f(a)<0 amp="" b="" br="" f="">    printf("possible\n");
    else
    {
       while(1)
       {
           x=(a+b)/2;
           printf("%lf\n",x);
           if(fabs(f(x)-0)<=0.001)
           {
               printf("%lf\n",x);
               break;
           }
           if((f(a)>=0 && f(x)>=0) || (f(a)<0 amp="" br="" f="" x="">              a=x;
           else
              b=x;
       }
    }
    getch();
    return 0;
}


sample o/p:
2 3
2.500000
2.250000
2.125000
2.062500
2.093750
2.109375
2.101563
2.097656
2.095703
2.094727
2.094238
2.094482
2.094482

Analysis :  

The method is guaranteed to converge to a root of f if f is a continuous function on the interval [a, b] and f(a) and f(b) have opposite signs. The absolute error is halved at each step so the method converges linearly, which is comparatively slow.
Specifically, if c1 = (a+b)/2 is the midpoint of the initial interval, and cn is the midpoint of the interval in the nth step, then the difference between cn and a solution c is bounded by
|c_n-c|\le\frac{|b-a|}{2^n}.
This formula can be used to determine in advance the number of iterations that the bisection method would need to converge to a root to within a certain tolerance. The number of iterations needed, n, to achieve a given error (or tolerance), ε, is given by: n = \log_2\left(\frac{\epsilon_0}{\epsilon}\right)=\frac{\log\epsilon_0-\log\epsilon}{\log2} ,
where \epsilon_0 = \text{initial bracket size} = b-a .
Therefore, the linear convergence is expressed by\epsilon_{n+1} = \text{constant} \times \epsilon_n^m, \ m=1 .                     


No comments:

Post a Comment