Some stuff
This commit is contained in:
parent
c9f9757957
commit
5f0363b895
Binary file not shown.
BIN
latex/images/problem7.pdf
Normal file
BIN
latex/images/problem7.pdf
Normal file
Binary file not shown.
Binary file not shown.
@ -1,3 +1,9 @@
|
|||||||
\section*{Problem 7}
|
\section*{Problem 7}
|
||||||
|
|
||||||
|
\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
|
||||||
|
|
||||||
|
\subsection*{b)}
|
||||||
|
|
||||||
|
\includegraphics{problem7}
|
||||||
|
|||||||
@ -4,6 +4,10 @@ double f(double x) {
|
|||||||
return 100*std::exp(-10*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) {
|
void build_g_vec(int n_steps, arma::vec& g_vec) {
|
||||||
g_vec.resize(n_steps-1);
|
g_vec.resize(n_steps-1);
|
||||||
|
|
||||||
|
|||||||
@ -4,10 +4,12 @@
|
|||||||
#include <armadillo>
|
#include <armadillo>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
#define PRECISION 8
|
#define PRECISION 20
|
||||||
|
|
||||||
double f(double x);
|
double f(double x);
|
||||||
|
|
||||||
|
double u(double x);
|
||||||
|
|
||||||
void build_g_vec(int n_steps, arma::vec& g_vec);
|
void build_g_vec(int n_steps, arma::vec& g_vec);
|
||||||
|
|
||||||
void build_arrays(
|
void build_arrays(
|
||||||
|
|||||||
@ -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 < 6; 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;
|
||||||
|
|
||||||
@ -69,16 +69,16 @@ void general_algorithm_error()
|
|||||||
ofile.open("output/error/out_" + std::to_string(steps) + ".txt");
|
ofile.open("output/error/out_" + std::to_string(steps) + ".txt");
|
||||||
|
|
||||||
for (int j=0; j < v_vec.n_elem; j++) {
|
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);
|
v_i = v_vec(j);
|
||||||
|
abs_err = u_i - v_i;
|
||||||
ofile << std::setprecision(PRECISION) << std::scientific
|
ofile << std::setprecision(PRECISION) << std::scientific
|
||||||
<< step_size*(j+1) << ","
|
<< step_size*(j+1) << ","
|
||||||
<< std::setprecision(PRECISION) << std::scientific
|
<< 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::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();
|
ofile.close();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,12 +4,12 @@ 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(2):
|
for i in range(6):
|
||||||
x = []
|
x = []
|
||||||
y = []
|
y = []
|
||||||
x.append(0.)
|
x.append(0.)
|
||||||
y.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()
|
lines = f.readlines()
|
||||||
for line in lines:
|
for line in lines:
|
||||||
x_i, y_i = line.strip().split(",")
|
x_i, y_i = line.strip().split(",")
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
_, axs = plt.subplots(2)
|
fig, axs = plt.subplots(1)
|
||||||
for i in range(6):
|
for i in range(6):
|
||||||
x = []
|
x = []
|
||||||
abs_err = []
|
abs_err = []
|
||||||
@ -16,10 +16,14 @@ def main():
|
|||||||
|
|
||||||
axs[0].plot(x, abs_err, label=f"abs_err {10**(i+1)} steps")
|
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[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[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.)
|
axs[1].legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
|
||||||
plt.savefig("../latex/images/problem8.pdf", bbox_inches="tight")
|
plt.savefig("../latex/images/problem8.pdf", bbox_inches="tight")
|
||||||
|
fig.2
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user