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*}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*}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,$
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$
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$
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
In [1]:
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)
In [2]:
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])
In [3]:
metodo_gauss_jacobi(A,B)
In [4]:
l = [22, 13, 45, 50, 98, 69, 43, 44, 1]
In [5]:
[[x+1,x+5][x >= 45] for x in l]
Out[5]:
In [6]:
[x+5 if x >= 45 else x+1 for x in l]
Out[6]:
In [7]:
[x+1 for x in l if x >= 45]
Out[7]: