-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGaussElimination
More file actions
48 lines (37 loc) · 1.06 KB
/
Copy pathGaussElimination
File metadata and controls
48 lines (37 loc) · 1.06 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
%% Q1
%% Coded By Dr. Raghavan Ranganathan, Ranga Teja
clc
clear
close
a = [4 1 -1 0 0 0 0 0; 1 6 -2 1 -1 0 0 0; 0 1 5 0 -1 1 0 0; 0 2 0 5 -1 0 -1 -1; 0 0 -1 -1 6 -1 0 -1; 0 0 -1 0 -1 5 0 0; 0 0 0 -1 0 0 4 -1; 0 0 0 -1 -1 0 -1 5];
b = [3 -6 -5 0 12 -12 -2 2]'; % column vector
%% Matlab way of solving the linear system
x_direct = a \ b;
n = size(c,1);
m = zeros(n,n); % Matrix containing multipliers
if det(a) == 0
print('Can''t evaluate the linear system')
else
for k = 1 : n-1 % Need to iterate over n-1 steps
for i = k+1 : n
m(i,k) = a(i,k)/a(k,k); % Define the multipliers
end
for i = k+1 : n
for j = k : n
a(i,j) = a(i,j) - m(i,k)*a(k,j);
end
b(i) = b(i) - m(i,k)*b(k);
end
end
end
%% Backward substitution:
x(n) = b(n)/a(n,n);
for i = n-1 : -1 : 1
rsum = 0;
for j = i+1 : n
rsum = rsum + a(i,j)*x(j);
end
x(i) = (b(i)-rsum)/a(i,i);
end
%% compare the solutions:
x_direct - x