diff --git a/.gitignore b/.gitignore index a82fcc1..9c2126e 100644 --- a/.gitignore +++ b/.gitignore @@ -43,4 +43,5 @@ src/* !src/Makefile !src/*.cpp +!src/*.hpp !src/*.py diff --git a/README.md b/README.md index 61e5d52..914af04 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,36 @@ ## Practical information - [Project](https://anderkve.github.io/FYS3150/book/projects/project1.html) + +## How to compile C++ code + +Make sure that you are inside the **src** directory before compiling the code. + +Now you can execute the command shown under to compile: + +´´´ + make +´´´ + +This will create object files and link them together into 2 executable files. +These files are called **main** and **analyticPlot**. +To run them, you can simply use the commands below: + +´´´ + ./main +´´´ + +´´´ + ./analyticPlot +´´´ + +## How to generate plots + +For generating the plots, there are 4 Python scripts. +You can run each one of them by using this command: + +´´´ + python +´´´ + +The plots will be saved inside **latex/images**. diff --git a/latex/assignment_1.pdf b/latex/assignment_1.pdf index 1f17a59..bd74ae3 100644 Binary files a/latex/assignment_1.pdf and b/latex/assignment_1.pdf differ diff --git a/latex/images/analytical_solution.pdf b/latex/images/analytical_solution.pdf index f874a22..18f05d2 100644 Binary files a/latex/images/analytical_solution.pdf and b/latex/images/analytical_solution.pdf differ diff --git a/latex/images/problem10.pdf b/latex/images/problem10.pdf new file mode 100644 index 0000000..8c658db Binary files /dev/null and b/latex/images/problem10.pdf differ diff --git a/latex/images/problem7.pdf b/latex/images/problem7.pdf new file mode 100644 index 0000000..d9fbd30 Binary files /dev/null and b/latex/images/problem7.pdf differ diff --git a/latex/images/problem8.pdf b/latex/images/problem8.pdf new file mode 100644 index 0000000..d26eba3 Binary files /dev/null and b/latex/images/problem8.pdf differ diff --git a/latex/images/problem8_a.pdf b/latex/images/problem8_a.pdf new file mode 100644 index 0000000..1a326ec Binary files /dev/null and b/latex/images/problem8_a.pdf differ diff --git a/latex/images/problem8_b.pdf b/latex/images/problem8_b.pdf new file mode 100644 index 0000000..023c75a Binary files /dev/null and b/latex/images/problem8_b.pdf differ diff --git a/latex/images/problem8_c.pdf b/latex/images/problem8_c.pdf new file mode 100644 index 0000000..294107f Binary files /dev/null and b/latex/images/problem8_c.pdf differ diff --git a/latex/problems/problem10.tex b/latex/problems/problem10.tex index 72de7f2..fd4ccc4 100644 --- a/latex/problems/problem10.tex +++ b/latex/problems/problem10.tex @@ -1,3 +1,8 @@ \section*{Problem 10} % Time and show result, and link to relevant files +\begin{figure}[H] + \centering + \includegraphics[width=0.7\linewidth]{images/problem10.pdf} + \caption{Timing of general algorithm vs. special for step sizes $n_{steps}$} +\end{figure} \ No newline at end of file diff --git a/latex/problems/problem2.tex b/latex/problems/problem2.tex index bfad89c..9558ad0 100644 --- a/latex/problems/problem2.tex +++ b/latex/problems/problem2.tex @@ -6,6 +6,8 @@ Point generator code: https://github.uio.no/FYS3150-G2-203/Project-1/blob/main/s Plotting code: https://github.uio.no/FYS3150-G2-2023/Project-1/blob/main/src/analyticPlot.py -Here is the plot of the analytical solution for $u(x)$. - -\includegraphics[scale=.5]{analytical_solution.pdf} +\begin{figure}[H] + \centering + \includegraphics[width=0.8\linewidth]{images/analytical_solution.pdf} + \caption{Plot of the analytical solution $u(x)$.} +\end{figure} diff --git a/latex/problems/problem7.tex b/latex/problems/problem7.tex index a39867c..6d02873 100644 --- a/latex/problems/problem7.tex +++ b/latex/problems/problem7.tex @@ -1,3 +1,14 @@ \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)} +Increasing the number of steps results in an approximation close to the exact solution. +\begin{figure}[H] + \centering + \includegraphics[width=0.8\linewidth]{images/problem7.pdf} + \caption{Plot showing the numeric solution of $u_{approx}$ for $n_{steps}$ and the exact solution $u_{exact}$.} +\end{figure} + diff --git a/latex/problems/problem8.tex b/latex/problems/problem8.tex index 4d20743..c7344f8 100644 --- a/latex/problems/problem8.tex +++ b/latex/problems/problem8.tex @@ -1,3 +1,27 @@ \section*{Problem 8} %link to relvant files and show plots +\subsection*{a)} +Increasing number of steps result in a decrease of absolute error. +\begin{figure}[H] + \centering + \includegraphics[width=0.8\linewidth]{images/problem8_a.pdf} + \caption{Absolute error for different step sizes $n_{steps}$.} +\end{figure} + +\subsection*{b)} +Increasing number of steps also result in a decrease of absolute error. +\begin{figure}[H] + \centering + \includegraphics[width=0.8\linewidth]{images/problem8_b.pdf} + \caption{Relative error for different step sizes $n_{steps}$.} +\end{figure} + +\subsection*{c)} +Increasing number of steps result in a decrease of maximum relative error, up to a certain number of steps. At $n_{steps} \approx 10^{5}$ the maximumrelative error increase. +This can be related to loss of numerical precicion when step size is small. +\begin{figure}[H] + \centering + \includegraphics[width=0.7\linewidth]{images/problem8_c.pdf} + \caption{Maximum relative error for each step sizes $n_{steps}$.} +\end{figure} \ No newline at end of file diff --git a/src/Makefile b/src/Makefile index 1e668e6..cf45c51 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,17 +1,30 @@ CC=g++ -.PHONY: clean +CCFLAGS= -std=c++11 -all: simpleFile analyticPlot +OBJS=generalAlgorithm.o specialAlgorithm.o funcs.o -simpleFile: simpleFile.o - $(CC) -o $@ $^ +EXEC=main analyticPlot + +.PHONY: clean create_dirs + +all: create_dirs $(EXEC) + +main: main.o $(OBJS) + $(CC) $(CCFLAGS) -o $@ $^ analyticPlot: analyticPlot.o - $(CC) -o $@ $^ + $(CC) $(CCFLAGS) -o $@ $^ %.o: %.cpp - $(CC) -c $< -o $@ + $(CC) $(CCFLAGS) -c -o $@ $^ clean: rm *.o + rm $(EXEC) + rm -r output + +create_dirs: + mkdir -p output/general + mkdir -p output/special + mkdir -p output/error diff --git a/src/analyticPlot.cpp b/src/analyticPlot.cpp index 07c932a..965b481 100644 --- a/src/analyticPlot.cpp +++ b/src/analyticPlot.cpp @@ -7,7 +7,7 @@ #include #define RANGE 1000 -#define FILENAME "analytical_solution.txt" +#define FILENAME "output/analytical_solution.txt" double u(double x); void generate_range(std::vector &vec, double start, double stop, int n); diff --git a/src/funcs.cpp b/src/funcs.cpp new file mode 100644 index 0000000..c79a19a --- /dev/null +++ b/src/funcs.cpp @@ -0,0 +1,38 @@ +#include "funcs.hpp" + +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); + + double step_size = 1./ (double) n_steps; + for (int i=0; i < n_steps-1; i++) { + g_vec(i) = step_size*step_size*f((i+1)*step_size); + } +} + +void build_arrays( + int n_steps, + arma::vec& sub_diag, + arma::vec& main_diag, + arma::vec& sup_diag, + arma::vec& g_vec +) +{ + sub_diag.resize(n_steps-2); + main_diag.resize(n_steps-1); + sup_diag.resize(n_steps-2); + + sub_diag.fill(-1); + main_diag.fill(2); + sup_diag.fill(-1); + + build_g_vec(n_steps, g_vec); +} + diff --git a/src/funcs.hpp b/src/funcs.hpp new file mode 100644 index 0000000..ba8f9e3 --- /dev/null +++ b/src/funcs.hpp @@ -0,0 +1,24 @@ +#ifndef __FUNCS__ +#define __FUNCS__ + +#include +#include + +#define PRECISION 8 + +#define N_STEPS_EXP 7 + +double f(double x); + +double u(double x); + +void build_g_vec(int n_steps, arma::vec& g_vec); + +void build_arrays( + int n_steps, + arma::vec& sub_diag, + arma::vec& main_diag, + arma::vec& sup_diag, + arma::vec& g_vec +); +#endif diff --git a/src/generalAlgorithm.cpp b/src/generalAlgorithm.cpp new file mode 100644 index 0000000..92a7f0a --- /dev/null +++ b/src/generalAlgorithm.cpp @@ -0,0 +1,84 @@ +#include "funcs.hpp" +#include "generalAlgorithm.hpp" +#include + +arma::vec& general_algorithm( + arma::vec& sub_diag, + arma::vec& main_diag, + arma::vec& sup_diag, + arma::vec& g_vec +) +{ + int n = g_vec.n_elem; + double d; + + for (int i = 1; i < n; i++) { + d = sub_diag(i-1) / main_diag(i-1); + main_diag(i) -= d*sup_diag(i-1); + g_vec(i) -= d*g_vec(i-1); + } + + g_vec(n-1) /= main_diag(n-1); + + for (int i = n-2; i >= 0; i--) { + g_vec(i) = (g_vec(i) - sup_diag(i) * g_vec(i+1)) / main_diag(i); + } + return g_vec; +} + +void general_algorithm_main() +{ + arma::vec sub_diag, main_diag, sup_diag, g_vec, v_vec; + std::ofstream ofile; + int steps; + double step_size; + + for (int i = 0; i < N_STEPS_EXP; i++) { + steps = std::pow(10, i+1); + step_size = 1./(double) steps; + + build_arrays(steps, sub_diag, main_diag, sup_diag, g_vec); + + v_vec = general_algorithm(sub_diag, main_diag, sup_diag, g_vec); + + ofile.open("output/general/out_" + std::to_string(steps) + ".txt"); + + for (int j=0; j < v_vec.n_elem; j++) { + ofile << std::setprecision(PRECISION) << std::scientific << step_size*(j+1) << "," + << std::setprecision(PRECISION) << std::scientific << v_vec(j) << std::endl; + } + ofile.close(); + } +} + +void general_algorithm_error() +{ + arma::vec sub_diag, main_diag, sup_diag, g_vec, v_vec; + std::ofstream ofile; + int steps; + double step_size, abs_err, rel_err, u_i, v_i; + + for (int i=0; i < N_STEPS_EXP; i++) { + steps = std::pow(10, i+1); + step_size = 1./(double) steps; + + build_arrays(steps, sub_diag, main_diag, sup_diag, g_vec); + + v_vec = general_algorithm(sub_diag, main_diag, sup_diag, g_vec); + + ofile.open("output/error/out_" + std::to_string(steps) + ".txt"); + + for (int j=0; j < v_vec.n_elem; j++) { + 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(abs_err)) << "," + << std::setprecision(PRECISION) << std::scientific + << std::log10(std::abs(abs_err/u_i)) << std::endl; + } + ofile.close(); + } +} diff --git a/src/generalAlgorithm.hpp b/src/generalAlgorithm.hpp new file mode 100644 index 0000000..216e065 --- /dev/null +++ b/src/generalAlgorithm.hpp @@ -0,0 +1,18 @@ +#ifndef __GENERAL_ALG__ +#define __GENERAL_ALG__ + +#include +#include + +arma::vec& general_algorithm( + arma::vec& sub_diag, + arma::vec& main_diag, + arma::vec& sup_diag, + arma::vec& g_vec +); + +void general_algorithm_main(); + +void general_algorithm_error(); + +#endif diff --git a/src/main.cpp b/src/main.cpp index 65bbf4a..e9c593d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,24 +1,68 @@ -#include "GeneralAlgorithm.hpp" #include -#include +#include +#include +#include +#include +#include +#include -double f(double x) { - return 100. * std::exp(-10.*x); +#include "funcs.hpp" +#include "generalAlgorithm.hpp" +#include "specialAlgorithm.hpp" + +#define TIMING_ITERATIONS 5 + +void timing() { + arma::vec sub_diag, main_diag, sup_diag, g_vec; + int n_steps; + + std::ofstream ofile; + ofile.open("output/timing.txt"); + + // Timing + for (int i=1; i < N_STEPS_EXP; i++) { + n_steps = std::pow(10, i); + clock_t g_1, g_2, s_1, s_2; + double g_res = 0, s_res = 0; + + // Repeat a number of times to take an average + for (int j=0; j < TIMING_ITERATIONS; j++) { + + build_arrays(n_steps, sub_diag, main_diag, sup_diag, g_vec); + + g_1 = clock(); + + general_algorithm(sub_diag, main_diag, sup_diag, g_vec); + + g_2 = clock(); + + g_res += (double) (g_2 - g_1) / CLOCKS_PER_SEC; + // Rebuild g_vec for the special alg + build_g_vec(n_steps, g_vec); + + s_1 = clock(); + + special_algorithm(-1., 2., -1., g_vec); + + s_2 = clock(); + + s_res += (double) (s_2 - s_1) / CLOCKS_PER_SEC; + + } + // Write the average time to file + ofile + << n_steps << "," + << g_res / (double) TIMING_ITERATIONS << "," + << s_res / (double) TIMING_ITERATIONS << std::endl; + } + + ofile.close(); } -double a_sol(double x) { - return 1. - (1. - std::exp(-10)) * x - std::exp(-10*x); -} - -int main() { - arma::mat A = arma::eye(3,3); - - GeneralAlgorithm ga(3, &A, f, a_sol, 0., 1.); - - ga.solve(); - std::cout << "Time: " << ga.time(5) << std::endl; - ga.error(); - - return 0; - +int main() +{ + timing(); + general_algorithm_main(); + general_algorithm_error(); + special_algorithm_main(); } diff --git a/src/analyticPlot.py b/src/plot_analytic_solution.py similarity index 72% rename from src/analyticPlot.py rename to src/plot_analytic_solution.py index ff12ef5..3223154 100644 --- a/src/analyticPlot.py +++ b/src/plot_analytic_solution.py @@ -2,11 +2,11 @@ import numpy as np import matplotlib.pyplot as plt def main(): - FILENAME = "analytical_solution.pdf" + FILENAME = "../latex/images/analytical_solution.pdf" x = [] v = [] - with open('analytical_solution.txt') as f: + with open('output/analytical_solution.txt') as f: for line in f: a, b = line.strip().split() x.append(float(a)) diff --git a/src/plot_general_alg.py b/src/plot_general_alg.py new file mode 100644 index 0000000..8f8a07f --- /dev/null +++ b/src/plot_general_alg.py @@ -0,0 +1,30 @@ +import matplotlib.pyplot as plt +import numpy as np + +analytical_func = lambda x: 1 - (1 - np.exp(-10))*x - np.exp(-10*x) + +def main(): + for i in range(7): + x = [] + y = [] + x.append(0.) + y.append(0.) + 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(",") + x.append(float(x_i)) + y.append(float(y_i)) + + x.append(1.) + y.append(0.) + + plt.plot(x, y, label=f"n$_{{steps}} = 10^{i+1}$") + + x = np.linspace(0, 1, 1001) + plt.plot(x, analytical_func(x), label=f"u$_{{exact}}$") + plt.legend() + plt.savefig("../latex/images/problem7.pdf") + +if __name__ == "__main__": + main() diff --git a/src/plot_general_alg_error.py b/src/plot_general_alg_error.py new file mode 100644 index 0000000..cc1ce97 --- /dev/null +++ b/src/plot_general_alg_error.py @@ -0,0 +1,40 @@ +import matplotlib.pyplot as plt + +# plt.rc('text', usetex=True) +# plt.rc('font', family='serif') + +def main(): + for i in range(7): + x = [] + abs_err = [] + rel_err = [] + with open(f"output/error/out_{10**(i+1)}.txt", "r") as f: + lines = f.readlines() + for line in lines: + x_i, abs_err_i, rel_err_i = line.strip().split(",") + x.append(float(x_i)) + abs_err.append(float(abs_err_i)) + rel_err.append(float(rel_err_i)) + + plt.figure(1) + plt.plot(x, abs_err, label=f"n$_{{steps}} = 10^{i+1}$") + plt.figure(2) + plt.plot(x, rel_err, label=f"n$_{{steps}} = 10^{i+1}$") + + plt.figure(3) + plt.plot(i+1, max(rel_err), marker="o", markersize=10) + + plt.figure(1) + plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) + plt.figure(2) + plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) + + plt.figure(1) + plt.savefig("../latex/images/problem8_a.pdf", bbox_inches="tight") + plt.figure(2) + plt.savefig("../latex/images/problem8_b.pdf", bbox_inches="tight") + plt.figure(3) + plt.savefig("../latex/images/problem8_c.pdf", bbox_inches="tight") + +if __name__ == "__main__": + main() diff --git a/src/simpleFile.cpp b/src/simpleFile.cpp deleted file mode 100644 index 344e7a6..0000000 --- a/src/simpleFile.cpp +++ /dev/null @@ -1,162 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#define TIMING_ITERATIONS 5 - -arma::vec* general_algorithm( - arma::vec* sub_diag, - arma::vec* main_diag, - arma::vec* sup_diag, - arma::vec* g_vec -) -{ - int n = g_vec->n_elem; - double d; - - for (int i = 1; i < n; i++) { - d = (*sub_diag)(i-1) / (*main_diag)(i-1); - (*main_diag)(i) -= d*(*sup_diag)(i-1); - (*g_vec)(i) -= d*(*g_vec)(i-1); - } - - (*g_vec)(n-1) /= (*main_diag)(n-1); - - for (int i = n-2; i >= 0; i--) { - (*g_vec)(i) = ((*g_vec)(i) - (*sup_diag)(i) * (*g_vec)(i+1)) / (*main_diag)(i); - } - return g_vec; -} - - -arma::vec* special_algorithm( - double sub_sig, - double main_sig, - double sup_sig, - arma::vec* g_vec -) -{ - int n = g_vec->n_elem; - arma::vec diag = arma::vec(n); - - for (int i = 1; i < n; i++) { - // Calculate values for main diagonal based on indices - diag(i-1) = (double)(i+1) / i; - (*g_vec)(i) += (*g_vec)(i-1) / diag(i-1); - } - // The last element in main diagonal has value (i+1)/i = (n+1)/n - (*g_vec)(n-1) /= (double)(n+1) / (n); - - for (int i = n-2; i >= 0; i--) { - (*g_vec)(i) = ((*g_vec)(i) + (*g_vec)(i+1))/ diag(i); - } - - return g_vec; -} - -void error( - std::string filename, - arma::vec* x_vec, - arma::vec* v_vec, - arma::vec* a_vec -) -{ - std::ofstream ofile; - ofile.open(filename); - - if (!ofile.is_open()) { - exit(1); - } - - for (int i=0; i < a_vec->n_elem; i++) { - double sub = (*a_vec)(i) - (*v_vec)(i); - ofile << std::setprecision(8) << std::scientific << (*x_vec)(i) - << std::setprecision(8) << std::scientific << std::log10(std::abs(sub)) - << std::setprecision(8) << std::scientific << std::log10(std::abs(sub/(*a_vec)(i))) - << std::endl; - } - - ofile.close(); - -} - -double f(double x) { - return 100*std::exp(-10*x); -} - -void build_array( - int n_steps, - arma::vec* sub_diag, - arma::vec* main_diag, - arma::vec* sup_diag, - arma::vec* g_vec -) -{ - sub_diag->resize(n_steps-2); - main_diag->resize(n_steps-1); - sup_diag->resize(n_steps-2); - - sub_diag->fill(-1); - main_diag->fill(2); - sup_diag->fill(-1); - - g_vec->resize(n_steps-1); - - double step_size = 1./ (double) n_steps; - for (int i=0; i < n_steps-1; i++) { - (*g_vec)(i) = f((i+1)*step_size); - } - -} - -void timing() { - arma::vec sub_diag, main_diag, sup_diag, g_vec; - int n_steps; - - std::ofstream ofile; - ofile.open("timing.txt"); - - // Timing - for (int i=1; i <= 8; i++) { - n_steps = std::pow(10, i); - clock_t g_1, g_2, s_1, s_2; - double g_res = 0, s_res = 0; - - for (int j=0; j < TIMING_ITERATIONS; j++) { - build_array(n_steps, &sub_diag, &main_diag, &sup_diag, &g_vec); - - g_1 = clock(); - - general_algorithm(&sub_diag, &main_diag, &sup_diag, &g_vec); - - g_2 = clock(); - - g_res += (double) (g_2 - g_1) / CLOCKS_PER_SEC; - build_array(n_steps, &sub_diag, &main_diag, &sup_diag, &g_vec); - - s_1 = clock(); - - special_algorithm(-1., 2., -1., &g_vec); - - s_2 = clock(); - - s_res += (double) (s_2 - s_1) / CLOCKS_PER_SEC; - - } - ofile - << n_steps << "," - << g_res / (double) TIMING_ITERATIONS << "," - << s_res / (double) TIMING_ITERATIONS << std::endl; - } - - ofile.close(); -} - -int main() -{ - timing(); -} diff --git a/src/specialAlgorithm.cpp b/src/specialAlgorithm.cpp new file mode 100644 index 0000000..8a918fc --- /dev/null +++ b/src/specialAlgorithm.cpp @@ -0,0 +1,52 @@ +#include "funcs.hpp" +#include "specialAlgorithm.hpp" + +arma::vec& special_algorithm( + double sub_sig, + double main_sig, + double sup_sig, + arma::vec& g_vec +) +{ + int n = g_vec.n_elem; + arma::vec diag = arma::vec(n); + + for (int i = 1; i < n; i++) { + // Calculate values for main diagonal based on indices + diag(i-1) = (double)(i+1) / i; + g_vec(i) += g_vec(i-1) / diag(i-1); + } + // The last element in main diagonal has value (i+1)/i = (n+1)/n + g_vec(n-1) /= (double)(n+1) / (n); + + for (int i = n-2; i >= 0; i--) { + g_vec(i) = (g_vec(i) + g_vec(i+1))/ diag(i); + } + + return g_vec; +} + +void special_algorithm_main() +{ + arma::vec g_vec, v_vec; + std::ofstream ofile; + int steps; + double sub_sig, main_sig, sup_sig, step_size; + + for (int i = 0; i < N_STEPS_EXP; i++) { + steps = std::pow(10, i+1); + step_size = 1./(double) steps; + build_g_vec(steps, g_vec); + + v_vec = special_algorithm(sub_sig, main_sig, sup_sig, g_vec); + + ofile.open("output/special/out_" + std::to_string(steps) + ".txt"); + + for (int j=0; j < v_vec.n_elem; j++) { + ofile << std::setprecision(PRECISION) << std::scientific << step_size*(j+1) << "," + << std::setprecision(PRECISION) << std::scientific << v_vec(j) << std::endl; + } + ofile.close(); + } +} + diff --git a/src/specialAlgorithm.hpp b/src/specialAlgorithm.hpp new file mode 100644 index 0000000..68c442b --- /dev/null +++ b/src/specialAlgorithm.hpp @@ -0,0 +1,16 @@ +#ifndef __SPECIAL_ALG__ +#define __SPECIAL_ALG__ + +#include +#include + +arma::vec& special_algorithm( + double sub_sig, + double main_sig, + double sup_sig, + arma::vec& g_vec +); + +void special_algorithm_main(); + +#endif diff --git a/src/timing.py b/src/timing.py new file mode 100644 index 0000000..ea08c0b --- /dev/null +++ b/src/timing.py @@ -0,0 +1,22 @@ +import matplotlib.pyplot as plt + +def main(): + x = [] + gen_alg = [] + spec_alg = [] + with open(f"output/timing.txt", "r") as f: + lines = f.readlines() + for line in lines: + x_i, gen_i, spec_i = line.strip().split(",") + x.append(float(x_i)) + gen_alg.append(float(gen_i)) + spec_alg.append(float(spec_i)) + + plt.plot(x, gen_alg, label=f"General algorithm") + plt.plot(x, spec_alg, label=f"Special algorithm") + + plt.legend() + plt.savefig("../latex/images/problem10.pdf") + +if __name__ == "__main__": + main()