0% found this document useful (0 votes)
13 views4 pages

07.3 Bisection Method

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)
13 views4 pages

07.3 Bisection Method

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

chapter19.

03-Bisection-Method 21/05/24 20:27

Bisection Method

file:///Users/diegouribe/Downloads/chapter19.03-Bisection-Method.html Page 1 of 4
chapter19.03-Bisection-Method 21/05/24 20:27

The Intermediate Value Theorem says that if f(x) is a continuous function between a
and b, and sign(f(a)) ≠ sign(f(b)), then there must be a c, such that a < c < b and
f(c) = 0. This is illustrated in the following figure.

The bisection method uses the intermediate value theorem iteratively to find roots. Let
f(x) be a continuous function, and a and b be real scalar values such that a < b.
Assume, without loss of generality, that f(a) > 0 and f(b) < 0. Then by the
intermediate value theorem, there must be a root on the open interval (a, b). Now let
b+a
m = 2 , the midpoint between and a and b. If f(m) = 0 or is close enough, then m is
a root. If f(m) > 0, then m is an improvement on the left bound, a, and there is
guaranteed to be a root on the open interval (m, b). If f(m) < 0, then m is an
improvement on the right bound, b, and there is guaranteed to be a root on the open
interval (a, m). This scenario is depicted in the following figure.

The process of updating a and b can be repeated until the error is acceptably low.

TRY IT! Program a function my_bisection(f, a, b, tol) that approximates a root r of f ,


a+b
bounded by a and b to within |f( 2 )| < tol.

file:///Users/diegouribe/Downloads/chapter19.03-Bisection-Method.html Page 2 of 4
chapter19.03-Bisection-Method 21/05/24 20:27

In [1]: import numpy as np

def my_bisection(f, a, b, tol):


# approximates a root, R, of f bounded
# by a and b to within tolerance
# | f(m) | < tol with m the midpoint
# between a and b Recursive implementation

# check if a and b bound a root


if np.sign(f(a)) == np.sign(f(b)):
raise Exception(
"The scalars a and b do not bound a root")

# get midpoint
m = (a + b)/2

if np.abs(f(m)) < tol:


# stopping condition, report m as root
return m
elif np.sign(f(a)) == np.sign(f(m)):
# case where m is an improvement on a.
# Make recursive call with a = m
return my_bisection(f, m, b, tol)
elif np.sign(f(b)) == np.sign(f(m)):
# case where m is an improvement on b.
# Make recursive call with b = m
return my_bisection(f, a, m, tol)

TRY IT! The √2 can be computed as the root of the function f(x) = x2 − 2. Starting at
a = 0 and b = 2, use my_bisection to approximate the √2 to a tolerance of
|f(x)| < 0.1 and |f(x)| < 0.01. Verify that the results are close to a root by plugging
the root back into the function.

In [2]: f = lambda x: x**2 - 2

r1 = my_bisection(f, 0, 2, 0.1)
print("r1 =", r1)
r01 = my_bisection(f, 0, 2, 0.01)
print("r01 =", r01)

print("f(r1) =", f(r1))


print("f(r01) =", f(r01))

r1 = 1.4375
r01 = 1.4140625
f(r1) = 0.06640625
f(r01) = -0.00042724609375

TRY IT! See what will happen if you use a = 2 and b = 4 for the above function.

file:///Users/diegouribe/Downloads/chapter19.03-Bisection-Method.html Page 3 of 4
chapter19.03-Bisection-Method 21/05/24 20:27

In [3]: my_bisection(f, 2, 4, 0.01)

---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-3-4158b7a9ae67> in <module>
----> 1 my_bisection(f, 2, 4, 0.01)

<ipython-input-1-36f06123e87c> in my_bisection(f, a, b, tol)


10 if np.sign(f(a)) == np.sign(f(b)):
11 raise Exception(
---> 12 "The scalars a and b do not bound a root")
13
14 # get midpoint

Exception: The scalars a and b do not bound a root

file:///Users/diegouribe/Downloads/chapter19.03-Bisection-Method.html Page 4 of 4

You might also like