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: 23
Image: ubuntu2004
Kernel: Python 3 (system-wide)
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

We use the bisection method which has already been written.

import numpy as np import matplotlib.pyplot as plt

We import these items so that we can produce a graph.

def formula(x): return np.log(x) - np.sin(x)
x = np.linspace(2,4,1000) y = np.log(x) - np.sin(x) plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x7f2002d929d0>]
Image in a Jupyter notebook

We see in the graph that 2.25 is the closest to zero so we put 2.25 into the bisection formula to see how close it is.

bisection(2,2.25,formula,0.001)
2.2197265625
formula(2.2197265625)
0.0006532685362758972

This is basically pretty much zero so this works out.