-
Notifications
You must be signed in to change notification settings - Fork 0
/
GaussianElimination11_rowchange.m
61 lines (54 loc) · 1.69 KB
/
GaussianElimination11_rowchange.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
n = 2; % the number of equations
A=[10^(-20) 1; 1 1]; %define the 3x3 matrix A
b=[2;1]; %define the column vector b
B = [A b] % 3x4 matrix
L = eye(2); % 3x3 identity matrix
P=eye(n); %3x3 matrix for permutation matrix P
for k=1:n-1 % step number (and row to be multipied)
display('Step')
k
for i=k+1:n % row number to be changed
if(B(k,k)==0) %pivot element is equal to zero
for r=i:n %search nonzero pivot element
if(B(r,k)~=0) %found nonzero pivot element
%interchange row k (B(k,:)) and row r (B(r,:)) of B
B([k,r],:)=B([r,k],:);
%interchange row k (P(k,:)) and row r (P(r,:)) of P
P([k,r],:)=P([r,k],:);
%interchange L(k,1:k-1) and L(r,1:k-1) of L
L([k,r],1:k-1)=L([r,k],1:k-1);
end
end
end
B
i
L(i,k)=B(i,k)/B(k,k); % this is the multiplier
display('The multiplier is')
L(i,k)
B(i,:)= B(i,:)-L(i,k)*B(k,:) % row operation
end
display('Matrix after the k-th elimination step is ')
B
end
display('Upper triangular matrix U= ')
U=B(:,1:n) %Upper triangular matrix U
display('Lower triangular matrix L= ')
L %Lower triangular matrix L
display('LU=')
L*U
display('Permutation matrix P')
P %Permutation matrix P
display('LU=')
L*U
display('PA=')
P*A
B
%perform backward substitution
x=B(:,n+1);
x(n)=B(n,n+1)/B(n,n)
for i=n-1:-1:1
x(i)=(B(i,n+1)-B(i,i+1:n)*x(i+1:n))/B(i,i);
end
%solution
display('The computed solution is x=')
x