-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEscalonamento_Gauss.sci
81 lines (61 loc) · 2.31 KB
/
Escalonamento_Gauss.sci
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
function [Ab, P, sub] = gauss(A, b)
[linhas colunas] = size(A);
sub = zeros(linhas,colunas+1);
P = eye(linhas,colunas);
//----------------------------------------------------------
//----------------------------------------------------------
//----------------------------------------------------------
//----PIVOTAMENTO PARCIAL
for j = 1:colunas
pivo = A(j,j);
//Pivotamento Parcial
maiorLinha = j;
for i = (j+1):linhas
//Procurar o maior elemento em modulo abaixo do pivo
if (abs(A(i,j)) > abs(A(maiorLinha,j))) then
maiorLinha = i;
end
end
//Trocar a linha do maior elemento pela linha do pivo
aux = A(j, :);
A(j,:) = A(maiorLinha, :);
A(maiorLinha, :) = aux;
//Matriz de Permutacao
aux = P(j, :);
P(j,:) = P(maiorLinha, :);
P(maiorLinha, :) = aux;
//Atualizar o pivo apos a troca de linhas
pivo = A(j,j)
end
b_n = P*b;
//----------------------------------------------------------
//----------------------------------------------------------
//---------------------------------------------------------
Ab = [A b_n]
[linha col] = size(A);
for j = 1:col //percorrer os pivos/ colunas
for i = (j+1):1:linha//i = percorrer as linhas abaixo do pivo
sub = Ab(i,:) - (Ab(i,j)/A(j,j))*Ab(j,:);
Ab(i,:) = sub;
// disp(Ab(i,:));
//zerar o elemento que esta na linha 'i' coluna 'j'
end
end
function x = triangSup(A, b) //creates an upper matrix solving function
for i = linha:-1:1 //starts from the latest row 'till the first one.
somatorio = 0; // sum is set to start from zero
for j = i+1:linha //now it will sum the terms of this line multiplied by the 'x'related
somatorio = somatorio + Ab(i,j)*x(j);
end
x(i)= (Ab(i,$)- somatorio)/Ab(i,i); //for each iteration in rows, we find the current 'x' value
end
endfunction
//MATRIZ ESCALONADA
disp(Ab)
//VETOR SOLUCAO X
disp(triangSup(Ab, b_n));
endfunction
//========ENTRADAS PARA A MATRIZ A
A = [ 2 4 1;6 2 -1; 3 2 8]
b = [2;3;4]
[Ab P] = gauss(A, b)