CoCalc Public FilesMakeupWork / Done / 4-5DiscussionE.ipynbOpen with one click!
Author: Kaleb Cruz
Views : 79
Compute Environment: Ubuntu 20.04 (Default)

Use the Bisection Method to find a root of the function f(x) = ln(x)-sin(x) in the interval (2,4) with an error bound of 0.001. Hint: Use a graphical method (like using Matplotlib, say) to get an idea of what values to use as the starting choices for a and b in the algorithm.

In [1]:
import numpy as np import matplotlib.pyplot as plt
In [2]:
x = np.linspace(2,4,100) y = np.log(x)-np.sin(x)
In [ ]:
In [3]:
plt.plot(x,y,label='$f(x)$') plt.legend() plt.xlabel('x') plt.ylabel('y') plt.title('Bisection Method') plt.show()
In [4]:
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 [5]:
bisection(2,2.5,p,0.001)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-5-55a8616adfcc> in <module> ----> 1 bisection(2,2.5,p,0.001) NameError: name 'p' is not defined
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: