-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_g.py
172 lines (118 loc) · 4.16 KB
/
main_g.py
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
import numpy as np
import sympy as sm
from sympy.abc import x, y
import matplotlib.pyplot as plt
import scipy.special as sc
import pandas as pd
import matplotx
ode = lambda u: u.diff(x).diff(x) + u.diff(x) - (1/x)*u - (4*x**2 - x + 3/2)
px = 1
qx = -1/x
fx = 4*x**2 - x + 3/2
alpha0 = 1
alpha1 = 0
beta0 = 0
beta1 = 1
a = 0
b = 1
A = 0
B = 1
N = 2
def Galerkin(alpha0, alpha1, beta0, beta1, a, b, A, B, N):
"""
Galerkin method for boundary-value problem
"""
sys0_coef = np.array([
[alpha0, alpha0*a + alpha1],
[beta0, beta0*b + beta1]
])
sys0_right = np.array([
A,
B
])
phi0_coef = np.linalg.solve(sys0_coef, sys0_right)
phi0 = phi0_coef[0] + phi0_coef[1]*x
PHI = [phi0]
for i in range(1, N):
gamma_i = -(beta0*(b - a)**2 + (i + 1)*beta1*(b - a))/(beta0*(b - a) + i*beta1)
phi_i = gamma_i*(x - a)**i + (x - a)**(i + 1)
PHI.append(phi_i)
CONST = [1]
for i in range(1, N):
c_i = sm.Symbol(f'c{i}')
CONST.append(c_i)
A_MTX = np.zeros((N-1, N-1))
D_MTX = np.zeros(N-1)
for i in range(1, N):
for j in range(1, N):
A_MTX[i-1][j-1] = sm.simplify(
sm.lambdify([x], PHI[i]*PHI[j].diff(x))(b) - sm.lambdify([x], PHI[i]*PHI[j].diff(x))(a) - \
sm.integrate(PHI[j].diff(x)*PHI[i].diff(x), (x, a, b)) + \
sm.integrate(px*PHI[j].diff(x)*PHI[i], (x, a, b)) + \
sm.integrate(qx*PHI[j]*PHI[i], (x, a, b))
)
D_MTX[i-1] = sm.simplify(
sm.integrate((fx - PHI[0].diff(x).diff(x) - px*PHI[0].diff(x) - qx*PHI[0])*PHI[i], (x, a, b))
)
COEFS = np.linalg.solve(A_MTX, D_MTX)
# print(A_MTX)
# print(D_MTX)
# print(COEFS)
ANS = PHI[0]
for i in range(1, N):
ANS += COEFS[i-1]*PHI[i]
return sm.simplify(ANS)
def ans(x):
return 1/2 * (55 - 55*2.718281**(-x) - 13*x - 26*x**2 + 4*x**3 + 55*x*sc.expi(-1) - 55*x*sc.expi(-x) + 55*x*np.log(abs(-x)))
def test(FUNC, N, n_iter=10):
from datetime import datetime
start = datetime.now()
for i in range(n_iter):
FUNC(alpha0=alpha0, alpha1=alpha1, beta0=beta0, beta1=beta1, a=a, b=b, A=A, B=B, N=N)
end = datetime.now()
return end - start
if __name__ == "__main__":
ANS_2 = sm.lambdify([x], Galerkin(alpha0=alpha0, alpha1=alpha1, beta0=beta0, beta1=beta1, a=a, b=b, A=A, B=B, N=2))
ANS_3 = sm.lambdify([x], Galerkin(alpha0=alpha0, alpha1=alpha1, beta0=beta0, beta1=beta1, a=a, b=b, A=A, B=B, N=3))
ANS_4 = sm.lambdify([x], Galerkin(alpha0=alpha0, alpha1=alpha1, beta0=beta0, beta1=beta1, a=a, b=b, A=A, B=B, N=4))
# print(f'N = 2: {test(Galerkin, 2).microseconds/1000}')
# print(f'N = 3: {test(Galerkin, 3).microseconds/1000}')
# print(f'N = 4: {test(Galerkin, 4).microseconds/1000}')
x = np.linspace(a, b, 1000)
fig, ax = plt.subplots()
fig.canvas.manager.set_window_title('Galerkin')
plt.title('Galerkin')
plt.ylim(-0.31, 0.02)
plt.grid(1)
ax.plot(x, [ans(t) for t in x], color='black', label='1')
ax.plot(x, [ANS_2(t) for t in x], color='red', label='2')
ax.plot(x, [ANS_3(t) for t in x], color='green', label='3')
ax.plot(x, [ANS_4(t) for t in x], color='blue', label='4')
matplotx.line_labels()
ax.legend(loc='lower left', labels=[
'1 - Correct answer',
'2 - Galerkin 2',
'3 - Galerkin 3',
'4 - Galerkin 4'
])
plt.savefig('img/Galerkin2.png', dpi=600)
plt.show()
# DATA
x_val = np.linspace(a, b, 10)
y_val = ans(x_val)
y_2 = ANS_2(x_val)
y_3 = ANS_3(x_val)
y_4 = ANS_4(x_val)
df = pd.DataFrame({
'x_i': x_val,
'y_i': y_val,
'n = 2': y_2,
'delta2': y_val-y_2,
'n = 3': y_3,
'delta3': y_val-y_3,
'n = 4': y_4,
'delta4': y_val-y_4,
})
# print(df)
# df.to_excel('data/Galerkin.xlsx')
# df.to_csv('data/Galerkin.csv')