The Bisection Method

Algorithm

In [ ]:
'''
To find a solution to f(x) = 0 given the continuous function f on the interval [a,b], where f(a) and f(b) have opposite signs:
INPUT endpoints a,b; tolerance TOL; maximum number of iterations N₀.
OUTPUT approximate solution p or message of failure.
Step 1 Set i = 1;
         FA = f(a).
Step 2 While i ≤ N₀ do Steps 3–6.
Step 3 Set p = a + (b − a)/2; (Compute pᵢ.)
         FP = f(p).
Step 4 If FP = 0 or (b − a)/2 < TOL then
         OUTPUT (p); (Procedure completed successfully.)
         STOP.
Step 5 Set i = i + 1.
Step 6 If FA · FP > 0 then set a = p; (Compute aᵢ ,bᵢ .)
         FA = FP
         else set b = p. (FA is unchanged.)
Step 7 OUTPUT (‘Method failed after N₀ iterations, N₀ =’, N₀ );
         (The procedure was unsuccessful.)
         STOP. 
'''

Code

In [2]:
import pandas as pd
In [3]:
def Bisection(a, b, f, tolerance=0.000001, max_iterations=100, output='answer', stopping_criterion='tolerance', iteration=100):
    """The Bisection Algorithm

    Keyword arguments:
    a (double) -- starting interval
    b (double) -- ending interval
    f (function) -- function defined on interval
    tolerance (double) -- error tolerance (default = 0.000001)
    max_iterations (int) -- number of maximum iterations (default = 100)
    output (string) -- how to output the result (default = 'answer')
                possible values are:
                  - 'answer' : to show the value of "p"
                  - 'table' : to show all the values in tabular form
    stopping_criterion (string) -- when to stop the processing (default = 'tolerance')
                possible vaulse are:
                  - 'tolerance' : to stop at a certain tolerance level
                  - 'iteration' : to stop at a certain iteration (must provide value for 'iteration' argument)
    iteration (int) -- on which iteration to stop the processing (default = 100)
    
    Returns:
    
    bool: False if there's a problem
    
    Otherwise it will return the value according to the value in 'output' argument.
    If value of output is 'answer', it will return a float. If it is 'table', it will return a DataFrame.
    """
    
    # check if 'f' is a function
    if(not callable(f)):
        print ("Please provide a function 'f'")
        return False
    
    fa = f(a)
    fb = f(b)
    # check if fa and fb have the same signs
    if ((fa > 0 and fb > 0) or (fa < 0 and fb < 0)):
        print ("This problem is unsolvable as")
        print ("f(a) = ", fa)
        print ("f(b) = ", fb)
        print ("They are both ", "negative." if fa < 0 else "positive.")
        return False
    
    if (output == "table"):
        A = list()
        B = list()
        P = list()
        FA = list()
        FB = list()
        FP = list()
    
    for i in range(1, max_iterations):
        p = round(a + (b - a)/2, 9)
        fp = f(p)
        
        if (output == "table"):
            A.append(a)
            B.append(b)
            P.append(p)
            FA.append(fa)
            FB.append(fb)
            FP.append(fp)
        
        if (fp == 0 or (b-a)/2 < tolerance or (stopping_criterion == 'iteration' and iteration == i)):
            if (output == 'answer'):
                return p
            if (output == 'table'):
                # create a table and return it
                d = {'a': A, 'f(a)': FA, 'b' : B, 'f(b)' : FB, 'p' : P, 'f(p)': FP}
                df = pd.DataFrame(data=d, columns=['a', 'f(a)', 'b', 'f(b)', 'p', 'f(p)'])
                df.index = df.index + 1
                return df
        
        if (fa * fp > 0): 
            a = p
            fa = fp
        else:
            b = p
            fb = fp
    
    print ("Method failed after ", max_iterations, " iterations.")
    return False

Example 1

Show that f(x) = x³ + 4x² − 10 = 0 has a root in [1,2], and use the Bisection method to determine an approximation to the root that is accurate to at least within 10⁻⁴

In [4]:
def f(x):
    return pow(x, 3) + 4 * pow(x, 2) - 10
In [5]:
Bisection(1, 2, f, pow(10, -4), output="table")
Out[5]:
a f(a) b f(b) p f(p)
1 1.000000 -5.000000 2.000000 14.000000 1.500000 2.375000
2 1.000000 -5.000000 1.500000 2.375000 1.250000 -1.796875
3 1.250000 -1.796875 1.500000 2.375000 1.375000 0.162109
4 1.250000 -1.796875 1.375000 0.162109 1.312500 -0.848389
5 1.312500 -0.848389 1.375000 0.162109 1.343750 -0.350983
6 1.343750 -0.350983 1.375000 0.162109 1.359375 -0.096409
7 1.359375 -0.096409 1.375000 0.162109 1.367188 0.032356
8 1.359375 -0.096409 1.367188 0.032356 1.363281 -0.032150
9 1.363281 -0.032150 1.367188 0.032356 1.365234 0.000072
10 1.363281 -0.032150 1.365234 0.000072 1.364258 -0.016047
11 1.364258 -0.016047 1.365234 0.000072 1.364746 -0.007989
12 1.364746 -0.007989 1.365234 0.000072 1.364990 -0.003959
13 1.364990 -0.003959 1.365234 0.000072 1.365112 -0.001944
14 1.365112 -0.001944 1.365234 0.000072 1.365173 -0.000936
In [6]:
print("The answer is : " , Bisection(1, 2, f, pow(10, -4), output="answer"))
The answer is :  1.36517334

Exercise Set 2.1

(1) Use the Bisection method to find p₃ for f(x) = √x − cosx on [0,1].

In [27]:
import math
def f(x):
    return math.sqrt(x) - math.cos(x)
In [28]:
Bisection(0, 1, f, output="table", stopping_criterion='iteration', iteration=3)
Out[28]:
a f(a) b f(b) p f(p)
1 0.0 -1.000000 1.00 0.459698 0.500 -0.170476
2 0.5 -0.170476 1.00 0.459698 0.750 0.134337
3 0.5 -0.170476 0.75 0.134337 0.625 -0.020394
In [29]:
print("The answer is : " , Bisection(0, 1, f, output="answer", stopping_criterion='iteration', iteration=3))
The answer is :  0.625

(2) Let f(x) = 3(x + 1)(x −1/2)(x − 1). Use the Bisection method on the following intervals to find p₃

In [30]:
def f(x):
    return 3*(x + 1)*(x-(1/2))*(x-1)

a. [-2, 1.5]

In [31]:
Bisection(-2, 1.5, f, output="table", stopping_criterion='iteration', iteration=3)
Out[31]:
a f(a) b f(b) p f(p)
1 -2.000 -22.500000 1.50 3.750000 -0.2500 2.109375
2 -2.000 -22.500000 -0.25 2.109375 -1.1250 -1.294922
3 -1.125 -1.294922 -0.25 2.109375 -0.6875 1.878662
In [32]:
print("The answer is : " , Bisection(-2, 1.5, f, output="answer", stopping_criterion='iteration', iteration=3))
The answer is :  -0.6875

b. [−1.25,2.5]

In [33]:
Bisection(-1.25, 2.5, f, output="table", stopping_criterion='iteration', iteration=3)
Out[33]:
a f(a) b f(b) p f(p)
1 -1.250 -2.953125 2.5000 31.500000 0.62500 -0.228516
2 0.625 -0.228516 2.5000 31.500000 1.56250 4.594482
3 0.625 -0.228516 1.5625 4.594482 1.09375 0.349640
In [34]:
print("The answer is : " , Bisection(-1.25, 2.5, f, output="answer", stopping_criterion='iteration', iteration=3))
The answer is :  1.09375

(3) Use the Bisection method to find solutions accurate to within 10⁻² for x³ − 7x² + 14x − 6 = 0 on each interval.

In [35]:
def f(x):
    return pow(x, 3) - 7*pow(x, 2) + 14*x - 6

a. [0,1]

In [36]:
Bisection(0, 1, f, tolerance=pow(10, -2), output="table")
Out[36]:
a f(a) b f(b) p f(p)
1 0.000000 -6.000000 1.00000 2.000000 0.500000 -0.625000
2 0.500000 -0.625000 1.00000 2.000000 0.750000 0.984375
3 0.500000 -0.625000 0.75000 0.984375 0.625000 0.259766
4 0.500000 -0.625000 0.62500 0.259766 0.562500 -0.161865
5 0.562500 -0.161865 0.62500 0.259766 0.593750 0.054047
6 0.562500 -0.161865 0.59375 0.054047 0.578125 -0.052624
7 0.578125 -0.052624 0.59375 0.054047 0.585938 0.001031
In [37]:
print("The answer is : " , Bisection(0, 1, f, tolerance=pow(10, -2), output="answer"))
The answer is :  0.5859375

b. [1,3.2]

In [38]:
Bisection(1, 3.2, f, tolerance=pow(10, -2), output="table")
Out[38]:
a f(a) b f(b) p f(p)
1 1.00000 2.000000 3.200000 -0.112000 2.100000 1.791000
2 2.10000 1.791000 3.200000 -0.112000 2.650000 0.552125
3 2.65000 0.552125 3.200000 -0.112000 2.925000 0.085828
4 2.92500 0.085828 3.200000 -0.112000 3.062500 -0.054443
5 2.92500 0.085828 3.062500 -0.054443 2.993750 0.006328
6 2.99375 0.006328 3.062500 -0.054443 3.028125 -0.026521
7 2.99375 0.006328 3.028125 -0.026521 3.010937 -0.010697
8 2.99375 0.006328 3.010937 -0.010697 3.002344 -0.002333
In [39]:
print("The answer is : " , Bisection(1, 3.2, f, tolerance=pow(10, -2), output="answer"))
The answer is :  3.00234375

c. [3.2,4]

In [40]:
Bisection(3.2, 4, f, tolerance=pow(10, -2), output="table")
Out[40]:
a f(a) b f(b) p f(p)
1 3.2000 -0.112000 4.000 2.000000 3.60000 0.336000
2 3.2000 -0.112000 3.600 0.336000 3.40000 -0.016000
3 3.4000 -0.016000 3.600 0.336000 3.50000 0.125000
4 3.4000 -0.016000 3.500 0.125000 3.45000 0.046125
5 3.4000 -0.016000 3.450 0.046125 3.42500 0.013016
6 3.4000 -0.016000 3.425 0.013016 3.41250 -0.001998
7 3.4125 -0.001998 3.425 0.013016 3.41875 0.005382
In [41]:
print("The answer is : " , Bisection(3.2, 4, f, tolerance=pow(10, -2), output="answer"))
The answer is :  3.41875

(4) Use the Bisection method to find solutions accurate to within 10⁻² for x⁴ − 2x³ − 4x² + 4x + 4 = 0 on each interval.

In [42]:
def f(x):
    return pow(x, 4) - 2*pow(x, 3) - 4*pow(x, 2) + 4*x + 4

a. [−2,−1]

In [43]:
Bisection(-2, -1, f, tolerance=pow(10, -2), output="table")
Out[43]:
a f(a) b f(b) p f(p)
1 -2.000000 12.000000 -1.00000 -1.000000 -1.500000 0.812500
2 -1.500000 0.812500 -1.00000 -1.000000 -1.250000 -0.902344
3 -1.500000 0.812500 -1.25000 -0.902344 -1.375000 -0.288818
4 -1.500000 0.812500 -1.37500 -0.288818 -1.437500 0.195328
5 -1.437500 0.195328 -1.37500 -0.288818 -1.406250 -0.062667
6 -1.437500 0.195328 -1.40625 -0.062667 -1.421875 0.062263
7 -1.421875 0.062263 -1.40625 -0.062667 -1.414062 -0.001208
In [44]:
print("The answer is : " , Bisection(-2, -1, f, tolerance=pow(10, -2), output="answer"))
The answer is :  -1.4140625

b. [0,2]

In [45]:
Bisection(0, 2, f, tolerance=pow(10, -2), output="table")
Out[45]:
a f(a) b f(b) p f(p)
1 0.00000 4.000000 2.000000 -4.000000 1.000000 3.000000
2 1.00000 3.000000 2.000000 -4.000000 1.500000 -0.687500
3 1.00000 3.000000 1.500000 -0.687500 1.250000 1.285156
4 1.25000 1.285156 1.500000 -0.687500 1.375000 0.312744
5 1.37500 0.312744 1.500000 -0.687500 1.437500 -0.186508
6 1.37500 0.312744 1.437500 -0.186508 1.406250 0.063676
7 1.40625 0.063676 1.437500 -0.186508 1.421875 -0.061318
8 1.40625 0.063676 1.421875 -0.061318 1.414062 0.001208
In [46]:
print("The answer is : " , Bisection(0, 2, f, tolerance=pow(10, -2), output="answer"))
The answer is :  1.4140625

c. [2,3]

In [47]:
Bisection(2, 3, f, tolerance=pow(10, -2), output="table")
Out[47]:
a f(a) b f(b) p f(p)
1 2.00000 -4.000000 3.000000 7.000000 2.500000 -3.187500
2 2.50000 -3.187500 3.000000 7.000000 2.750000 0.347656
3 2.50000 -3.187500 2.750000 0.347656 2.625000 -1.757568
4 2.62500 -1.757568 2.750000 0.347656 2.687500 -0.795639
5 2.68750 -0.795639 2.750000 0.347656 2.718750 -0.247466
6 2.71875 -0.247466 2.750000 0.347656 2.734375 0.044125
7 2.71875 -0.247466 2.734375 0.044125 2.726562 -0.103151
In [48]:
print("The answer is : " , Bisection(2, 3, f, tolerance=pow(10, -2), output="answer"))
The answer is :  2.7265625

d. [−1,0]

In [49]:
Bisection(-1, 0, f, tolerance=pow(10, -2), output="table")
Out[49]:
a f(a) b f(b) p f(p)
1 -1.000000 -1.000000 0.00000 4.000000 -0.500000 1.312500
2 -1.000000 -1.000000 -0.50000 1.312500 -0.750000 -0.089844
3 -0.750000 -0.089844 -0.50000 1.312500 -0.625000 0.578369
4 -0.750000 -0.089844 -0.62500 0.578369 -0.687500 0.232681
5 -0.750000 -0.089844 -0.68750 0.232681 -0.718750 0.068086
6 -0.750000 -0.089844 -0.71875 0.068086 -0.734375 -0.011768
7 -0.734375 -0.011768 -0.71875 0.068086 -0.726562 0.027943
In [50]:
print("The answer is : " , Bisection(-1, 0, f, tolerance=pow(10, -2), output="answer"))
The answer is :  -0.7265625

(5) Use the Bisection method to find solutions accurate to within 10 −5 for the following problems.

a. x − 2⁻ˣ = 0 for 0 ≤ x ≤ 1

In [51]:
def f(x):
    return x - pow(2, -x)
In [52]:
Bisection(0, 1, f, tolerance=pow(10, -5), output="table")
Out[52]:
a f(a) b f(b) p f(p)
1 0.000000 -1.000000 1.000000 0.500000 0.500000 -0.207107
2 0.500000 -0.207107 1.000000 0.500000 0.750000 0.155396
3 0.500000 -0.207107 0.750000 0.155396 0.625000 -0.023420
4 0.625000 -0.023420 0.750000 0.155396 0.687500 0.066571
5 0.625000 -0.023420 0.687500 0.066571 0.656250 0.021725
6 0.625000 -0.023420 0.656250 0.021725 0.640625 -0.000810
7 0.640625 -0.000810 0.656250 0.021725 0.648438 0.010467
8 0.640625 -0.000810 0.648438 0.010467 0.644531 0.004831
9 0.640625 -0.000810 0.644531 0.004831 0.642578 0.002011
10 0.640625 -0.000810 0.642578 0.002011 0.641602 0.000601
11 0.640625 -0.000810 0.641602 0.000601 0.641113 -0.000105
12 0.641113 -0.000105 0.641602 0.000601 0.641357 0.000248
13 0.641113 -0.000105 0.641357 0.000248 0.641235 0.000072
14 0.641113 -0.000105 0.641235 0.000072 0.641174 -0.000017
15 0.641174 -0.000017 0.641235 0.000072 0.641205 0.000028
16 0.641174 -0.000017 0.641205 0.000028 0.641190 0.000006
17 0.641174 -0.000017 0.641190 0.000006 0.641182 -0.000005
In [53]:
print("The answer is : " , Bisection(0, 1, f, tolerance=pow(10, -5), output="answer"))
The answer is :  0.641181945

b. eˣ − x² + 3x − 2 = 0 for 0 ≤ x ≤ 1

In [54]:
import math

def f(x):
    return math.exp(x) - pow(x, 2) + 3*x - 2
In [55]:
Bisection(0, 1, f, tolerance=pow(10, -5), output="table")
Out[55]:
a f(a) b f(b) p f(p)
1 0.000000 -1.000000 1.000000 2.718282 0.500000 8.987213e-01
2 0.000000 -1.000000 0.500000 0.898721 0.250000 -2.847458e-02
3 0.250000 -0.028475 0.500000 0.898721 0.375000 4.393664e-01
4 0.250000 -0.028475 0.375000 0.439366 0.312500 2.066817e-01
5 0.250000 -0.028475 0.312500 0.206682 0.281250 8.943320e-02
6 0.250000 -0.028475 0.281250 0.089433 0.265625 3.056423e-02
7 0.250000 -0.028475 0.265625 0.030564 0.257812 1.066368e-03
8 0.250000 -0.028475 0.257812 0.001066 0.253906 -1.369868e-02
9 0.253906 -0.013699 0.257812 0.001066 0.255859 -6.314807e-03
10 0.255859 -0.006315 0.257812 0.001066 0.256836 -2.623880e-03
11 0.256836 -0.002624 0.257812 0.001066 0.257324 -7.786722e-04
12 0.257324 -0.000779 0.257812 0.001066 0.257568 1.438707e-04
13 0.257324 -0.000779 0.257568 0.000144 0.257446 -3.173936e-04
14 0.257446 -0.000317 0.257568 0.000144 0.257507 -8.676012e-05
15 0.257507 -0.000087 0.257568 0.000144 0.257538 2.855751e-05
16 0.257507 -0.000087 0.257538 0.000029 0.257523 -2.910122e-05
17 0.257523 -0.000029 0.257538 0.000029 0.257530 -2.737264e-07
In [56]:
print("The answer is : " , Bisection(0, 1, f, tolerance=pow(10, -5), output="answer"))
The answer is :  0.257530213

c. 2x cos(2x) − (x + 1)² = 0 for −3 ≤ x ≤ −2 and −1 ≤ x ≤ 0

In [57]:
import math

def f(x):
    return 2*x*math.cos(2*x) - pow(x + 1, 2)
In [58]:
Bisection(-3, -2, f, tolerance=pow(10, -5), output="table")
Out[58]:
a f(a) b f(b) p f(p)
1 -3.000000 -9.761022 -2.000000 1.614574 -2.500000 -3.668311
2 -2.500000 -3.668311 -2.000000 1.614574 -2.250000 -0.613919
3 -2.250000 -0.613919 -2.000000 1.614574 -2.125000 0.630247
4 -2.250000 -0.613919 -2.125000 0.630247 -2.187500 0.038076
5 -2.250000 -0.613919 -2.187500 0.038076 -2.218750 -0.280836
6 -2.218750 -0.280836 -2.187500 0.038076 -2.203125 -0.119557
7 -2.203125 -0.119557 -2.187500 0.038076 -2.195312 -0.040279
8 -2.195312 -0.040279 -2.187500 0.038076 -2.191406 -0.000985
9 -2.191406 -0.000985 -2.187500 0.038076 -2.189453 0.018574
10 -2.191406 -0.000985 -2.189453 0.018574 -2.190430 0.008802
11 -2.191406 -0.000985 -2.190430 0.008802 -2.190918 0.003910
12 -2.191406 -0.000985 -2.190918 0.003910 -2.191162 0.001463
13 -2.191406 -0.000985 -2.191162 0.001463 -2.191284 0.000239
14 -2.191406 -0.000985 -2.191284 0.000239 -2.191345 -0.000373
15 -2.191345 -0.000373 -2.191284 0.000239 -2.191315 -0.000067
16 -2.191315 -0.000067 -2.191284 0.000239 -2.191299 0.000086
17 -2.191315 -0.000067 -2.191299 0.000086 -2.191307 0.000009
In [59]:
print("The answer is : " , Bisection(-3, -2, f, tolerance=pow(10, -5), output="answer"))
The answer is :  -2.191307068
In [60]:
Bisection(-1, 0, f, tolerance=pow(10, -5), output="table")
Out[60]:
a f(a) b f(b) p f(p)
1 -1.000000 0.832294 0.000000 -1.000000 -0.500000 -0.790302
2 -1.000000 0.832294 -0.500000 -0.790302 -0.750000 -0.168606
3 -1.000000 0.832294 -0.750000 -0.168606 -0.875000 0.296306
4 -0.875000 0.296306 -0.750000 -0.168606 -0.812500 0.052882
5 -0.812500 0.052882 -0.750000 -0.168606 -0.781250 -0.060814
6 -0.812500 0.052882 -0.781250 -0.060814 -0.796875 -0.004681
7 -0.812500 0.052882 -0.796875 -0.004681 -0.804688 0.023925
8 -0.804688 0.023925 -0.796875 -0.004681 -0.800781 0.009578
9 -0.800781 0.009578 -0.796875 -0.004681 -0.798828 0.002438
10 -0.798828 0.002438 -0.796875 -0.004681 -0.797852 -0.001124
11 -0.798828 0.002438 -0.797852 -0.001124 -0.798340 0.000656
12 -0.798340 0.000656 -0.797852 -0.001124 -0.798096 -0.000234
13 -0.798340 0.000656 -0.798096 -0.000234 -0.798218 0.000211
14 -0.798218 0.000211 -0.798096 -0.000234 -0.798157 -0.000012
15 -0.798218 0.000211 -0.798157 -0.000012 -0.798187 0.000100
16 -0.798187 0.000100 -0.798157 -0.000012 -0.798172 0.000044
17 -0.798172 0.000044 -0.798157 -0.000012 -0.798164 0.000016
In [61]:
print("The answer is : " , Bisection(-1, 0, f, tolerance=pow(10, -5), output="answer"))
The answer is :  -0.798164367

d. x cosx − 2x² + 3x − 1 = 0 for 0.2 ≤ x ≤ 0.3 and 1.2 ≤ x ≤ 1.3

In [62]:
import math

def f(x):
    return x * math.cos(x) - 2*pow(x, 2) + 3*x - 1
In [63]:
Bisection(0.2, 0.3, f, tolerance=pow(10, -5), output="table")
Out[63]:
a f(a) b f(b) p f(p)
1 0.200000 -0.283987 0.300000 0.006601 0.250000 -0.132772
2 0.250000 -0.132772 0.300000 0.006601 0.275000 -0.061583
3 0.275000 -0.061583 0.300000 0.006601 0.287500 -0.027113
4 0.287500 -0.027113 0.300000 0.006601 0.293750 -0.010161
5 0.293750 -0.010161 0.300000 0.006601 0.296875 -0.001756
6 0.296875 -0.001756 0.300000 0.006601 0.298438 0.002428
7 0.296875 -0.001756 0.298438 0.002428 0.297656 0.000338
8 0.296875 -0.001756 0.297656 0.000338 0.297266 -0.000709
9 0.297266 -0.000709 0.297656 0.000338 0.297461 -0.000186
10 0.297461 -0.000186 0.297656 0.000338 0.297559 0.000076
11 0.297461 -0.000186 0.297559 0.000076 0.297510 -0.000055
12 0.297510 -0.000055 0.297559 0.000076 0.297534 0.000011
13 0.297510 -0.000055 0.297534 0.000011 0.297522 -0.000022
14 0.297522 -0.000022 0.297534 0.000011 0.297528 -0.000006
In [64]:
print("The answer is : " , Bisection(0.2, 0.3, f, tolerance=pow(10, -5), output="answer"))
The answer is :  0.297528077
In [65]:
Bisection(1.2, 1.3, f, tolerance=pow(10, -5), output="table")
Out[65]:
a f(a) b f(b) p f(p)
1 1.200000 0.154829 1.300000 -0.132252 1.250000 0.019153
2 1.250000 0.019153 1.300000 -0.132252 1.275000 -0.054585
3 1.250000 0.019153 1.275000 -0.054585 1.262500 -0.017225
4 1.250000 0.019153 1.262500 -0.017225 1.256250 0.001087
5 1.256250 0.001087 1.262500 -0.017225 1.259375 -0.008038
6 1.256250 0.001087 1.259375 -0.008038 1.257812 -0.003468
7 1.256250 0.001087 1.257812 -0.003468 1.257031 -0.001189
8 1.256250 0.001087 1.257031 -0.001189 1.256641 -0.000050
9 1.256250 0.001087 1.256641 -0.000050 1.256445 0.000518
10 1.256445 0.000518 1.256641 -0.000050 1.256543 0.000234
11 1.256543 0.000234 1.256641 -0.000050 1.256592 0.000092
12 1.256592 0.000092 1.256641 -0.000050 1.256616 0.000021
13 1.256616 0.000021 1.256641 -0.000050 1.256628 -0.000015
14 1.256616 0.000021 1.256628 -0.000015 1.256622 0.000003
In [66]:
print("The answer is : " , Bisection(1.2, 1.3, f, tolerance=pow(10, -5), output="answer"))
The answer is :  1.256622314

(6) Use the Bisection method to find solutions, accurate to within 10⁻⁵ for the following problems.

a. 3x − eˣ = 0 for 1 ≤ x ≤ 2

In [67]:
import math

def f(x):
    return 3*x - math.exp(x)
In [68]:
Bisection(1, 2, f, tolerance=pow(10, -5), output="table")
Out[68]:
a f(a) b f(b) p f(p)
1 1.000000 0.281718 2.000000 -1.389056 1.500000 0.018311
2 1.500000 0.018311 2.000000 -1.389056 1.750000 -0.504603
3 1.500000 0.018311 1.750000 -0.504603 1.625000 -0.203419
4 1.500000 0.018311 1.625000 -0.203419 1.562500 -0.083233
5 1.500000 0.018311 1.562500 -0.083233 1.531250 -0.030203
6 1.500000 0.018311 1.531250 -0.030203 1.515625 -0.005390
7 1.500000 0.018311 1.515625 -0.005390 1.507812 0.006598
8 1.507812 0.006598 1.515625 -0.005390 1.511719 0.000638
9 1.511719 0.000638 1.515625 -0.005390 1.513672 -0.002367
10 1.511719 0.000638 1.513672 -0.002367 1.512695 -0.000862
11 1.511719 0.000638 1.512695 -0.000862 1.512207 -0.000111
12 1.511719 0.000638 1.512207 -0.000111 1.511963 0.000264
13 1.511963 0.000264 1.512207 -0.000111 1.512085 0.000076
14 1.512085 0.000076 1.512207 -0.000111 1.512146 -0.000018
15 1.512085 0.000076 1.512146 -0.000018 1.512115 0.000029
16 1.512115 0.000029 1.512146 -0.000018 1.512131 0.000006
17 1.512131 0.000006 1.512146 -0.000018 1.512138 -0.000006
In [69]:
print("The answer is : " , Bisection(1, 2, f, tolerance=pow(10, -5), output="answer"))
The answer is :  1.512138366

b. 2x + 3cosx − eˣ = 0 for 0 ≤ x ≤ 1

In [70]:
import math

def f(x):
    return 2*x + 3*math.cos(x) - math.exp(x)
In [71]:
Bisection(0, 1, f, tolerance=pow(10, -5), output="table")
This problem is unsolvable as
f(a) =  2.0
f(b) =  0.9026250891453738
They are both  positive.
Out[71]:
False

c. x² − 4x + 4 − lnx = 0 for 1 ≤ x ≤ 2 and 2 ≤ x ≤ 4

In [72]:
import math

def f(x):
    return pow(x, 2) - 4*x + 4 - math.log(x)
In [73]:
Bisection(1, 2, f, tolerance=pow(10, -5), output="table")
Out[73]:
a f(a) b f(b) p f(p)
1 1.000000 1.000000 2.000000 -0.693147 1.500000 -1.554651e-01
2 1.000000 1.000000 1.500000 -0.155465 1.250000 3.393564e-01
3 1.250000 0.339356 1.500000 -0.155465 1.375000 7.217127e-02
4 1.375000 0.072171 1.500000 -0.155465 1.437500 -4.649924e-02
5 1.375000 0.072171 1.437500 -0.046499 1.406250 1.161248e-02
6 1.406250 0.011612 1.437500 -0.046499 1.421875 -1.774791e-02
7 1.406250 0.011612 1.421875 -0.017748 1.414062 -3.144013e-03
8 1.406250 0.011612 1.414062 -0.003144 1.410156 4.215136e-03
9 1.410156 0.004215 1.414062 -0.003144 1.412109 5.307898e-04
10 1.412109 0.000531 1.414062 -0.003144 1.413086 -1.307805e-03
11 1.412109 0.000531 1.413086 -0.001308 1.412598 -3.888068e-04
12 1.412109 0.000531 1.412598 -0.000389 1.412354 7.091698e-05
13 1.412354 0.000071 1.412598 -0.000389 1.412476 -1.589645e-04
14 1.412354 0.000071 1.412476 -0.000159 1.412415 -4.402746e-05
15 1.412354 0.000071 1.412415 -0.000044 1.412384 1.344454e-05
16 1.412384 0.000013 1.412415 -0.000044 1.412399 -1.529176e-05
17 1.412384 0.000013 1.412399 -0.000015 1.412392 -9.227408e-07
In [74]:
print("The answer is : " , Bisection(1, 2, f, tolerance=pow(10, -5), output="answer"))
The answer is :  1.412391662
In [75]:
Bisection(2, 4, f, tolerance=pow(10, -5), output="table")
Out[75]:
a f(a) b f(b) p f(p)
1 2.000000 -0.693147 4.000000 2.613706 3.000000 -0.098612
2 3.000000 -0.098612 4.000000 2.613706 3.500000 0.997237
3 3.000000 -0.098612 3.500000 0.997237 3.250000 0.383845
4 3.000000 -0.098612 3.250000 0.383845 3.125000 0.126191
5 3.000000 -0.098612 3.125000 0.126191 3.062500 0.009675
6 3.000000 -0.098612 3.062500 0.009675 3.031250 -0.045499
7 3.031250 -0.045499 3.062500 0.009675 3.046875 -0.018169
8 3.046875 -0.018169 3.062500 0.009675 3.054688 -0.004312
9 3.054688 -0.004312 3.062500 0.009675 3.058594 0.002665
10 3.054688 -0.004312 3.058594 0.002665 3.056641 -0.000827
11 3.056641 -0.000827 3.058594 0.002665 3.057617 0.000918
12 3.056641 -0.000827 3.057617 0.000918 3.057129 0.000045
13 3.056641 -0.000827 3.057129 0.000045 3.056885 -0.000391
14 3.056885 -0.000391 3.057129 0.000045 3.057007 -0.000173
15 3.057007 -0.000173 3.057129 0.000045 3.057068 -0.000064
16 3.057068 -0.000064 3.057129 0.000045 3.057098 -0.000009
17 3.057098 -0.000009 3.057129 0.000045 3.057114 0.000018
18 3.057098 -0.000009 3.057114 0.000018 3.057106 0.000004
In [76]:
print("The answer is : " , Bisection(2, 4, f, tolerance=pow(10, -5), output="answer"))
The answer is :  3.057106019

d. x + 1 − 2sinπx = 0 for 0 ≤ x ≤ 0.5 and 0.5 ≤ x ≤ 1

In [77]:
import math

def f(x):
    return x + 1 - 2*math.sin(math.pi * x)
In [78]:
Bisection(0, 0.5, f, tolerance=pow(10, -5), output="table")
Out[78]:
a f(a) b f(b) p f(p)
1 0.000000 1.000000 0.500000 -0.500000 0.250000 -0.164214
2 0.000000 1.000000 0.250000 -0.164214 0.125000 0.359633
3 0.125000 0.359633 0.250000 -0.164214 0.187500 0.076360
4 0.187500 0.076360 0.250000 -0.164214 0.218750 -0.050037
5 0.187500 0.076360 0.218750 -0.050037 0.203125 0.011726
6 0.203125 0.011726 0.218750 -0.050037 0.210938 -0.019526
7 0.203125 0.011726 0.210938 -0.019526 0.207031 -0.003991
8 0.203125 0.011726 0.207031 -0.003991 0.205078 0.003845
9 0.205078 0.003845 0.207031 -0.003991 0.206055 -0.000079
10 0.205078 0.003845 0.206055 -0.000079 0.205566 0.001882
11 0.205566 0.001882 0.206055 -0.000079 0.205811 0.000901
12 0.205811 0.000901 0.206055 -0.000079 0.205933 0.000411
13 0.205933 0.000411 0.206055 -0.000079 0.205994 0.000166
14 0.205994 0.000166 0.206055 -0.000079 0.206024 0.000044
15 0.206024 0.000044 0.206055 -0.000079 0.206039 -0.000017
16 0.206024 0.000044 0.206039 -0.000017 0.206032 0.000013
In [79]:
print("The answer is : " , Bisection(0, 0.5, f, tolerance=pow(10, -5), output="answer"))
The answer is :  0.2060318
In [80]:
Bisection(0.5, 1, f, tolerance=pow(10, -5), output="table")
Out[80]:
a f(a) b f(b) p f(p)
1 0.500000 -0.500000 1.000000 2.000000 0.750000 0.335786
2 0.500000 -0.500000 0.750000 0.335786 0.625000 -0.222759
3 0.625000 -0.222759 0.750000 0.335786 0.687500 0.024561
4 0.625000 -0.222759 0.687500 0.024561 0.656250 -0.107593
5 0.656250 -0.107593 0.687500 0.024561 0.671875 -0.043582
6 0.671875 -0.043582 0.687500 0.024561 0.679688 -0.010020
7 0.679688 -0.010020 0.687500 0.024561 0.683594 0.007144
8 0.679688 -0.010020 0.683594 0.007144 0.681641 -0.001469
9 0.681641 -0.001469 0.683594 0.007144 0.682617 0.002830
10 0.681641 -0.001469 0.682617 0.002830 0.682129 0.000678
11 0.681641 -0.001469 0.682129 0.000678 0.681885 -0.000396
12 0.681885 -0.000396 0.682129 0.000678 0.682007 0.000141
13 0.681885 -0.000396 0.682007 0.000141 0.681946 -0.000128
14 0.681946 -0.000128 0.682007 0.000141 0.681976 0.000007
15 0.681946 -0.000128 0.681976 0.000007 0.681961 -0.000060
16 0.681961 -0.000060 0.681976 0.000007 0.681969 -0.000027
In [81]:
print("The answer is : " , Bisection(0.5, 1, f, tolerance=pow(10, -5), output="answer"))
The answer is :  0.68196869
In [ ]: