import numpy as np
import matplotlib.pyplot as plt
from cycler import cycler


def fig2_10():
    a = -1
    b = 8
    nx = 100
    x = np.linspace(a, b, nx)

    x0 = 2
    m = (1 - x0**2) / (1 + x0**2) ** 2
    x1 = x0 - f(x0) / m
    print(x1)
    y = np.zeros(nx)
    yy = np.zeros(nx)
    for ix in range(nx):
        y[ix] = f(x[ix])
        yy[ix] = f(x0) + m * (x[ix] - x0)

    plt.close("all")
    # get(gcf)
    fig, ax = plt.subplots(
        figsize=(658 / 100, 230 / 100), dpi=100, constrained_layout=True
    )
    co = [
        (0, 0, 1),
        (0, 0.5, 0),
        (1, 0, 0),
        (0, 0.75, 0.75),
        (0.75, 0, 0.75),
        (0.75, 0.75, 0),
        (0.25, 0.25, 0.25),
    ]
    plt.rcParams["axes.prop_cycle"] = cycler(color=co)

    ax.plot(x, y, linewidth=1.6)
    ax.plot(x, yy, "r", linewidth=1.6)
    ax.plot([a, b], [0, 0], "k", linewidth=0.8)

    ax.plot([x0], [0], "|k", linewidth=2, markersize=12)
    ax.text(1.9, -0.13, r"$x_0$", fontsize=18)

    ax.plot([x1], [0], "+k", linewidth=1.4, markersize=12)
    xx1 = 0.97 * x1
    ax.text(xx1, -0.13, r"$x_1$", fontsize=18)

    ax.axis([a, b, -0.5, 0.6])

    ax.set_xlabel("x-axis")
    ax.set_ylabel("y-axis")
    ax.grid(True)
    for spine in ax.spines.values():
        spine.set_visible(True)
    ax.tick_params(labelsize=14)
    for label in ax.get_xticklabels() + ax.get_yticklabels():
        label.set_fontweight("bold")
    ax.xaxis.label.set_fontsize(14)
    ax.yaxis.label.set_fontsize(14)
    ax.xaxis.label.set_fontweight("bold")
    ax.yaxis.label.set_fontweight("bold")

    # fig.savefig('/Users/mark/Desktop/fail.eps')
    plt.show()


def f(x):
    # g=2*x*exp(-x^2)+0.5;
    g = x / (1 + x**2)
    return g


if __name__ == "__main__":
    fig2_10()
