Using Brent's method to find the root of a polynomial

import matplotlib.pyplot as plt import scipy.optimize as opt from inspect import getsource import numpy as np def f(x): return x**2 - 600*x + 9000 # Instead of having eval of a str within the function f_str = getsource(f).strip().split('return ')[1] start, end = -100, 100 root = opt.brentq(f, start, end) x = np.linspace(start, end, 100) y = f(x) plt.title('Using Brent\'s method to find the root of the polynomial\n{0} = 0\nx = {1}'.format(f_str, root)) plt.plot(x, y, color='lightblue', lw=3.4) plt.axhline(0, color='gray', lw=2) plt.scatter(root, [0], c='k', s=40) plt.xlim(start, end) plt.tight_layout() plt.show() Using Brent's method to find the root of a polynomial'