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()