Lab 01 NC
Lab 01 NC
Lab Report-01
Registration FA20-BEE-088
Number
Class/Section 7TH- B
2
Bisection Method Algorithm
Follow the below procedure to get the solution for the continuous
function:
For any continuous function f(x),
• Find two points, say a and b such that a < b and f(a)* f(b) <
0
• Find the midpoint of a and b, say “t”
• t is the root of the given function if f(t) = 0; else follow the
next step
• Divide the interval [a, b] – If f(t)*f(a) <0, there exist a root
between t and a
– else if f(t) *f (b) < 0, there exist a root between t and b
• Repeat above three steps until f(t) = 0.
3
5. Bisection method is very simple and easy to program in computer.
6. Bisection method is fast in case of multiple roots.
𝑥^3−𝑥^2+7𝑥−9
# Defining Function
def f(x):
return x**3-x**2+7*x-9
4
x2 = (x0 + x1)/2
print('Iteration-%d, x2 = %0.6f and f(x2) = %0.6f' % (step, x2, f(x2)))
step = step + 1
condition = abs(f(x2)) > e
# Input Section
x0 = input('First Guess: ')
x1 = input('Second Guess: ')
e = input('Tolerable Error: ')
5
Output:
*** BISECTION METHOD IMPLEMENTATION ***
Iteration-1, x2 = 2.500000 and f(x2) = 17.875000
Iteration-2, x2 = 1.250000 and f(x2) = 0.140625
Iteration-3, x2 = 0.625000 and f(x2) = -4.771484
Iteration-4, x2 = 0.937500 and f(x2) = -2.492432
Iteration-5, x2 = 1.093750 and f(x2) = -1.231598
Iteration-6, x2 = 1.171875 and f(x2) = -0.560841
Iteration-7, x2 = 1.210938 and f(x2) = -0.214125
Iteration-8, x2 = 1.230469 and f(x2) = -0.037777
Iteration-9, x2 = 1.240234 and f(x2) = 0.051165
Iteration-10, x2 = 1.235352 and f(x2) = 0.006629
Iteration-11, x2 = 1.232910 and f(x2) = -0.015590
Iteration-12, x2 = 1.234131 and f(x2) = -0.004484
Iteration-13, x2 = 1.234741 and f(x2) = 0.001072
Iteration-14, x2 = 1.234436 and f(x2) = -0.001707
Iteration-15, x2 = 1.234589 and f(x2) = -0.000318
Iteration-16, x2 = 1.234665 and f(x2) = 0.000377
Iteration-17, x2 = 1.234627 and f(x2) = 0.000030
Iteration-18, x2 = 1.234608 and f(x2) = -0.000144
Iteration-19, x2 = 1.234617 and f(x2) = -0.000057
Iteration-20, x2 = 1.234622 and f(x2) = -0.000014
Iteration-21, x2 = 1.234624 and f(x2) = 0.000008
| Step | x2 | f(x2) |
| 1 | 2.5 | 17.875 |
| 2 | 1.25 | 0.140625 |
| 3 | 0.625 | -4.77148 |
| 4 | 0.9375 | -2.49243 |
| 5 | 1.09375 | -1.2316 |
| 6 | 1.17188 | -0.560841 |
| 7 | 1.21094 | -0.214125 |
| 8 | 1.23047 | -0.0377768 |
| 9 | 1.24023 | 0.0511646 |
| 10 | 1.23535 | 0.00662942 |
| 11 | 1.23291 | -0.0155898 |
| 12 | 1.23413 | -0.00448419 |
| 13University
COMSATS | 1.23474 | 0.00107161
Islamabad |
(CUI), Islamabad Campus Page
6
| 14 | 1.23444 | -0.00170655 |
| 15 | 1.23459 | -0.000317532 |
| 16 | 1.23466 | 0.000377022 |
| 17 | 1.23463 | 2.97409e-05 |
| 18 | 1.23461 | -0.000143897 |
| 19 | 1.23462 | -5.70781e-05 |
| 20 | 1.23462 | -1.36687e-05 |
| 21 | 1.23462 | 8.03606e-06 |
Plot:
7
Conclusion:
In this Bisection Method lab, we implemented the bisection algorithm to find
the root of a given function. The method successfully converged to the root with
the specified tolerable error by iteratively narrowing down the search interval.
The key takeaway is that the bisection method is a reliable numerical technique
for finding roots of functions when an initial interval that brackets the root is
provided. This method is simple, robust, and widely applicable in various fields
of mathematics and science.