import numpy as np
import matplotlib.pyplot as plt


def fig1_9():
    """Literal Python conversion of fig1_9.m."""

    nes = 10000
    ##### y=(sqrt(1+x^2)-1)/x^2
    exs = np.linspace(-10, -6, nes)

    xr = np.empty(nes)
    fr = np.empty(nes)
    xl = np.empty(nes)
    fl = np.empty(nes)
    for i in range(nes):
        xr[i] = 10**exs[i]
        fr[i] = f(xr[i])
        xl[i] = -10**exs[i]
        fl[i] = f(xl[i])

    plt.close('all')
    # get(gcf)
    # MATLAB Position [25 1115 658 230]: preserve the 658 x 230 pixel size.
    fig, ax = plt.subplots(figsize=(6.58, 2.30), dpi=100, layout='constrained')
    co = np.array([
        [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'] = plt.cycler(color=co)

    ax.plot(xr, fr, 'b', linewidth=1.2)
    ax.plot(xl, fl, 'b', linewidth=1.2)
    ax.grid(True)
    ax.set_frame_on(True)
    ax.axis([-3e-7, 3e-7, -0.05, 0.8])
    ax.set_xlabel('x-axis', fontsize=14, fontweight='bold')
    ax.set_ylabel('y-axis', fontsize=14, fontweight='bold')
    ax.tick_params(labelsize=14)
    for label in ax.get_xticklabels() + ax.get_yticklabels():
        label.set_fontweight('bold')

    # exportgraphics(gcf,'/Users/mark/Desktop/ex1.eps')

    plt.show()


def f(x):
    y = (np.sqrt(1 + x**2) - 1) / x**2
    return y


if __name__ == '__main__':
    fig1_9()
