Método de Gauss-Jacobi

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-1)} & &+ \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-1)} &+ \cfrac{1}{10} x_{2}^{(k-1)} & &+ \cfrac{1}{10} x_{4}^{(k-1)} \\ x_{4}^{(k)} &= \cfrac{15}{8} & &- \cfrac{3}{8} x_{2}^{(k-1)} &+ \cfrac{1}{8} x_{3}^{(k-1)} & \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}^{(0)} + x_{3}^{(0)} - 3 x_{4}^{(0)}}{11} = \frac{25 + \color{blue}{0} + \color{blue}{0} - 3 (\color{blue}{0})}{11} = \color{green}{2.272727} \\ x_{3}^{(1)} &= \frac{-11 - 2 x_{1}^{(0)} + x_{2}^{(0)} + x_{4}^{(0)}}{10} = \frac{-11 - 2 (\color{blue}{0}) + \color{blue}{0} + \color{blue}{0}}{10} = \color{green}{-1.1} \\ x_{4}^{(1)} &= \frac{15 - 3 x_{2}^{(0)} + x_{3}^{(0)}}{8} = \frac{15 - 3 (\color{blue}{0}) + \color{blue}{0}}{8} = \color{green}{1.875} \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.272727} - 2 (\color{green}{-1.1})}{10} = \color{red}{1.047273} \\ x_{2}^{(2)} &= \frac{25 + x_{1}^{(1)} + x_{3}^{(1)} - 3 x_{4}^{(1)}}{11} = \frac{25 + \color{green}{0.6} + (\color{green}{- 1.1}) - 3 (\color{green}{1.875})}{11} = \color{red}{1.715909} \\ x_{3}^{(2)} &= \frac{-11 - 2 x_{1}^{(1)} + x_{2}^{(1)} + x_{4}^{(1)}}{10} = \frac{-11 - 2 (\color{green}{0.6}) + \color{green}{2.272727} + \color{green}{1.875}}{10} = \color{red}{-0.805227} \\ x_{4}^{(2)} &= \frac{15 - 3 x_{2}^{(1)} + x_{3}^{(1)}}{8} = \frac{15 - 3 (\color{green}{2.272727}) + (\color{green}{- 1.1})}{8} = \color{red}{0.885227} \end{align*} $$

Iteración 3

$$ \begin{align*} x_{1}^{(3)} &= \frac{6 + x_{2}^{(2)} - 2 x_{3}^{(2)}}{10} = \frac{6 + \color{red}{1.715909} - 2 (\color{red}{-0.805227})}{10} = \color{fuchsia}{0.932636} \\ x_{2}^{(3)} &= \frac{25 + x_{1}^{(2)} + x_{3}^{(2)} - 3 x_{4}^{(2)}}{11} = \frac{25 + \color{red}{1.047273} + (\color{red}{-0.805227}) - 3 (\color{red}{0.885227})}{11} = \color{fuchsia}{2.053306} \\ x_{3}^{(3)} &= \frac{-11 - 2 x_{1}^{(2)} + x_{2}^{(2)} + x_{4}^{(2)}}{10} = \frac{-11 - 2 (\color{red}{1.047273}) + \color{red}{1.715909} + \color{red}{0.885227}}{10} = \color{fuchsia}{-1.049341} \\ x_{4}^{(3)} &= \frac{15 - 3 x_{2}^{(2)} + x_{3}^{(2)}}{8} = \frac{15 - 3 (\color{red}{1.715909}) + (\color{red}{-0.805227})}{8} = \color{fuchsia}{1.130881} \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} - 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} - 1)} &- \cfrac{a_{32}}{a_{33}} x_{2}^{(\color{blue}{1} - 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} - 1)} &- \cfrac{a_{42}}{a_{44}} x_{2}^{(\color{blue}{1} - 1)} &- \cfrac{a_{43}}{a_{44}} x_{3}^{(\color{blue}{1} - 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} - 1)} & &- \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} - 1)} &- \cfrac{a_{32}}{a_{33}} x_{2}^{(\color{green}{2} - 1)} & &- \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} - 1)} &- \cfrac{a_{42}}{a_{44}} x_{2}^{(\color{green}{2} - 1)} &- \cfrac{a_{43}}{a_{44}} x_{3}^{(\color{green}{2} - 1)} & \\ 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} - 1)} & &- \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} - 1)} &- \cfrac{a_{32}}{a_{33}} x_{2}^{(\color{red}{3} - 1)} & &- \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} - 1)} &- \cfrac{a_{42}}{a_{44}} x_{2}^{(\color{red}{3} - 1)} &- \cfrac{a_{43}}{a_{44}} x_{3}^{(\color{red}{3} - 1)} & \end{alignat*} $$

Lo anterior puede ser escrito 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-1)} & &- \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-1)} &- \cfrac{a_{32}}{a_{33}} x_{2}^{(k-1)} & &- \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-1)} &- \cfrac{a_{42}}{a_{44}} x_{2}^{(k-1)} &- \cfrac{a_{43}}{a_{44}} x_{3}^{(k-1)} & \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-1)} & &- \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-1)} &- \cfrac{a_{\color{red}{3}2}}{a_{\color{red}{33}}} x_{2}^{(k-1)} & &- \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-1)} &- \cfrac{a_{\color{fuchsia}{4}2}}{a_{\color{fuchsia}{44}}} x_{2}^{(k-1)} &- \cfrac{a_{\color{fuchsia}{4}3}}{a_{\color{fuchsia}{44}}} x_{3}^{(k-1)} & \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-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-1)} & &- \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-1)} &- \cfrac{a_{3\color{green}{2}}}{a_{33}} x_{\color{green}{2}}^{(k-1)} & &- \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-1)} &- \cfrac{a_{4\color{green}{2}}}{a_{44}} x_{\color{green}{2}}^{(k-1)} &- \cfrac{a_{4\color{red}{3}}}{a_{44}} x_{\color{red}{3}}^{(k-1)} & \end{alignat*} $$

Lo anterior puede ser escrito como $$ \begin{equation*} x_{i}^{(k)} = \frac{1}{a_{ii}} \biggl( b_{i} - \sum_{\substack{j = 1 \\ j \neq i}}^{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_{\substack{j = 1 \\ j \neq i}}^{n} a_{ij} x_{j}^{(k-1)} \biggr) \end{align*} $$

Seudocódigo

function metodo_gauss_jacobi(A,b)
    m, n = tamaño(A)
    iteraciones = ...
    x_actual = [0,...,0]
    for k=1 to iteraciones do
        x_anterior = x_actual
        for i=1 to m do
            sumatoria = 0
            for j=1 to n do
                if (j != i) then
                    sumatoria = sumatoria + A(i,j)*x_anterior(j)
                end if
            end for
            x_actual(i) = (b(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_jacobi(A, b):
#    m, n = A.shape
#    x_actual = np.zeros(n)
#    for k in range(0,10):
#        x_anterior = np.copy(x_actual)
#        for i in range(0,m):
#            sumatoria = b[i]
#            for j in range(0,n):
#                if j != i:
#                    sumatoria = sumatoria - A[i,j]*x_anterior[j]
#            x_actual[i] = sumatoria/A[i,i]
#        print(x_actual)

def metodo_gauss_jacobi(A, b):
    m, n = A.shape
    x_actual = np.zeros(n)
    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):
        x_anterior = np.copy(x_actual)
        for i in range(0,m):
            sumatoria = b[i]
            for j in range(0,n):
                if j != i:
                    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 matricial
#
#def metodo_gauss_jacobi(A, b):
#    m, n = A.shape
#    T = np.copy(A)
#    diagonal = np.diag_indices(m)
#    T[diagonal] = 0
#    x_actual = np.zeros(n)
#    for k in range(0,10):
#        x_anterior = np.copy(x_actual)
#        x_actual = (b - np.dot(T, x_anterior))/np.diag(A)
#        print(x_actual)

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])

metodo_gauss_jacobi(A,B)

    k 	 x(1)      	 x(2)      	  x(3)      	 x(4)      	 error relativo %
    0 	 0.000000000 	 0.000000000 	  0.000000000 	 0.000000000 	 ??????????????
    1 	 0.600000000 	 2.272727273 	 -1.100000000 	 1.875000000 	 200.0000000000
    2 	 1.047272727 	 1.715909091 	 -0.805227273 	 0.885227273 	 129.3004487833
    3 	 0.932636364 	 2.053305785 	 -1.049340909 	 1.130880682 	  37.8700582539
    4 	 1.015198760 	 1.953695764 	 -0.968108626 	 0.973842717 	  20.5566463252
    5 	 0.988991302 	 2.011414726 	 -1.010285904 	 1.021350510 	   7.3703118056
    6 	 1.003198653 	 1.992241261 	 -0.994521737 	 0.994433740 	   3.5736350152
    7 	 0.998128473 	 2.002306882 	 -1.001972231 	 1.003594310 	   1.3772496526
    8 	 1.000625134 	 1.998670301 	 -0.999035576 	 0.998888391 	   0.6353873367
    9 	 0.999674145 	 2.000447672 	 -1.000369158 	 1.000619190 	   0.2542335085
    10 	 1.000118599 	 1.999767947 	 -0.999828143 	 0.999785978 	   0.1140334089
    11 	 0.999942423 	 2.000084775 	 -1.000068327 	 1.000108502 	   0.0466705097
    12 	 1.000022143 	 1.999958963 	 -0.999969157 	 0.999959669 	   0.0205671464
    13 	 0.999989728 	 2.000015816 	 -1.000012565 	 1.000019244 	   0.0085394468
    14 	 1.000004095 	 1.999992675 	 -0.999994439 	 0.999992498 	   0.0037204984
    15 	 0.999998155 	 2.000002924 	 -1.000002302 	 1.000003442 	   0.0015591888
    16 	 1.000000753 	 1.999998684 	 -0.999998995 	 0.999998616 	   0.0006742738
    17 	 0.999999667 	 2.000000537 	 -1.000000421 	 1.000000619 	   0.0002842911
    18 	 1.000000138 	 1.999999763 	 -0.999999818 	 0.999999746 	   0.0001223459
    19 	 0.999999940 	 2.000000098 	 -1.000000077 	 1.000000112 	   0.0000517881
    20 	 1.000000025 	 1.999999957 	 -0.999999967 	 0.999999954 	   0.0000222166
    21 	 0.999999989 	 2.000000018 	 -1.000000014 	 1.000000020 	   0.0000094283
    22 	 1.000000005 	 1.999999992 	 -0.999999994 	 0.999999992 	   0.0000040363
    23 	 0.999999998 	 2.000000003 	 -1.000000003 	 1.000000004 	   0.0000017158
    24 	 1.000000001 	 1.999999999 	 -0.999999999 	 0.999999998 	   0.0000007335

l = [22, 13, 45, 50, 98, 69, 43, 44, 1]

[[x+1,x+5][x >= 45] for x in l]

    [23, 14, 50, 55, 103, 74, 44, 45, 2]

[x+5 if x >= 45 else x+1 for x in l]

    [23, 14, 50, 55, 103, 74, 44, 45, 2]

[x+1 for x in l if x >= 45]

    [46, 51, 99, 70]