How to do the Bisection method in Python
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The bisection method is one of the safest ways to find a root of a continuous function numerically. It is slower than Newton's method, but it is much easier to reason about because it only needs a sign change over an interval and repeatedly halves that interval until the root is isolated to the desired tolerance.
The Core Idea
If a continuous function has opposite signs at two endpoints a and b, then there is at least one root somewhere between them. The bisection method repeatedly computes the midpoint and keeps the half-interval that still contains a sign change.
The algorithm is:
- choose
aandbsuch thatf(a)andf(b)have opposite signs - compute midpoint
c = (a + b) / 2 - decide whether the root is in
[a, c]or[c, b] - repeat until the interval is small enough or
f(c)is close enough to zero
This makes the method robust and predictable.
A Basic Python Implementation
Here is a simple implementation with error checks and a stopping tolerance.
This version is enough for many small scientific and educational tasks.
Example: Solve x^3 - x - 2 = 0
Now apply the function to a concrete example.
The interval [1, 2] works because f(1) = -2 and f(2) = 4, so the function changes sign across the interval.
Track Iterations for Debugging or Teaching
For learning or debugging, it is useful to record each midpoint and interval update.
Seeing the interval shrink makes the convergence behavior much easier to understand.
Choose a Sensible Stopping Rule
There are two common stopping rules:
- the function value at the midpoint is close enough to zero
- the interval width is smaller than the required tolerance
Using both is common because some functions flatten near the root, and in that case interval width is a more reliable indicator than the function value alone.
Common Pitfalls
- Choosing an interval where
f(a)andf(b)have the same sign. - Applying the method to a discontinuous function and assuming the sign test still guarantees a root.
- Using an interval so large that convergence takes more iterations than expected.
- Forgetting to stop when the interval width is sufficiently small.
- Expecting bisection to be fast when the real advantage is robustness, not speed.
Summary
- The bisection method finds a root by repeatedly halving an interval with a sign change.
- It needs a continuous function and endpoints with opposite signs.
- A small Python implementation is enough for many practical tasks.
- The method is reliable and easy to debug, even if it is not the fastest root finder.
- Use both function-value and interval-width stopping rules for better numerical behavior.
Related reading
- How to dynamically build a JSON object?
- How to dynamically create a class?
- How to easily remember Red-Black Tree insert and delete?
- How to efficiently calculate a row in pascal's triangle?
- How to downgrade Python from 3.7 to 3.5 in Anaconda
- How to download datasets for sklearn? - python
- How to efficiently check if a list of consecutive numbers is missing any elements
- How to efficiently compare two unordered lists not sets?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.