Compare commits
16 Commits
coryab/fin
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| b93c1d054a | |||
| 8095773cb8 | |||
| 255a9c4d30 | |||
| 0ec1324c35 | |||
| 12ae9cf087 | |||
| 7d39248a15 | |||
|
|
93f534c9fa | ||
|
|
d1796b6b01 | ||
|
|
b88a9027bc | ||
|
|
e516104576 | ||
|
|
14a46d6057 | ||
|
|
9e900b8369 | ||
|
|
67b23770e1 | ||
|
|
bc5436cf1e | ||
|
|
aa8fc27947 | ||
|
|
f87af40008 |
16
README.md
16
README.md
@ -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.
BIN
latex/images/problem8_a.pdf
Normal file
BIN
latex/images/problem8_a.pdf
Normal file
Binary file not shown.
BIN
latex/images/problem8_b.pdf
Normal file
BIN
latex/images/problem8_b.pdf
Normal file
Binary file not shown.
BIN
latex/images/problem8_c.pdf
Normal file
BIN
latex/images/problem8_c.pdf
Normal file
Binary file not shown.
@ -1,3 +1,8 @@
|
|||||||
\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}
|
||||||
@ -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
|
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)$.
|
\begin{figure}[H]
|
||||||
|
\centering
|
||||||
\includegraphics[scale=.5]{analytical_solution.pdf}
|
\includegraphics[width=0.8\linewidth]{images/analytical_solution.pdf}
|
||||||
|
\caption{Plot of the analytical solution $u(x)$.}
|
||||||
|
\end{figure}
|
||||||
|
|||||||
@ -2,8 +2,13 @@
|
|||||||
|
|
||||||
\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/coryab/final-run/src/generalAlgorithm.cpp
|
The code can be found at https://github.uio.no/FYS3150-G2-2023/Project-1/blob/main/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}
|
|
||||||
|
|||||||
@ -1,3 +1,27 @@
|
|||||||
\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}
|
||||||
@ -7,7 +7,7 @@
|
|||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
|
||||||
#define RANGE 1000
|
#define RANGE 1000
|
||||||
#define FILENAME "analytical_solution.txt"
|
#define FILENAME "output/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);
|
||||||
|
|||||||
@ -4,7 +4,9 @@
|
|||||||
#include <armadillo>
|
#include <armadillo>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
#define PRECISION 20
|
#define PRECISION 8
|
||||||
|
|
||||||
|
#define N_STEPS_EXP 7
|
||||||
|
|
||||||
double f(double x);
|
double f(double x);
|
||||||
|
|
||||||
|
|||||||
@ -33,7 +33,7 @@ void general_algorithm_main()
|
|||||||
int steps;
|
int steps;
|
||||||
double step_size;
|
double step_size;
|
||||||
|
|
||||||
for (int i = 0; i < 6; i++) {
|
for (int i = 0; i < N_STEPS_EXP; 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 < 7; i++) {
|
for (int i=0; i < N_STEPS_EXP; i++) {
|
||||||
steps = std::pow(10, i+1);
|
steps = std::pow(10, i+1);
|
||||||
step_size = 1./(double) steps;
|
step_size = 1./(double) steps;
|
||||||
|
|
||||||
|
|||||||
@ -20,7 +20,7 @@ void timing() {
|
|||||||
ofile.open("output/timing.txt");
|
ofile.open("output/timing.txt");
|
||||||
|
|
||||||
// Timing
|
// Timing
|
||||||
for (int i=1; i <= 6; i++) {
|
for (int i=1; i < N_STEPS_EXP; 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;
|
||||||
|
|||||||
@ -6,7 +6,7 @@ def main():
|
|||||||
x = []
|
x = []
|
||||||
v = []
|
v = []
|
||||||
|
|
||||||
with open('analytical_solution.txt') as f:
|
with open('output/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))
|
||||||
|
|||||||
@ -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(6):
|
for i in range(7):
|
||||||
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="analytical plot")
|
plt.plot(x, analytical_func(x), label=f"u$_{{exact}}$")
|
||||||
plt.legend()
|
plt.legend()
|
||||||
plt.savefig("../latex/images/problem7.pdf")
|
plt.savefig("../latex/images/problem7.pdf")
|
||||||
|
|
||||||
|
|||||||
@ -1,8 +1,10 @@
|
|||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
# plt.rc('text', usetex=True)
|
||||||
|
# plt.rc('font', family='serif')
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
fig, axs = plt.subplots(1)
|
for i in range(7):
|
||||||
for i in range(6):
|
|
||||||
x = []
|
x = []
|
||||||
abs_err = []
|
abs_err = []
|
||||||
rel_err = []
|
rel_err = []
|
||||||
@ -14,16 +16,25 @@ 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))
|
||||||
|
|
||||||
axs[0].plot(x, abs_err, label=f"abs_err {10**(i+1)} steps")
|
plt.figure(1)
|
||||||
axs[1].plot(x, rel_err, label=f"rel_err {10**(i+1)} steps")
|
plt.plot(x, abs_err, label=f"n$_{{steps}} = 10^{i+1}$")
|
||||||
print(max(rel_err))
|
plt.figure(2)
|
||||||
axs[2].plot(i+1, max(rel_err), marker="o", markersize=10)
|
plt.plot(x, rel_err, label=f"n$_{{steps}} = 10^{i+1}$")
|
||||||
|
|
||||||
axs[0].legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
|
plt.figure(3)
|
||||||
axs[1].legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
|
plt.plot(i+1, max(rel_err), marker="o", markersize=10)
|
||||||
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()
|
||||||
|
|||||||
@ -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 < 6; i++) {
|
for (int i = 0; i < N_STEPS_EXP; 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);
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
x = []
|
x = []
|
||||||
@ -13,8 +12,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"general algorithm")
|
plt.plot(x, spec_alg, label=f"Special algorithm")
|
||||||
|
|
||||||
plt.legend()
|
plt.legend()
|
||||||
plt.savefig("../latex/images/problem10.pdf")
|
plt.savefig("../latex/images/problem10.pdf")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user