Método de Gauss-Seidel
Resolver el sistema de ecuaciones
$$
\begin{align*}
10 x_{1} - x_{2} + 2 x_{3} &= 6 \\
-x_{1} + 11 x_{2} - x_{3} + 3 x_{4} &= 25 \\
2 x_{1} - x_{2} + 10 x_{3} - x_{4} &= -11 \\
3 x_{2} - x_{3} + 8 x_{4} &= 15
\end{align*}
$$
Despejando \( x_{i} \)
$$
\begin{alignat*}{5}
x_{1}^{(k)} &= \cfrac{6}{10} & &+ \cfrac{1}{10} x_{2}^{(k-1)} &- \cfrac{2}{10} x_{3}^{(k-1)} & \\
x_{2}^{(k)} &= \cfrac{25}{11} &+ \cfrac{1}{11} x_{1}^{(k)} & &+ \cfrac{1}{11} x_{3}^{(k-1)} &- \cfrac{3}{11} x_{4}^{(k-1)} \\
x_{3}^{(k)} &= \cfrac{-11}{10} &- \cfrac{2}{10} x_{1}^{(k)} &+ \cfrac{1}{10} x_{2}^{(k)} & &+ \cfrac{1}{10} x_{4}^{(k-1)} \\
x_{4}^{(k)} &= \cfrac{15}{8} & &- \cfrac{3}{8} x_{2}^{(k)} &+ \cfrac{1}{8} x_{3}^{(k)} &
\end{alignat*}
$$
Iteración 0
$$
\begin{align*}
x_{1}^{(0)} &= \color{blue}{0} \\
x_{2}^{(0)} &= \color{blue}{0} \\
x_{3}^{(0)} &= \color{blue}{0} \\
x_{4}^{(0)} &= \color{blue}{0}
\end{align*}
$$
Iteración 1
$$
\begin{align*}
x_{1}^{(1)} &= \frac{6 + x_{2}^{(0)} - 2 x_{3}^{(0)}}{10} = \frac{6 + \color{blue}{0} - 2 (\color{blue}{0})}{10} = \color{green}{0.6} \\
x_{2}^{(1)} &= \frac{25 + x_{1}^{(1)} + x_{3}^{(0)} - 3 x_{4}^{(0)}}{11} = \frac{25 + \color{green}{0.6} + \color{blue}{0} - 3 (\color{blue}{0})}{11} = \color{green}{2.327273} \\
x_{3}^{(1)} &= \frac{-11 - 2 x_{1}^{(1)} + x_{2}^{(1)} + x_{4}^{(0)}}{10} = \frac{-11 - 2 (\color{green}{0.6}) + \color{green}{2.327273} + \color{blue}{0}}{10} = \color{green}{-0.987273} \\
x_{4}^{(1)} &= \frac{15 - 3 x_{2}^{(1)} + x_{3}^{(1)}}{8} = \frac{15 - 3 (\color{green}{2.327273}) + (\color{green}{-0.987273})}{8} = \color{green}{0.878864}
\end{align*}
$$
Iteración 2
$$
\begin{align*}
x_{1}^{(2)} &= \frac{6 + x_{2}^{(1)} - 2 x_{3}^{(1)}}{10} = \frac{6 + \color{green}{2.327273} - 2 (\color{green}{-0.987273})}{10} = \color{red}{1.030182} \\
x_{2}^{(2)} &= \frac{25 + x_{1}^{(2)} + x_{3}^{(1)} - 3 x_{4}^{(1)}}{11} = \frac{25 + \color{red}{1.030182} + (\color{green}{-0.987273}) - 3 (\color{green}{0.878864})}{11} = \color{red}{2.036938} \\
x_{3}^{(2)} &= \frac{-11 - 2 x_{1}^{(2)} + x_{2}^{(2)} + x_{4}^{(1)}}{10} = \frac{-11 - 2 (\color{red}{1.030182}) + \color{red}{2.036938} + \color{green}{0.878864}}{10} = \color{red}{-1.014456} \\
x_{4}^{(2)} &= \frac{15 - 3 x_{2}^{(2)} + x_{3}^{(2)}}{8} = \frac{15 - 3 (\color{red}{2.036938}) + (\color{red}{-1.014456})}{8} = \color{red}{0.984341}
\end{align*}
$$
Iteración 3
$$
\begin{align*}
x_{1}^{(3)} &= \frac{6 + x_{2}^{(2)} - 2 x_{3}^{(2)}}{10} = \frac{6 + \color{red}{2.036938} - 2 (\color{red}{-1.014456})}{10} = \color{fuchsia}{1.006585} \\
x_{2}^{(3)} &= \frac{25 + x_{1}^{(2)} + x_{3}^{(2)} - 3 x_{4}^{(2)}}{11} = \frac{25 + \color{fuchsia}{1.006585} + (\color{red}{-1.014456}) - 3 (\color{red}{0.984341})}{11} = \color{fuchsia}{2.003555} \\
x_{3}^{(3)} &= \frac{-11 - 2 x_{1}^{(2)} + x_{2}^{(2)} + x_{4}^{(2)}}{10} = \frac{-11 - 2 (\color{fuchsia}{1.006585}) + \color{fuchsia}{2.003555} + \color{red}{0.984341}}{10} = \color{fuchsia}{-1.002527} \\
x_{4}^{(3)} &= \frac{15 - 3 x_{2}^{(2)} + x_{3}^{(2)}}{8} = \frac{15 - 3 (\color{fuchsia}{2.003555}) + (\color{fuchsia}{-1.002527})}{8} = \color{fuchsia}{0.998351}
\end{align*}
$$
Patrón de cálculo
$$
\begin{align*}
a_{11} x_{1} + a_{12} x_{2} + a_{13} x_{3} + a_{14} x_{4} &= b_{1} \\
a_{21} x_{1} + a_{22} x_{2} + a_{23} x_{3} + a_{24} x_{4} &= b_{2} \\
a_{31} x_{1} + a_{32} x_{2} + a_{33} x_{3} + a_{34} x_{4} &= b_{3} \\
a_{41} x_{1} + a_{42} x_{2} + a_{43} x_{3} + a_{44} x_{4} &= b_{4}
\end{align*}
$$
Despejando \( x_{i} \)
$$
\begin{alignat*}{5}
x_{1} &= \cfrac{b_{1}}{a_{11}} & &- \cfrac{a_{12}}{a_{11}} x_{2} &- \cfrac{a_{13}}{a_{11}} x_{3} &- \cfrac{a_{14}}{a_{11}} x_{4} \\
x_{2} &= \cfrac{b_{2}}{a_{22}} &- \cfrac{a_{21}}{a_{22}} x_{1} & &- \cfrac{a_{23}}{a_{22}} x_{3} &- \cfrac{a_{24}}{a_{22}} x_{4} \\
x_{3} &= \cfrac{b_{3}}{a_{33}} &- \cfrac{a_{31}}{a_{33}} x_{1} &- \cfrac{a_{32}}{a_{33}} x_{2} & &- \cfrac{a_{34}}{a_{33}} x_{4} \\
x_{4} &= \cfrac{b_{4}}{a_{44}} &- \cfrac{a_{41}}{a_{44}} x_{1} &- \cfrac{a_{42}}{a_{44}} x_{2} &- \cfrac{a_{43}}{a_{44}} x_{3} &
\end{alignat*}
$$
Primer patrón
$$
\begin{alignat*}{5}
x_{1}^{(\color{blue}{1})} &= \cfrac{b_{1}}{a_{11}} & &- \cfrac{a_{12}}{a_{11}} x_{2}^{(\color{blue}{1} - 1)} &- \cfrac{a_{13}}{a_{11}} x_{3}^{(\color{blue}{1} - 1)} &- \cfrac{a_{14}}{a_{11}} x_{4}^{(\color{blue}{1} - 1)} \\
x_{2}^{(\color{blue}{1})} &= \cfrac{b_{2}}{a_{22}} &- \cfrac{a_{21}}{a_{22}} x_{1}^{(\color{blue}{1})} & &- \cfrac{a_{23}}{a_{22}} x_{3}^{(\color{blue}{1} - 1)} &- \cfrac{a_{24}}{a_{22}} x_{4}^{(\color{blue}{1} - 1)} \\
x_{3}^{(\color{blue}{1})} &= \cfrac{b_{3}}{a_{33}} &- \cfrac{a_{31}}{a_{33}} x_{1}^{(\color{blue}{1})} &- \cfrac{a_{32}}{a_{33}} x_{2}^{(\color{blue}{1})} & &- \cfrac{a_{34}}{a_{33}} x_{4}^{(\color{blue}{1} - 1)} \\
x_{4}^{(\color{blue}{1})} &= \cfrac{b_{4}}{a_{44}} &- \cfrac{a_{41}}{a_{44}} x_{1}^{(\color{blue}{1})} &- \cfrac{a_{42}}{a_{44}} x_{2}^{(\color{blue}{1})} &- \cfrac{a_{43}}{a_{44}} x_{3}^{(\color{blue}{1})} & \\
x_{1}^{(\color{green}{2})} &= \cfrac{b_{1}}{a_{11}} & &- \cfrac{a_{12}}{a_{11}} x_{2}^{(\color{green}{2} - 1)} &- \cfrac{a_{13}}{a_{11}} x_{3}^{(\color{green}{2} - 1)} &- \cfrac{a_{14}}{a_{11}} x_{4}^{(\color{green}{2} - 1)} \\
x_{2}^{(\color{green}{2})} &= \cfrac{b_{2}}{a_{22}} &- \cfrac{a_{21}}{a_{22}} x_{1}^{(\color{green}{2})} & &- \cfrac{a_{23}}{a_{22}} x_{3}^{(\color{green}{2} - 1)} &- \cfrac{a_{24}}{a_{22}} x_{4}^{(\color{green}{2} - 1)} \\
x_{3}^{(\color{green}{2})} &= \cfrac{b_{3}}{a_{33}} &- \cfrac{a_{31}}{a_{33}} x_{1}^{(\color{green}{2})} &- \cfrac{a_{32}}{a_{33}} x_{2}^{(\color{green}{2})} & &- \cfrac{a_{34}}{a_{33}} x_{4}^{(\color{green}{2} - 1)} \\
x_{4}^{(\color{green}{2})} &= \cfrac{b_{4}}{a_{44}} &- \cfrac{a_{41}}{a_{44}} x_{1}^{(\color{green}{2})} &- \cfrac{a_{42}}{a_{44}} x_{2}^{(\color{green}{2})} &- \cfrac{a_{43}}{a_{44}} x_{3}^{(\color{green}{2})} & \\
x_{1}^{(\color{red}{3})} &= \cfrac{b_{1}}{a_{11}} & &- \cfrac{a_{12}}{a_{11}} x_{2}^{(\color{red}{3} - 1)} &- \cfrac{a_{13}}{a_{11}} x_{3}^{(\color{red}{3} - 1)} &- \cfrac{a_{14}}{a_{11}} x_{4}^{(\color{red}{3} - 1)} \\
x_{2}^{(\color{red}{3})} &= \cfrac{b_{2}}{a_{22}} &- \cfrac{a_{21}}{a_{22}} x_{1}^{(\color{red}{3})} & &- \cfrac{a_{23}}{a_{22}} x_{3}^{(\color{red}{3} - 1)} &- \cfrac{a_{24}}{a_{22}} x_{4}^{(\color{red}{3} - 1)} \\
x_{3}^{(\color{red}{3})} &= \cfrac{b_{3}}{a_{33}} &- \cfrac{a_{31}}{a_{33}} x_{1}^{(\color{red}{3})} &- \cfrac{a_{32}}{a_{33}} x_{2}^{(\color{red}{3})} & &- \cfrac{a_{34}}{a_{33}} x_{4}^{(\color{red}{3} - 1)} \\
x_{4}^{(\color{red}{3})} &= \cfrac{b_{4}}{a_{44}} &- \cfrac{a_{41}}{a_{44}} x_{1}^{(\color{red}{3})} &- \cfrac{a_{42}}{a_{44}} x_{2}^{(\color{red}{3})} &- \cfrac{a_{43}}{a_{44}} x_{3}^{(\color{red}{3})} &
\end{alignat*}
$$
Lo anterior puede escribirse como
$$
\begin{alignat*}{5}
x_{1}^{(k)} &= \cfrac{b_{1}}{a_{11}} & &- \cfrac{a_{12}}{a_{11}} x_{2}^{(k-1)} &- \cfrac{a_{13}}{a_{11}} x_{3}^{(k-1)} &- \cfrac{a_{14}}{a_{11}} x_{4}^{(k-1)} \\
x_{2}^{(k)} &= \cfrac{b_{2}}{a_{22}} &- \cfrac{a_{21}}{a_{22}} x_{1}^{(k)} & &- \cfrac{a_{23}}{a_{22}} x_{3}^{(k-1)} &- \cfrac{a_{24}}{a_{22}} x_{4}^{(k-1)} \\
x_{3}^{(k)} &= \cfrac{b_{3}}{a_{33}} &- \cfrac{a_{31}}{a_{33}} x_{1}^{(k)} &- \cfrac{a_{32}}{a_{33}} x_{2}^{(k)} & &- \cfrac{a_{34}}{a_{33}} x_{4}^{(k-1)} \\
x_{4}^{(k)} &= \cfrac{b_{4}}{a_{44}} &- \cfrac{a_{41}}{a_{44}} x_{1}^{(k)} &- \cfrac{a_{42}}{a_{44}} x_{2}^{(k)} &- \cfrac{a_{43}}{a_{44}} x_{3}^{(k)} &
\end{alignat*}
$$
para \( k = 1, \dots, \)
Segundo patrón
$$
\begin{alignat*}{5}
x_{\color{blue}{1}}^{(k)} &= \cfrac{b_{\color{blue}{1}}}{a_{\color{blue}{11}}} & &- \cfrac{a_{\color{blue}{1}2}}{a_{\color{blue}{11}}} x_{2}^{(k-1)} &- \cfrac{a_{\color{blue}{1}3}}{a_{\color{blue}{11}}} x_{3}^{(k-1)} &- \cfrac{a_{\color{blue}{1}4}}{a_{\color{blue}{11}}} x_{4}^{(k-1)} \\
x_{\color{green}{2}}^{(k)} &= \cfrac{b_{\color{green}{2}}}{a_{\color{green}{22}}} &- \cfrac{a_{\color{green}{2}1}}{a_{\color{green}{22}}} x_{1}^{(k)} & &- \cfrac{a_{\color{green}{2}3}}{a_{\color{green}{22}}} x_{3}^{(k-1)} &- \cfrac{a_{\color{green}{2}4}}{a_{\color{green}{22}}} x_{4}^{(k-1)} \\
x_{\color{red}{3}}^{(k)} &= \cfrac{b_{\color{red}{3}}}{a_{\color{red}{33}}} &- \cfrac{a_{\color{red}{3}1}}{a_{\color{red}{33}}} x_{1}^{(k)} &- \cfrac{a_{\color{red}{3}2}}{a_{\color{red}{33}}} x_{2}^{(k)} & &- \cfrac{a_{\color{red}{3}4}}{a_{\color{red}{33}}} x_{4}^{(k-1)} \\
x_{\color{fuchsia}{4}}^{(k)} &= \cfrac{b_{\color{fuchsia}{4}}}{a_{\color{fuchsia}{44}}} &- \cfrac{a_{\color{fuchsia}{4}1}}{a_{\color{fuchsia}{44}}} x_{1}^{(k)} &- \cfrac{a_{\color{fuchsia}{4}2}}{a_{\color{fuchsia}{44}}} x_{2}^{(k)} &- \cfrac{a_{\color{fuchsia}{4}3}}{a_{\color{fuchsia}{44}}} x_{3}^{(k)} &
\end{alignat*}
$$
Lo anterior puede ser escrito como
$$
\begin{equation*}
x_{i}^{(k)} = \frac{1}{a_{ii}} \biggl( b_{i} - \sum a_{i?} x_{?}^{(k)} - \sum a_{i?} x_{?}^{(k-1)} \biggr)
\end{equation*}
$$
para \( i = 1, \dots, m \)
Tercer patrón
$$
\begin{alignat*}{5}
x_{1}^{(k)} &= \cfrac{b_{1}}{a_{11}} & &- \cfrac{a_{1\color{green}{2}}}{a_{11}} x_{\color{green}{2}}^{(k-1)} &- \cfrac{a_{1\color{red}{3}}}{a_{11}} x_{\color{red}{3}}^{(k-1)} &- \cfrac{a_{1\color{fuchsia}{4}}}{a_{11}} x_{\color{fuchsia}{4}}^{(k-1)} \\
x_{2}^{(k)} &= \cfrac{b_{2}}{a_{22}} &- \cfrac{a_{2\color{blue}{1}}}{a_{22}} x_{\color{blue}{1}}^{(k)} & &- \cfrac{a_{2\color{red}{3}}}{a_{22}} x_{\color{red}{3}}^{(k-1)} &- \cfrac{a_{2\color{fuchsia}{4}}}{a_{22}} x_{\color{fuchsia}{4}}^{(k-1)} \\
x_{3}^{(k)} &= \cfrac{b_{3}}{a_{33}} &- \cfrac{a_{3\color{blue}{1}}}{a_{33}} x_{\color{blue}{1}}^{(k)} &- \cfrac{a_{3\color{green}{2}}}{a_{33}} x_{\color{green}{2}}^{(k)} & &- \cfrac{a_{3\color{fuchsia}{4}}}{a_{33}} x_{\color{fuchsia}{4}}^{(k-1)} \\
x_{4}^{(k)} &= \cfrac{b_{4}}{a_{44}} &- \cfrac{a_{4\color{blue}{1}}}{a_{44}} x_{\color{blue}{1}}^{(k)} &- \cfrac{a_{4\color{green}{2}}}{a_{44}} x_{\color{green}{2}}^{(k)} &- \cfrac{a_{4\color{red}{3}}}{a_{44}} x_{\color{red}{3}}^{(k)} &
\end{alignat*}
$$
Lo anterior puede ser escrito como
$$
\begin{equation*}
x_{i}^{(k)} = \frac{1}{a_{ii}} \biggl( b_{i} - \sum_{j = 1}^{i-1} a_{ij} x_{j}^{(k)} - \sum_{j = i+1}^{n} a_{ij} x_{j}^{(k-1)} \biggr)
\end{equation*}
$$
para \( j = 1, \dots, n \)
Fórmula matemática
$$
\begin{align*}
k &= 1, \dots \\
& \quad i = 1, \dots, m \\
& \quad \quad x_{i}^{(k)} = \frac{1}{a_{ii}} \biggl( b_{i} - \sum_{j = 1}^{i-1} a_{ij} x_{j}^{(k)} - \sum_{j = i+1}^{n} a_{ij} x_{j}^{(k-1)} \biggr)
\end{align*}
$$
Seudocódigo
function gauss_seidel(A, b)
m, n = tamaño(A)
x_actual = [0,...,0]
for k=1 to iteraciones do
for i=1 to m do
x_anterior = x_actual
sumatoria = b(i)
for j=1 to i-1 do
sumatoria = sumatoria - A(i,j)*x_actual(j)
end for
for j=i+1 to n do
sumatoria = sumatoria - A(i,j)*x_anterior(j)
end for
x_actual(i) = sumatoria/A(i,i)
end for
end for
return x
end function
Implementación
import numpy as np
#versión simple
#
#def metodo_gauss_seidel(A,b):
# m, n = A.shape
# x_actual = np.zeros(m)
# for k in range(0,10):
# for i in range(0,m):
# x_anterior = np.copy(x_actual)
# sumatoria = b[i]
# for j in range(0,i):
# sumatoria = sumatoria - A[i,j]*x_actual[j]
# for j in range(i+1,n):
# sumatoria = sumatoria - A[i,j]*x_anterior[j]
# x_actual[i] = sumatoria/A[i,i]
# print(x_actual)
def metodo_gauss_seidel(A,b):
m, n = A.shape
x_actual = np.zeros(m)
error_permitido = 0.000001
print("{0:s} \t {1:9s} \t {2:9s} \t {3:9s} \t {4:9s} \t {5:9s}".format('k', 'x(1)', 'x(2)', 'x(3)', 'x(4)', 'error relativo %'))
print("{0:d} \t {1:10.9f} \t {2:10.9f} \t {3:10.9f} \t {4:10.9f} \t {5:9s}".format(0, x_actual[0], x_actual[1], x_actual[2], x_actual[3], '??????????????'))
for k in range(0,30):
for i in range(0,m):
x_anterior = np.copy(x_actual)
sumatoria = b[i]
for j in range(0,i):
sumatoria = sumatoria - A[i,j]*x_actual[j]
for j in range(i+1,n):
sumatoria = sumatoria - A[i,j]*x_anterior[j]
x_actual[i] = sumatoria/A[i,i]
error_relativo = np.linalg.norm(((x_actual - x_anterior)/x_actual)*100)
print("{0:d} \t {1:10.9f} \t {2:10.9f} \t {3:10.9f} \t {4:10.9f} \t {5:14.10f}".format(k+1, x_actual[0], x_actual[1], x_actual[2], x_actual[3], error_relativo))
if error_relativo < error_permitido:
break
#versión in-place
#
#def metodo_gauss_seidel(A, b):
# m, n = A.shape
# x = np.zeros(n)
# for k in range(0,10):
# for i in range(0,m):
# sumatoria = 0
# for j in range(0,n):
# if j != i:
# sumatoria = sumatoria + A[i,j]*x[j]
# x[i] = (b[i] - sumatoria)/A[i,i]
# print(x)
A = np.array([[10,-1,2,0],
[-1,11,-1,3],
[2,-1,10,-1],
[0,3,-1,8]])
B = np.array([6,25,-11,15])
k x(1) x(2) x(3) x(4) error relativo %
0 0.000000000 0.000000000 0.000000000 0.000000000 ??????????????
1 0.600000000 2.327272727 -0.987272727 0.878863636 100.0000000000
2 1.030181818 2.036938017 -1.014456198 0.984341219 10.7155507265
3 1.006585041 2.003555017 -1.002527385 0.998350946 1.4032867531
4 1.000860979 2.000298251 -1.000307276 0.999849746 0.1499026148
5 1.000091280 2.000021342 -1.000031147 0.999988103 0.0138358415
6 1.000008364 2.000001173 -1.000002745 0.999999217 0.0011113614
7 1.000000666 2.000000025 -1.000000209 0.999999965 0.0000747767
8 1.000000044 1.999999995 -1.000000013 1.000000000 0.0000035754
9 1.000000002 1.999999999 -1.000000000 1.000000000 0.0000000036