from sympy import diff, symbols, lambdify


def newton1D(f, x_0, df=None, delta=0.00001):
    """
    Find solution to f(x) = 0 with newton's method
    :param f: function f
    :param x_0: starting point for x
    :param df: first order derivative of f
    :param delta: threshold for solution
    :return: x
    """
    x_n = x_0
    if df is None:
        x = symbols('x')
        df = lambdify(x, diff(f(x), x))
    while True:
        x_n1 = x_n - f(x_n) / df(x_n)
        if abs(x_n - x_n1) < delta:
            return x_n1
        x_n = x_n1