-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgaussElimination.m
More file actions
86 lines (79 loc) · 2.94 KB
/
gaussElimination.m
File metadata and controls
86 lines (79 loc) · 2.94 KB
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
82
83
84
85
86
function [X, isSingular] = gaussElimination(coefficients, results, n, tolerance)
% coefficients is the coeffecients square matrix
% results is the results matrix
% n is the number of equations
% tolerance is the smallest allowable scaled pivot
% X is the solution matrix
% isSingular is a boolean indicating if the system is singular, ie not solvable
scaledFactors = zeros(n, 1); % an n-element array for storing scaling factors
for i = 1 : n
scaledFactors(i) = abs(coefficients(i, 1));
for j = 2 : n
if abs(coefficients(i, j)) > scaledFactors(i)
scaledFactors(i) = abs(coefficients(i, j));
end
end
end
[coefficients, results, isSingular] = forwardElimination(coefficients, results, scaledFactors, n, tolerance);
if ~isSingular
[X] = backwardSubstitution(coefficients, results, n);
end
end
function [coefficients, results, isSingular] = forwardElimination(coefficients, results, scaledFactors, n, tolerance)
for row = 1 : n - 1
[coefficients, results, scaledFactors] = pivot(coefficients, results, scaledFactors, n, row);
if abs(coefficients(row, row) / scaledFactors(row)) < tolerance
isSingular = true;
return;
end
% forward elimination
for i = row + 1 : n
factor = coefficients(i, row) / coefficients(row, row);
for j = row + 1 : n
coefficients(i, j) = coefficients(i, j) - factor * coefficients(row, j);
end
results(i) = results(i) - factor * results(row);
end
end
if abs(coefficients(n, n) / scaledFactors(n)) < tolerance
isSingular = true;
return;
end
isSingular = false;
end
function [coefficients, results, scaledFactors] = pivot(coefficients, results, scaledFactors, n, row)
pivotRow = row;
% Find the largest scaled coefficient in column k
max = abs(coefficients(row, row) / scaledFactors(row));
for i = row + 1 : n
temp = abs(coefficients(i, row) / scaledFactors(i));
if temp > max
max = temp;
pivotRow = i;
end
end
% swapping the rows
if pivotRow ~= row
for j = row : n
temp = coefficients(pivotRow, j);
coefficients(pivotRow, j) = coefficients(row, j);
coefficients(row, j) = temp;
end
temp = results(pivotRow);
results(pivotRow) = results(row);
results(row) = temp;
temp = scaledFactors(pivotRow);
scaledFactors(pivotRow) = scaledFactors(row);
scaledFactors(row) = temp;
end
end
function [X] = backwardSubstitution(coefficients, results, n)
X(n) = results(n) / coefficients(n, n);
for i = n - 1 : -1 : 1
sum = 0;
for j = i + 1 : n
sum = sum + coefficients(i, j) * X(j);
end
X(i) = (results(i) - sum) / coefficients(i, i);
end
end