Transformar el sistema de ecuaciones en un sistema triangular superior
\begin{equation*} \begin{bmatrix} 3 & -0.1 & -0.2 \\ 0.1 & 7 & -0.3 \\ 0.3 & -0.2 & 10 \end{bmatrix} \begin{bmatrix} x_{1} \\ x_{2} \\ x_{3} \end{bmatrix} = \begin{bmatrix} 7.85 \\ -19.3 \\ 71.4 \end{bmatrix} \end{equation*}Primera fila pivote
\begin{equation*} \begin{bmatrix} 3 & -0.1 & -0.2 \\ 0.1 - \frac{0.1}{3} (3) & 7 - \frac{0.1}{3} (-0.1) & -0.3 - \frac{0.1}{3} (-0.2) \\ 0.3 - \frac{0.3}{3} (3) & -0.2 - \frac{0.3}{3} (-0.1) & 10 - \frac{0.3}{3} (-0.2) \end{bmatrix} \begin{bmatrix} x_{1} \\ x_{2} \\ x_{3} \end{bmatrix} = \begin{bmatrix} 7.85 \\ -19.3 - \frac{0.1}{3} (7.85) \\ 71.4 - \frac{0.3}{3} (7.85) \end{bmatrix} \end{equation*}Simplificando
\begin{equation*} \begin{bmatrix} 3 & -0.1 & -0.2 \\ 0 & 7.003333 & -0.293333 \\ 0 & -0.19 & 10.02 \end{bmatrix} \begin{bmatrix} x_{1} \\ x_{2} \\ x_{3} \end{bmatrix} = \begin{bmatrix} 7.85 \\ -19.561667 \\ 70.615 \end{bmatrix} \end{equation*}Segunda fila pivote
\begin{equation*} \begin{bmatrix} 3 & -0.1 & -0.2 \\ 0 & 7.003333 & -0.293333 \\ 0 & -0.19 - \frac{-0.19}{7.003333} (7.003333) & 10.02 - \frac{-0.19}{7.003333} (-0.293333) \end{bmatrix} \begin{bmatrix} x_{1} \\ x_{2} \\ x_{3} \end{bmatrix} = \begin{bmatrix} 7.85 \\ -19.561667 \\ 70.615 - \frac{-0.19}{7.003333} (-19.561667) \end{bmatrix} \end{equation*}Simplificando
\begin{equation*} \begin{bmatrix} 3 & -0.1 & -0.2 \\ 0 & 7.003333 & -0.293333 \\ 0 & 0 & 10.011204 \end{bmatrix} \begin{bmatrix} x_{1} \\ x_{2} \\ x_{3} \end{bmatrix} = \begin{bmatrix} 7.85 \\ -19.561667 \\ 70.084293 \end{bmatrix} \end{equation*}Expandiendo y agrupando términos
\begin{align*} a_{11} x_{1} + a_{12} x_{2} + a_{13} x_{3} + a_{14} x_{4} &= b_{1} \\ \bigg( a_{21} - \frac{a_{21}}{a_{11}} a_{11} \bigg) x_{1} + \bigg( a_{22} - \frac{a_{21}}{a_{11}} a_{12} \bigg) x_{2} + \bigg( a_{23} - \frac{a_{21}}{a_{11}} a_{13} \bigg) x_{3} + \bigg( a_{24} - \frac{a_{21}}{a_{11}} a_{14} \bigg) x_{4} &= b_{2} - \frac{a_{21}}{a_{11}} b_{1} \\ \bigg( a_{31} - \frac{a_{31}}{a_{11}} a_{11} \bigg) x_{1} + \bigg( a_{32} - \frac{a_{31}}{a_{11}} a_{12} \bigg) x_{2} + \bigg( a_{33} - \frac{a_{21}}{a_{11}} a_{13} \bigg) x_{3} + \bigg( a_{34} - \frac{a_{21}}{a_{11}} a_{14} \bigg) x_{4} &= b_{3} - \frac{a_{31}}{a_{11}} b_{1} \\ \bigg( a_{41} - \frac{a_{41}}{a_{11}} a_{11} \bigg) x_{1} + \bigg( a_{42} - \frac{a_{41}}{a_{11}} a_{12} \bigg) x_{2} + \bigg( a_{43} - \frac{a_{21}}{a_{11}} a_{13} \bigg) x_{3} + \bigg( a_{44} - \frac{a_{21}}{a_{11}} a_{14} \bigg) x_{4} &= b_{4} - \frac{a_{41}}{a_{11}} b_{1} \end{align*}Usando un cambio de variable
\begin{align*} a_{11} x_{1} + a_{12} x_{2} + a_{13} x_{3} + a_{14} x_{4} &= b_{1} \\ a'_{22} x_{2} + a'_{23} x_{3} + a'_{24} x_{4} &= b'_{2} \\ a'_{32} x_{2} + a'_{33} x_{3} + a'_{34} x_{4} &= b'_{3} \\ a'_{42} x_{2} + a'_{43} x_{3} + a'_{44} x_{4} &= b'_{4} \end{align*}Expandiendo y agrupando términos
\begin{align*} a_{11} x_{1} + a_{12} x_{2} + a_{13} x_{3} + a_{14} x_{4} &= b_{1} \\ a'_{22} x_{2} + a'_{23} x_{3} + a'_{24} x_{4} &= b'_{2} \\ \bigg( a'_{32} - \frac{a'_{32}}{a'_{22}} a'_{22} \bigg) x_{2} + \bigg( a'_{33} - \frac{a'_{32}}{a'_{22}} a'_{23} \bigg) x_{3} + \bigg( a'_{34} - \frac{a'_{32}}{a'_{22}} a'_{24} \bigg) x_{4} &= b'_{3} - \frac{a'_{32}}{a'_{22}} b'_{2} \\ \bigg( a'_{42} - \frac{a'_{42}}{a'_{22}} a'_{22} \bigg) x_{2} + \bigg( a'_{43} - \frac{a'_{42}}{a'_{22}} a'_{23} \bigg) x_{3} + \bigg( a'_{44} - \frac{a'_{42}}{a'_{22}} a'_{24} \bigg) x_{4} &= b'_{4} - \frac{a'_{42}}{a'_{22}} b'_{2} \end{align*}Usando un cambio de variable
\begin{align*} a_{11} x_{1} + a_{12} x_{2} + a_{13} x_{3} + a_{14} x_{4} &= b_{1} \\ a'_{22} x_{2} + a'_{23} x_{3} + a'_{24} x_{4} &= b'_{2} \\ a''_{33} x_{3} + a''_{34} x_{4} &= b''_{3} \\ a''_{43} x_{3} + a''_{44} x_{4} &= b''_{4} \end{align*}Expandiendo y agrupando términos
\begin{align*} a_{11} x_{1} + a_{12} x_{2} + a_{13} x_{3} + a_{14} x_{4} &= b_{1} \\ a'_{22} x_{2} + a'_{23} x_{3} + a'_{24} x_{4} &= b'_{2} \\ a''_{33} x_{3} + a''_{34} x_{4} &= b''_{3} \\ \bigg( a''_{43} - \frac{a''_{43}}{a''_{33}} a''_{33} \bigg) x_{3} + \bigg( a''_{44} - \frac{a''_{43}}{a''_{33}} a''_{34} \bigg) x_{4} &= b''_{4} - \frac{a''_{43}}{a''_{33}} b''_{3} \end{align*}Usando un cambio de variable
\begin{align*} a_{11} x_{1} + a_{12} x_{2} + a_{13} x_{3} + a_{14} x_{4} &= b_{1} \\ a'_{22} x_{2} + a'_{23} x_{3} + a'_{24} x_{4} &= b'_{2} \\ a''_{33} x_{3} + a''_{34} x_{4} &= b''_{3} \\ a'''_{44} x_{4} &= b'''_{4} \end{align*}Lo anterior puede escribirse como
\begin{equation*} \begin{array}{llll:l} a_{11} = a_{11} & a_{12} = a_{12} & a_{13} = a_{13} & a_{14} = a_{14} & b_{1} = b_{1} \\ a_{21} = a_{21} - \frac{a_{21}}{a_{11}} a_{11} & a_{22} = a_{22} - \frac{a_{21}}{a_{11}} a_{12} & a_{23} = a_{23} - \frac{a_{21}}{a_{11}} a_{13} & a_{24} = a_{24} - \frac{a_{21}}{a_{11}} a_{14} & b_{2} = b_{2} - \frac{a_{21}}{a_{11}} b_{1} \\ a_{31} = a_{31} - \frac{a_{31}}{a_{11}} a_{11} & a_{32} = a_{32} - \frac{a_{31}}{a_{11}} a_{12} & a_{33} = a_{33} - \frac{a_{31}}{a_{11}} a_{13} & a_{34} = a_{34} - \frac{a_{31}}{a_{11}} a_{14} & b_{3} = b_{3} - \frac{a_{31}}{a_{11}} b_{1} \\ a_{41} = a_{41} - \frac{a_{41}}{a_{11}} a_{11} & a_{42} = a_{42} - \frac{a_{41}}{a_{11}} a_{12} & a_{43} = a_{43} - \frac{a_{41}}{a_{11}} a_{13} & a_{44} = a_{44} - \frac{a_{41}}{a_{11}} a_{14} & b_{4} = b_{4} - \frac{a_{41}}{a_{11}} b_{1} \\ \hline a_{11} = a_{11} & a_{12} = a_{12} & a_{13} = a_{13} & a_{14} = a_{14} & b_{1} = b_{1} \\ a_{21} = a_{21} & a_{22} = a_{22} & a_{23} = a_{23} & a_{24} = a_{24} & b_{2} = b_{2} \\ a_{31} = a_{31} - \frac{a_{32}}{a_{22}} a_{21} & a_{32} = a_{32} - \frac{a_{32}}{a_{22}} a_{22} & a_{33} = a_{33} - \frac{a_{32}}{a_{22}} a_{23} & a_{34} = a_{34} - \frac{a_{32}}{a_{22}} a_{24} & b_{3} = b_{3} - \frac{a_{32}}{a_{22}} b_{2} \\ a_{41} = a_{41} - \frac{a_{42}}{a_{22}} a_{21} & a_{42} = a_{42} - \frac{a_{42}}{a_{22}} a_{22} & a_{43} = a_{43} - \frac{a_{42}}{a_{22}} a_{23} & a_{44} = a_{44} - \frac{a_{42}}{a_{22}} a_{24} & b_{4} = b_{4} - \frac{a_{42}}{a_{22}} b_{2} \\ \hline a_{11} = a_{11} & a_{12} = a_{12} & a_{13} = a_{13} & a_{14} = a_{14} & b_{1} = b_{1} \\ a_{21} = a_{21} & a_{22} = a_{22} & a_{23} = a_{23} & a_{24} = a_{24} & b_{2} = b_{2} \\ a_{31} = a_{31} & a_{32} = a_{32} & a_{33} = a_{33} & a_{34} = a_{34} & b_{3} = b_{3} \\ a_{41} = a_{41} - \frac{a_{43}}{a_{33}} a_{31} & a_{42} = a_{42} - \frac{a_{43}}{a_{33}} a_{32} & a_{43} = a_{43} - \frac{a_{43}}{a_{33}} a_{33} & a_{44} = a_{44} - \frac{a_{43}}{a_{33}} a_{34} & b_{4} = b_{4} - \frac{a_{43}}{a_{33}} b_{3} \end{array} \end{equation*}lo anterior puede escribirse como
\begin{equation*} a_{?j} = a_{?j} - \frac{a_{??}}{a_{??}} a_{?j} \end{equation*}para $j = 1, 2, 3, 4 = 1 , \dots, n$
lo anterior puede escribirse como
\begin{align*} a_{ij} &= a_{ij} - \frac{a_{i?}}{a_{??}} a_{?j} \\ b_{i} &= b_{i} - \frac{a_{i?}}{a_{??}} b_{?} \end{align*}para
\begin{align*} i &= 2, 3, 4 = 2, \dots, m \\ &= 3, 4 = 3, \dots, m \\ &= 4 = 4 , \dots, m \end{align*}lo anterior puede escribirse como
\begin{align*} a_{ij} &= a_{ij} - \frac{a_{ik}}{a_{kk}} a_{kj} \\ b_{i} &= b_{i} - \frac{a_{ik}}{a_{kk}} b_{k} \end{align*}para $k = 1, 2, 3 = 1, \dots, m - 1$
function eliminacion_adelante(a,b)
m, n = tamaño(a)
for k=1 to m-1 do
for i=1+k to m do
for j=1 to n do
a(i,j) = a(i,j) - a(i,k)*a(k,j)/a(k,k)
end for
b(i) = b(i) - a(i,k)*b(k)/a(k,k)
end for
end for
end function
otra alternativa para reducir tiempo de cálculo
function eliminacion_adelante(a,b)
m, n = tamaño(a)
for k=1 to m-1 do
for i=1+k to m do
factor = a(i,k)/a(k,k)
for j=1 to n do
a(i,j) = a(i,j) - factor*a(k,j)
end for
b(i) = b(i) - factor*b(k)
end for
end for
end function
In [1]:
import numpy as np
def eliminacion_adelante(A,B):
a = np.copy(A)
b = np.copy(B)
m, n = a.shape
for k in range(0,m-1):
for i in range(1+k,m):
factor = a[i,k]/a[k,k]
for j in range(0,n):
a[i,j] = a[i,j] - factor*a[k,j]
b[i,0] = b[i,0] - factor*b[k,0]
return a,b
In [2]:
A = np.array([[3,-0.1,-0.2],[0.1,7,-0.3],[0.3,-0.2,10]])
print(A)
In [3]:
B = np.array([7.85,-19.3,71.4]).reshape((3,1))
print(B)
In [4]:
eliminacion_adelante(A,B)[0]
Out[4]:
In [5]:
eliminacion_adelante(A,B)[1]
Out[5]:
In [6]:
#revisando
from scipy.linalg import lu
p, l, u = lu(np.concatenate((A,B),axis=1), permute_l=False, overwrite_a=False, check_finite=True)
print(u)
Resolver el sistema de ecuaciones
\begin{equation*} \begin{bmatrix} 3 & -0.1 & -0.2 \\ 0 & 7.003333 & -0.293333 \\ 0 & 0 & 10.011204 \end{bmatrix} \begin{bmatrix} x_{1} \\ x_{2} \\ x_{3} \end{bmatrix} = \begin{bmatrix} 7.85 \\ -19.561667 \\ 70.084293 \end{bmatrix} \end{equation*}Incógnita $x_{3}$
\begin{equation*} x_{3} = \frac{70.084293}{10.011204} = 7.000586 \end{equation*}Incógnita $x_{2}$
\begin{equation*} x_{2} = \frac{-19.561667 - (-0.293333)(7.000586)}{7.003333} = -2.499976 \end{equation*}Incógnita $x_{1}$
\begin{equation*} x_{1} = \frac{7.85 - (-0.1)(-2.499976) - (-0.2)(7.000586)}{3} = 3.000040 \end{equation*}Incognita $x_{4}$
\begin{equation*} x_{4} = \frac{b_{4}}{a_{44}} \end{equation*}Incognita $x_{3}$
\begin{equation*} x_{3} = \frac{b_{3} - a_{34} x_{4}}{a_{33}} \end{equation*}Incognita $x_{2}$
\begin{equation*} x_{2} = \frac{b_{2} - a_{23} x_{3} - a_{24} x_{4} }{a_{22}} \end{equation*}Incognita $x_{1}$
\begin{equation*} x_{1} = \frac{b_{1} - a_{12} x_{2} - a_{13} x_{3} - a_{14} x_{4} }{a_{11}} \end{equation*}lo anterior puede escribirse como
\begin{equation*} x_{m} = \frac{b_{m}}{a_{mm}} \end{equation*}lo anterior puede escribirse como
\begin{equation*} x_{?} = \frac{b_{?} - \sum_{n}^{j} a_{?j} x_{j}}{a_{??}} \end{equation*}para
\begin{align*} j &= 4 = n, \dots, 4 \\ &= 4, 3 = n, \dots, 3 \\ &= 4, 3, 2 = n, \dots, 2 \end{align*}lo anterior puede escribirse como
\begin{equation*} x_{i} = \frac{b_{i} - \sum_{n}^{j} a_{ij} x_{j}}{a_{ii}} \end{equation*}para $i = 3, 2, 1 = m - 1, \dots, 1$
function sustitucion_atras(a,b)
m, n = tamaño(a)
x(m) = b(m)/a(m,m)
for i=m-1 to 1 do
sumatoria = b(i)
for j=n to 1+i do
sumatoria = sumatoria - a(i,j)*x(j)
end for
x(i) = sumatoria / a(i,i)
end for
return x
end function
In [7]:
import numpy as np
def sustitucion_atras(a,b):
m, n = a.shape
x = np.zeros(b.shape)
x[m-1,0] = b[m-1,0]/a[m-1,m-1]
for i in range(m-2,-1,-1):
sumatoria = b[i,0]
for j in range(n-1,i,-1):
sumatoria = sumatoria - a[i,j]*x[j,0]
x[i,0] = sumatoria/a[i,i]
return x
In [8]:
A = np.array([[3,-0.1,-0.2],[0,7.003333,-0.293333],[0,0,10.011204]])
print(A)
In [9]:
B = np.array([[7.85],[-19.561667],[70.084293]])
print(B)
In [10]:
sustitucion_atras(A,B)
Out[10]:
In [11]:
#revisando el resultado
X = np.linalg.solve(A, B)
print(X)
#revisando la solución
np.allclose(np.dot(A, X), B)
Out[11]:
In [12]:
A = np.array([[1,2,3],[0,4,5],[0,0,6]],float)
print(A)
In [13]:
B = np.array([[7],[8],[9]],float)
print(B)
In [14]:
sustitucion_atras(A,B)
Out[14]:
In [15]:
#revisando el resultado
X = np.linalg.solve(A, B)
print(X)
#revisando la solución
np.allclose(np.dot(A, X), B)
Out[15]:
In [16]:
def eliminacion_gauss(a,b):
A, B = eliminacion_adelante(a,b)
x = sustitucion_atras(A,B)
print(x)
In [17]:
A = np.array([[3,-0.1,-0.2],[0.1,7,-0.3],[0.3,-0.2,10]])
print(A)
In [18]:
B = np.array([7.85,-19.3,71.4]).reshape((3,1))
print(B)
In [19]:
eliminacion_gauss(A,B)
In [20]:
#revisando el resultado
solucion = np.linalg.solve(A, B)
print(solucion)
#revisando la solución
np.allclose(np.dot(A, solucion), B)
Out[20]:
In [21]:
C = np.array([[4,-1,1],
[2,5,2],
[1,2,4]],float)
D = np.array([[8],
[3],
[11]],float)
In [22]:
eliminacion_gauss(C,D)
In [23]:
#revisando el resultado
solucion = np.linalg.solve(C, D)
print(solucion)
#revisando la solución
np.allclose(np.dot(C, solucion), D)
Out[23]:
In [24]:
E = np.array([[1,5,-1,1,-1],
[2,2,4,-1,1],
[3,12,-3,-2,3],
[4,10,-2,4,-5],
[16,-10,6,-1,-1]],float)
F = np.array([[2],
[4],
[8],
[16],
[32]],float)
In [25]:
eliminacion_gauss(E,F)
In [26]:
#revisando el resultado
solucion = np.linalg.solve(E, F)
print(solucion)
#revisando la solución
np.allclose(np.dot(E, solucion), F)
Out[26]: