Merge pull request #18 from FYS3150-G2-2023/coryab/implement-problem-2
Coryab/implement problem 2
This commit is contained in:
commit
3cfee4ebdc
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}
|
||||||
|
|||||||
@ -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
|
||||||
|
|
||||||
x = []
|
def main():
|
||||||
y = []
|
FILENAME = "analytical_solution.pdf"
|
||||||
v = []
|
x = []
|
||||||
with open('testdata.txt') as f:
|
v = []
|
||||||
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()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user