diff --git a/latex/assignment_1.pdf b/latex/assignment_1.pdf index e188bd7..2af19e1 100644 Binary files a/latex/assignment_1.pdf and b/latex/assignment_1.pdf differ diff --git a/latex/images/problem7.pdf b/latex/images/problem7.pdf new file mode 100644 index 0000000..3c9faf4 Binary files /dev/null and b/latex/images/problem7.pdf differ diff --git a/latex/images/problem8.pdf b/latex/images/problem8.pdf index d52e586..d26eba3 100644 Binary files a/latex/images/problem8.pdf and b/latex/images/problem8.pdf differ diff --git a/latex/problems/problem7.tex b/latex/problems/problem7.tex index a39867c..08ae575 100644 --- a/latex/problems/problem7.tex +++ b/latex/problems/problem7.tex @@ -1,3 +1,9 @@ \section*{Problem 7} +\subsection*{a)} % Link to relevant files on gh and possibly add some comments +The code can be found at https://github.uio.no/FYS3150-G2-2023/Project-1/blob/coryab/final-run/src/generalAlgorithm.cpp + +\subsection*{b)} + +\includegraphics{problem7} diff --git a/src/funcs.cpp b/src/funcs.cpp index bda3344..c79a19a 100644 --- a/src/funcs.cpp +++ b/src/funcs.cpp @@ -4,6 +4,10 @@ double f(double x) { return 100*std::exp(-10*x); } +double u(double x) { + return 1. - (1. - std::exp(-10.))*x - std::exp(-10.*x); +} + void build_g_vec(int n_steps, arma::vec& g_vec) { g_vec.resize(n_steps-1); diff --git a/src/funcs.hpp b/src/funcs.hpp index 94053ed..0d4a96e 100644 --- a/src/funcs.hpp +++ b/src/funcs.hpp @@ -4,10 +4,12 @@ #include #include -#define PRECISION 8 +#define PRECISION 20 double f(double x); +double u(double x); + void build_g_vec(int n_steps, arma::vec& g_vec); void build_arrays( diff --git a/src/generalAlgorithm.cpp b/src/generalAlgorithm.cpp index cd90384..b6ee8d9 100644 --- a/src/generalAlgorithm.cpp +++ b/src/generalAlgorithm.cpp @@ -58,7 +58,7 @@ void general_algorithm_error() int steps; double step_size, abs_err, rel_err, u_i, v_i; - for (int i=0; i < 6; i++) { + for (int i=0; i < 7; i++) { steps = std::pow(10, i+1); step_size = 1./(double) steps; @@ -69,16 +69,16 @@ void general_algorithm_error() ofile.open("output/error/out_" + std::to_string(steps) + ".txt"); for (int j=0; j < v_vec.n_elem; j++) { - u_i = f(step_size*(j+1)); + u_i = u(step_size*(j+1)); v_i = v_vec(j); + abs_err = u_i - v_i; ofile << std::setprecision(PRECISION) << std::scientific << step_size*(j+1) << "," << std::setprecision(PRECISION) << std::scientific - << std::log10(std::abs(u_i - v_i)) << "," + << std::log10(std::abs(abs_err)) << "," << std::setprecision(PRECISION) << std::scientific - << std::log10(std::abs((u_i - v_i)/u_i)) << std::endl; + << std::log10(std::abs(abs_err/u_i)) << std::endl; } ofile.close(); - } } diff --git a/src/plot_general_alg.py b/src/plot_general_alg.py index 9f37750..903ac67 100644 --- a/src/plot_general_alg.py +++ b/src/plot_general_alg.py @@ -4,12 +4,12 @@ import numpy as np analytical_func = lambda x: 1 - (1 - np.exp(-10))*x - np.exp(-10*x) def main(): - for i in range(2): + for i in range(6): x = [] y = [] x.append(0.) y.append(0.) - with open(f"output/problem7/out_{10**(i+1)}.txt", "r") as f: + with open(f"output/general/out_{10**(i+1)}.txt", "r") as f: lines = f.readlines() for line in lines: x_i, y_i = line.strip().split(",") diff --git a/src/plot_general_alg_error.py b/src/plot_general_alg_error.py index 71b89ab..7acbc19 100644 --- a/src/plot_general_alg_error.py +++ b/src/plot_general_alg_error.py @@ -1,7 +1,7 @@ import matplotlib.pyplot as plt def main(): - _, axs = plt.subplots(2) + fig, axs = plt.subplots(1) for i in range(6): x = [] abs_err = [] @@ -16,10 +16,14 @@ def main(): axs[0].plot(x, abs_err, label=f"abs_err {10**(i+1)} steps") axs[1].plot(x, rel_err, label=f"rel_err {10**(i+1)} steps") + print(max(rel_err)) + axs[2].plot(i+1, max(rel_err), marker="o", markersize=10) axs[0].legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) axs[1].legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) plt.savefig("../latex/images/problem8.pdf", bbox_inches="tight") + fig.2 + if __name__ == "__main__": main()