Exemplo n.º 1
0
def test_composite_quad_degree(v):
    """
    Проверяем сходимость СКФ при наличии неравных весов
    Q: скорость сходимости оказывается дробной, почему?
    """
    from .variants import params
    a, b, alpha, beta, f = params(v)
    x0, x1 = a, b
    # a, b = -10, 10

    L = 2
    n_intervals = [L**q for q in range(2, 11)]
    n_nodes = 3

    exact = sp_quad(lambda x: f(x) / (x - a)**alpha / (b - x)**beta, x0, x1)[0]

    Y = [
        composite_quad(f,
                       x0,
                       x1,
                       n_intervals=n,
                       n_nodes=n_nodes,
                       a=a,
                       b=b,
                       alpha=alpha,
                       beta=beta) for n in n_intervals
    ]
    accuracy = get_accuracy(Y, exact * np.ones_like(Y))

    x = np.log10(n_intervals)
    aitken_degree = aitken(*Y[5:8], L)
    a1, a0 = np.polyfit(x, accuracy, 1)
    assert a1 > 1, 'composite quad did not converge!'

    fig, (ax1, ax2) = plt.subplots(1, 2)

    # график весовой функции
    xs = np.linspace(x0, x1, n_intervals[-1] + 1)
    ys = 1 / ((xs - a)**alpha * (b - xs)**beta)

    ax1.plot(xs, ys, label='weights')
    ax = list(ax1.axis())
    ax[2] = 0.
    ax1.axis(ax)
    ax1.set_xlabel('x')
    ax1.set_ylabel('p(x)')
    ax1.legend()

    # график точности
    ax2.plot(x, accuracy, 'kh')
    ax2.plot(x, a1 * x + a0, 'b:', label=f'{a1:.2f}*x+{a0:.2f}')
    ax2.set_xlabel('log10(n_intervals)')
    ax2.set_ylabel('accuracy')
    ax2.legend()

    fig.suptitle(f'variant #{v} (alpha={alpha:4.2f}, beta={beta:4.2f})\n'
                 f'aitken estimation: {aitken_degree:.2f}')
    fig.tight_layout()

    plt.show()
Exemplo n.º 2
0
def test_composite_quad_degree(v):
    """
    Q: convergence maybe somewhat between 3 and 4, why?
    """
    from variants import params

    plt.figure()
    a, b, alpha, beta, f = params(v)
    x0, x1 = a, b
    # a, b = -10, 10
    exact = sp_quad(lambda x: f(x) / (x - a)**alpha / (b - x)**beta, x0, x1)[0]

    # plot weights
    xs = np.linspace(x0, x1, 101)[1:-1]
    ys = 1 / ((xs - a)**alpha * (b - xs)**beta)

    #my addition
    # for x in xs:
    #     if (x-a)**alpha * (b-x)**beta:
    #         print("HERE")
    # print(ys)
    #

    plt.subplot(1, 2, 1)
    plt.plot(xs, ys, label='weights')
    ax = list(plt.axis())
    ax[2] = 0.
    plt.axis(ax)
    plt.xlabel('x')
    plt.ylabel('p(x)')
    plt.legend()

    L = 2
    n_intervals = [L**q for q in range(2, 10)]
    n_nodes = 3
    Y = [
        composite_quad(f,
                       x0,
                       x1,
                       n_intervals=n,
                       n_nodes=n_nodes,
                       a=a,
                       b=b,
                       alpha=alpha,
                       beta=beta) for n in n_intervals
    ]
    accuracy = get_log_error(Y, exact * np.ones_like(Y))
    x = np.log10(n_intervals)
    aitken_degree = aitken(*Y[5:8], L)

    # plot acc
    plt.subplot(1, 2, 2)
    plt.plot(x, accuracy, 'kh')
    plt.xlabel('log10(node count)')
    plt.ylabel('accuracy')
    plt.suptitle(f'variant #{v} (alpha={alpha:4.2f}, beta={beta:4.2f})\n'
                 f'aitken estimation: {aitken_degree:.2f}')
    plt.show()
Exemplo n.º 3
0
def test_composite_quad_degree(v):
    """
    Q: convergence maybe somewhat between 3 and 4, why?
    """
    from .variants import params
    fig, (ax1, ax2) = plt.subplots(1, 2)

    a, b, alpha, beta, f = params(v)
    x0, x1 = a, b
    # a, b = -10, 10
    exact = sp_quad(lambda x: f(x) / (x - a)**alpha / (b - x)**beta, x0, x1)[0]

    # plot weights
    xs = np.linspace(x0, x1, 101)
    ys = 1 / ((xs - a)**alpha * (b - xs)**beta)

    ax1.plot(xs, ys, label='weights')
    ax = list(ax1.axis())
    ax[2] = 0.
    ax1.axis(ax)
    ax1.set_xlabel('x')
    ax1.set_ylabel('p(x)')
    ax1.legend()

    L = 2
    n_intervals = [L**q for q in range(2, 10)]
    n_nodes = 4
    Y = [
        composite_quad(f,
                       x0,
                       x1,
                       n_intervals=n,
                       n_nodes=n_nodes,
                       a=a,
                       b=b,
                       alpha=alpha,
                       beta=beta) for n in n_intervals
    ]
    accuracy = get_log_error(Y, exact * np.ones_like(Y))
    x = np.log10(n_intervals)
    k, b = np.polyfit(x, accuracy, 1)
    assert k > 1, 'composite quad did not converge!'
    aitken_degree = aitken(*Y[5:8], L)

    # plot acc
    ax2.plot(x, accuracy, 'kh')
    ax2.plot(x, k * x + b, 'b:', label=f'{k:.2f}*x+{b:.2f}')
    ax2.set_xlabel('log10(n_intervals)')
    ax2.set_ylabel('accuracy')
    ax2.legend()
    fig.suptitle(f'variant #{v} (alpha={alpha:4.2f}, beta={beta:4.2f})\n'
                 f'aitken estimation: {aitken_degree:.2f}')
    plt.show()