import math


def table2_5():
    #  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

    xe = -(5 + 2 * math.sqrt(6)) ** (1 / 3) / 2 - 1 / (
        2 * (5 + 2 * math.sqrt(6)) ** (1 / 3)
    ) + 1 / 2

    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  \t\t  Rel Error")
    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}  \t  {abs(1 - x / xe):5.1e}")
        # print(f'\\, {k:d} \\, & \\,\\, {x:19.15f} \\,\\, & \\,\\, {err:5.1e} & \\,\\, {abs(1-x/xe):5.1e} \\\\\\hline')
    print()


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


def df(x):
    g = 6 * x**2 - 6 * x
    return g


if __name__ == "__main__":
    table2_5()
