Compare commits
10 Commits
coryab/edi
...
6-solve-pr
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4919f488d4 | ||
|
|
1ab0a1a490 | ||
|
|
fc492b5cbf | ||
|
|
8ebd561f0d | ||
| 3cfee4ebdc | |||
| 6e12f2b050 | |||
| c5c64da196 | |||
| c256d9b1da | |||
| 3d87b400d8 | |||
| d886d3761e |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -41,4 +41,6 @@
|
|||||||
|
|
||||||
# C++ specifics
|
# C++ specifics
|
||||||
src/*
|
src/*
|
||||||
|
!src/Makefile
|
||||||
!src/*.cpp
|
!src/*.cpp
|
||||||
|
!src/*.py
|
||||||
|
|||||||
Binary file not shown.
@@ -74,6 +74,9 @@
|
|||||||
%%
|
%%
|
||||||
%% Don't ask me why, I don't know.
|
%% Don't ask me why, I don't know.
|
||||||
|
|
||||||
|
% custom stuff
|
||||||
|
\graphicspath{{./images/}}
|
||||||
|
|
||||||
\begin{document}
|
\begin{document}
|
||||||
|
|
||||||
\title{Project 1} % self-explanatory
|
\title{Project 1} % self-explanatory
|
||||||
|
|||||||
BIN
latex/images/analytical_solution.pdf
Normal file
BIN
latex/images/analytical_solution.pdf
Normal file
Binary file not shown.
@@ -1,3 +1,11 @@
|
|||||||
\section*{Problem 2}
|
\section*{Problem 2}
|
||||||
|
|
||||||
% Write which .cpp/.hpp/.py (using a link?) files are relevant for this and show the plot generated.
|
The code for generating the points and plotting them can be found under.
|
||||||
|
|
||||||
|
Point generator code: https://github.uio.no/FYS3150-G2-203/Project-1/blob/main/src/analyticPlot.cpp
|
||||||
|
|
||||||
|
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)$.
|
||||||
|
|
||||||
|
\includegraphics[scale=.5]{analytical_solution.pdf}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
% Show that each iteration of the discretized version naturally creates a matrix equation.
|
% Show that each iteration of the discretized version naturally creates a matrix equation.
|
||||||
|
|
||||||
The value of $u(x_{0})$ and $u(x_{1})$ is known, using the discretized equation we can approximate the value of $f(x_{i}) = f_{i}$. This will result in a set of equations
|
The value of $u(x_{0})$ and $u(x_{1})$ is known, using the discretized equation we solve for $\vec{v}$. This will result in a set of equations
|
||||||
\begin{align*}
|
\begin{align*}
|
||||||
- v_{0} + 2 v_{1} - v_{2} &= h^{2} \cdot f_{1} \\
|
- v_{0} + 2 v_{1} - v_{2} &= h^{2} \cdot f_{1} \\
|
||||||
- v_{1} + 2 v_{2} - v_{3} &= h^{2} \cdot f_{2} \\
|
- v_{1} + 2 v_{2} - v_{3} &= h^{2} \cdot f_{2} \\
|
||||||
@@ -10,7 +10,7 @@ The value of $u(x_{0})$ and $u(x_{1})$ is known, using the discretized equation
|
|||||||
- v_{m-2} + 2 v_{m-1} - v_{m} &= h^{2} \cdot f_{m-1} \\
|
- v_{m-2} + 2 v_{m-1} - v_{m} &= h^{2} \cdot f_{m-1} \\
|
||||||
\end{align*}
|
\end{align*}
|
||||||
|
|
||||||
Rearranging the first and last equation, moving terms of known boundary values to the RHS
|
where $v_{i} = v(x_{i})$ and $f_{i} = f(x_{i})$. Rearranging the first and last equation, moving terms of known boundary values to the RHS
|
||||||
\begin{align*}
|
\begin{align*}
|
||||||
2 v_{1} - v_{2} &= h^{2} \cdot f_{1} + v_{0} \\
|
2 v_{1} - v_{2} &= h^{2} \cdot f_{1} + v_{0} \\
|
||||||
- v_{1} + 2 v_{2} - v_{3} &= h^{2} \cdot f_{2} \\
|
- v_{1} + 2 v_{2} - v_{3} &= h^{2} \cdot f_{2} \\
|
||||||
@@ -40,5 +40,5 @@ We now have a number of linear eqations, corresponding to the number of unknown
|
|||||||
\right.
|
\right.
|
||||||
\right]
|
\right]
|
||||||
\end{align*}
|
\end{align*}
|
||||||
where $g_{i} = h^{2} f_{i}$. An augmented matrix can be represented as $\boldsymbol{A} \vec{x} = \vec{b}$. In this case $\boldsymbol{A}$ is the coefficient matrix with a tridiagonal signature $(-1, 2, -1)$ and dimension $n \cross n$, where $n=m-2$.
|
Since the boundary values are equal to $0$ the RHS can be renamed $g_{i} = h^{2} f_{i}$ for all $i$. An augmented matrix can be represented as $\boldsymbol{A} \vec{x} = \vec{b}$. In this case $\boldsymbol{A}$ is the coefficient matrix with a tridiagonal signature $(-1, 2, -1)$ and dimension $n \cross n$, where $n=m-2$.
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,46 @@
|
|||||||
\section*{Problem 6}
|
\section*{Problem 6}
|
||||||
|
|
||||||
\subsection*{a)}
|
\subsection*{a)}
|
||||||
|
|
||||||
% Use Gaussian elimination, and then use backwards substitution to solve the equation
|
% Use Gaussian elimination, and then use backwards substitution to solve the equation
|
||||||
|
Renaming the sub-, main-, and supdiagonal of matrix $\boldsymbol{A}$
|
||||||
|
\begin{align*}
|
||||||
|
\vec{a} &= [a_{2}, a_{3}, ..., a_{n-1}, a_{n}] \\
|
||||||
|
\vec{b} &= [b_{1}, b_{2}, b_{3}, ..., b_{n-1}, b_{n}] \\
|
||||||
|
\vec{c} &= [c_{1}, c_{2}, c_{3}, ..., c_{n-1}] \\
|
||||||
|
\end{align*}
|
||||||
|
|
||||||
|
Following Thomas algorithm for gaussian elimination, we first perform a forward sweep followed by a backward sweep to obtain $\vec{v}$
|
||||||
|
\begin{algorithm}[H]
|
||||||
|
\caption{General algorithm}\label{algo:general}
|
||||||
|
\begin{algorithmic}
|
||||||
|
\Procedure{Forward sweep}{$\vec{a}$, $\vec{b}$, $\vec{c}$}
|
||||||
|
\State $n \leftarrow$ length of $\vec{b}$
|
||||||
|
\State $\vec{\hat{b}}$, $\vec{\hat{g}} \leftarrow$ vectors of length $n$.
|
||||||
|
\State $\hat{b}_{1} \leftarrow b_{1}$ \Comment{Handle first element in main diagonal outside loop}
|
||||||
|
\For{$i = 2, 3, ..., n$}
|
||||||
|
\State $d \leftarrow \frac{a_{i}}{\hat{b}_{i-1}}$ \Comment{Calculating common expression}
|
||||||
|
\State $\hat{b}_{i} \leftarrow b_{i} - d \cdot c_{i-1}$
|
||||||
|
\State $\hat{g}_{i} \leftarrow g_{i} - d \cdot \hat{g}_{i-1}$
|
||||||
|
\EndFor
|
||||||
|
\Return $\vec{\hat{b}}$, $\vec{\hat{g}}$
|
||||||
|
\EndProcedure
|
||||||
|
|
||||||
|
\Procedure{Backward sweep}{$\vec{\hat{b}}$, $\vec{\hat{g}}$}
|
||||||
|
\State $n \leftarrow$ length of $\vec{\hat{b}}$
|
||||||
|
\State $\vec{v} \leftarrow$ vector of length $n$.
|
||||||
|
\State $v_{n} \leftarrow \frac{\hat{g}_{n}}{\hat{b}_{n}}$
|
||||||
|
\For{$i = n-1, n-2, ..., 1$}
|
||||||
|
\State $v_{i} \leftarrow \frac{\hat{g}_{i} - c_{i} \cdot v_{i+1}}{\hat{b}_{i}}$
|
||||||
|
\EndFor
|
||||||
|
\Return $\vec{v}$
|
||||||
|
\EndProcedure
|
||||||
|
\end{algorithmic}
|
||||||
|
\end{algorithm}
|
||||||
|
|
||||||
|
|
||||||
\subsection*{b)}
|
\subsection*{b)}
|
||||||
|
% Figure out FLOPs
|
||||||
% Figure it out
|
Counting the number of FLOPs for the general algorithm by looking at one procedure at a time.
|
||||||
|
For every iteration of i in forward sweep we have 1 division, 2 multiplications, and 2 subtractions, resulting in $5(n-1)$ FLOPs.
|
||||||
|
For backward sweep we have 1 division, and for every iteration of i we have 1 subtraction, 1 multiplication, and 1 division, resulting in $3(n-1)+1$ FLOPs.
|
||||||
|
Total FLOPs for the general algorithm is $8(n-1)+1$.
|
||||||
@@ -1,10 +1,15 @@
|
|||||||
CC=g++
|
CC=g++
|
||||||
|
|
||||||
all: simpleFile
|
.PHONY: clean
|
||||||
|
|
||||||
|
all: simpleFile analyticPlot
|
||||||
|
|
||||||
simpleFile: simpleFile.o
|
simpleFile: simpleFile.o
|
||||||
$(CC) -o $@ $^
|
$(CC) -o $@ $^
|
||||||
|
|
||||||
|
analyticPlot: analyticPlot.o
|
||||||
|
$(CC) -o $@ $^
|
||||||
|
|
||||||
%.o: %.cpp
|
%.o: %.cpp
|
||||||
$(CC) -c $< -o $@
|
$(CC) -c $< -o $@
|
||||||
|
|
||||||
|
|||||||
@@ -6,17 +6,36 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
|
||||||
|
#define RANGE 1000
|
||||||
|
#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);
|
||||||
|
void write_analytical_solution(std::string filename, int n);
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int n = 1000;
|
write_analytical_solution(FILENAME, RANGE);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
double u(double x) {
|
||||||
|
return 1 - (1 - exp(-10))*x - exp(-10*x);
|
||||||
|
};
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void write_analytical_solution(std::string filename, int n) {
|
||||||
std::vector<double> x(n), y(n);
|
std::vector<double> x(n), y(n);
|
||||||
generate_range(x, 0.0, 1.0, n);
|
generate_range(x, 0.0, 1.0, n);
|
||||||
|
|
||||||
// Set up output file and strem
|
// Set up output file and strem
|
||||||
std::string filename = "datapoints.txt";
|
|
||||||
std::ofstream outfile;
|
std::ofstream outfile;
|
||||||
outfile.open(filename);
|
outfile.open(filename);
|
||||||
|
|
||||||
@@ -32,21 +51,5 @@ int main() {
|
|||||||
<< std::endl;
|
<< std::endl;
|
||||||
}
|
}
|
||||||
outfile.close();
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +1,19 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
def main():
|
||||||
|
FILENAME = "analytical_solution.pdf"
|
||||||
x = []
|
x = []
|
||||||
y = []
|
|
||||||
v = []
|
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()
|
with open('analytical_solution.txt') as f:
|
||||||
ax.plot(x, v)
|
for line in f:
|
||||||
plt.show()
|
a, b = line.strip().split()
|
||||||
# plt.savefig("main.png")
|
x.append(float(a))
|
||||||
|
v.append(float(b))
|
||||||
|
|
||||||
|
plt.plot(x, v)
|
||||||
|
plt.savefig(FILENAME)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user