CoCalc Public Files2 / 3 / 05 / 21.ipynbOpen with one click!
Author: Dylan Cortes
Views : 21
Compute Environment: Ubuntu 20.04 (Default)
In [1]:
import numpy as np import matplotlib.pyplot as plt
In [2]:
def bisection(a,b,f,err): c = (a+b)/2 while c-a > err: if f(c) == 0: return c elif f(a)*f(c) > 0: a = c else: b = c c = (a+b)/2 return c
In [3]:
x = np.linspace(.5,8,1000) y = np.log(x) - np.sin(x) #Don't think we covered this in class about how to write In(x) and sin(x) plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x7ff9d87aa160>]
In [4]:
x = np.linspace(2,4,1000) y = np.log(x) - np.sin(x) plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x7ff9d071bb80>]
In [5]:
def f(x): return np.log(x) - np.sin(x)
In [6]:
bisection(2,4,f,0.001)
2.2197265625

Conclusion

The x intercept for the function above is roughly 2.2197265625