<jupyter_start><jupyter_text>Finding the root of a mathematical function<jupyter_code>import numpy as np
import scipy as sp
import scipy.optimize as opt
import matplotlib.pyplot as plt
%matplotlib inline
def f(x):
    return np.cos(x) - x
x = np.linspace(-5, 5, 1000)
y = f(x)
fig, ax = plt.subplots(1, 1, figsize=(5, 3))
ax.axhline(0, color='k')
ax.plot(x, y)
ax.set_xlim(-5, 5)
opt.bisect(f, -5, 5)
fig, ax = plt.subplots(1, 1, figsize=(5, 3))
ax.axhline(0, color='k')
ax.plot(x, y)
# The zorder argument is used to put
# the dot on top of the other elements.
ax.scatter([_], [0], c='r', s=100,
           zorder=10)
ax.set_xlim(-5, 5)
opt.brentq(f, -5, 5)
%timeit opt.bisect(f, -5, 5)
%timeit opt.brentq(f, -5, 5)<jupyter_output>34.5 µs ± 855 ns per loop (mean ± std. dev. of 7 runs,
    10000 loops each)
7.71 µs ± 170 ns per loop (mean ± std. dev. of 7 runs,
    100000 loops each)
{'fraction_non_alphanumeric': 0.1613995485327314, 'fraction_numerical': 0.06659142212189616, 'mean_word_length': 3.7180851063829787, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '334374', 'license': 'MIT', 'n_tokens_mistral': 426, 'n_tokens_neox': 387, 'n_words': 126, 'path': 'Section03/.ipynb_checkpoints/01_root-checkpoint.ipynb', 'repo_name': 'PacktPublishing/Statistical-Methods---Applied-Mathematics-in-Data-Science', 'size': '886'}