diff --git a/.gitignore b/.gitignore index 79efed8..a82fcc1 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,9 @@ *.bib *.synctex.gz *.bbl + +# C++ specifics +src/* +!src/Makefile +!src/*.cpp +!src/*.py diff --git a/latex/assignment_1.pdf b/latex/assignment_1.pdf deleted file mode 100644 index f2b8dea..0000000 Binary files a/latex/assignment_1.pdf and /dev/null differ diff --git a/latex/assignment_1.tex b/latex/assignment_1.tex index 45f77c5..b4f8fa9 100644 --- a/latex/assignment_1.tex +++ b/latex/assignment_1.tex @@ -74,6 +74,9 @@ %% %% Don't ask me why, I don't know. +% custom stuff +\graphicspath{{./images/}} + \begin{document} \title{Project 1} % self-explanatory diff --git a/latex/images/analytical_solution.pdf b/latex/images/analytical_solution.pdf new file mode 100644 index 0000000..f874a22 Binary files /dev/null and b/latex/images/analytical_solution.pdf differ diff --git a/latex/problems/problem1.tex b/latex/problems/problem1.tex index 779a58f..8f53c11 100644 --- a/latex/problems/problem1.tex +++ b/latex/problems/problem1.tex @@ -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 \\ diff --git a/latex/problems/problem2.tex b/latex/problems/problem2.tex index b90aa58..bfad89c 100644 --- a/latex/problems/problem2.tex +++ b/latex/problems/problem2.tex @@ -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} diff --git a/latex/problems/problem3.tex b/latex/problems/problem3.tex index 2d7ab8b..da9188e 100644 --- a/latex/problems/problem3.tex +++ b/latex/problems/problem3.tex @@ -2,7 +2,7 @@ \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$. +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) @@ -24,8 +24,8 @@ If we add the equations above, we get this new equation: 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} \\ + - \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 @@ -33,5 +33,5 @@ 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} \\ + \frac{ - v_{i+1} + 2 v_i - v_{i-1}}{h^2} &= 100 e^{-10x_i} \\ \end{align*} diff --git a/src/Makefile b/src/Makefile index 1121395..1e668e6 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,10 +1,15 @@ CC=g++ -all: simpleFile +.PHONY: clean + +all: simpleFile analyticPlot simpleFile: simpleFile.o $(CC) -o $@ $^ +analyticPlot: analyticPlot.o + $(CC) -o $@ $^ + %.o: %.cpp $(CC) -c $< -o $@ diff --git a/src/analyticPlot.cpp b/src/analyticPlot.cpp index 1b364c5..07c932a 100644 --- a/src/analyticPlot.cpp +++ b/src/analyticPlot.cpp @@ -6,17 +6,36 @@ #include #include +#define RANGE 1000 +#define FILENAME "analytical_solution.txt" + double u(double x); void generate_range(std::vector &vec, double start, double stop, int n); +void write_analytical_solution(std::string filename, int n); 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 &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 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); @@ -32,21 +51,5 @@ int main() { << 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 &vec, double start, double stop, int n) { - double step = (stop - start) / n; - - for (int i = 0; i <= vec.size(); i++) { - vec[i] = i * step; - } -} \ No newline at end of file diff --git a/src/analyticPlot.py b/src/analyticPlot.py index d21d188..ff12ef5 100644 --- a/src/analyticPlot.py +++ b/src/analyticPlot.py @@ -1,17 +1,19 @@ 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)) +def main(): + FILENAME = "analytical_solution.pdf" + x = [] + v = [] -fig, ax = plt.subplots() -ax.plot(x, v) -plt.show() -# plt.savefig("main.png") \ No newline at end of file + 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()