0% found this document useful (0 votes)
39 views8 pages

Lab 01 NC

The document describes the bisection method for finding roots of continuous functions. It provides: 1) A definition and explanation of the bisection method, which iteratively bisects an interval and selects a sub-interval containing the root. 2) An algorithm showing the bisection method procedure. 3) An example of using the bisection method in Python code to find the solution of a given nonlinear equation. 4) Advantages and disadvantages of the bisection method. 5) Output from running the Python code showing convergence of the solution within the specified tolerable error. 6) A conclusion stating that the bisection method is a reliable technique for finding roots when an initial bracket

Uploaded by

haiderkomail113
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views8 pages

Lab 01 NC

The document describes the bisection method for finding roots of continuous functions. It provides: 1) A definition and explanation of the bisection method, which iteratively bisects an interval and selects a sub-interval containing the root. 2) An algorithm showing the bisection method procedure. 3) An example of using the bisection method in Python code to find the solution of a given nonlinear equation. 4) Advantages and disadvantages of the bisection method. 5) Output from running the Python code showing convergence of the solution within the specified tolerable error. 6) A conclusion stating that the bisection method is a reliable technique for finding roots when an initial bracket

Uploaded by

haiderkomail113
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Numerical Computation MTH375

Lab Report-01

Student Name Kumail Haider

Registration FA20-BEE-088
Number

Class/Section 7TH- B

Instructor’s Name DR. Iftikhar Ahmad

COMSATS University Islamabad (CUI), Islamabad Campus Page


1
Bisection Method
Definition
This method is a root-finding method that applies to any continuous
functions with two known values of opposite signs. It is a very simple but
cumbersome method. The interval defined by these two values is bisected
and a sub-interval in which the function changes sign is selected. This sub-
interval must contain the root. These two steps are repeatedly executed until
the root is in the form of the required precision level. This method is also
called the interval halving method.

The Method: Explaination


Let f be a continuous function defined on an interval [a,b] where f(a) and
f(b) have opposite signs. Bisection method is applicable for solving the
equation f(x)=0 for a real variable x. At each step, the interval is divided into
two parts/halves by computing the midpoint, c=(a+b)2, and the value of f(c)
at that point. Unless the root is c, there are two possibilities:f(a) and f(c) have
opposite signs and bracket a root, f(c) and f(b) have opposite signs and
bracket a root. One of the sub-intervals is chosen as the new interval to be
used in the next step. This process is carried out again and again until the
interval is sufficiently small.If f(a) and f(c) have opposite signs, then the
value of b is replaced by c.If f(b) and f(c) have opposite signs, then the value
of a is replaced by c.In the case that f(c)=0

COMSATS University Islamabad (CUI), Islamabad Campus Page

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.

Bisection Method Advantages


1. Convergence is guarenteed: Bisection method is bracketing method and it is always
convergent.
2. Error can be controlled: In Bisection method, increasing number of iteration always
yields more accurate root.
3. Does not involve complex calculations: Bisection method does not require any
complex calculations. To perform Bisection method, all we need is to calculate
average of two numbers.
4. Guaranteed error bound: In this method, there is a guaranteed error bound, and it
decreases with each successive iteration. The error bound decreases by ½ with each
iteration.

COMSATS University Islamabad (CUI), Islamabad Campus Page

3
5. Bisection method is very simple and easy to program in computer.
6. Bisection method is fast in case of multiple roots.

Bisection Method Disadvantages


1. Slow Rate of Convergence: Although convergence of Bisection method is
guaranteed, it is generally slow.
2. Choosing one guess close to root has no advantage: Choosing one guess close to
the root may result in requiring many iterations to converge.
3. Can not find root of some equations. For example: f(x) = x2 as there are no
bracketing values.
4. It has linear rate of convergence.
5. It fails to determine complex roots.
6. It can not be applied if there are discontinuities in the guess interval.
7. It can not be applied over an interval where the function takes values of the same
sign.

Question: Find the solution of the following nonlinear equation

𝑥^3−𝑥^2+7𝑥−9

Code the Bisection Method Using Python:


INPUT:
First Guess: 0
Second Guess: 5
Tolerable Error: 0.00001

# Defining Function
def f(x):
return x**3-x**2+7*x-9

# Implementing Bisection Method


def bisection(x0,x1,e):
step = 1
print('\n\n*** BISECTION METHOD IMPLEMENTATION ***')
condition = True
while condition:
COMSATS University Islamabad (CUI), Islamabad Campus Page

4
x2 = (x0 + x1)/2
print('Iteration-%d, x2 = %0.6f and f(x2) = %0.6f' % (step, x2, f(x2)))

if f(x0) * f(x2) < 0:


x1 = x2
else:
x0 = x2

step = step + 1
condition = abs(f(x2)) > e

print('\nRequired Root is : %0.8f' % x2)

# Input Section
x0 = input('First Guess: ')
x1 = input('Second Guess: ')
e = input('Tolerable Error: ')

# Converting input to float


x0 = float(x0)
x1 = float(x1)
e = float(e)

#Note: You can combine above two section like this


# x0 = float(input('First Guess: '))
# x1 = float(input('Second Guess: '))
# e = float(input('Tolerable Error: '))

# Checking Correctness of initial guess values and bisecting


if f(x0) * f(x1) > 0.0:
print('Given guess values do not bracket the root.')
print('Try Again with different guess values.')
else:
bisection(x0,x1,e)

COMSATS University Islamabad (CUI), Islamabad Campus Page

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

Required Root is : 1.23462439


Table of Iterations:

| 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 |

Required Root is : 1.23462439

Plot:

COMSATS University Islamabad (CUI), Islamabad Campus Page

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.

COMSATS University Islamabad (CUI), Islamabad Campus Page

You might also like