Compare commits

..

No commits in common. "main" and "coryab/final-run" have entirely different histories.

21 changed files with 36 additions and 84 deletions

View File

@ -10,29 +10,29 @@ Make sure that you are inside the **src** directory before compiling the code.
Now you can execute the command shown under to compile: Now you can execute the command shown under to compile:
``` ´´´
make make
``` ´´´
This will create object files and link them together into 2 executable files. This will create object files and link them together into 2 executable files.
These files are called **main** and **analyticPlot**. These files are called **main** and **analyticPlot**.
To run them, you can simply use the commands below: To run them, you can simply use the commands below:
``` ´´´
./main ./main
``` ´´´
``` ´´´
./analyticPlot ./analyticPlot
``` ´´´
## How to generate plots ## How to generate plots
For generating the plots, there are 4 Python scripts. For generating the plots, there are 4 Python scripts.
You can run each one of them by using this command: You can run each one of them by using this command:
``` ´´´
python <PythonFile> python <PythonFile>
``` ´´´
The plots will be saved inside **latex/images**. The plots will be saved inside **latex/images**.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,8 +1,3 @@
\section*{Problem 10} \section*{Problem 10}
% Time and show result, and link to relevant files % 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}

View File

@ -6,8 +6,6 @@ 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 Plotting code: https://github.uio.no/FYS3150-G2-2023/Project-1/blob/main/src/analyticPlot.py
\begin{figure}[H] Here is the plot of the analytical solution for $u(x)$.
\centering
\includegraphics[width=0.8\linewidth]{images/analytical_solution.pdf} \includegraphics[scale=.5]{analytical_solution.pdf}
\caption{Plot of the analytical solution $u(x)$.}
\end{figure}

View File

@ -2,13 +2,8 @@
\subsection*{a)} \subsection*{a)}
% Link to relevant files on gh and possibly add some comments % 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/main/src/generalAlgorithm.cpp The code can be found at https://github.uio.no/FYS3150-G2-2023/Project-1/blob/coryab/final-run/src/generalAlgorithm.cpp
\subsection*{b)} \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}
\includegraphics{problem7}

View File

@ -1,27 +1,3 @@
\section*{Problem 8} \section*{Problem 8}
%link to relvant files and show plots %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}

View File

@ -7,7 +7,7 @@
#include <iomanip> #include <iomanip>
#define RANGE 1000 #define RANGE 1000
#define FILENAME "output/analytical_solution.txt" #define FILENAME "analytical_solution.txt"
double u(double x); double u(double x);
void generate_range(std::vector<double> &vec, double start, double stop, int n); void generate_range(std::vector<double> &vec, double start, double stop, int n);

View File

@ -4,9 +4,7 @@
#include <armadillo> #include <armadillo>
#include <cmath> #include <cmath>
#define PRECISION 8 #define PRECISION 20
#define N_STEPS_EXP 7
double f(double x); double f(double x);

View File

@ -33,7 +33,7 @@ void general_algorithm_main()
int steps; int steps;
double step_size; double step_size;
for (int i = 0; i < N_STEPS_EXP; i++) { for (int i = 0; i < 6; i++) {
steps = std::pow(10, i+1); steps = std::pow(10, i+1);
step_size = 1./(double) steps; step_size = 1./(double) steps;
@ -58,7 +58,7 @@ void general_algorithm_error()
int steps; int steps;
double step_size, abs_err, rel_err, u_i, v_i; double step_size, abs_err, rel_err, u_i, v_i;
for (int i=0; i < N_STEPS_EXP; i++) { for (int i=0; i < 7; i++) {
steps = std::pow(10, i+1); steps = std::pow(10, i+1);
step_size = 1./(double) steps; step_size = 1./(double) steps;

View File

@ -20,7 +20,7 @@ void timing() {
ofile.open("output/timing.txt"); ofile.open("output/timing.txt");
// Timing // Timing
for (int i=1; i < N_STEPS_EXP; i++) { for (int i=1; i <= 6; i++) {
n_steps = std::pow(10, i); n_steps = std::pow(10, i);
clock_t g_1, g_2, s_1, s_2; clock_t g_1, g_2, s_1, s_2;
double g_res = 0, s_res = 0; double g_res = 0, s_res = 0;

View File

@ -6,7 +6,7 @@ def main():
x = [] x = []
v = [] v = []
with open('output/analytical_solution.txt') as f: with open('analytical_solution.txt') as f:
for line in f: for line in f:
a, b = line.strip().split() a, b = line.strip().split()
x.append(float(a)) x.append(float(a))

View File

@ -4,7 +4,7 @@ import numpy as np
analytical_func = lambda x: 1 - (1 - np.exp(-10))*x - np.exp(-10*x) analytical_func = lambda x: 1 - (1 - np.exp(-10))*x - np.exp(-10*x)
def main(): def main():
for i in range(7): for i in range(6):
x = [] x = []
y = [] y = []
x.append(0.) x.append(0.)
@ -19,10 +19,10 @@ def main():
x.append(1.) x.append(1.)
y.append(0.) y.append(0.)
plt.plot(x, y, label=f"n$_{{steps}} = 10^{i+1}$") plt.plot(x, y, label=f"n_steps={10**(i+1)}")
x = np.linspace(0, 1, 1001) x = np.linspace(0, 1, 1001)
plt.plot(x, analytical_func(x), label=f"u$_{{exact}}$") plt.plot(x, analytical_func(x), label="analytical plot")
plt.legend() plt.legend()
plt.savefig("../latex/images/problem7.pdf") plt.savefig("../latex/images/problem7.pdf")

View File

@ -1,10 +1,8 @@
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
# plt.rc('text', usetex=True)
# plt.rc('font', family='serif')
def main(): def main():
for i in range(7): fig, axs = plt.subplots(1)
for i in range(6):
x = [] x = []
abs_err = [] abs_err = []
rel_err = [] rel_err = []
@ -16,25 +14,16 @@ def main():
abs_err.append(float(abs_err_i)) abs_err.append(float(abs_err_i))
rel_err.append(float(rel_err_i)) rel_err.append(float(rel_err_i))
plt.figure(1) axs[0].plot(x, abs_err, label=f"abs_err {10**(i+1)} steps")
plt.plot(x, abs_err, label=f"n$_{{steps}} = 10^{i+1}$") axs[1].plot(x, rel_err, label=f"rel_err {10**(i+1)} steps")
plt.figure(2) print(max(rel_err))
plt.plot(x, rel_err, label=f"n$_{{steps}} = 10^{i+1}$") axs[2].plot(i+1, max(rel_err), marker="o", markersize=10)
plt.figure(3) axs[0].legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.plot(i+1, max(rel_err), marker="o", markersize=10) axs[1].legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.savefig("../latex/images/problem8.pdf", bbox_inches="tight")
fig.2
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__": if __name__ == "__main__":
main() main()

View File

@ -33,7 +33,7 @@ void special_algorithm_main()
int steps; int steps;
double sub_sig, main_sig, sup_sig, step_size; double sub_sig, main_sig, sup_sig, step_size;
for (int i = 0; i < N_STEPS_EXP; i++) { for (int i = 0; i < 6; i++) {
steps = std::pow(10, i+1); steps = std::pow(10, i+1);
step_size = 1./(double) steps; step_size = 1./(double) steps;
build_g_vec(steps, g_vec); build_g_vec(steps, g_vec);

View File

@ -1,4 +1,5 @@
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np
def main(): def main():
x = [] x = []
@ -12,8 +13,8 @@ def main():
gen_alg.append(float(gen_i)) gen_alg.append(float(gen_i))
spec_alg.append(float(spec_i)) spec_alg.append(float(spec_i))
plt.plot(x, gen_alg, label=f"General algorithm") plt.plot(x, gen_alg, label=f"general algorithm")
plt.plot(x, spec_alg, label=f"Special algorithm") plt.plot(x, spec_alg, label=f"general algorithm")
plt.legend() plt.legend()
plt.savefig("../latex/images/problem10.pdf") plt.savefig("../latex/images/problem10.pdf")