Signup/Sign In

Bisection Method program in C

Are you confused about what is Bisection method program and why we have to implement it? Then this tutorial is for you. Let's start by understanding what is bisection method.

What is Bisection Method?

Bisection method, also known as "the interval halving method", "binary search method" and the "Bolzano's method" is used to calculate root of a polynomial function within an interval. It is based on the Bolzano's theorem which states that,

"If on an interval a, b and f(a)·f(b) < 0, a function f(x) is found to be continuous, then there exists a value c such that c ? (a, b) or which f(c) = 0. "

In Bisection Method, we bisect the interval into subintervals and work with the interval in which the root is supposed to lie. We reach the solution iteratively by narrowing down the values. In this method, we treat the initial beginning and end points as a line segment and keep replacing one of the two points by the mid point.

Algorithm for Bisection Method Program in C

To implement this algorithm, we assume that f(x) is a continuous function in interval [a, b] and f(a) * f(b) < 0.

  1. We input the function of which we have to find root.
  2. We input the values of a and b (such that f(a) > 0 and f(b) < 0) and the maximum number of iterations we want to perform.
  3. Calculate c = (a + b)/2
  4. Evaluate function for the value of c. If f(c) = 0, c is the root, else if f(c) has the same sign as f(a), we replace a with c and keep the same value for b or f(c) has the same sign as f(b), we replace b with c if and keep the same value for a.
  5. We keep doing the above step till we find the root or we reach the maximum number of iterations.

Program on Bisection Method in C Language

Let's the program now.

#include<stdio.h>
#include<math.h>

double F( double x) //Function definition
{
    //This return the value of the Function
    return (10 - pow(x,2));
}

int main()
{
    printf("Studytonight - Best place to learn\n");

    printf("This program illustrates the bisection method in C:\n");
    printf(" f(x) = 10 - x^2");
    double x0,x1;

    printf("\nEnter the first approximation to the root : ");
    scanf("%lf",&x0);

    printf("Enter the second approximation to the root : ");
    scanf("%lf",&x1);

    int iter;
    printf("Enter the number of iteration you want to perform : ");
    scanf("%d",&iter);

    int ctr = 1;
    double l1 = x0;
    double l2 = x1;
    double r,f1,f2,f3;

    //We check if the initial approximations are the root or not
    if(F(l1)==0)// it is a root
        r = l1;
    else if(F(l2)==0)
        r = l2;
    else //If the above two values are not the roots of the given function
    {
        while(ctr <= iter) //Since, ctr is initialized to 1
        {
            //This is an implementation of the algorithm mentioned above
            f1 = F(l1);
            r = (l1+l2)/2.0; //Returns a double value
            f2 = F(r);
            f3 = F(l2);

            if(f2 == 0)
            {
                r = f2;
                break; //Execution moves out of the while loop to the statement just after it
            }
            printf("The root after %d iteration is %lf\n",ctr,r);

            if(f1*f2 < 0)//Both are of opposite sign
                l2 = r;
            else if(f2*f3 < 0)
                l1 = r;
            ctr++;
        }
    }

    printf("The approximation to the root is %lf\n",r); //Gives the final value after mentioned iteration
    printf("Coding is Fun !");
    return 0;
}


Studytonight - Best place to learn
This program illustrates the bisection method in C:
f(x) = 10 - x^2
Enter the first approximation to the root : -2
Enter the second approximation to the root : 5
Enter the number of iteration you want to perform : 10
The root after 1 iteration is 1.500000
The root after 2 iteration is 3.250000
The root after 3 iteration is 2.375000
The root after 4 iteration is 2.812500
The root after 5 iteration is 3.031250
The root after 6 iteration is 3.140625
The root after 7 iteration is 3.195312
The root after 8 iteration is 3.167969
The root after 9 iteration is 3.154297
The root after 10 iteration is 3.161133
The approximation to the root is 3.161133
Coding is Fun !

Advantages of the Bisection Method:

  • It is easy to implement and there are no complicated calculations.
  • Convergence is guaranteed in this method.
  • We can get precise results by increasing the number of iterations. Thus, we can handle error easily.

Disadvantages of the Bisection Method:

  • It is slow.
  • We cannot determine complex roots using this method.
  • We cannot apply it over an interval where the function returns values of the same sign.