<jupyter_start><jupyter_text>[View in Colaboratory](https://colab.research.google.com/github/nishi1612/SC374-Computational-and-Numerical-Methods/blob/master/Set_3.ipynb) Set 3--- **Finding roots of polynomial by bisection method**<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import math
from google.colab import files
def iterations(n, arr , i):
  plt.plot(range(n),arr)
  plt.xlabel('No. of iterations')
  plt.ylabel('Value of c')
  plt.grid(True)
  plt.savefig("Iterations" + str(i) + ".png")
  files.download("Iterations" + str(i) + ".png")
  plt.show()
def graph(i):
  plt.xlabel('x')
  plt.ylabel('y')
  plt.grid(True)
  plt.legend(loc='upper right')
  plt.savefig("Graph" + str(i) + ".png")
  files.download("Graph" + str(i) + ".png")
  plt.show()
def bissection( a,b,epsilon,k):
    table = pd.DataFrame(columns=['a','b','c','b-c','f(a)*f(c)','Assign'])
    c = (a+b)/2;
    dist = b-c;
    i = 0
    arr = []
    while(dist>epsilon):
        ans_a = func(a,k);
        ans_b = func(b,k);
        ans_c = func(c,k);
        ans = ""
        if(ans_a*ans_c < 0):
            b=c;
            ans = "b=c"
        else:
            a=c;
            ans = "a=c";
        table.loc[i] = [a,b,c,dist,ans_a*ans_c,ans]
        arr.append(c)
        i = i+1
        c = (a+b) / 2
        dist = b-c 
    return (a+b)/2 ,i , arr , table;
def func(x,k):
    if k==1:
        return x**6 - x - 1;
    elif k==2:
        return x**3 - x**2 - x - 1;
    elif k==3:
        return x - 1 - 0.3*math.cos(x);
    elif k==4:
        return 0.5 + math.sin(x) - math.cos(x);
    elif k==5:
        return x - math.e**(-x);
    elif k==6:
        return math.e**(-x) - math.sin(x);
    elif k==7:
        return x**3 - 2*x - 2;
    elif k==8:
        return x**4 - x - 1;
    elif k==9:
      return math.e**(x) - x - 2;
    elif k==10:
      return 1- x + math.sin(x);
    elif k==11:
      return x - math.tan(x);
x = np.arange(-2,3,0.001)
plt.plot(x,x**6,label='$x^6$')
plt.plot(x,x+1,label="x+1")
graph(1)
plt.plot(x**6-x-1,label='$x^6$ - x - 1')
graph(1)
a , n , arr , table = bissection(1,2,0.001,1)
iterations(n,arr,1)
print(str(a) + "\n" + str(func(a,1)))
table
b , n , arr , table = bissection(-1,0,0.001,1)
iterations(n,arr,1)
print(str(b) + "\n" + str(func(b,1)))
table
x = np.arange(-2,3,0.001)
plt.plot(x,x**3,label='$x^3$')
plt.plot(x,x**2 + x + 1,label='$x^2 + x + 1$')
graph(2)
plt.plot(x**3 - (x**2 + x + 1),label='$x^3 - x^2 - x - 1$')
graph(2)
a , n , arr, table = bissection(1,2,0.0001,2)
iterations(n,arr,2)
print(str(a) + "\n" + str(func(a,2)))
table

x = np.arange(-3,5,0.001)
plt.plot(x,x-1,label='$x-1$')
plt.plot(x,0.3*np.cos(x),label='$0.3cos(x)$')
graph(3)
plt.plot(x,x-1-0.3*np.cos(x) , label='$x - 1 - 0.3cos(x)$')
graph(3)
a , n , arr , table = bissection(0,2,0.0001,3)
iterations(n,arr,3)
print(str(a) + "\n" + str(func(a,3)))
table
x = np.arange(-10,10,0.001)
plt.plot(x,0.5 + np.sin(x),label='$0.5 + sin(x)$')
plt.plot(x,np.cos(x),label='$cos(x)$')
graph(4)
plt.plot(x,0.5 + np.sin(x) - np.cos(x),label='$0.5 + sin(x) - cos(x)$')
graph(4)
a , n , arr , table = bissection(0,2,0.0001,4)
iterations(n,arr,4)
print(str(a) + "\n" + str(func(a,4)))
table
x = np.arange(-0,5,0.001)
plt.plot(x,x,label='$x$')
plt.plot(x,np.e**(-x),label='$e^{-x}$')
graph(5)
plt.plot(x,x - np.e**(-x),label='$x - e^{-x}$')
graph(5)
a , n , arr , table = bissection(0,1,0.0001,5)
iterations(n,arr,5)
print(str(a) + "\n" + str(func(a,5)))
table
x = np.arange(0,5,0.001)
plt.plot(x,np.sin(x),label='$sin(x)$')
plt.plot(x,np.e**(-x),label='$e^{-x}$')
graph(6)
plt.plot(x,np.sin(x) - np.e**(-x),label='$sin(x) - e^{-x}$')
graph(6)
a , n , arr , table = bissection(0,1,0.0001,6)
iterations(n,arr,6)
print(str(a) + "\n" + str(func(a,6)))
table
a , n , arr , table = bissection(3,4,0.0001,6)
iterations(n,arr,6)
print(str(a) + "\n" + str(func(a,6)))
table
x = np.arange(-2,4,0.001)
plt.plot(x,x**3,label='$x^3$')
plt.plot(x,2*x+2,label='$2x + 2$')
graph(7)
plt.plot(x,x**3 - 2*x - 2,label='$x^3 - 2x - 2$')
graph(7)
a , n , arr , table = bissection(1,2,0.0001,7)
iterations(n,arr,7)
print(str(a) + "\n" + str(func(a,7)))
table
x = np.arange(-2,4,0.001)
plt.plot(x,x**4,label='$x^4$')
plt.plot(x,x+1,label='$x+1$')
graph(8)
plt.plot(x,x**4 - x - 1,label='$x^4 - x - 1$')
graph(8)
a , n , arr , table = bissection(-1,0,0.0001,8)
iterations(n,arr,8)
print(str(a) + "\n" + str(func(a,8)))
table
a , n , arr , table = bissection(1,2,0.0001,8)
iterations(n,arr,8)
print(str(a) + "\n" + str(func(a,8)))
table
x = np.arange(-5,4,0.001)
plt.plot(x,np.e**(x),label='$e^x$')
plt.plot(x,x+2,label='$x+2$')
graph(9)
plt.plot(x,np.e**(x) - x - 2,label='$e^2 - x - 2$')
graph(9)
a , n , arr , table = bissection(1,2,0.0001,9)
iterations(n,arr,9) 
print(str(a) + "\n" + str(func(a,9)))
table
x = np.arange(-5,4,0.001)
plt.plot(x,-np.sin(x),label='$-sin(x)$')
plt.plot(x,1-x,label='$1 - x$')
graph(10)
plt.plot(x,-np.sin(x) - 1 + x,label='$-sin(x) - 1 + x$')
graph(10)
a , n , arr , table = bissection(0,2,0.0001,10)
iterations(n,arr,10)
print(str(a) + "\n" + str(func(a,10)))
table
x = np.arange(-10,10,.001)
plt.plot(np.tan(x),label='$tan(x)$')
plt.plot(x,label='$x$')
graph(11)
plt.plot(np.tan(x) - x,label='$x - tan(x)$')
graph(11)
a , n , arr , table = bissection(4,5,0.0001,11)
iterations(n,arr,11)
print(str(a) + "\n" + str(func(a,11)))
table
a , n , arr , table = bissection(80,120,0.0001,11)
iterations(n,arr,11) 
print(str(a) + "\n" + str(func(a,11)))
table<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.26765934265480296, 'fraction_numerical': 0.06700562919920101, 'mean_word_length': 3.54080791426216, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '12368', 'license': 'MIT', 'n_tokens_mistral': 2851, 'n_tokens_neox': 2677, 'n_words': 444, 'path': 'Smit/Sem V/CS/Set_3.ipynb', 'repo_name': 'Vishu26/Academics_Projects', 'size': '5507'}