In [ ]:
from numpy import log, power, sin, arange
from random import randint

def bisec(f, a, b, p=0.001, step=None):
    """
    Bisection method
    """
    while abs(a - b) > p:
        c = (a + b) / 2
        
        if f(c) > 0:
            b = c
        elif f(c) < 0:
            a = c
        
        if step is not None:
            step.append((a,b))
    
    return c

In [ ]:
def f1(x):
    return sin(x) + x - 1

def f2(x):
    return x ** 3 - 8 * x ** 2 + 6 * x

def f3(x):
    return log(x) - x ** 2 + 3

def f4(x):
    return sin(x) + 0.1 * x - 1

In [ ]:
# Charts

In [ ]:
from matplotlib import pyplot as plt

x = arange(0, 1, 0.001)

plt.figure(figsize=(10,9))
plt.grid(color='k', linestyle='--')
plt.xlabel('x')
plt.ylabel('y = sin(x) + x - 1')

plt.xlim((0, 1))

plt.rcParams["font.size"] = 24

plt.plot(x, f1(x), color='k')

step = []
print(bisec(f1, 0, 1, step=step))
for ab in step:
    plt.axvline(x=ab[0], color='red')
    plt.axvline(x=ab[1], color='blue')

plt.savefig("1.pdf", bbox_inches='tight')
plt.show()

In [ ]:
from matplotlib import pyplot as plt

xmin = -2.5
xmax = 8

x = arange(xmin, xmax, 0.001)

plt.figure(figsize=(10,9))
plt.grid(color='k', linestyle='--')
plt.xlabel('x')
plt.ylabel('y = x ** 3 - 8 * x ** 2 + 6 * x')

plt.xlim((xmin, xmax))

plt.rcParams["font.size"] = 24

plt.plot(x, f2(x), color='k')

step = []
print(bisec(f2, -2, 1, step=step))
for ab in step:
    plt.axvline(x=ab[0], color='red')
    plt.axvline(x=ab[1], color='blue')

plt.savefig("2.pdf", bbox_inches='tight')
plt.show()

In [ ]:
from matplotlib import pyplot as plt

xmin = -2.5
xmax = 8

x = arange(xmin, xmax, 0.001)

plt.figure(figsize=(10,9))
plt.grid(color='k', linestyle='--')
plt.xlabel('x')
plt.ylabel('y = x ** 3 - 8 * x ** 2 + 6 * x')

plt.xlim((xmin, xmax))

plt.rcParams["font.size"] = 24

plt.plot(x, f2(x), color='k')

step = []
print(bisec(f2, 6, 8, step=step))
for ab in step:
    plt.axvline(x=ab[0], color='red')
    plt.axvline(x=ab[1], color='blue')

plt.savefig("3.pdf", bbox_inches='tight')
plt.show()

In [ ]:
from matplotlib import pyplot as plt

xmin = -2.5
xmax = 8

x = arange(xmin, xmax, 0.001)

plt.figure(figsize=(10,9))
plt.grid(color='k', linestyle='--')
plt.xlabel('x')
plt.ylabel('y = x ** 3 - 8 * x ** 2 + 6 * x')

plt.xlim((xmin, xmax))

plt.rcParams["font.size"] = 24

plt.plot(x, f2(x), color='k')

step = []
print(bisec(f2, 2, 0.1, step=step))
for ab in step:
    plt.axvline(x=ab[0], color='red')
    plt.axvline(x=ab[1], color='blue')

plt.savefig("4.pdf", bbox_inches='tight')
plt.show()

In [ ]:
from matplotlib import pyplot as plt

xmin = -1
xmax = 3

x = arange(xmin, xmax, 0.0001)

plt.figure(figsize=(10,9))
plt.grid(color='k', linestyle='--')
plt.xlabel('x')
plt.ylabel('y = log(x) - x ** 2 + 3')

plt.xlim((xmin, xmax))
plt.ylim((-2, 3))

plt.rcParams["font.size"] = 24

plt.plot(x, f3(x), color='k')

step = []
print(bisec(f3, -0.5, 0.5, p=0.00001, step=step))
for ab in step:
    plt.axvline(x=ab[0], color='red')
    plt.axvline(x=ab[1], color='blue')

plt.savefig("5.pdf", bbox_inches='tight')
plt.show()

In [ ]:
from matplotlib import pyplot as plt

xmin = -1
xmax = 3

x = arange(xmin, xmax, 0.0001)

plt.figure(figsize=(10,9))
plt.grid(color='k', linestyle='--')
plt.xlabel('x')
plt.ylabel('y = log(x) - x ** 2 + 3')

plt.xlim((xmin, xmax))
plt.ylim((-2, 3))

plt.rcParams["font.size"] = 24

plt.plot(x, f3(x), color='k')

step = []
print(bisec(f3, 2.5, 1.5, p=0.0001, step=step))
for ab in step:
    plt.axvline(x=ab[0], color='red')
    plt.axvline(x=ab[1], color='blue')

plt.savefig("6.pdf", bbox_inches='tight')
plt.show()

In [ ]:
from matplotlib import pyplot as plt

xmin = -5
xmax = 25

x = arange(xmin, xmax, 0.0001)

plt.figure(figsize=(10,9))
plt.grid(color='k', linestyle='--')
plt.xlabel('x')
plt.ylabel('y = sin(x) + 0.1 * x - 1')

plt.xlim((xmin, xmax))
#plt.ylim((-2, 3))

plt.rcParams["font.size"] = 24

plt.plot(x, f4(x), color='k')

# 1
step = []
print(bisec(f4, 0, 1.5, p=0.00001, step=step))
for ab in step:
    plt.axvline(x=ab[0], color='red')
    plt.axvline(x=ab[1], color='blue')

# 2
step = []
print(bisec(f4, 2.5, 1.5, p=0.00001, step=step))
for ab in step:
    plt.axvline(x=ab[0], color='red')
    plt.axvline(x=ab[1], color='blue')

# 3
step = []
print(bisec(f4, 6, 7, p=0.00001, step=step))
for ab in step:
    plt.axvline(x=ab[0], color='red')
    plt.axvline(x=ab[1], color='blue')

# 4
step = []
print(bisec(f4, 10, 9, p=0.00001, step=step))
for ab in step:
    plt.axvline(x=ab[0], color='red')
    plt.axvline(x=ab[1], color='blue')

# 5
step = []
print(bisec(f4, 12, 13, p=0.00001, step=step))
for ab in step:
    plt.axvline(x=ab[0], color='red')
    plt.axvline(x=ab[1], color='blue')

# 6
step = []
print(bisec(f4, 17, 16, p=0.00001, step=step))
for ab in step:
    plt.axvline(x=ab[0], color='red')
    plt.axvline(x=ab[1], color='blue')

# 7
step = []
print(bisec(f4, 17, 18, p=0.00001, step=step))
for ab in step:
    plt.axvline(x=ab[0], color='red')
    plt.axvline(x=ab[1], color='blue')

plt.savefig("7.pdf", bbox_inches='tight')
plt.show()