
def newton():
    #  Solve  f(x) = 0  using Newton's method

    #  Input:
    # x = starting point
    # tol = tolerance for stopping
    # f(x) and f'(x) are at end of file

    x = 3 / 2
    tol = 10 ** (-10)

    k = 0
    err = 10 * tol
    print("\n RIE = relative iterative error \n")
    print(" k  \t   x_k \t\t\t  RIE ")
    while err > tol:
        k = k + 1
        z = f(x) / df(x)
        x = x - f(x) / df(x)
        err = abs(z / x)
        print(f" {k:d} \t  {x:19.12e} \t  {err:5.1e}  ")
    print()


def f(x):
    g = 2 * x**3 - 3 * x**2 + 3
    # g=exp(-x)-2-x;
    return g


def df(x):
    g = 6 * x**2 - 6 * x
    # g=-exp(-x)-1;
    return g


if __name__ == "__main__":
    newton()
