In [1]:
# input: number of iterations
n = 2
# input: equations in the form Ax = b
A = matrix([
[5, -3],
[4, -6]
])
b = vector([-2, 1])
# input: start vector
x0 = vector([1, 1])
#
Matrix Form der Gauß-Seidel Iteration:
In [2]:
L = matrix([[A[i,j] if j < i else 0
for j in range(A.dimensions()[1])]
for i in range(A.dimensions()[0])
])
R = matrix([[A[i,j] if j > i else 0
for j in range(A.dimensions()[1])]
for i in range(A.dimensions()[0])
])
D = matrix([[A[i,j] if j == i else 0
for j in range(A.dimensions()[1])]
for i in range(A.dimensions()[0])
])
show(LatexExpr("L = "), L, LatexExpr(r"\quad D = "), D, LatexExpr(r"\quad R = "), R)
Gauss-Seidel-Schritt: $$ x_{k+1} = T_{GS} \cdot x_k + (D + L)^{-1} b $$
In [3]:
# compute intermediate values
TGS = -(D + L)^-1 * R
DLb = (D+L)^-1 * b
show(TGS, DLb)
In [4]:
# perform n steps of the iteration, saving the result
x = [x0]
show(LatexExpr("x_0 = "), x0)
for i in range(n):
xk = TGS * x[-1] + DLb
x.append(xk)
show(LatexExpr("x_{} = ".format(i + 1)), xk)
In [5]:
show(LatexExpr(r"x_n \approx "), vector(RDF, x[-1]))
Aufteilen von $A$ in $$A = S - T$$ wobei $S$ eine linke untere Dreiecksmatrix ist
In [6]:
S = matrix([[A[i,j] if j <= i else 0
for j in range(A.dimensions()[1])]
for i in range(A.dimensions()[0])
])
T = matrix([[A[i,j] if j > i else 0
for j in range(A.dimensions()[1])]
for i in range(A.dimensions()[0])
])
assert A == S + T
show(S, T)
In [7]:
sit = S^-1 * T
show(sit)
Spekatralradius $\rho(S^{-1}T) = \max |\lambda_i|$
In [8]:
spektral_radius = lambda M: max(map(abs, M.eigenvalues()))
show(spektral_radius(sit))
Konvergenzrate $r = - \log_{10} \rho(S^{-1} T)$
In [9]:
r = - log(spektral_radius(sit), 10)
show(LatexExpr("r = "), r, LatexExpr(r"\approx"), RDF(r))
Exakte Lösung zum Vergleich
In [10]:
show(A^-1 * b)