Last active
December 22, 2015 05:58
-
-
Save senhorinha/6427506 to your computer and use it in GitHub Desktop.
Introdução ao Octave.
Experimento: Resolver um sistema possível determinado.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% Função para Pivotar a Matriz | |
% 1 - Procurar o maior valor na coluna K e armazenar a linha | |
function g = pivot(n, A, k) | |
Amax = 0; | |
imax = 0; | |
for i = k : n | |
if(abs(A(i,k)) > Amax) | |
Amax = abs(A(i,k)); % Buscar o maior valor da coluna | |
imax = i; % Guarda a a linha que contem o maior valor | |
endif; | |
endfor | |
Aaux = A(k,:); % Guarda o vetor linha | |
A(k, :) = A(imax, :); | |
A(imax, :) = Aaux; | |
g = A; % Retorna a Matriz | |
endfunction |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% Função para Cálculo de Resíduos | |
function g = residuos(n, x, A) | |
for i = 1 : n | |
soma = 0; | |
for j = 1 :n | |
soma += A(i,j) * x(j); | |
endfor | |
r(i) = abs(soma - A(i, n+1)); %r(1) = |a11*x1 + a12*x2 + a13*x3 - a14| | |
endfor | |
g = r; | |
endfunction |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = 3 % Matriz de ordem 3 | |
a = [ | |
[ 0.448 0.832 0.193 1]; | |
[ 0.421 0.784 -0.207 0]; | |
[-0.421 0.784 0.279 0]; | |
] | |
A_Orig = a; | |
% -- Escalonar Matriz -- | |
for k = 1 : n - 1 | |
%Pivotação Parcial | |
a=pivot(n,a,k) | |
for i = k + 1 : n | |
aux = a(i,k)/a(k,k); | |
for j = k + 1 : n + 1 %O k+1 evita fazer operações em que já sabemos o resultado | |
a(i,j) = a(i,j) - aux*a(k,j); | |
endfor | |
a(i,k) = 0; | |
endfor | |
endfor | |
a % Imprime a matriz escalonada | |
% -- Encontrar as Soluções -- | |
% a(3,3)*x(3) = a(3,4) | |
% a(n,n)*x(n) = a(n, n+1) | |
x(n) = a(n,n+1)/a(n,n) | |
for i = n - 1: -1:1 % -1 é o delta, ou seja, o espaçamento ou decremento | |
soma = 0; | |
for j = i + 1 : n | |
soma += a(i,j) * x(j); | |
endfor | |
x(i) = ( a(i, n+1) - soma) / a(i,i); | |
endfor | |
x | |
% Calcular os residuos | |
r = residuos(n,x,A_Orig) | |
r(1) | |
r(2) | |
r(3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = 3 % Matriz de ordem 3 | |
a = [ | |
[ 0.448 0.832 0.193 1]; | |
[ 0.421 0.784 0.207 0]; | |
[ 0.421 0.784 0.279 12]; % Linha 2 * -1 , ou seja, linha linearmente depentende a linha 2 | |
] | |
A_Orig = a; | |
% -- Escalonar Matriz -- | |
for k = 1 : n - 1 | |
a = pivot(n,a,k) %Pivotação Parcial | |
% Esse é o melhor momento para normalizar a matriz, pois já foi escolhida a melhor linha na pivotação | |
a = normalizacao(n,a,k) # Retorna a matriz normalizacao | |
for i = k + 1 : n | |
aux = a(i,k)/a(k,k); | |
for j = k + 1 : n + 1 % O k+1 evita fazer operações em que já sabemos o resultado | |
a(i,j) = a(i,j) - aux*a(k,j); | |
endfor | |
a(i,k) = 0; | |
endfor | |
endfor | |
a = normalizacao(n,a,n) % Normaliza a linha que não foi normalizada, ou seja, a última | |
a % Imprime a matriz escalonada | |
% -- Encontrar as Soluções -- | |
if(abs(a(n,n)) > 1.e-15) | |
% a(3,3)*x(3) = a(3,4) | |
% a(n,n)*x(n) = a(n, n+1) | |
x(n) = a(n,n+1)/a(n,n) | |
else | |
if(abs(a(n,n+1)) > 1.e-15) | |
x(n) = 1; | |
printf("Sistema Impossível") | |
end; | |
end | |
for i = n - 1: -1:1 % -1 é o delta, ou seja, o espaçamento ou decremento | |
soma = 0; | |
for j = i + 1 : n | |
soma += a(i,j) * x(j); % Somatorio de j = i+1 até n de a(i,j)*x(j) | |
endfor | |
x(i) = ( a(i, n+1) - soma); | |
endfor | |
x | |
% Calcular os residuos | |
r = residuos(n,x,A_Orig) | |
r(1) | |
r(2) | |
r(3) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = 3 % Matriz de ordem 3 | |
a = [ | |
[ 0.448 0.832 0.193 1]; | |
[ 0.421 0.784 -0.207 0]; | |
[-0.421 -0.784 0.279 0]; % Linha 2 * -1 , ou seja, linha linearmente depentende a linha 2 | |
] | |
A_Orig = a; | |
% -- Escalonar Matriz -- | |
for k = 1 : n - 1 | |
a = pivot(n,a,k) %Pivotação Parcial | |
% Esse é o melhor momento para normalizar a matriz, pois já foi escolhida a melhor linha na pivotação | |
a = normalizacao(n,a,k) # Retorna a matriz normalizacao | |
for i = k + 1 : n | |
aux = a(i,k)/a(k,k); | |
for j = k + 1 : n + 1 % O k+1 evita fazer operações em que já sabemos o resultado | |
a(i,j) = a(i,j) - aux*a(k,j); | |
endfor | |
a(i,k) = 0; | |
endfor | |
endfor | |
a = normalizacao(n,a,n) % Normaliza a linha que não foi normalizada, ou seja, a última | |
a % Imprime a matriz escalonada | |
% -- Encontrar as Soluções -- | |
if(abs(a(n,n)) > 1.e-15) | |
% a(3,3)*x(3) = a(3,4) | |
% a(n,n)*x(n) = a(n, n+1) | |
x(n) = a(n,n+1)/a(n,n) | |
else | |
x(n) = 1; | |
printf("Sistema Indeterminado") | |
end | |
for i = n - 1: -1:1 % -1 é o delta, ou seja, o espaçamento ou decremento | |
soma = 0; | |
for j = i + 1 : n | |
soma += a(i,j) * x(j); % Somatorio de j = i+1 até n de a(i,j)*x(j) | |
endfor | |
x(i) = ( a(i, n+1) - soma); | |
endfor | |
x | |
% Calcular os residuos | |
r = residuos(n,x,A_Orig) | |
r(1) | |
r(2) | |
r(3) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = 3 % Matriz de ordem 3 | |
a = [ | |
[ 0.448 0.832 0.193 1]; | |
[ 0.421 0.784 -0.207 0]; | |
[-0.421 0.784 0.279 0]; | |
] | |
A_Orig = a; | |
% -- Escalonar Matriz -- | |
for k = 1 : n - 1 | |
a = pivot(n,a,k) %Pivotação Parcial | |
% Esse é o melhor momento para normalizar a matriz, pois já foi escolhida a melhor linha na pivotação | |
a = normalizacao(n,a,k) # Retorna a matriz normalizacao | |
for i = k + 1 : n | |
aux = a(i,k)/a(k,k); | |
for j = k + 1 : n + 1 % O k+1 evita fazer operações em que já sabemos o resultado | |
a(i,j) = a(i,j) - aux*a(k,j); | |
endfor | |
a(i,k) = 0; | |
endfor | |
endfor | |
a = normalizacao(n,a,n) % Normaliza a linha que não foi normalizada, ou seja, a última | |
a % Imprime a matriz escalonada | |
% -- Encontrar as Soluções -- | |
% a(3,3)*x(3) = a(3,4) | |
% a(n,n)*x(n) = a(n, n+1) | |
x(n) = a(n,n+1)/a(n,n) | |
for i = n - 1: -1:1 % -1 é o delta, ou seja, o espaçamento ou decremento | |
soma = 0; | |
for j = i + 1 : n | |
soma += a(i,j) * x(j); % Somatorio de j = i+1 até n de a(i,j)*x(j) | |
endfor | |
x(i) = ( a(i, n+1) - soma); | |
endfor | |
x | |
% Calcular os residuos | |
r = residuos(n,x,A_Orig) | |
r(1) | |
r(2) | |
r(3) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Passos seguidos em aula:
1 - Escalonar Matriz
2 - Encontrar as Soluções
3 - Calcular Resíduos
4 - Criar método de Pivotação
5 - Novo sistema com resolução de equações com normalização
6 - Criar método para Normalização
7 - Resolução de Sistemas Indeterminados
8 - Resolução de Sistemas Impossíveis