Skip to content

Commit 8a9addd

Browse files
refactor
1 parent bf6faee commit 8a9addd

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

src/fdm_heat_eq_explicit_1D.py

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import math
22
import numpy
3-
import matplotlib.pyplot as plt
3+
4+
from src.utils import visu_1D
45

56
"""
67
Original version was in Matlab. See [1] for further details.
@@ -9,23 +10,6 @@
910
"""
1011

1112

12-
def plot_solution(x, wn):
13-
"""
14-
15-
:param x:
16-
:param wn:
17-
:return:
18-
"""
19-
xx = numpy.linspace(0, 1, 1000)
20-
plt.plot(xx, numpy.exp(-time / 4 * math.pi ** 2) * numpy.sin(2 * math.pi * xx), label="exact solution")
21-
plt.plot(x, wn, label="approximate solution")
22-
plt.xlabel("x")
23-
plt.ylabel("u")
24-
plt.title("Heat eq")
25-
plt.legend()
26-
plt.show()
27-
28-
2913
if __name__ == "__main__":
3014
# FDM, forward difference, explicit scheme for the heat equation:
3115
# INPUT
@@ -44,5 +28,6 @@ def plot_solution(x, wn):
4428
wn[1:M + 1] = wn[1:M + 1] + mu * (wn[2:M + 2] - 2 * wn[1:M + 1] + wn[0:M])
4529
time += Dt
4630

47-
plot_solution(x, wn)
31+
w = numpy.exp(-time / 4 * math.pi ** 2) * numpy.sin(2 * math.pi * x)
32+
visu_1D.plot_solution(x, wn, w)
4833

src/utils/__init__.py

Whitespace-only changes.

src/utils/visu_1D.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import matplotlib.pyplot as plt
2+
3+
4+
def plot_solution(grid, approximate_sol, exact_sol, title="Heat eq"):
5+
"""
6+
7+
:param grid:
8+
:param approximate_sol:
9+
:param exact_sol:
10+
:param title:
11+
:return:
12+
"""
13+
plt.plot(grid, exact_sol, label="exact solution")
14+
plt.plot(grid, approximate_sol, label="approximate solution")
15+
plt.xlabel("x")
16+
plt.ylabel("u")
17+
plt.title(title)
18+
plt.legend()
19+
plt.show()

0 commit comments

Comments
 (0)