if __name__ == "__main__": d = 0.025 n_min, n_max = 25, 40 size = 100 N_q = 300 N_c_list = array([i for i in range(n_min, n_max + 1)]) x_q, w_q = legendre_gauss(0, 1, N_q) F_analytic = load(open(rel_path + "/Data_files/F.pkl", "rb")) max_diff, max_diff_rho = empty(len(N_c_list)), empty(len(N_c_list)) y_to_plot = empty((n_max - n_min + 1, size)) x_list = linspace(0, 1, size) 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 = solve(A, b) y_to_plot[i - n_min] = get_biggest_difference(x_s, rho_hat, x_list) max_diff_rho[i - n_min] = max(abs(y_to_plot[i - n_min] - rho(x_list))) max_diff[i - n_min] = max(abs(rho_hat - rho(x_s))) fig, ax = init_plot('Question 5') plot_normal(ax, N_c_list, max_diff_rho, 'b-', r'Err whole intervall') plot_normal(ax, N_c_list, max_diff, 'r-', r'Err(N$_c$)') show_plot(fig, ax)
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)
x_c = shift_chebyshev(0, 1, chebyshev_n(N_c)) 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
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),
return sin(w * x) * exp(y * x) 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")
if __name__ == "__main__": d = 0.25 N_c, N_s = 30, 30 n_lambda = 14 N_q = 900 seed(1) F_analytic = load(open(rel_path + "/Data_files/F.pkl", "rb")) x_c = shift_chebyshev(0, 1, chebyshev_n(N_c)) x_s = shift_chebyshev(0, 1, chebyshev_n(N_s)) x_q, w_q = legendre_gauss(0, 1, N_q) b = F_analytic(x_c, d) b_tilde = disturb_rhs(b) fig, ax = init_plot('Question 6 - b') rho_analytical = rho(x_c) A = fredholm_lhs(d, x_c, x_s, x_q, w_q, f) lambdas = array([10**(-i) for i in flip(linspace(1, 14, n_lambda), 0)]) max_error = empty(n_lambda) for i in range(n_lambda): rho_lambda = solve((dot(A.transpose(), A) + lambdas[i] * eye(N_c)), A.transpose()) max_error[i] = max(abs(rho_lambda - rho_analytical)) ax = plot_loglog(ax, lambdas, max_error, 'r-', 'Err(lambda') show_plot(fig, ax)
if __name__ == "__main__": n_min, n_max = 5, 30 N_c_list = array([i for i in range(n_min, n_max + 1)]) d_list = array([0.025, 0.25, 2.5]) c_list = array(['r-', 'b-', 'g-']) N_q = 900 x_q, w_q = legendre_gauss(0, 1, N_q) max_diff_1, max_diff_2, max_diff_3 = empty(len(N_c_list)), empty( len(N_c_list)), empty(len(N_c_list)) F_analytic = load(open(rel_path + "/Data_files/F.pkl", "rb")) fig, ax = init_plot('Question 5 - Error d=0.025 & 0.25') d = 0.025 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 = solve(A, b) max_diff_1[i - n_min] = max(abs(rho_hat - rho(x_c))) plot_logy_test(ax, N_c_list, max_diff_1, 'ro--', r'Err(N$_{c}$) - d = ' + '{}'.format(d)) d = 0.25