x_s = shift_chebyshev(0, 1, chebyshev_n(N_s))
    N_q = 900
    x_q, w_q = legendre_gauss(0, 1, N_q)


    #---------------------------------------
    d= 0.025
    A = fredholm_lhs(d, x_c, x_s, x_q, w_q, f)
    b = F_analytic(x_c, d)
    b_tilde = disturb_rhs(b)

    rho_hat = solve(A, b)
    rho_hat_tilde = solve(A, b_tilde)

    fig1, ax1 = init_plot('Question 6-1 - d=0,025')
    ax1 = set_axis_specs(ax1, r'$x^s$', r'$b$', 18, 18)
    ax1 = plot_normal(ax1, x_c, b, 'r-', r'Unperturbed $b$')
    ax1 = plot_small(ax1, x_c, b_tilde, 'b--', r'Perturbed $\~{b}$')

    fig2, ax2 = init_plot('Question 6-2 - d=0,025')
    ax2 = set_axis_specs(ax2, r'$x^s$', r'$\rho$', 18, 18)
    ax2 = plot_normal(ax2, x_s, rho_hat, 'r-', r'Unperturbed $\hat{\rho}$')
    ax2 = plot_normal(ax2, x_s, rho_hat_tilde, 'b-', r'Perturbed $\hat{\rho}$')
    ax2 = plot_small(ax2, x_s, rho(x_s), 'g--', r'Analytic $\rho$')

    file = rel_path + r'/Data_files/Oppgave6_d0025'
    savez(file, b=b, b_tilde=b_tilde, rho_hat=rho_hat, rho_hat_tilde=rho_hat_tilde)

    #-----------------------------------------------------------------
    d = 0.25
    A = fredholm_lhs(d, x_c, x_s, x_q, w_q, f)
        b_tilde = disturb_rhs(b)

        for i in range(n_lambda):
            rho_lambda = solve((dot(A.transpose(), A) + lambdas[i]*eye(N_c)), dot(A.transpose(), b_tilde))
            max_error[i] = max(abs(rho_lambda-rho_analytical))
        errors_by_lambda[argmin(max_error)] += 1

    fig, ax = init_plot('Oppgave 7 -b')
    ax.scatter(lambdas, errors_by_lambda, c='r', s=30)
    ax.set_xscale('log')
    ax.set_xlim(1e-6, 1.5*1e-3)
    ax = set_axis_specs(ax, r'$\lambda$', r'# min |Error|', 23, 23)
    plt.show()

    file = rel_path + r'/Data_files/Oppgave7d025'
    savez(file, lambdas=lambdas, errors_by_lambda=errors_by_lambda)"""

    n_lambda = 100000
    lambdas = geomspace(1e-14, 1, n_lambda)
    max_error = empty(n_lambda)
    seed(int(time()))
    b_tilde = disturb_rhs(b)
    for i in range(n_lambda):
        rho_lambda = solve((dot(A.transpose(), A) + lambdas[i] * eye(N_c)),
                           dot(A.transpose(), b_tilde))
        max_error[i] = max(abs(rho_lambda - rho_analytical))
    fig, ax = init_plot('Oppgave 7 -b')
    plot_loglog(ax, lambdas, max_error, 'r-', "Error($\lambda$)")
    ax = set_axis_specs(ax, r'$\lambda$', r'Error', 30, 30)
    show_plot(fig, ax)
from numpy import load
from sys import path
from os.path import dirname
rel_path = dirname(dirname(__file__))

path.insert(0, rel_path + r'/Help_files')
from Plotting import init_plot, plot_normal, show_plot, save_fig, plot_logy_test, save_fig_legend_on_top, set_axis_specs, plot_small
path.__delitem__(0)

file = rel_path + r'/Data_files/Oppgave5.npz'
if __name__ == "__main__":

    npz_file = load(file)
    fig, ax = init_plot('Question 5 - Error')
    ax = set_axis_specs(ax, r'$N_c$', r'Max $|$Error$_j|$', 36, 36)

    N_c_list, d_0025, d_025, d_25 = npz_file['N_c_list'], npz_file[
        'max_diff_1'], npz_file['max_diff_2'], npz_file['max_diff_3']

    plot_logy_test(ax,
                   N_c_list,
                   d_0025,
                   'ro--',
                   r'Err(N$_{c}$) - d = ' + '{}'.format(0.025),
                   linewidth=4,
                   ms=10)
    plot_logy_test(ax,
                   N_c_list,
                   d_025,
                   'bo--',
                   r'Err($N_{c}$) - d = ' + '{}'.format(0.25),
import matplotlib.gridspec as grd
from os.path import dirname
rel_path = dirname(dirname(__file__))

path.insert(0, rel_path + r'/Help_files')
from Plotting import init_plot, plot_normal, show_plot, plot_loglog, save_fig, plot_logy_test, save_fig_legend_on_top, set_axis_specs, plot_small
path.__delitem__(0)

if __name__ == "__main__":
    npzfile_d025 = load(rel_path + r'/Data_files/Oppgave7d025.npz')
    npzfile_d25 = load(rel_path + r'/Data_files/Oppgave7d25.npz')

    fig, ax = init_plot('Oppgave 7 -b')
    ax.scatter(npzfile_d25['lambdas'][1:],
               npzfile_d25['errors_by_lambda'][1:],
               c='r',
               s=30)
    ax.set_xscale('log')
    ax.set_xlim(1e-11, 3 * 1e-9)
    ax = set_axis_specs(ax, r'$\lambda$', r'# min |Error|', 30, 30)
    plt.show()

    fig, ax = init_plot('Oppgave 7 -b')
    ax.scatter(npzfile_d025['lambdas'][1:],
               npzfile_d025['errors_by_lambda'][1:],
               c='r',
               s=30)
    ax.set_xscale('log')
    ax.set_xlim(1e-6, 1.5 * 1e-3)
    ax = set_axis_specs(ax, r'$\lambda$', r'# min |Error|', 30, 30)
    plt.show()
Ejemplo n.º 5
0

if __name__ == "__main__":
    d = 0.025
    n_c = 40
    n_s = 40

    x_c = shift_chebyshev(0, 1, chebyshev_n(n_c))
    x_s = shift_chebyshev(0, 1, chebyshev_n(n_s))
    w_s = get_lagrange_poly_weights(x_s, array([1 for i in range(n_s)]))
    rho_hat = array([rho(x_s[i]) for i in range(n_s)])
    F_analytic = load(open(rel_path + "/Data_files/F.pkl", "rb"))
    f_eval = F_analytic(x_c, d)

    fig, ax = init_plot('Oppgave 3')
    ax = set_axis_specs(ax, '$N_q$', 'Max Error', 24, 24)

    panel_list = arange(20, 301, 1)
    y_list_midpoint = empty(len(panel_list))
    y_list_simpson = empty(len(panel_list))
    y_list_LG = empty(len(panel_list))
    for i in range(len(panel_list)):
        x_q, w_q = midpoint(0, 1, panel_list[i])
        A = fredholm_lhs(d, x_c, x_s, w_s, x_q, w_q, f)
        F = dot(A, rho_hat)
        y_list_midpoint[i] = norm((F - f_eval), 0)

    ax = plot_logy(ax, panel_list, y_list_midpoint, 'r-',
                   "Err($N_q$) - midpoint")

    for i in range(len(panel_list)):
Ejemplo n.º 6
0
    print("yo^2")
    for j in range(100000):
        seed(int(time() + 3 * j))
        b_tilde = disturb_rhs(b)

        for i in range(n_lambda):
            rho_lambda = solve((dot(A.transpose(), A) + lambdas[i] * eye(N_c)),
                               dot(A.transpose(), b_tilde))
            max_error[i] = max(abs(rho_lambda - rho_analytical))
        errors_by_lambda[argmin(max_error)] += 1

    fig, ax = init_plot('Oppgave 7 -b')
    ax.scatter(lambdas, errors_by_lambda, c='r', s=30)
    ax.set_xscale('log')
    ax.set_xlim(1e-10, 1e-7)
    ax = set_axis_specs(ax, r'$\lambda$', r'# min |Error|', 23, 23)
    plt.show()

    file = rel_path + r'/Data_files/Oppgave7d1'
    savez(file, lambdas=lambdas, errors_by_lambda=errors_by_lambda)

    n_lambda = 100000
    lambdas = geomspace(1e-14, 1, n_lambda)
    max_error = empty(n_lambda)
    seed(int(time()))
    b_tilde = disturb_rhs(b)
    for i in range(n_lambda):
        rho_lambda = solve((dot(A.transpose(), A) + lambdas[i] * eye(N_c)),
                           dot(A.transpose(), b_tilde))
        max_error[i] = max(abs(rho_lambda - rho_analytical))
    fig, ax = init_plot('Oppgave 7 -b')
    d = 0.25
    for i in N_c_list:
        print(i)
        N_c, N_s = i, i
        x_c = shift_chebyshev(0, 1, chebyshev_n(N_c))
        x_s = x_c
        A = fredholm_lhs(d, x_c, x_s, x_q, w_q, f)
        b = F_analytic(x_c, d)
        rho_hat = numpy.linalg.solve(A, b)

        max_diff_2[i - n_min] = max(abs(rho_hat - rho(x_c)))

    plot_logy_test(ax, N_c_list, max_diff_2, 'bo--',
                   r'Err($N_{c}$) - d = ' + '{}'.format(d))
    ax = set_axis_specs(ax, r'$N_c$', r'Max $|$Error$_j|$', 24, 24)
    #save_fig_legend_on_top(fig, ax, 'Question 5 - Error d=0,025 & 0,25.pdf')

    #fig, ax = init_plot('Question 5 - Error d=2.5')
    d = 2.5
    for i in N_c_list:
        print(i)
        N_c, N_s = i, i
        x_c = shift_chebyshev(0, 1, chebyshev_n(N_c))
        x_s = x_c
        A = fredholm_lhs(d, x_c, x_s, x_q, w_q, f)
        b = F_analytic(x_c, d)
        rho_hat = numpy.linalg.solve(A, b)
        max_diff_3[i - n_min] = max(abs(rho_hat - rho(x_c)))

    plot_logy_test(ax, N_c_list, max_diff_3, 'go--',