11 Commits

Author SHA1 Message Date
92bb63abb5 Merge branch 'main' into 7-solve-problem-7 2023-09-07 14:35:54 +02:00
Janita Willumsen
afef7c9d1a Finish special algo and add write to file and plot 2023-09-07 14:33:54 +02:00
Janita Willumsen
dd16ffd822 Finish special algo and add write to file and plot 2023-09-07 14:28:44 +02:00
9a6eaddf8e Clean up garbage 2023-09-07 14:11:44 +02:00
2e6b2cf6bc Merge pull request #15 from FYS3150-G2-2023/coryab/change-latex-structure
Coryab/change latex structure
2023-09-07 14:07:13 +02:00
4cf07eb274 Put each problem into their own file 2023-09-07 14:05:27 +02:00
aee15203df Made some changes 2023-09-07 13:59:19 +02:00
b7195f8c17 Merge branch '7-solve-problem-7' of github.uio.no:FYS3150-G2-2023/Project-1 into 7-solve-problem-7 2023-09-04 12:35:56 +02:00
70a3da3624 Implemented build_array 2023-09-04 12:35:40 +02:00
Janita Willumsen
c26b788503 Implemented general algorithm 2023-09-04 12:27:14 +02:00
6cff40e678 Add functions 2023-09-04 11:29:25 +02:00
17 changed files with 336 additions and 103 deletions

View File

Binary file not shown.

View File

@@ -84,116 +84,22 @@
\textit{https://github.uio.no/FYS3150-G2-2023/Project-1} \textit{https://github.uio.no/FYS3150-G2-2023/Project-1}
\section*{Problem 1} \input{problems/problem1}
\begin{align*} \input{problems/problem2}
u(x) &= \int \int \frac{d^2 u}{dx^2} dx^2\\
&= \int \int -100 e^{-10x} dx^2 \\
&= \int \frac{-100 e^{-10x}}{-10} + c_1 dx \\
&= \int 10 e^{-10x} + c_1 dx \\
&= \frac{10 e^{-10x}}{-10} + c_1 x + c_2 \\
&= -e^{-10x} + c_1 x + c_2
\end{align*}
Using the boundary conditions, we can find $c_1$ and $c_2$ as shown below: \input{problems/problem3}
\begin{align*} \input{problems/problem4}
u(0) &= 0 \\
-e^{-10 \cdot 0} + c_1 \cdot 0 + c_2 &= 0 \\
-1 + c_2 &= 0 \\
c_2 &= 1
\end{align*}
\begin{align*} \input{problems/problem5}
u(1) &= 0 \\
-e^{-10 \cdot 1} + c_1 \cdot 1 + c_2 &= 0 \\
-e^{-10} + c_1 + c_2 &= 0 \\
c_1 &= e^{-10} - c_2\\
c_1 &= e^{-10} - 1\\
\end{align*}
Using the values that we found for $c_1$ and $c_2$, we get \input{problems/problem6}
\begin{align*} \input{problems/problem7}
u(x) &= -e^{-10x} + (e^{-10} - 1) x + 1 \\
&= 1 - (1 - e^{-10}) - e^{-10x}
\end{align*}
\section*{Problem 2} \input{problems/problem8}
% Write which .cpp/.hpp/.py (using a link?) files are relevant for this and show the plot generated. \input{problems/problem9}
\section*{Problem 3}
To derive the discretized version of the Poisson equation, we first need
the taylor expansion for $u(x)$ around $x + h$ and $x - h$.
\begin{align*}
u(x+h) &= u(x) + u'(x) h + \frac{1}{2} u''(x) h^2 + \frac{1}{6} u'''(x) h^3 + \mathcal{O}(h^4)
\end{align*}
\begin{align*}
u(x-h) &= u(x) - u'(x) h + \frac{1}{2} u''(x) h^2 - \frac{1}{6} u'''(x) h^3 + \mathcal{O}(h^4)
\end{align*}
If we add the equations above, we get this new equation:
\begin{align*}
u(x+h) + u(x-h) &= 2 u(x) + u''(x) h^2 + \mathcal{O}(h^4) \\
u(x+h) - 2 u(x) + u(x-h) + \mathcal{O}(h^4) &= u''(x) h^2 \\
u''(x) &= \frac{u(x+h) - 2 u(x) + u(x-h)}{h^2} + \mathcal{O}(h^2) \\
u_i''(x) &= \frac{u_{i+1} - 2 u_i + u_{i-1}}{h^2} + \mathcal{O}(h^2) \\
\end{align*}
We can then replace $\frac{d^2u}{dx^2}$ with the RHS (right-hand side) of the equation:
\begin{align*}
- \frac{d^2u}{dx^2} &= 100 e^{-10x} \\
\frac{ - u_{i+1} + 2 u_i - u_{i-1}}{h^2} + \mathcal{O}(h^2) &= 100 e^{-10x} \\
\end{align*}
And lastly, we leave out $\mathcal{O}(h^2)$ and change $u_i$ to $v_i$ to
differentiate between the exact solution and the approximate solution,
and get the discretized version of the equation:
\begin{align*}
align* \frac{ - u_{i+1} + 2 u_i - u_{i-1}}{h^2} &= 100 e^{-10x} \\
\end{align*}
\section*{Problem 4}
% Show that each iteration of the discretized version naturally creates a matrix equation.
\section*{Problem 5}
\subsection*{a)}
\subsection*{b)}
\section*{Problem 6}
\subsection*{a)}
% Use Gaussian elimination, and then use backwards substitution to solve the equation
\subsection*{b)}
% Figure it out
\section*{Problem 7}
% Link to relevant files on gh and possibly add some comments
\section*{Problem 8}
%link to relvant files and show plots
\section*{Problem 9}
% Show the algorithm, then calculate FLOPs, then link to relevant files
\section*{Problem 10}
% Time and show result, and link to relevant files
\end{document} \end{document}

View File

@@ -0,0 +1,35 @@
\section*{Problem 1}
% Do the double integral
\begin{align*}
u(x) &= \int \int \frac{d^2 u}{dx^2} dx^2\\
&= \int \int -100 e^{-10x} dx^2 \\
&= \int \frac{-100 e^{-10x}}{-10} + c_1 dx \\
&= \int 10 e^{-10x} + c_1 dx \\
&= \frac{10 e^{-10x}}{-10} + c_1 x + c_2 \\
&= -e^{-10x} + c_1 x + c_2
\end{align*}
Using the boundary conditions, we can find $c_1$ and $c_2$ as shown below:
\begin{align*}
u(0) &= 0 \\
-e^{-10 \cdot 0} + c_1 \cdot 0 + c_2 &= 0 \\
-1 + c_2 &= 0 \\
c_2 &= 1
\end{align*}
\begin{align*}
u(1) &= 0 \\
-e^{-10 \cdot 1} + c_1 \cdot 1 + c_2 &= 0 \\
-e^{-10} + c_1 + c_2 &= 0 \\
c_1 &= e^{-10} - c_2\\
c_1 &= e^{-10} - 1\\
\end{align*}
Using the values that we found for $c_1$ and $c_2$, we get
\begin{align*}
u(x) &= -e^{-10x} + (e^{-10} - 1) x + 1 \\
&= 1 - (1 - e^{-10}) - e^{-10x}
\end{align*}

View File

@@ -0,0 +1,3 @@
\section*{Problem 10}
% Time and show result, and link to relevant files

View File

@@ -0,0 +1,3 @@
\section*{Problem 2}
% Write which .cpp/.hpp/.py (using a link?) files are relevant for this and show the plot generated.

View File

@@ -0,0 +1,4 @@
\section*{Problem 3}
% Show how it's derived and where we found the derivation.

View File

@@ -0,0 +1,3 @@
\section*{Problem 4}
% Show that each iteration of the discretized version naturally creates a matrix equation.

View File

@@ -0,0 +1,6 @@
\section*{Problem 5}
\subsection*{a)}
\subsection*{b)}

View File

@@ -0,0 +1,9 @@
\section*{Problem 6}
\subsection*{a)}
% Use Gaussian elimination, and then use backwards substitution to solve the equation
\subsection*{b)}
% Figure it out

View File

@@ -0,0 +1,3 @@
\section*{Problem 7}
% Link to relevant files on gh and possibly add some comments

View File

@@ -0,0 +1,3 @@
\section*{Problem 8}
%link to relvant files and show plots

View File

@@ -0,0 +1,3 @@
\section*{Problem 9}
% Show the algorithm, then calculate FLOPs, then link to relevant files

52
src/analyticPlot.cpp Normal file
View File

@@ -0,0 +1,52 @@
#include <iostream>
#include <cmath>
#include <vector>
#include <string>
#include <numeric>
#include <fstream>
#include <iomanip>
double u(double x);
void generate_range(std::vector<double> &vec, double start, double stop, int n);
int main() {
int n = 1000;
std::vector<double> x(n), y(n);
generate_range(x, 0.0, 1.0, n);
// Set up output file and strem
std::string filename = "datapoints.txt";
std::ofstream outfile;
outfile.open(filename);
// Parameters for formatting
int width = 12;
int prec = 4;
// Calculate u(x) and write to file
for (int i = 0; i <= x.size(); i++) {
y[i] = u(x[i]);
outfile << std::setw(width) << std::setprecision(prec) << std::scientific << x[i]
<< std::setw(width) << std::setprecision(prec) << std::scientific << y[i]
<< std::endl;
}
outfile.close();
return 0;
};
double u(double x) {
double result;
result = 1 - (1 - exp(-10))*x - exp(-10*x);
return result;
};
void generate_range(std::vector<double> &vec, double start, double stop, int n) {
double step = (stop - start) / n;
for (int i = 0; i <= vec.size(); i++) {
vec[i] = i * step;
}
}

17
src/analyticPlot.py Normal file
View File

@@ -0,0 +1,17 @@
import numpy as np
import matplotlib.pyplot as plt
x = []
y = []
v = []
with open('testdata.txt') as f:
for line in f:
a, b, c = line.strip().split()
x.append(float(a))
# y.append(float(b))
v.append(float(c))
fig, ax = plt.subplots()
ax.plot(x, v)
plt.show()
# plt.savefig("main.png")

24
src/main.cpp Normal file
View File

@@ -0,0 +1,24 @@
#include "GeneralAlgorithm.hpp"
#include <armadillo>
#include <iostream>
double f(double x) {
return 100. * std::exp(-10.*x);
}
double a_sol(double x) {
return 1. - (1. - std::exp(-10)) * x - std::exp(-10*x);
}
int main() {
arma::mat A = arma::eye(3,3);
GeneralAlgorithm ga(3, &A, f, a_sol, 0., 1.);
ga.solve();
std::cout << "Time: " << ga.time(5) << std::endl;
ga.error();
return 0;
}

162
src/simpleFile.cpp Normal file
View File

@@ -0,0 +1,162 @@
#include <armadillo>
#include <cmath>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <ios>
#include <string>
#define TIMING_ITERATIONS 5
arma::vec* general_algorithm(
arma::vec* sub_diag,
arma::vec* main_diag,
arma::vec* sup_diag,
arma::vec* g_vec
)
{
int n = g_vec->n_elem;
double d;
for (int i = 1; i < n; i++) {
d = (*sub_diag)(i-1) / (*main_diag)(i-1);
(*main_diag)(i) -= d*(*sup_diag)(i-1);
(*g_vec)(i) -= d*(*g_vec)(i-1);
}
(*g_vec)(n-1) /= (*main_diag)(n-1);
for (int i = n-2; i >= 0; i--) {
(*g_vec)(i) = ((*g_vec)(i) - (*sup_diag)(i) * (*g_vec)(i+1)) / (*main_diag)(i);
}
return g_vec;
}
arma::vec* special_algorithm(
double sub_sig,
double main_sig,
double sup_sig,
arma::vec* g_vec
)
{
int n = g_vec->n_elem;
arma::vec diag = arma::vec(n);
for (int i = 1; i < n; i++) {
// Calculate values for main diagonal based on indices
diag(i-1) = (double)(i+1) / i;
(*g_vec)(i) += (*g_vec)(i-1) / diag(i-1);
}
// The last element in main diagonal has value (i+1)/i = (n+1)/n
(*g_vec)(n-1) /= (double)(n+1) / (n);
for (int i = n-2; i >= 0; i--) {
(*g_vec)(i) = ((*g_vec)(i) + (*g_vec)(i+1))/ diag(i);
}
return g_vec;
}
void error(
std::string filename,
arma::vec* x_vec,
arma::vec* v_vec,
arma::vec* a_vec
)
{
std::ofstream ofile;
ofile.open(filename);
if (!ofile.is_open()) {
exit(1);
}
for (int i=0; i < a_vec->n_elem; i++) {
double sub = (*a_vec)(i) - (*v_vec)(i);
ofile << std::setprecision(8) << std::scientific << (*x_vec)(i)
<< std::setprecision(8) << std::scientific << std::log10(std::abs(sub))
<< std::setprecision(8) << std::scientific << std::log10(std::abs(sub/(*a_vec)(i)))
<< std::endl;
}
ofile.close();
}
double f(double x) {
return 100*std::exp(-10*x);
}
void build_array(
int n_steps,
arma::vec* sub_diag,
arma::vec* main_diag,
arma::vec* sup_diag,
arma::vec* g_vec
)
{
sub_diag->resize(n_steps-2);
main_diag->resize(n_steps-1);
sup_diag->resize(n_steps-2);
sub_diag->fill(-1);
main_diag->fill(2);
sup_diag->fill(-1);
g_vec->resize(n_steps-1);
double step_size = 1./ (double) n_steps;
for (int i=0; i < n_steps-1; i++) {
(*g_vec)(i) = f((i+1)*step_size);
}
}
void timing() {
arma::vec sub_diag, main_diag, sup_diag, g_vec;
int n_steps;
std::ofstream ofile;
ofile.open("timing.txt");
// Timing
for (int i=1; i <= 8; i++) {
n_steps = std::pow(10, i);
clock_t g_1, g_2, s_1, s_2;
double g_res = 0, s_res = 0;
for (int j=0; j < TIMING_ITERATIONS; j++) {
build_array(n_steps, &sub_diag, &main_diag, &sup_diag, &g_vec);
g_1 = clock();
general_algorithm(&sub_diag, &main_diag, &sup_diag, &g_vec);
g_2 = clock();
g_res += (double) (g_2 - g_1) / CLOCKS_PER_SEC;
build_array(n_steps, &sub_diag, &main_diag, &sup_diag, &g_vec);
s_1 = clock();
special_algorithm(-1., 2., -1., &g_vec);
s_2 = clock();
s_res += (double) (s_2 - s_1) / CLOCKS_PER_SEC;
}
ofile
<< n_steps << ","
<< g_res / (double) TIMING_ITERATIONS << ","
<< s_res / (double) TIMING_ITERATIONS << std::endl;
}
ofile.close();
}
int main()
{
timing();
}