From 0edaae5b3fa058ac048ad53169449d022c581099 Mon Sep 17 00:00:00 2001 From: Cory Date: Mon, 11 Sep 2023 18:28:14 +0200 Subject: [PATCH] Make some changes --- src/plot_analytic_solution.py | 19 +++++++++++++++++++ src/plot_general_alg.py | 30 ++++++++++++++++++++++++++++++ src/plot_general_alg_error.py | 25 +++++++++++++++++++++++++ src/timing.py | 23 +++++++++++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 src/plot_analytic_solution.py create mode 100644 src/plot_general_alg.py create mode 100644 src/plot_general_alg_error.py create mode 100644 src/timing.py diff --git a/src/plot_analytic_solution.py b/src/plot_analytic_solution.py new file mode 100644 index 0000000..31ba66f --- /dev/null +++ b/src/plot_analytic_solution.py @@ -0,0 +1,19 @@ +import numpy as np +import matplotlib.pyplot as plt + +def main(): + FILENAME = "../latex/images/analytical_solution.pdf" + x = [] + v = [] + + with open('analytical_solution.txt') as f: + for line in f: + a, b = line.strip().split() + x.append(float(a)) + v.append(float(b)) + + plt.plot(x, v) + plt.savefig(FILENAME) + +if __name__ == "__main__": + main() diff --git a/src/plot_general_alg.py b/src/plot_general_alg.py new file mode 100644 index 0000000..9f37750 --- /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(2): + x = [] + y = [] + x.append(0.) + y.append(0.) + with open(f"output/problem7/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="analytical plot") + 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..71b89ab --- /dev/null +++ b/src/plot_general_alg_error.py @@ -0,0 +1,25 @@ +import matplotlib.pyplot as plt + +def main(): + _, axs = plt.subplots(2) + for i in range(6): + 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)) + + 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") + + 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") + +if __name__ == "__main__": + main() diff --git a/src/timing.py b/src/timing.py new file mode 100644 index 0000000..3f6a10f --- /dev/null +++ b/src/timing.py @@ -0,0 +1,23 @@ +import matplotlib.pyplot as plt +import numpy as np + +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"general algorithm") + + plt.legend() + plt.savefig("../latex/images/problem10.pdf") + +if __name__ == "__main__": + main()