-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid_method.py
More file actions
175 lines (137 loc) · 4.63 KB
/
grid_method.py
File metadata and controls
175 lines (137 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import numpy as np
import matplotlib.pyplot as plt
from typing import Callable
import pandas as pd
import sys
class GridMethod:
def __init__(self, a: float, m: int, n: int, v: float,
alpha_t: Callable[[float], float],
y_x: Callable[[float], float],
f_x_t: Callable[[float, float], float]):
self.m = m
self.n = n
self.v = v
self.h = 1 / m
self.tau = (v * self.h) / a
self.time_b = n * self.tau
self.x_interval = np.arange(0, 1 + self.h, self.h)
self.t_interval = np.arange(0, self.time_b + self.tau, self.tau)
self.alpha_t = alpha_t
self.y_x = y_x
self.f_x_t = f_x_t
def solve(self):
y_sol = np.zeros((self.n + 1, self.m + 1))
# borders
for i in range(self.m + 1):
x = self.x_interval[i]
y_sol[0][i] = self.y_x(x)
for i in range(self.n + 1):
t = self.t_interval[i]
y_sol[i][0] = self.alpha_t(t)
# calculating solution
for i in range(self.n):
for j in range(1, self.m + 1):
t = self.t_interval[i]
x = self.x_interval[j]
y_sol[i + 1][j] = (
self.v * y_sol[i][j - 1]
+ (1 - self.v) * y_sol[i][j]
+ self.tau * self.f_x_t(x, t)
)
return y_sol
if __name__ == '__main__':
def alpha_t(t: float):
return 1 - 2 * t
def y_x(x: float):
return x + np.exp(x)
def f_x_t(x: float, t: float):
return 2 * np.exp(x)
def actual_solution(x: float, t: float):
return x - 2 * t + np.exp(x)
a = 2
m_arr = [100, 200, 400]
n_arr = [40, 80, 160]
v_arr = [0.8, 0.8, 0.8]
points = [10, 20, 40]
fig, ax = plt.subplots(2, figsize=(10, 10))
# overwriting the files
sys.stdout = open('data/grid_method_1.txt', 'w')
print()
sys.stdout = open('data/grid_method_2.txt', 'w')
print()
# first plot
for i in range(len(m_arr)):
solver = GridMethod(
a, m_arr[i], n_arr[i], v_arr[i], alpha_t, y_x, f_x_t
)
y_grid_method = solver.solve()
y_actual = np.zeros((solver.n + 1, solver.m + 1))
x_arr = np.arange(0, 1 + 0.1, 0.1)
for j in range(solver.n + 1):
for k in range(solver.m + 1):
x = solver.x_interval[k]
t = solver.t_interval[j]
y_actual[j][k] = actual_solution(x, t)
y_grid = y_grid_method[-1:, ::solver.m // 10][0]
y_act = y_actual[-1:, ::solver.m // 10][0]
# printing results
df = pd.DataFrame({
'x': x_arr,
'y_numerical': y_grid,
'y_actual': y_act,
'diff': [a - b for a, b in zip(y_act, y_grid)]
})
sys.stdout = open('data/grid_method_1.txt', 'a')
print(f'M={m_arr[i]}, N={n_arr[i]}, mu={v_arr[i]}')
print(df, '\n')
ax[0].plot(
solver.x_interval, y_grid_method[-1, :],
label=f'M={m_arr[i]}, N={n_arr[i]}, mu={v_arr[i]}'
)
ax[0].plot(
solver.x_interval, y_actual[-1, :],
label='Actual solution'
)
# second plot
m_arr = [100, 100, 100]
n_arr = [40, 80, 20]
v_arr = [0.8, 0.4, 1.6]
for i in range(len(m_arr)):
solver = GridMethod(
a, m_arr[i], n_arr[i], v_arr[i], alpha_t, y_x, f_x_t
)
y_grid_method = solver.solve()
y_actual = np.zeros((solver.n + 1, solver.m + 1))
x_arr = np.arange(0, 1 + 0.1, 0.1)
for j in range(solver.n + 1):
for k in range(solver.m + 1):
x = solver.x_interval[k]
t = solver.t_interval[j]
y_actual[j][k] = actual_solution(x, t)
y_grid = y_grid_method[-1:, ::solver.m // 10][0]
y_act = y_actual[-1:, ::solver.m // 10][0]
# printing results
df = pd.DataFrame({
'x': x_arr,
'y_numerical': y_grid,
'y_actual': y_act,
'diff': [a - b for a, b in zip(y_act, y_grid)]
})
sys.stdout = open('data/grid_method_2.txt', 'a')
print(f'M={m_arr[i]}, N={n_arr[i]}, mu={v_arr[i]}')
print(df, '\n')
ax[1].plot(
solver.x_interval, y_grid_method[-1, :],
label=f'M={m_arr[i]}, N={n_arr[i]}, mu={v_arr[i]}'
)
ax[1].plot(
solver.x_interval, y_actual[-1, :],
label='Actual solution'
)
ax[0].legend()
ax[1].legend()
ax[0].set_title('Task 1')
ax[1].set_title('Task 2')
ax[0].grid()
ax[1].grid()
plt.show()