| Hosted by CoCalc | Download
Kernel: Python 3 (system-wide)
import numpy as np import matplotlib.pyplot as plt
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
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>]
Image in a Jupyter notebook
x = np.linspace(2,4,1000) y = np.log(x) - np.sin(x) plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x7ff9d071bb80>]
Image in a Jupyter notebook
def f(x): return np.log(x) - np.sin(x)
bisection(2,4,f,0.001)
2.2197265625

Conclusion

The x intercept for the function above is roughly 2.2197265625