Contact Us!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

| Download
Views: 27
Image: ubuntu2004
Kernel: Python 3 (system-wide)

In this assignment we will use Bisection Method to find a root of the function f(x)=ln(x)−sin(x)f(x) = ln(x)-sin(x) in the interval (2, 4) with an error bound of 0.001.

def bisection(a,b,f,err): #Our bisection function. 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
import numpy as np import matplotlib.pyplot as plt #import libs def p(x): return np.log(x)-np.sin(x) #We use np.log(x) from NumPy to find ln(x).
bisection(2,4,p,0.001) #Our root is at approximately ~2.2197265625
2.2197265625
p(2.2197265625)
0.0006532685362758972
x = np.linspace(1,10,1000) y = np.log(x)-np.sin(x) plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x7f4c7d2ea8e0>]
Image in a Jupyter notebook

On this plot we can assume that the roots of the function f(x)=ln(x)-sin(x) are not located on (2, 4) interval.