The Bisection Method - Theory and Code
Introduction
The first few algorithms introduced in numerical methods courses are typically root-finding algorithms. In my opinion, these algorithms are taught first because they are relatively easy to understand and code, and determining roots of a function is a very common math operation. They also introduce fundamental numerical analysis topics, such as convergence criteria and iterative operations.
A root, or zero, of a function is a point that satisfies . An arbitrary function may have multiple roots. The Bisection Method is one of the most utilized root-finding algorithms due to its simplicity. Note that the Bisection Method is also sometimes referred to as the Binary-Search Method. The Bisection Method is derived from the Intermediate Value Theorem. In this context, the Intermediate Value Theorem is defined as: : For some function that is defined on the interval , if the sign of and is opposite, there must exist a value such that .
I’ll translate this definition into something more general. Root-finding numerical methods typically accept a function and boundary points (x-values) where we believe a root lies. The lower(left) bound is and the upper (right) bound is . First, we need to make sure our function is continuous and exists between our boundaries . An easy way to verify this is to plot the function. Next, we evaluate our function at and , i.e. determine and . If the signs of and differ, the function must have crossed zero at some point within . This means that there must be some point where the function crossed the x-axis, or in other words, make - a root!
The Bisection Method
Next, I’ll explain how the Bisection Method determines roots. The first step is choosing initial and boundary values that we believe the root is within. Let’s call these and . Next, we determine the midpoint between and via:
Then, the boundary points and and the computed midpoint can be compared:
- If and have the same sign, must exist between and .
- If and have opposite signs, must exist between and .
This relationship can be seen in Figure 1. Since and have the same sign in Figure 1, the root must lie between and . Then, we can update the new interval to be and . Therefore, we can set and . Then, using the above equation, a new midpoint can be computed. The sign check is performed again, and a new interval is determined. This continues until the interval becomes sufficiently small, with the root approximation at the midpoint of the small interval.
Figure 1. Visualization of the bisection method.
Bisection Method Steps
The steps for the Bisection Method looks something like:
- Choose initial boundary points and .
- Compute the midpoint .
- Determine the new interval:
- If and have the same sign, set and .
- If and have opposite signs, set and
$b_2 = p_1$.
- Repeat until interval becomes sufficiently small.
Pros and Cons
Advantages
- Easy to understand conceptually.
- Easy to implement in code.
- Always will converge to a solution, but not necessarily the correct one.
Drawbacks
- Relatively slow to converge compared to other methods (takes more iterations).
- Doesn’t work well when the root is located where the function is flat (near-zero slope).
- Convergence speed depends on how ‘wide’ the initial interval is (smaller = faster).
Convergence Check
As the Bisection Method converges to a zero, the interval will become smaller. To check if the Bisection Method converged to a small interval width, the following inequality should be true:
The Greek letter epsilon, , is commonly used to denote tolerance. In code, I like to use the variable name TOL. The above convergence check is very easy to implement and works just fine. Another way to check convergence is by computing the change in the value of between the current () and prevoius () iteration.
Both ways work fine, but I personally prefer to use the second method, as it is a convergence criteria for many other numerical methods.
Code Implementation (Python)
So, now that we understand how the Bisection Method works, let’s code it. We will try to find a value of that solves:
We can rearrange the equation such that one side of the equation is equal to zero:
Let’s define this in Python code:
import numpy as np
import matplotlib.pyplot as plt
def funct(x):
fx = (x * np.exp(2 * x)) - np.sqrt(x) - (4 * x)
return fxUpon inspection of , one solution/root of the equation is . This is a trivial solution, however. Let’s plot it to determine where the other solution/root is.
x_points = np.linspace(0, 1, 80) # Points to pass to function
plt.figure() # Plot f(x)
plt.plot(x_points, funct(x_points))
plt.title(r'$f(x) = xe^{2x} - \sqrt{x} - 4x$')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.grid()Figure 2. Plot of f(x).
As we can see, the other solution is between and . We’ll use these as our initial boundary points: and . We can choose a tolerance value of and limit the number of iterations to 500. Here is the code for the Bisection Method:
# Initial bounds where we believe the solution/root is.
a = 0.6 # Left boundary
b = 1.0 # Right boundary
TOL = 1e-6 # Relative tolerance convergence criteria
MAX_ITER = 500 # Max. number of iterations
p_prev = 0.0 # Keep track of old p-values
soln = 0.0 # Store final solution in this variable
f_a = funct(a) # Evaluate left point
for iters in range(MAX_ITER): # Iterate until max. iterations are reached.
p = a + ((b - a) / 2.0) # Determine center of the interval, p
f_p = funct(p) # Evaluate midpoint
# Check if tolerance is satisfied
if f_p == 0.0 or np.abs(p - p_prev) / np.abs(p) < TOL:
# Break if tolerance is met, return answer!
soln = p
break
# Determine new bounds depending on the values of f(a) and f(p)
if (np.sign(f_a) * np.sign(f_p)) > 0.0:
a = p # If positive, move to the left
f_a = f_p
else:
b = p # Otherwise (if negative), move to the right
p_prev = p # Replace old with new
# PRINT RESULTS!
print("Bisection Method Results:")
print(" Final Tolerance: %g" % (np.abs(p - p_prev) / np.abs(p)))
print(" Number of Iterations: %g" % (iters))
print(" Solution: x = %g" % (soln))Bisection Method Results:
Final Tolerance: 9.35719e-07
Number of Iterations: 18
Solution: x = 0.815351Therefore, satisfies the equality . We can plot this point over top of the plot of to verify our solution.
x_points = np.linspace(0.6, 1.0, 80)
plt.figure() # Plot f(x)
plt.plot(x_points, funct(x_points), label='f(x)')
plt.plot(soln, funct(soln), 'r*', label='Root of f(x)')
plt.title(r'Root of $f(x) = xe^{2x} - \sqrt{x} - 4x$')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.legend()
plt.grid()Figure 3. Thr root of f(x).
Bisection Method Python Function
Finally, here is a pretty good Python implementation of the Bisection Method:
import numpy as np
def BisectionMethod(
a0: float,
b0: float,
funct,
TOL:float=1e-6,
MAX_ITER:int=500) -> float:
"""Solve for a function's root via the Bisection Method.
Args
----
a0: Initial left boundary point
b0: Initial right boundary point
funct (function): Function of interest, f(x)
TOL: Solution tolerance
MAX_ITER: Maximum number of iterations
Returns
-------
p: Root of f(x) within [a, b]
"""
p_prev = 0.0 # Keep track of old p-values
soln = 0.0 # Store final solution in this variable
f_a = funct(a) # Evaluate left point
for iters in range(MAX_ITER): # Iterate until max. iterations are reached.
p = a + ((b - a) / 2.0) # Determine center of the interval, p
f_p = funct(p) # Evaluate midpoint
# Check if tolerance is satisfied
if f_p == 0.0 or np.abs(p - p_prev) / np.abs(p) < TOL:
# Break if tolerance is met, return answer!
soln = p
break
# Determine new bounds depending on the values of f(a) and f(p)
if (np.sign(f_a) * np.sign(f_p)) > 0.0:
a = p # If positive, move to the left
f_a = f_p
else:
b = p # Otherwise (if negative), move to the right
p_prev = p # Replace old with new
return p
# Replace with your own function
def MyFunction(x):
return x - 2.0
# Initial bounds where we believe the solution/root is.
a = 1.0 # Left boundary
b = 4.0 # Right boundary
root = BisectionMethod(a, b, MyFunction)
print("Root x = ", root)

