-
Notifications
You must be signed in to change notification settings - Fork 0
/
gajopi.m
69 lines (53 loc) · 1.13 KB
/
gajopi.m
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
function sol = gajopi(a,b)
format long;
%this function solves a linear equation by gauss jordan elimination method
%with pivoting
% input is a and b, where the equation is ax = b;
%input validation
sa = size(a);
sb = size(b);
if sa(2) ~= sb(1)
sol = NaN;
return;
end
%main code starts
A=[a b];
n = length(a);
for i = 1:n
%code for pivotal condensation
max = abs(A(i,i));
p = i; %p holds the row number of the row where amk > akk
for l = i+1:n
if abs(A(l,i)) > max
max = A(l,i);
p = l;
end
end
%exchanging row
if (p ~= i)
%exchanging p-th and i-th row
for q = 1:(n+1)
temp = A(i,q);
A(i,q) = A(p,q);
A(p,q) = temp;
end
end
%
for j = 1:(n-i)
A(j+ i,:) = A(j+i,:) - ( (A(i,:)./A(i,i)) * A(j+i,i)) ;
end
end
x = zeros(n,1);
%back substitution
for i = 0:n-1
sum = 0;
for j = 1:n
sum = sum + A(n-i,j)*x(j, 1);
end
x(n-i) = (A(n-i, n+1) - sum)/A(n-i, n-i);
end
sol = x;
end
% %sample data
% a=[2 3 5;3 4 1;6 7 2];
% b=[23 14 26]';