import numpy as np
import matplotlib.pyplot as plt


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

    x0 = np.pi / 4
    dx = 2 * np.pi

    #  loop to generate function values
    nx = 1200
    ex = np.linspace(0, 16, nx)
    n = np.empty(nx)
    x = np.empty(nx)
    y = np.empty(nx)
    for i in range(nx):
        n[i] = round(10**ex[i])
        x[i] = x0 + n[i] * dx
        y[i] = abs(1 - np.sin(x[i]) / np.sin(x0))

    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.loglog(n, y, '-r', linewidth=0.9)
    ax.grid(True)
    ax.set_frame_on(True)
    ax.axis([1, 1e15, 1e-16, 1])
    ax.set_yticks([1e-16, 1e-12, 1e-8, 1e-4, 1])
    ax.set_xticks([1, 1e5, 1e10, 1e15])
    ax.set_xlabel('n-axis', fontsize=14, fontweight='bold')
    ax.set_ylabel('Relative Error', 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/sink.eps')

    plt.show()


if __name__ == '__main__':
    fig1_3()
