-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearSystemUsingGaussElimination
More file actions
56 lines (47 loc) · 1.14 KB
/
Copy pathLinearSystemUsingGaussElimination
File metadata and controls
56 lines (47 loc) · 1.14 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
%% Q2
%% Solved by Dr. Raghavan Ranganathan, Ranga Teja
clc
clear
close
a = [1 2 -1 1; 0 1 1 1; 1 1 1 1;-1 2 0 1];
n = size(a,2);
b = eye(n);
if det(a)== 0 % if det is zero, we cant find the solution
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
end
for i = 1 : n
for j = 1 : n
b(i,j) = b(i,j) - m(i,k)*b(k,j);
end
end
% a
% b
% pause
end
end
%% Backward substitution:
x = zeros(n,n); % variable to store the inverse
for j = 1 : n
x(n,j) = b(n,j)/a(n,n);
end
for k = 1 : n % Repeat for k columns of the transformed 'b' Identity matrix
for i = n-1 : -1 : 1
rsum = 0;
for j = i+1 : n
rsum = rsum + a(i,j)*x(j,k);
end
x(i,k) = (b(i,k)-rsum)/a(i,i);
end
end
% Check for correctness:
a = [1 2 -1 1; 0 1 1 1; 1 1 1 1;-1 2 0 1];
x - inv(a)