Algoritmo de Eliminação

Para a disciplina de Álgebra Linear e Matricial por Ronan Soares

Solução para Sistema Canônico

$$Ax = b$$
Conjunto Solução
São todas as soluções
Solução Paramétrica
Variáveis livres recebem valores arbitrários

Exemplo

$$ \begin{bmatrix} \textcolor{magenta}{1} & 3 & 0 & 4 & 0 & 0\\ 0 & 0 & \textcolor{magenta}{1} & 2 & 0 & 0\\ 0 & 0 & 0 & 0 & \textcolor{magenta}{1} & 5\\ 0 & 0 & 0 & 0 & 0 & 0 \end{bmatrix} \begin{bmatrix} x_1\\ \textcolor{cyan}{x_2}\\ x_3\\ \textcolor{cyan}{x_4}\\ x_5\\ \textcolor{cyan}{x_6} \end{bmatrix} = \begin{bmatrix} 7\\ 5\\ 2\\ 0 \end{bmatrix} $$

Exemplo

$$\mathcal{S} = \left\{\begin{bmatrix} x_1\\ \textcolor{cyan}{x_2}\\ x_3\\ \textcolor{cyan}{x_4}\\ x_5\\ \textcolor{cyan}{x_6} \end{bmatrix} = \begin{bmatrix} 7 - 3a - 4b\\ \textcolor{cyan}{a}\\ 5 - 2b\\ \textcolor{cyan}{b}\\ 2 - 5c\\ \textcolor{cyan}{c} \end{bmatrix}: \textcolor{cyan}{a}, \textcolor{cyan}{b}, \textcolor{cyan}{c} \in \mathbb{R}\right\}$$

Exemplo

$$\mathcal{S} = \left\{\begin{bmatrix} 7 - 3a - 4b\\ \textcolor{cyan}{a}\\ 5 - 2b\\ \textcolor{cyan}{b}\\ 2 - 5c\\ \textcolor{cyan}{c} \end{bmatrix}: \textcolor{cyan}{a}, \textcolor{cyan}{b}, \textcolor{cyan}{c} \in \mathbb{R}\right\}$$

Matriz Aumentada

$$Ax = b$$
$$M = \begin{bmatrix}A \; b\end{bmatrix}$$

Adiciona a coluna $b$ em $A$

Operação Elementar

modifica o sistema sem modificar o conjunto solução

Troca de Linhas

$$\begin{bmatrix} \textcolor{magenta}{1} & \textcolor{magenta}{2} & \textcolor{magenta}{3}\\ \textcolor{cyan}{4} & \textcolor{cyan}{5} & \textcolor{cyan}{6}\\ 7 & 8 & 9 \end{bmatrix} \begin{bmatrix} x_1\\ x_2\\ x_3 \end{bmatrix} = \begin{bmatrix} \textcolor{magenta}{10}\\ \textcolor{cyan}{20}\\ 30 \end{bmatrix}$$
$$\begin{bmatrix} \textcolor{cyan}{4} & \textcolor{cyan}{5} & \textcolor{cyan}{6}\\ \textcolor{magenta}{1} & \textcolor{magenta}{2} & \textcolor{magenta}{3}\\ 7 & 8 & 9 \end{bmatrix} \begin{bmatrix} x_1\\ x_2\\ x_3 \end{bmatrix} = \begin{bmatrix} \textcolor{cyan}{20}\\ \textcolor{magenta}{10}\\ 30 \end{bmatrix}$$

Multiplicar Linha por $k \neq 0$

$$\begin{bmatrix} \textcolor{magenta}{1} & \textcolor{magenta}{2} & \textcolor{magenta}{3}\\ 4 & 5 & 6\\ 7 & 8 & 9 \end{bmatrix} \begin{bmatrix} x_1\\ x_2\\ x_3 \end{bmatrix} = \begin{bmatrix} \textcolor{magenta}{10}\\ 20\\ 30 \end{bmatrix}$$
$$\begin{bmatrix} \textcolor{magenta}{2} & \textcolor{magenta}{4} & \textcolor{magenta}{6}\\ 4 & 5 & 6\\ 7 & 8 & 9 \end{bmatrix} \begin{bmatrix} x_1\\ x_2\\ x_3 \end{bmatrix} = \begin{bmatrix} \textcolor{magenta}{20}\\ 20\\ 30 \end{bmatrix}$$

Combinar Linhas

Substituir uma linha por uma combinação linear dela com outras linhas...

$$L_i \leftarrow L_i + \textnormal{ Combinação das Outras}$$

Combinar Linhas

$$\begin{bmatrix} \textcolor{magenta}{1} & \textcolor{magenta}{2} & \textcolor{magenta}{3}\\ \textcolor{cyan}{4} & \textcolor{cyan}{5} & \textcolor{cyan}{6}\\ 7 & 8 & 9 \end{bmatrix} \begin{bmatrix} x_1\\ x_2\\ x_3 \end{bmatrix} = \begin{bmatrix} \textcolor{magenta}{10}\\ \textcolor{cyan}{20}\\ 30 \end{bmatrix}$$
$$ L_3 \leftarrow L_3 + 2 \textcolor{magenta}{L_1} + \textcolor{cyan}{L_2} $$
$$\begin{bmatrix} 1 & 2 & 3\\ 4 & 5 & 6\\ \textcolor{yellow}{13} & \textcolor{yellow}{17} & \textcolor{yellow}{21} \end{bmatrix} \begin{bmatrix} x_1\\ x_2\\ x_3 \end{bmatrix} = \begin{bmatrix} 10\\ 20\\ \textcolor{yellow}{70} \end{bmatrix}$$

Eliminação

$$Ax = b$$
$$ \begin{bmatrix}3 & 6 & 27\\ 6 & 18 & 33\end{bmatrix} \begin{bmatrix}x_1\\x_2\end{bmatrix} \begin{bmatrix}9\\12\end{bmatrix} $$
$$M = \begin{bmatrix}3 & 6 & 27 & 9\\ 6 & 18 & 33 & 12\end{bmatrix}$$

Eliminação

$$\begin{bmatrix}\textcolor{magenta}{3} & 6 & 27 & 9\\ \textcolor{cyan}{6} & 18 & 33 & 12\end{bmatrix}$$
$$L_2 \leftarrow L_2 - \frac{\textcolor{cyan}{a_{21}}}{\textcolor{magenta}{a_{11}}}L_1$$
$$\begin{bmatrix}3 & 6 & 27 & 9\\ \textcolor{yellow}{0} & \textcolor{yellow}{6} & \textcolor{yellow}{-21} & \textcolor{yellow}{-6}\end{bmatrix}$$

Eliminação

  1. Escolhe um pivô...
  2. Elimina todos os coeficientes abaixo dele...
  3. Repete com o próximo pivô

Dá quase certo...

Código Eliminação

              
                  def elimina(Lj, Li, pivo):
                      for k in range(len(Lj)):
                          Lj[k] = Lj[k] - pivo * Li[k]
              
            

Algoritmo da Eliminação

              
              def escalona(M, m, n):
                  i = 0
                  j = 0
                  while i < m - 1 and j < n:
                      k = i + 1
                      if M[i][j] == 0:
                          return "Erro"
                      else:
                          while k <= m - 1 and k <= n - 1:
                              elimina(M[k], M[i], M[i][j])
                              k = k + 1
                          i = i + 1
                          j = j + 1
                  return M
              
            
                  
                  def escalona(M, m, n):
                      i = 0
                      j = 0
                      while i < m - 1 and j < n:
                          k = i + 1
                          if M[i][j] == 0:
                              return "Erro"
                          else:
                              while k <= m - 1 and k <= n - 1:
                                  elimina(M[k], M[i], M[i][j])
                                  k = k + 1
                              i = i + 1
                              j = j + 1
                      return M
                  
                
$$\begin{bmatrix} 1 & 1 & -2 & 4 & 5\\2 & 3 & -3 & 1 & 3\\3 & 6 & -4 & -2 & 1\end{bmatrix} $$
                  
                  def escalona(M, m, n):
                      i = 0
                      j = 0
                      while i < m - 1 and j < n:
                          k = i + 1
                          if M[i][j] == 0:
                              return "Erro"
                          else:
                              while k <= m - 1 and k <= n - 1:
                                  elimina(M[k], M[i], M[i][j])
                                  k = k + 1
                              i = i + 1
                              j = j + 1
                      return M
                  
                
$$\begin{bmatrix} 1 & 1 & -2 & 4 & 5\\2 & 3 & -3 & 1 & 3\\3 & 6 & -4 & -2 & 1\end{bmatrix} $$
  • i = 0
  • j = 0
                  
                  def escalona(M, m, n):
                      i = 0
                      j = 0
                      while i < m - 1 and j < n:
                          k = i + 1
                          if M[i][j] == 0:
                              return "Erro"
                          else:
                              while k <= m - 1 and k <= n - 1:
                                  elimina(M[k], M[i], M[i][j])
                                  k = k + 1
                              i = i + 1
                              j = j + 1
                      return M
                  
                
$$\begin{bmatrix} 1 & 1 & -2 & 4 & 5\\2 & 3 & -3 & 1 & 3\\3 & 6 & -4 & -2 & 1\end{bmatrix} $$
  • i = 0
  • j = 0
                  
                  def escalona(M, m, n):
                      i = 0
                      j = 0
                      while i < m - 1 and j < n:
                          k = i + 1
                          if M[i][j] == 0:
                              return "Erro"
                          else:
                              while k <= m - 1 and k <= n - 1:
                                  elimina(M[k], M[i], M[i][j])
                                  k = k + 1
                              i = i + 1
                              j = j + 1
                      return M
                  
                
$$\begin{bmatrix} 1 & 1 & -2 & 4 & 5\\2 & 3 & -3 & 1 & 3\\3 & 6 & -4 & -2 & 1\end{bmatrix} $$
  • i = 0
  • j = 0
  • k = 1
                  
                  def escalona(M, m, n):
                      i = 0
                      j = 0
                      while i < m - 1 and j < n:
                          k = i + 1
                          if M[i][j] == 0:
                              return "Erro"
                          else:
                              while k <= m - 1 and k <= n - 1:
                                  elimina(M[k], M[i], M[i][j])
                                  k = k + 1
                              i = i + 1
                              j = j + 1
                      return M
                  
                
$$\begin{bmatrix} \textcolor{magenta}{1} & 1 & -2 & 4 & 5\\2 & 3 & -3 & 1 & 3\\3 & 6 & -4 & -2 & 1\end{bmatrix} $$
  • i = 0
  • j = 0
  • k = 1
                  
                  def escalona(M, m, n):
                      i = 0
                      j = 0
                      while i < m - 1 and j < n:
                          k = i + 1
                          if M[i][j] == 0:
                              return "Erro"
                          else:
                              while k <= m - 1 and k <= n - 1:
                                  elimina(M[k], M[i], M[i][j])
                                  k = k + 1
                              i = i + 1
                              j = j + 1
                      return M
                  
                
$$\begin{bmatrix} \textcolor{magenta}{1} & 1 & -2 & 4 & 5\\2 & 3 & -3 & 1 & 3\\3 & 6 & -4 & -2 & 1\end{bmatrix} $$
  • i = 0
  • j = 0
  • k = 1
                  
                  def escalona(M, m, n):
                      i = 0
                      j = 0
                      while i < m - 1 and j < n:
                          k = i + 1
                          if M[i][j] == 0:
                              return "Erro"
                          else:
                              while k <= m - 1 and k <= n - 1:
                                  elimina(M[k], M[i], M[i][j])
                                  k = k + 1
                              i = i + 1
                              j = j + 1
                      return M
                  
                
$$\begin{bmatrix} \textcolor{magenta}{1} & 1 & -2 & 4 & 5\\\textcolor{cyan}{2} & \textcolor{cyan}{3} & \textcolor{cyan}{-3} & \textcolor{cyan}{1} & \textcolor{cyan}{3}\\3 & 6 & -4 & -2 & 1\end{bmatrix} $$
  • i = 0
  • j = 0
  • k = 1
                  
                  def escalona(M, m, n):
                      i = 0
                      j = 0
                      while i < m - 1 and j < n:
                          k = i + 1
                          if M[i][j] == 0:
                              return "Erro"
                          else:
                              while k <= m - 1 and k <= n - 1:
                                  elimina(M[k], M[i], M[i][j])
                                  k = k + 1
                              i = i + 1
                              j = j + 1
                      return M
                  
                
$$\begin{bmatrix} \textcolor{magenta}{1} & 1 & -2 & 4 & 5\\\textcolor{yellow}{0} & \textcolor{yellow}{1} & \textcolor{yellow}{1} & \textcolor{yellow}{-7} & \textcolor{yellow}{-7}\\3 & 6 & -4 & -2 & 1\end{bmatrix} $$
  • i = 0
  • j = 0
  • k = 1
                  
                  def escalona(M, m, n):
                      i = 0
                      j = 0
                      while i < m - 1 and j < n:
                          k = i + 1
                          if M[i][j] == 0:
                              return "Erro"
                          else:
                              while k <= m - 1 and k <= n - 1:
                                  elimina(M[k], M[i], M[i][j])
                                  k = k + 1
                              i = i + 1
                              j = j + 1
                      return M
                  
                
$$\begin{bmatrix} \textcolor{magenta}{1} & 1 & -2 & 4 & 5\\0 & 1 & 1 & -7 & -7\\3 & 6 & -4 & -2 & 1\end{bmatrix} $$
  • i = 0
  • j = 0
  • k = 2
                  
                  def escalona(M, m, n):
                      i = 0
                      j = 0
                      while i < m - 1 and j < n:
                          k = i + 1
                          if M[i][j] == 0:
                              return "Erro"
                          else:
                              while k <= m - 1 and k <= n - 1:
                                  elimina(M[k], M[i], M[i][j])
                                  k = k + 1
                              i = i + 1
                              j = j + 1
                      return M
                  
                
$$\begin{bmatrix} \textcolor{magenta}{1} & 1 & -2 & 4 & 5\\0 & 1 & 1 & -7 & -7\\3 & 6 & -4 & -2 & 1\end{bmatrix} $$
  • i = 0
  • j = 0
  • k = 2
                  
                  def escalona(M, m, n):
                      i = 0
                      j = 0
                      while i < m - 1 and j < n:
                          k = i + 1
                          if M[i][j] == 0:
                              return "Erro"
                          else:
                              while k <= m - 1 and k <= n - 1:
                                  elimina(M[k], M[i], M[i][j])
                                  k = k + 1
                              i = i + 1
                              j = j + 1
                      return M
                  
                
$$\begin{bmatrix} \textcolor{magenta}{1} & 1 & -2 & 4 & 5\\0 & 1 & 1 & -7 & -7\\\textcolor{cyan}{3} & \textcolor{cyan}{6} & \textcolor{cyan}{-4} & \textcolor{cyan}{-2} & \textcolor{cyan}{1}\end{bmatrix} $$
  • i = 0
  • j = 0
  • k = 2
                  
                  def escalona(M, m, n):
                      i = 0
                      j = 0
                      while i < m - 1 and j < n:
                          k = i + 1
                          if M[i][j] == 0:
                              return "Erro"
                          else:
                              while k <= m - 1 and k <= n - 1:
                                  elimina(M[k], M[i], M[i][j])
                                  k = k + 1
                              i = i + 1
                              j = j + 1
                      return M
                  
                
$$\begin{bmatrix} \textcolor{magenta}{1} & 1 & -2 & 4 & 5\\0 & 1 & 1 & -7 & -7\\\textcolor{yellow}{0} & \textcolor{yellow}{3} & \textcolor{yellow}{2} & \textcolor{yellow}{-14} & \textcolor{yellow}{-14}\end{bmatrix} $$
  • i = 0
  • j = 0
  • k = 2
                  
                  def escalona(M, m, n):
                      i = 0
                      j = 0
                      while i < m - 1 and j < n:
                          k = i + 1
                          if M[i][j] == 0:
                              return "Erro"
                          else:
                              while k <= m - 1 and k <= n - 1:
                                  elimina(M[k], M[i], M[i][j])
                                  k = k + 1
                              i = i + 1
                              j = j + 1
                      return M
                  
                
$$\begin{bmatrix} \textcolor{magenta}{1} & 1 & -2 & 4 & 5\\0 & 1 & 1 & -7 & -7\\0 & 3 & 2 & -14 & -14\end{bmatrix} $$
  • i = 0
  • j = 0
  • k = 3
                  
                  def escalona(M, m, n):
                      i = 0
                      j = 0
                      while i < m - 1 and j < n:
                          k = i + 1
                          if M[i][j] == 0:
                              return "Erro"
                          else:
                              while k <= m - 1 and k <= n - 1:
                                  elimina(M[k], M[i], M[i][j])
                                  k = k + 1
                              i = i + 1
                              j = j + 1
                      return M
                  
                
$$\begin{bmatrix} \textcolor{magenta}{1} & 1 & -2 & 4 & 5\\0 & 1 & 1 & -7 & -7\\0 & 3 & 2 & -14 & -14\end{bmatrix} $$
  • i = 0
  • j = 0
  • k = 3
                  
                  def escalona(M, m, n):
                      i = 0
                      j = 0
                      while i < m - 1 and j < n:
                          k = i + 1
                          if M[i][j] == 0:
                              return "Erro"
                          else:
                              while k <= m - 1 and k <= n - 1:
                                  elimina(M[k], M[i], M[i][j])
                                  k = k + 1
                              i = i + 1
                              j = j + 1
                      return M
                  
                
$$\begin{bmatrix} 1 & 1 & -2 & 4 & 5\\0 & 1 & 1 & -7 & -7\\0 & 3 & 2 & -14 & -14\end{bmatrix} $$
  • i = 1
  • j = 1
  • k = 3
                  
                  def escalona(M, m, n):
                      i = 0
                      j = 0
                      while i < m - 1 and j < n:
                          k = i + 1
                          if M[i][j] == 0:
                              return "Erro"
                          else:
                              while k <= m - 1 and k <= n - 1:
                                  elimina(M[k], M[i], M[i][j])
                                  k = k + 1
                              i = i + 1
                              j = j + 1
                      return M
                  
                
$$\begin{bmatrix} 1 & 1 & -2 & 4 & 5\\0 & 1 & 1 & -7 & -7\\0 & 3 & 2 & -14 & -14\end{bmatrix} $$
  • i = 1
  • j = 1
  • k = 3
                  
                  def escalona(M, m, n):
                      i = 0
                      j = 0
                      while i < m - 1 and j < n:
                          k = i + 1
                          if M[i][j] == 0:
                              return "Erro"
                          else:
                              while k <= m - 1 and k <= n - 1:
                                  elimina(M[k], M[i], M[i][j])
                                  k = k + 1
                              i = i + 1
                              j = j + 1
                      return M
                  
                
$$\begin{bmatrix} 1 & 1 & -2 & 4 & 5\\0 & 1 & 1 & -7 & -7\\0 & 3 & 2 & -14 & -14\end{bmatrix} $$
  • i = 1
  • j = 1
  • k = 2
                  
                  def escalona(M, m, n):
                      i = 0
                      j = 0
                      while i < m - 1 and j < n:
                          k = i + 1
                          if M[i][j] == 0:
                              return "Erro"
                          else:
                              while k <= m - 1 and k <= n - 1:
                                  elimina(M[k], M[i], M[i][j])
                                  k = k + 1
                              i = i + 1
                              j = j + 1
                      return M
                  
                
$$\begin{bmatrix} 1 & 1 & -2 & 4 & 5\\0 & \textcolor{magenta}{1} & 1 & -7 & -7\\0 & 3 & 2 & -14 & -14\end{bmatrix} $$
  • i = 1
  • j = 1
  • k = 2
                  
                  def escalona(M, m, n):
                      i = 0
                      j = 0
                      while i < m - 1 and j < n:
                          k = i + 1
                          if M[i][j] == 0:
                              return "Erro"
                          else:
                              while k <= m - 1 and k <= n - 1:
                                  elimina(M[k], M[i], M[i][j])
                                  k = k + 1
                              i = i + 1
                              j = j + 1
                      return M
                  
                
$$\begin{bmatrix} 1 & 1 & -2 & 4 & 5\\0 & \textcolor{magenta}{1} & 1 & -7 & -7\\0 & 3 & 2 & -14 & -14\end{bmatrix} $$
  • i = 1
  • j = 1
  • k = 2
                  
                  def escalona(M, m, n):
                      i = 0
                      j = 0
                      while i < m - 1 and j < n:
                          k = i + 1
                          if M[i][j] == 0:
                              return "Erro"
                          else:
                              while k <= m - 1 and k <= n - 1:
                                  elimina(M[k], M[i], M[i][j])
                                  k = k + 1
                              i = i + 1
                              j = j + 1
                      return M
                  
                
$$\begin{bmatrix} 1 & 1 & -2 & 4 & 5\\0 & \textcolor{magenta}{1} & 1 & -7 & -7\\0 & \textcolor{cyan}{3} & \textcolor{cyan}{2} & \textcolor{cyan}{-14} & \textcolor{cyan}{-14}\end{bmatrix} $$
  • i = 1
  • j = 1
  • k = 2
                  
                  def escalona(M, m, n):
                      i = 0
                      j = 0
                      while i < m - 1 and j < n:
                          k = i + 1
                          if M[i][j] == 0:
                              return "Erro"
                          else:
                              while k <= m - 1 and k <= n - 1:
                                  elimina(M[k], M[i], M[i][j])
                                  k = k + 1
                              i = i + 1
                              j = j + 1
                      return M
                  
                
$$\begin{bmatrix} 1 & 1 & -2 & 4 & 5\\0 & \textcolor{magenta}{1} & 1 & -7 & -7\\0 & \textcolor{yellow}{0} & \textcolor{yellow}{-1} & \textcolor{yellow}{7} & \textcolor{yellow}{7}\end{bmatrix} $$
  • i = 1
  • j = 1
  • k = 2
                  
                  def escalona(M, m, n):
                      i = 0
                      j = 0
                      while i < m - 1 and j < n:
                          k = i + 1
                          if M[i][j] == 0:
                              return "Erro"
                          else:
                              while k <= m - 1 and k <= n - 1:
                                  elimina(M[k], M[i], M[i][j])
                                  k = k + 1
                              i = i + 1
                              j = j + 1
                      return M
                  
                
$$\begin{bmatrix} 1 & 1 & -2 & 4 & 5\\0 & \textcolor{magenta}{1} & 1 & -7 & -7\\0 & 0 & -1 & 7 & 7\end{bmatrix} $$
  • i = 1
  • j = 1
  • k = 3
                  
                  def escalona(M, m, n):
                      i = 0
                      j = 0
                      while i < m - 1 and j < n:
                          k = i + 1
                          if M[i][j] == 0:
                              return "Erro"
                          else:
                              while k <= m - 1 and k <= n - 1:
                                  elimina(M[k], M[i], M[i][j])
                                  k = k + 1
                              i = i + 1
                              j = j + 1
                      return M
                  
                
$$\begin{bmatrix} 1 & 1 & -2 & 4 & 5\\0 & 1 & 1 & -7 & -7\\0 & 0 & -1 & 7 & 7\end{bmatrix} $$
  • i = 2
  • j = 2
  • k = 3
                  
                  def escalona(M, m, n):
                      i = 0
                      j = 0
                      while i < m - 1 and j < n:
                          k = i + 1
                          if M[i][j] == 0:
                              return "Erro"
                          else:
                              while k <= m - 1 and k <= n - 1:
                                  elimina(M[k], M[i], M[i][j])
                                  k = k + 1
                              i = i + 1
                              j = j + 1
                      return M
                  
                
$$\begin{bmatrix} 1 & 1 & -2 & 4 & 5\\0 & 1 & 1 & -7 & -7\\0 & 0 & -1 & 7 & 7\end{bmatrix} $$
  • i = 2
  • j = 2
  • k = 3
                  
                  def escalona(M, m, n):
                      i = 0
                      j = 0
                      while i < m - 1 and j < n:
                          k = i + 1
                          if M[i][j] == 0:
                              return "Erro"
                          else:
                              while k <= m - 1 and k <= n - 1:
                                  elimina(M[k], M[i], M[i][j])
                                  k = k + 1
                              i = i + 1
                              j = j + 1
                      return M
                  
                
$$\begin{bmatrix} 1 & 1 & -2 & 4 & 5\\0 & 1 & 1 & -7 & -7\\0 & 0 & -1 & 7 & 7\end{bmatrix} $$

Resultado

Fim