forked from luca-heltai/numerical-analysis-2021-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
346 lines (237 loc) · 7.13 KB
/
main.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import numpy as np
from matplotlib import pyplot as plt
'''
Implement a function that given the domain interval, the forcing function, the number of discretization points,
the boundary conditions, returns the matrix and the the right hand side b.
'''
def finDif(omega, f, n, bc):
span = omega[ -1 ] - omega[ 0 ]
delta = span / ( n - 1 )
# A's diagonals
diags = [
30 * np.ones( (n,) ),
-16 * np.ones( (n - 1,) ),
np.ones( (n - 2,) )
]
A = np.diag( diags[ 0 ], 0 ) \
+ np.diag( diags[ 1 ], -1 ) \
+ np.diag( diags[ 1 ], 1 ) \
+ np.diag( diags[ 2 ], -2 ) \
+ np.diag( diags[ 2 ], 2 )
A /= (12 * delta**2)
x = np.linspace( omega[ 0 ], omega[ -1 ], n )
b = f( x )
# boundary conditions
A[ 0, : ] = 0
A[ :, 0 ] = 0
A[ 0, 0 ] = 1
b[ 0 ] = bc[ 0 ]
A[ -1, : ] = 0
A[ :, -1 ] = 0
A[ -1, -1 ] = 1
b[ -1 ] = bc[ -1 ]
return (A, b)
omega = [ 0, np.pi ]
f = lambda x : np.sin(x)
n = 100
bc = [ 0, 0 ]
(A, b) = finDif( omega, f, n, bc )
print(A)
print(b)
'''
Implement two functions that compute the LU and the Cholesky factorization of the system matrix A
'''
def LU(A, tol=1e-15):
A = A.copy()
size = len( A )
for k in range(size - 1):
pivot = A[ k, k ]
if abs(pivot) < tol:
raise RuntimeError("Null pivot")
for j in range( k + 1, size ):
A[ j, k ] /= pivot
for j in range( k + 1, size ):
A[ k + 1 : size, j ] -= A[ k + 1 : size, k ] * A[ k, j ]
L = np.tril( A )
for i in range( size ):
L[ i, i ] = 1.0
U = np.triu( A )
return (L, U)
(L, U) = LU( A )
print(L, U)
def cholesky(A):
A = A.copy()
size = len( A )
for k in range( size - 1 ):
A[ k, k ] = np.sqrt(A[ k, k ])
A[ k + 1 : size, k ] = A[ k + 1 : size, k ] / A[ k, k ]
for j in range(k+1,size):
A[ j : size, j ] = A[ j : size, j ] - A[ j : size, k ] * A[ j, k ]
A[ -1, -1 ] = np.sqrt(A[ -1, -1 ])
L = np.tril( A )
Lt = L.transpose()
return (L, Lt)
(Ht, H) = cholesky( A )
print(Ht, H)
# Implement forward and backward substitution functions to exploit the developed factorization methods to solve the
# derived linear system of equations.
def L_solve(L, rhs):
size = len( L )
x = np.zeros( size )
x[ 0 ] = rhs[ 0 ] / L[ 0, 0 ]
for i in range( 1, size ):
x[ i ] = ( rhs[ i ] - np.dot( L[ i, 0 : i ], x[ 0 : i ] ) ) / L[ i, i ]
return x
def U_solve(U, rhs):
size = len( U )
x = np.zeros( size )
x[ -1 ] = rhs[ -1 ] / L[ -1, -1 ]
for i in reversed( range( size - 1 ) ):
x[ i ] = ( rhs[ i ] - np.dot( U[ i, i + 1 : size ], x[ i + 1 : size ] ) ) / U[ i, i ]
return x
'''
Solve the derived linear system using the implemented functions and plot the computed solution:
'''
(fig, ax) = plt.subplots()
# exact solution
x = np.linspace( omega[0], omega[-1], n)
u_exact = np.sin( x )
ax.plot( x, u_exact, label = 'exact' )
# using LU factorization
w = L_solve( L, b )
u = U_solve( U, w )
ax.plot( x, u, label = 'lu' )
# using cholesky factorizations
w = L_solve( Ht, b )
u = U_solve( H, w )
ax.plot( x, u, label = 'cholesky' )
ax.legend()
ax.grid()
fig.savefig( 'plot1.svg' )
'''
Considering the new domain [0, 1] and the forcing term with B.C. ,
on produce a plot and a table where you show the decay of the error w.r.t. the number of grid points.
(The analytical solution for the above problems is )
'''
def compute_errors(omega, f, fex, bc, npoints):
errors = []
for idx in range( len(npoints) ):
npts = npoints[ idx ]
x = np.linspace( omega[0], omega[1], npts )
ex = fex( x )
( A, b ) = finDif( omega, f, npts, bc )
( L, U ) = LU( A )
w = L_solve( L, b )
u = U_solve( U, w )
err = sum( ( ex - u )**2 )**0.5
errors.append( err )
return errors
omega = [ 0, 1 ]
def func(x):
return x * (1 - x)
def fex(x):
return x**4/12 - x**3/6 + x/12
bc = [ 0, 0 ]
npoints = np.arange( 10, 310, 10 )
errors = compute_errors( omega, func, fex, bc, npoints )
(fig, ax) = plt.subplots()
ax.set_yscale( 'log' )
ax.plot( npoints, errors )
fig.savefig( 'plot2.svg' )
'''
Exploit the derived LU factorizations to compute the condition number of the system's matrix using the original problem formulation.
'''
def PM(A, z0, tol=1e-12, nmax=10000):
q = z0 / np.linalg.norm( z0, 2 )
it = 0
err = tol + 1
while it < nmax and err > tol:
z = A.dot( q )
l = q.T.dot( z )
err = np.linalg.norm( z - l*q, 2 )
q = z / np.linalg.norm( z, 2 )
it = it + 1
return (l, q)
def IPM(A, x0, mu, eps=1.0e-12, nmax=10000):
M = A - mu * np.eye(len(A))
(L, U) = LU( M )
q = x0 / np.linalg.norm( x0, 2 )
err = eps + 1.0
it = 0
while err > eps and it < nmax:
y = L_solve( L, q )
x = U_solve( U, y )
q = x / np.linalg.norm( x, 2 )
z = A.dot( q )
l = q.T.dot( z )
err = np.linalg.norm( z - l*q, 2 )
it = it + 1
return (l, q)
def condNumb(A):
z0 = np.ones( ( len(A), ))
lmax = PM( A, z0 )[ 0 ]
lmin = IPM( A, z0, 0.0 )[ 0 ]
condNum = lmax / lmin
return condNum
condNum = condNumb( A )
print( condNum )
'''
Implement a preconditioned Conjugant Gradient method to solve the original linear system of equations using an iterative method:
'''
def conjugate_gradient(A, b, P, nmax=len(A), eps=1e-10):
x = np.zeros_like( b )
r = b - A.dot( x )
rho0 = 1
p0 = np.zeros_like( b )
err = eps + 1.0
it = 1
while it < nmax and err > eps:
z = np.linalg.solve( P, r )
rho = r.dot( z )
if it > 1:
beta = rho / rho0
p = z + beta * p0
else:
p = z
q = A.dot( p )
alpha = rho / p.dot( q )
x += p * alpha
r -= q * alpha
p0 = p
rho0 = rho
err = np.linalg.norm( r, 2 )
it = it + 1
print( f'iterations: {it}' )
print( f'error: {err}' )
return x
omega = [ 0, np.pi ]
x = np.linspace( omega[ 0 ], omega[ -1 ], n )
ex = np.sin( x )
u = conjugate_gradient( A, b, np.diag(np.diag( A ) ))
(fig, ax) = plt.subplots()
ax.plot( x, ex, label = 'exact' )
ax.plot( x, u, label = 'CG' )
ax.legend()
fig.savefig( 'plot3.svg' )
exit( 0 )
'''
Consider the following time dependent variation of the PDE starting from the orginal problem formulation:
for , with and
Use the same finite difference scheme to derive the semi-discrete formulation and solve it using a forward Euler's method.
Plot the time dependent solution solution at , ,
'''
#TODO
'''
Given the original system, implement an algorithm to compute the eigenvalues and eigenvectors of the matrix .
oExploit the computed LU factorization
'''
#TODO
'''
Compute the inverse of the matrix A exploiting the derived LU factorization
'''
#TODO
'''
Consider the following Cauchy problem
Implement a Backward Euler's method in a suitable function and solve the resulting non-linear equation using a Newton's method.
'''
#TODO