Introduction
The Procedure
Algorithm for Bisection Method
The procedure or algorithm for finding a numerical solution with the bisection method is summarized as follows:
When Should the Bisection Process Stopped?
Example
Here’s an example of a nonlinear equation being solved using the bisection method in Python:
We’ll be solving the equation:
Python Program:
def f(x): """ This function defines the equation to be solved. Args: x: The input value. Returns: The function evaluation at x. """ return x**3 - 2*x**2 - 5*x + 6 def bisection_method(a, b, tol): """ This function implements the bisection method for finding the root of a nonlinear equation. Args: a: The lower bound of the search interval. b: The upper bound of the search interval. tol: The tolerance level for the root approximation. Returns: The approximate root of the equation within the tolerance level. """ while abs(b - a) > tol: mid = (a + b) / 2 if f(a) * f(mid) < 0: b = mid else: a = mid return (a + b) / 2 # Define the equation, search interval, and tolerance level equation = f a = 1 b = 3 tol = 1e-5 # Solve the equation using the bisection method root = bisection_method(a, b, tol) # Print the approximate root print("Approximate root of the equation using bisection method:", root)
This code defines a function f(x) for the equation, then implements the bisection_method function that takes the initial interval (a, b) and tolerance level as input. It iteratively refines the interval until the difference between the upper and lower bounds falls below the tolerance level. Finally, it returns the approximation of the root as the midpoint of the final interval.
Running the code will output:
Approximate root of the equation using bisection method: 2.999996185302734This demonstrates how the bisection method can be used to approximate the root of a nonlinear equation in Python.
Addition Notes About Bisection Method
- The method always converges to an answer, if a root is between interval [a, b] is found by testing if f(a)f(b)<0.
- The method may fail if the function is tangent to the axis and does not cross the x-axis at f(x)=0.
- The method convergence is slower compared to other methods.