Table of contents
1.
Introduction
2.
Bisection Method Algorithm
2.1.
C
3.
Examples
3.1.
Example 1
3.2.
Example 2
4.
Advantages of the Bisection Method
5.
Disadvantages of the Bisection Method
6.
Frequently Asked Questions
6.1.
Can the bisection method find complex roots?
6.2.
Is the bisection method efficient for finding multiple roots?
6.3.
Can the bisection method handle discontinuous functions?
7.
Conclusion
Last Updated: Dec 20, 2024
Medium

Bisection Method in C

Author Rinki Deka
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The bisection method is a simple but effective way to find the root of a equation. It works by repeatedly dividing an interval in half & checking which half contains the root. This method is easy to understand & implement, which makes it a popular choice for solving equations numerically. 

Bisection Method in C

In this article, we'll talk about the bisection method algorithm, see it’s examples, implementation in C, & discuss its advantages & disadvantages.

Bisection Method Algorithm

The bisection method algorithm follows these steps:

1. Choose an interval [a, b] where the function f(x) changes sign. This means f(a) and f(b) should have opposite signs.
 

2. Calculate the midpoint c of the interval using the formula: c = (a + b) / 2.
 

3. Evaluate f(c) at the midpoint.


4. If f(c) is close enough to zero (within a specified tolerance), then c is the root & the algorithm stops.


5. If f(c) has the same sign as f(a), update a = c. Otherwise, update b = c.


6. Repeat steps 2-5 until the root is found or the maximum number of iterations is reached.

Let’s see the implementation in C : 

  • C

C

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

double f(double x) {
return x * x - 4 * x + 3;
}

double bisection(double a, double b, double tolerance) {
double c;
int iterations = 0;

while (fabs(b - a) > tolerance) {
c = (a + b) / 2;

if (f(c) == 0 || fabs(b - a) / 2 < tolerance)
return c;

if (f(a) * f(c) < 0)
b = c;
else
a = c;

iterations++;
}

return c;
}

int main() {
double a = 0, b = 5, tolerance = 0.0001;
double root = bisection(a, b, tolerance);

printf("Root: %.4f\n", root);

return 0;
}
You can also try this code with Online C Compiler
Run Code


Output

Root: 1.0000


The bisection method guarantees convergence to a root if one exists within the initial interval. It narrows down the interval containing the root by half at each iteration, making it a reliable method for root-finding.

Examples

Now, let's look at a couple of examples to see how the bisection method works: 

Example 1

Find a root of the equation f(x) = x^3 - x - 1 in the interval [1, 2] with a tolerance of 0.0001.

Solution:

a = 1, b = 2
f(1) = -1, f(2) = 5


Iteration 1:

c = (1 + 2) / 2 = 1.5
f(1.5) = 1.375
f(1) * f(1.5) < 0, so update b = 1.5


Iteration 2:

c = (1 + 1.5) / 2 = 1.25
f(1.25) = 0.296875
f(1) * f(1.25) < 0, so update b = 1.25


After 14 iterations, the root is found at approximately 1.3247 with an error less than 0.0001.

Example 2

Find a root of the equation f(x) = cos(x) - x in the interval [0, 1] with a tolerance of 0.0001.

Solution:

a = 0, b = 1
f(0) = 1, f(1) = -0.4597


Iteration 1

c = (0 + 1) / 2 = 0.5
f(0.5) = 0.3776
f(0) * f(0.5) > 0, so update a = 0.5


After 14 iterations, the root is found at approximately 0.7391 with an error less than 0.0001.

Advantages of the Bisection Method

1. Simplicity: The bisection method is easy to understand & implement. It relies on a simple idea of repeatedly dividing an interval in half, making it accessible even to those with limited programming experience.
 

2. Guaranteed convergence: If a root exists within the initial interval, the bisection method is guaranteed to converge to it. The method always narrows down the interval containing the root, ensuring progress towards the solution.
 

3. Robustness: The bisection method is robust & reliable. It can handle functions with discontinuities or multiple roots within the initial interval. As long as the function changes sign within the interval, the method will converge to a root.
 

4. Error control: The bisection method allows for precise error control. By specifying a tolerance value, you can control the accuracy of the approximation. The method will continue iterating until the error is within the specified tolerance.
 

5. Predictable performance: The number of iterations required by the bisection method to achieve a certain accuracy can be predicted using the formula: n = log2((b - a) / tolerance). This predictability helps in estimating the computational cost of the method.

Disadvantages of the Bisection Method

1. Slow convergence: Compared to other root-finding methods like Newton's method or the secant method, the bisection method converges slowly. It requires a larger number of iterations to achieve the same level of accuracy, especially for functions with rapidly changing slopes near the root.
 

2. Requires a change of sign: The bisection method requires the function to change sign within the initial interval. If the function does not change sign or if the sign change is not detected due to numerical issues, the method may fail to converge or produce incorrect results.
 

3. Limited to one-dimensional problems: The bisection method is designed for finding roots of one-dimensional functions. It cannot be directly applied to higher-dimensional problems or systems of equations without modifications.
 

4. Requires an initial interval: The bisection method requires an initial interval [a, b] that contains the root. If the interval is not chosen appropriately or if the function has no roots within the interval, the method will not converge.
 

5. Inefficient for high-precision results: If very high precision is required, the bisection method may become inefficient. The number of iterations needed to achieve a certain accuracy increases logarithmically with the desired precision, leading to longer computation times.

Frequently Asked Questions

Can the bisection method find complex roots?

No, the bisection method is designed for finding real roots of real-valued functions.

Is the bisection method efficient for finding multiple roots?

The bisection method can find multiple roots, but it needs to be applied separately to each interval containing a root, which can be inefficient.

Can the bisection method handle discontinuous functions?

Yes, as long as the function changes sign within the initial interval, the bisection method can handle discontinuities.

Conclusion

In this article, we explained the bisection method, a simple & reliable technique for finding roots of equations. We discussed the algorithm, provided examples, & examined its advantages & disadvantages. The bisection method guarantees convergence & offers good error control, making it a robust choice for many root-finding problems. However, it may converge slowly & is limited to one-dimensional functions. 

Live masterclass