Sumona

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Experiment Name: Implementation of Bisection Method in Python.

Objectives:
 To understand the concept of the bisection method and how it works.
 To learn how to implement the bisection method in Python.
 To understand the advantages and disadvantages of the bisection method.
 To learn how to use the bisection method to solve problems in Python.
 To learn how to test the implementation of the bisection method and ensure that it works
correctly.

Introduction:
Bisection Method:

The bisection method is a numerical algorithm that is used to find the roots of a function. It is a
closed bracketing method, which means that it requires two initial guesses that bracket the root. The
method works by repeatedly dividing the interval between the two guesses in half and checking
which half contains the root. The process is repeated until the desired accuracy is achieved.

The bisection method is a simple and robust algorithm that is guaranteed to converge to a root if the
function is continuous and changes sign over the interval [a, b] where a and b are the initial guesses
for the root. The method is also easy to implement and does not require any derivatives of the
function.

ALGORITHM:
1. Define the function whose root you want to find.
2. Choose two endpoints of the interval, `x1` and `x2`, such that `f(x1)` and `f(x2)` have opposite
signs.
3. Calculate the midpoint of the interval, `x3 = (x1 + x2) / 2`.
4. Evaluate the function at the midpoint, `f(x3)`.
5. If `f(x3)` is zero, then `x3` is the root of the function.
6. If `f(x3)` and `f(x1)` have opposite signs, then the root is in the interval `[x1, x3]`. Set `x2 = x3`
and go to step 3.
7. If `f(x3)` and `f(x2)` have opposite signs, then the root is in the interval `[x3, x2]`. Set `x1 = x3`
and go to step 3.
8. Repeat steps 3-7 until the root is found or the desired accuracy is achieved.

Implementation code:

from math import log, ceil

def f(x):

return x * x * x - x - 1

def bisect(x1, x2, epsilon):

f1 = f(x1)

if f1 == 0.0:

return x1

f2 = f(x2)

if f2 == 0.0:

return x2

if f1 * f2 > 0.0:

print("Root not found")

n = ceil(log(abs(x2 - x1) / epsilon) / log(2.0))

for i in range(n):

x3 = 0.5 * (x1 + x2)

f3 = f(x3)

if (abs(f3) > abs(f1)) and (abs(f3) > abs(f2)):

return None

if f3 == 0.0:
return x3

if f2 * f3 < 0.0:

x1 = x3

f1 = f3

else:

x2 = x3

f1 = f3

return (x1 + x2) / 2.0

print("Result= ", bisect(1, 2, 0.0001))

Output:

Result = 1.32470703125
Conclusion: In conclusion, through Python, I successfully applied the bisection method to find roots
of equations. It's a simple yet robust technique, especially effective for complex functions. The
method's success hinges on choosing the right initial interval. Its simplicity and reliability make it a
valuable tool for numerical problem-solving.

Moreover, I observed the method's ability to handle functions with erratic behavior, showcasing its
versatility. This simplicity, combined with its iterative nature, makes the bisection method a reliable
choice for various mathematical scenarios. Understanding the problem and selecting the initial
interval thoughtfully play pivotal roles in obtaining accurate and meaningful results.

You might also like