Escolar Documentos
Profissional Documentos
Cultura Documentos
Faculdade de Tecnologia
Programa de Pós-Graduação em Ciências Mecânicas
given that N is the system dimension, 𝐀𝐢𝐣 , 𝒊, 𝒋 = 𝟏, . . . , 𝑵, are the A-matrix components, and 𝐛𝐢
are the b-vector components.
Select a ill-conditioned system of N ≥ 8 and do the following:
Data Extraction:
Python language:
#Impotar Biblioteca
import numpy as np
#print(atv3)
listas = list(dividir(atv3,1))
n = listas[0:1]
n = float(content[0])
n = np.array(n)
print("Núm de linhas e colunas:")
print(n)
print ("-x-" *5)
np.loadtxt('teste.txt',delimiter=',')
Data:
Python language:
#Impotar Biblioteca
import numpy as np
import pandas as pd
a) Use the “direct” Gaussian Elimination (GE) to find the solution of the system
Python language:
# ax=b ... x=INVER(a)*b
#Encontra a inversa de a
a_inversa = np.linalg.inv(a)
#print(a)
#print(a_inversa)
#print('')
#Encontra x
x = np.dot(a_inversa,b)
print('residual: ' ,np.linalg.norm(np.dot(a,x) - b))
print ("A solução de gauss direto é:" ,x)
Universidade de Brasília
Faculdade de Tecnologia
Programa de Pós-Graduação em Ciências Mecânicas
# outra resolução
b) Use the Gaussian Elimination in the LU decomposition form to find the solution of the
system.
Python language:
def lu(a):
n = a.shape[0]
U = a.copy()
L = np.eye(n, dtype=np.double)
for i in range(n):
return L, U
#Forward-substitution.
return y
#Back-substitution.
return x
L, U = lu(a)
y = forward_substitution(L, b)
return back_substitution(U, y)
x = (lu_solve(a, b))
print ("Resolução da Eliminação de Gauss \n")
print (lu_solve(a, b))
print ("-*-" *5)
delta_b = np.linalg.norm(np.dot(a,x) - b)
print ("Residual: \n")
print(delta_b)
print ("-x-" *5)
Answer:
Resolução da Eliminação de Gauss
8.140289677804162e-15
-x--x--x--x--x-
Calculations behind the LU factorization is not very complicated. This technique is a fast
decomposition which is used for solving linear equations and finding inverses of matrices.
c) Implement scaled pivoting for GE and LU. Dot not really change the rows, but use the
order vector o(N), instead.
Python language:
#triangular_up(a,b)
def triangular_up(a,b):
n = np.size(b) # Tamanho da mariz
x = np.zeros(n)
vector_o=list(range(n)) #vetor ordenamento
for i in range(0,n):
#Calculo do pivo
for k in range(n-1): #no python começa no zero
#Calculo dos multiplicadores
for i in range(k+1,n):
m = a[i,vector_o[k]]/a[vector_o[k],vector_o[k]] # Multiplicador
a[i,:] = a[i,:] - m*a[k,:] # o : pega todos os elementos da linha #
Atenção, ao menos 1 valor da matriz não pode ser inteiro (receber o ponto)
b[i] = b[i] - m*b[k] # b não tem linhas
Universidade de Brasília
Faculdade de Tecnologia
Programa de Pós-Graduação em Ciências Mecânicas
#print(triangular_up(a,b))
#solução de triangular_up(a,b)
def triangular_solver(a,b):
n = np.size(b)
x = np.zeros(n)
x[n-1]= b[n-1]/a[n-1,n-1]
for i in list(range(n-1,0,-1)):
s=0
for j in list(range(i+1,n+1,1)):
s = s + a[i-1,j-1]*x[j-1]
x[i-1] = (b[i-1]-s)/a[i-1,i-1]
return x
def eliminacao_de_gauss(a,b):
triangular_up(a,b)
triangular_solver(a,b)
return triangular_solver(a,b)
x = eliminacao_de_gauss(a,b)
delta_b = np.linalg.norm(np.dot(a,x) - b)
print ("Residual: \n")
print(delta_b)
print ("-x-" *5)
7.32410687763558e-15
-x--x--x--x--x-
Universidade de Brasília
Faculdade de Tecnologia
Programa de Pós-Graduação em Ciências Mecânicas
#print(triangular_up(a,b))
#solução de triangular_up(a,b)
def triangular_solver(a,b):
n = np.size(b)
x = np.zeros(n)
x[n-1]= b[n-1]/a[n-1,n-1]
for i in list(range(n-1,0,-1)):
s=0
for j in list(range(i+1,n+1,1)):
s = s + a[i-1,j-1]*x[j-1]
x[i-1] = (b[i-1]-s)/a[i-1,i-1]
return x
def eliminacao_de_gauss(a,b):
triangular_up(a,b)
triangular_solver(a,b)
return triangular_solver(a,b)
x = eliminacao_de_gauss(a,b)
delta_b = np.linalg.norm(np.dot(a,x) - b)
tol = 1.e-5
b = x
#triangular_up(a,b)
def triangular_up(a,b):
n = np.size(b) # Tamanho da mariz
x = np.zeros(n)
vector_o=list(range(n)) #vetor ordenamento
for i in range(0,n):
#Calculo do pivo
for k in range(n-1): #no python começa no zero
#Calculo dos multiplicadores
for i in range(k+1,n):
m = a[i,vector_o[k]]/a[vector_o[k],vector_o[k]] # Multiplicador
a[i,:] = a[i,:] - m*a[k,:] # o : pega todos os elementos da linha #
Atenção, ao menos 1 valor da matriz não pode ser inteiro (receber o ponto)
b[i] = b[i] - m*b[k] # b não tem linhas
#print(triangular_up(a,b))
#solução de triangular_up(a,b)
def triangular_solver(a,b):
n = np.size(b)
x = np.zeros(n)
x[n-1]= b[n-1]/a[n-1,n-1]
for i in list(range(n-1,0,-1)):
s=0
for j in list(range(i+1,n+1,1)):
s = s + a[i-1,j-1]*x[j-1]
x[i-1] = (b[i-1]-s)/a[i-1,i-1]
return x
def eliminacao_de_gauss(a,b):
triangular_up(a,b)
triangular_solver(a,b)
return triangular_solver(a,b)
Universidade de Brasília
Faculdade de Tecnologia
Programa de Pós-Graduação em Ciências Mecânicas
x = eliminacao_de_gauss(a,b)
delta_b = np.linalg.norm(np.dot(a,x) - b)
e) Use the LU method to find 𝑨−𝟏 , performing the matrix decomposition only once!
Python language:
#Deomposição LU
def lu(a):
n = a.shape[0]
U = a.copy()
L = np.eye(n, dtype=np.double)
for i in range(n):
return L, U
#Decomposição PLU
def plu(A):
#Alocação de P,L e U
U = A.copy()
L = np.eye(n, dtype=np.double)
P = np.eye(n, dtype=np.double)
for i in range(n):
return P, L, U
Universidade de Brasília
Faculdade de Tecnologia
Programa de Pós-Graduação em Ciências Mecânicas
#Forward-substitution
def forward_substitution(L, b):
#Forward-substitution.
return y
#Back-substitution
def back_substitution(U, y):
#Back-substitution.
return x
L, U = lu(a)
y = forward_substitution(L, b)
return back_substitution(U, y)
#Matrix inversa de a
def plu_inverse(a):
n = a.shape[0]
Universidade de Brasília
Faculdade de Tecnologia
Programa de Pós-Graduação em Ciências Mecânicas
b = np.eye(n)
Ainv = np.zeros((n, n))
P, L, U = plu(a)
for i in range(n):
Ainv[:, i] = back_substitution(U, y)
return Ainv
a) For LS solution methods, compute the residual vector 𝒓 = 𝒃 − 𝑨 ∗ 𝒙 and its norm ∥ 𝒓 ∥ 𝟐 =
𝒓 ∗ 𝒓. Then, for an ill-conditioned system, compare ∥ 𝒓 ∥𝟐 for GE, LU, and LU with iterative
improvement, with and without pivoting.
R- Evaluating the residues of each code presented in exercises 1 for all situations such as GE, LU, and
LU with iterative improvement, with and without pivoting, it is evident that they were close to zero, which
should be considered the computational cost as the determining factor for choosing the best method.
b) For matrix inversion, compute 𝑻 = (𝑨−𝟏 · 𝑨) − 𝑰 and study the influence of the pivoting
and iterative improvement in the norm of 𝑻. For matrices norms, use ∥ 𝑻 ∥ = ∑𝒏𝒊,𝒋 ∥ 𝑻𝒊𝒋 ∥ .
R- The propagation of rounding errors end up happening in the Gaussian elimination method especially
when the value of pivots are numbers close to zero. This problem can be mitigated with a partial pivot
Universidade de Brasília
Faculdade de Tecnologia
Programa de Pós-Graduação em Ciências Mecânicas
with scale or an iterative improvement. Reducing the errors derived from the "residues" that in the case
studied was calculated through the “residual” equation nominated with “delta_b”.
3) Implement Gaussian Elimination for tridiagonal matrices (Thomas TDMA algorithm) for a
system given in the form
𝑎𝑖 𝑥𝑖−1 + 𝑏𝑖 𝑥𝑖 + 𝑐𝑖 𝑥𝑖+1 = 𝑑𝑖 .
Select a tridiagonal system with know solution and test your implementation. Observe that
only the vectors a, b, c, d, and e need to be stored.
Data:
a = np.array([[ 2., 1., 0., 0., 0.],
[ 1., 2., 1., 0., 0.],
[ 0., 1., 2., 1., 0.],
[ 0., 0., 1., 2., 1.],
[ 0., 0., 0., 1., 2.]])
Python language:
import numpy as np
def TDMA(a):
#preliminares
aa = (1.,1.,1.,1.,1.)
bb = (2.,2.,2.,2.,2.)
cc = (1.,1.,1.,1.,1.)
dd = (4.,4.,0.,0.,2.)
a = np.array(aa)
b = np.array(bb)
c = np.array(cc)
d = np.array(dd)
#Tamanho do sistema
n=np.shape(a)[0]
#Vetor auxiliar
cl=np.zeros(n)
dl=np.zeros(n)
x=np.zeros(n)
dl[0]=d[0]/b[0]
for i in np.arange(1,n,1):
dl[i]=(d[i]-a[i]*dl[i-1])/(b[i]-a[i]*cl[i-1])
for i in np.arange(n-2,-1,-1):
x[i]=dl[i]-cl[i]*x[i+1]
return x
Or
Python language:
import numpy as np
def TDMA(a):
#preliminares
aa = (1.,1.,1.,1.,1.)
bb = (2.,2.,2.,2.,2.)
cc = (1.,1.,1.,1.,1.)
dd = (4.,4.,0.,0.,2.)
a = np.array(aa)
b = np.array(bb)
c = np.array(cc)
d = np.array(dd)
#Tamanho do sistema
n=np.shape(a)[0]
#inicializa vetor x
x=np.zeros(n)
d[0]=d[0]/b[0]
for i in np.arange(1,n,1):
d[i]=(d[i]-a[i]*d[i-1])/(b[i]-a[i]*c[i-1])
return x
Universidade de Brasília
Faculdade de Tecnologia
Programa de Pós-Graduação em Ciências Mecânicas