Some stuff

This commit is contained in:
Cory Balaton 2023-09-12 17:34:40 +02:00
parent c9f9757957
commit 5f0363b895
No known key found for this signature in database
GPG Key ID: 3E5FCEBFD80F432B
9 changed files with 25 additions and 9 deletions

Binary file not shown.

BIN
latex/images/problem7.pdf Normal file

Binary file not shown.

Binary file not shown.

View File

@ -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}

View File

@ -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);

View File

@ -4,10 +4,12 @@
#include <armadillo>
#include <cmath>
#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(

View File

@ -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();
}
}

View File

@ -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(",")

View File

@ -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()