DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Experiment 1.2
Student Name: Ravina UID: 23BCS12881
Branch: CSE Section/Group: 801- A
Semester: 6th Date of Performance:15/1/26
Subject Name: Numerical Method Subject Code: 23SMH-341
1. Aim: To implement the Bisection Method to approximate the real root of a nonlinear
equation using Python.
2. Objective: To find the real root of an algebraic and transcendental equation using the
Bisection Method.
3. S/W Requirement: Python 3.11 (Install latest according to your system).
4. Implementation/Code:
Algebraic equation
import math
# Define the function
def f(x):
return x**3 - x - 2
# Bisection Method with iteration table
def bisection(a, b, tol): # tol is tolerance
if f(a) * f(b) >= 0:
print("Bisection method fails")
return None
iteration = 1
# Table header
print("Iter\t a\t\t b\t\t c\t\t f(c)\t\t Error")
print("-" * 75)
while (b - a) / 2 > tol:
c = (a + b) / 2
error = (b - a) / 2
# Print iteration values
print(f"{iteration}\t {a:.6f}\t {b:.6f}\t {c:.6f}\t {f(c):.6f}\t {error:.6f}")
if f(c) == 0:
return c
elif f(a) * f(c) < 0:
b=c
else:
a=c
iteration += 1
return (a + b) / 2
# Input interval and tolerance
root = bisection(0, 2, 1e-5)
print("\nApproximate root:", root)
5. Output :
Transcendental Equation:
Code:
import math
# Define the function (transcendental equation)
def f(x):
return math.e**(-x) - x
# Bisection Method with iteration tsable
def bisection(a, b, tol): # tol is tolerance
if f(a) * f(b) >= 0:
print("Bisection method fails")
return None
iteration = 1
# Table header
print("Iter\t a\t\t b\t\t c\t\t f(c)\t\t Error")
print("-" * 75)
while (b - a) / 2 > tol:
c = (a + b) / 2
error = (b - a) / 2
# Print iteration values
print(f"{iteration}\t {a:.6f}\t {b:.6f}\t {c:.6f}\t {f(c):.6f}\t {error:.6f}")
if f(c) == 0:
return c
elif f(a) * f(c) < 0:
b=c
else:
a=c
iteration += 1
return (a + b) / 2
# Input interval and tolerance
root = bisection(0, 1, 1e-5)
print("\nApproximate root:", root)
Output:
6. Learning Outcomes:
Understood the concept of root finding using the Bisection Method
Learned how to apply the method to algebraic and transcendental equations
Gained experience in implementing iterative numerical methods in Python
Observed the effect of tolerance and interval selection on accuracy