20 Commits

Author SHA1 Message Date
6e12f2b050 Edit latex file 2023-09-08 13:13:10 +02:00
c5c64da196 Implement the program for problem 2 2023-09-08 12:46:21 +02:00
c256d9b1da Add rules 2023-09-08 12:45:55 +02:00
3d87b400d8 Modify gitignore 2023-09-08 12:45:25 +02:00
d886d3761e Merge pull request #17 from FYS3150-G2-2023/coryab/edit-problem-1
Coryab/edit problem 1
2023-09-08 12:11:27 +02:00
4e633d13b5 Merge branch 'main' into coryab/edit-problem-1 2023-09-08 12:11:12 +02:00
400e8a29df Edit problem 1 and add problem 3 2023-09-08 12:08:02 +02:00
996b99fca3 Modify .gitignore 2023-09-08 12:07:06 +02:00
Janita Willumsen
271e3dd944 Made changes to preamble affecting warning of revtex and use of amsmath 2023-09-08 09:41:58 +02:00
Janita Willumsen
c42426847d Finished exercise 4 2023-09-08 09:39:53 +02:00
Janita Willumsen
13cbbc4e0e Add build file extensions, included .bbl 2023-09-08 09:36:40 +02:00
e407ce073e Add Makefile 2023-09-07 14:39:35 +02:00
f2f50ed3e4 Merge branch 'main' of github.uio.no:FYS3150-G2-2023/Project-1 2023-09-07 14:39:06 +02:00
91cc7583af Add problem 3 2023-09-07 14:38:46 +02:00
6f5d71e1a4 Merge pull request #16 from FYS3150-G2-2023/7-solve-problem-7
7 solve problem 7
2023-09-07 14:36:25 +02:00
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
13 changed files with 217 additions and 7 deletions

8
.gitignore vendored
View File

@@ -36,3 +36,11 @@
*.log
*.out
*.bib
*.synctex.gz
*.bbl
# C++ specifics
src/*
!src/Makefile
!src/*.cpp
!src/*.py

View File

Binary file not shown.

View File

@@ -1,7 +1,9 @@
\documentclass[english,notitlepage]{revtex4-1} % defines the basic parameters of the document
%For preview: skriv i terminal: latexmk -pdf -pvc filnavn
% Silence warning of revtex4-1
\usepackage{silence}
\WarningFilter{revtex4-1}{Repair the float}
% if you want a single-column, remove reprint
@@ -13,7 +15,7 @@
%% I recommend downloading TeXMaker, because it includes a large library of the most common packages.
\usepackage{physics,amssymb} % mathematical symbols (physics imports amsmath)
\include{amsmath}
\usepackage{amsmath}
\usepackage{graphicx} % include graphics such as plots
\usepackage{xcolor} % set colors
\usepackage{hyperref} % automagic cross-referencing (this is GODLIKE)
@@ -72,6 +74,9 @@
%%
%% Don't ask me why, I don't know.
% custom stuff
\graphicspath{{./images/}}
\begin{document}
\title{Project 1} % self-explanatory

Binary file not shown.

View File

@@ -1,8 +1,17 @@
\section*{Problem 1}
First, we rearrange the equation.
\begin{align*}
- \frac{d^2u}{dx^2} &= 100 e^{-10x} \\
\frac{d^2u}{dx^2} &= -100 e^{-10x} \\
\end{align*}
Now we find $u(x)$.
% Do the double integral
\begin{align*}
u(x) &= \int \int \frac{d^2 u}{dx^2} dx^2\\
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 \\
@@ -10,7 +19,7 @@
&= -e^{-10x} + c_1 x + c_2
\end{align*}
Using the boundary conditions, we can find $c_1$ and $c_2$ as shown below:
Using the boundary conditions, we can find $c_1$ and $c_2$
\begin{align*}
u(0) &= 0 \\

View File

@@ -1,3 +1,11 @@
\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}

View File

@@ -1,4 +1,37 @@
\section*{Problem 3}
% Show how it's derived and where we found the derivation.
To derive the discretized version of the Poisson equation, we first need
the Taylor expansion for $u(x)$ around $x$ for $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} &= f(x) \\
\frac{ - u_{i+1} + 2 u_i - u_{i-1}}{h^2} + \mathcal{O}(h^2) &= f_i \\
\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*}
\frac{ - v_{i+1} + 2 v_i - v_{i-1}}{h^2} &= 100 e^{-10x_i} \\
\end{align*}

View File

@@ -1,3 +1,44 @@
\section*{Problem 4}
% 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
\begin{align*}
- v_{0} + 2 v_{1} - v_{2} &= h^{2} \cdot f_{1} \\
- v_{1} + 2 v_{2} - v_{3} &= h^{2} \cdot f_{2} \\
\vdots & \\
- v_{m-2} + 2 v_{m-1} - v_{m} &= h^{2} \cdot f_{m-1} \\
\end{align*}
Rearranging the first and last equation, moving terms of known boundary values to the RHS
\begin{align*}
2 v_{1} - v_{2} &= h^{2} \cdot f_{1} + v_{0} \\
- v_{1} + 2 v_{2} - v_{3} &= h^{2} \cdot f_{2} \\
\vdots & \\
- v_{m-2} + 2 v_{m-1} &= h^{2} \cdot f_{m-1} + v_{m} \\
\end{align*}
We now have a number of linear eqations, corresponding to the number of unknown values, which can be represented as an augmented matrix
\begin{align*}
\left[
\begin{matrix}
2v_{1} & -v_{2} & 0 & \dots & 0 \\
-v_{1} & 2v_{2} & -v_{3} & 0 & \\
0 & -v_{2} & 2v_{3} & -v_{4} & \\
\vdots & & & \ddots & \vdots \\
0 & & & -v_{m-2} & 2v_{m-1} \\
\end{matrix}
\left|
\,
\begin{matrix}
g_{1} \\
g_{2} \\
g_{2} \\
\vdots \\
g_{m-1} \\
\end{matrix}
\right.
\right]
\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$.

17
src/Makefile Normal file
View File

@@ -0,0 +1,17 @@
CC=g++
.PHONY: clean
all: simpleFile analyticPlot
simpleFile: simpleFile.o
$(CC) -o $@ $^
analyticPlot: analyticPlot.o
$(CC) -o $@ $^
%.o: %.cpp
$(CC) -c $< -o $@
clean:
rm *.o

55
src/analyticPlot.cpp Normal file
View File

@@ -0,0 +1,55 @@
#include <iostream>
#include <cmath>
#include <vector>
#include <string>
#include <numeric>
#include <fstream>
#include <iomanip>
#define RANGE 1000
#define FILENAME "analytical_solution.txt"
double u(double x);
void generate_range(std::vector<double> &vec, double start, double stop, int n);
void write_analytical_solution(std::string filename, int n);
int main() {
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);
generate_range(x, 0.0, 1.0, n);
// Set up output file and strem
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();
}

19
src/analyticPlot.py Normal file
View File

@@ -0,0 +1,19 @@
import numpy as np
import matplotlib.pyplot as plt
def main():
FILENAME = "analytical_solution.pdf"
x = []
v = []
with open('analytical_solution.txt') as f:
for line in f:
a, b = line.strip().split()
x.append(float(a))
v.append(float(b))
plt.plot(x, v)
plt.savefig(FILENAME)
if __name__ == "__main__":
main()

View File

@@ -15,7 +15,7 @@ arma::vec* general_algorithm(
arma::vec* g_vec
)
{
int n = main_diag->n_elem;
int n = g_vec->n_elem;
double d;
for (int i = 1; i < n; i++) {
@@ -40,6 +40,21 @@ arma::vec* special_algorithm(
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;
}