Você está na página 1de 5

YAŞAR UNIVERSITY

2017-2018 FALL SEMESTER


MATH 230 COMPUTATIONAL METHODS IN ENGINEERING
LECTURER: Prof. Dr. Mehmet TERZİLER COURSE ASSISTANT: Kazım ERDOĞDU
Assist. Prof. Dr. Refet POLAT Saadet ESKİİZMİRLİLER
Vedat Can AK

LAB NOTES 04 – Gauss Jordan Elimination Method


and Inverse of Matrices in MATLAB
1. Gauss-Jordan Elimination in MATLAB

In Gauss-Jordan elimination method, we first transfer the Linear Equation System (LES) to a matrix
system. Then, after applying necessary elementary row operations, we try to achieve a matrix which consists of
an Identity matrix in the first part and the solution column in the second part. Therefore, we may find the
solution directly by this solution column. For example,

𝑢− 𝑣 + 𝑤 = 5 1 −1 1 : 5 𝐴𝑓𝑡𝑒𝑟 𝑎𝑙𝑙 𝑡ℎ𝑒 1 0 0 : 3


−𝑢 + 𝑣 + 2𝑤 = 7
2𝑢 + 𝑣 − 𝑤 = 4
⇒ 𝐴 = [−1
2
1 2 : 7]
1 −1 : 4
𝑒𝑙𝑒𝑚𝑒𝑛𝑡𝑎𝑟𝑦
𝑟𝑜𝑤 𝑜𝑝𝑒𝑟𝑎𝑡𝑖𝑜𝑛𝑠

𝐴 = [0
0
1 0 :
0 1 :
2]
4

Then we get the solution: 𝑢 = 3, 𝑣 = 2, 𝑤 = 4.


Here are the MATLAB codes for the solution of this example:
% Gauss Jordan Elimination
A = [1 -1 1 5;
-1 1 2 7;
2 1 -1 4];

A(2,:) = A(2,:) + A(1,:);


A(3,:) = A(3,:) + (-2) * A(1,:);
A([2 3],:) = A([3 2],:); % swapping the rows 2 and 3!!!
A(2,:) = (1/3) * A(2,:);
A(1,:) = A(1,:) + A(2,:);
A(3,:) = (1/3) * A(3,:);
A(2,:) = A(2,:) + A(3,:);
w = A(3,4);
v = A(2,4);
u = A(1,4);
A,u,v,w

The generalized form of it:

% Gauss-Jordan Elimination Generalized Form


clear;
clc;
A = [1 -1 1 5;
-1 1 2 7;
2 1 -1 4];

1
[m,n] = size(A);
% All row operations
% Downwards
for i=1:m
A(i,:) = A(i,:)/A(i,i);
for j=(i+1):m
A(j,:) = A(j,:)+(-1)*A(j,i)*A(i,:);
end
end
%Upwards
for i=m:-1:1
for j=(i-1):-1:1
A(j,:) = A(j,:) + (-1)*A(j,i)*A(i,:);
end
end
%Finding the values
x = []; % Solution vector
for i=1:m
x(i) = A(i,n);
end

2. Inverse of a Matrix by Gauss-Jordan Elimination in MATLAB


In order to get the inverse of a matrix by Gauss–Jordan elimination, we need to augment this matrix with
the same size Identity matrix first. Then, we apply the necessary elementary row operations until we see the first
half of this augmented matrix contains the Identity matrix. If we can obtain it, then the second half of the matrix
is the inverse of the first matrix before augmentation. For example,
3 3 −1 3 3 −1 ∶ 1 0 0 1 1 −1/3 ∶ 1/3 0 0
𝐴 = [−2 −2 1] ⇒ [𝐴: 𝐼] = [−2 −2 1 ∶ 0 1 0]
𝑅1 →(1/3)𝑅1
→ [−2 −2 1 ∶ 0 1 0]
−4 −5 2 −4 −5 2 ∶ 0 0 1 −4 −5 2 ∶ 0 0 1

𝑅2 →𝑅2 +(2)𝑅1 1 1 −1/3 ∶ 1/3 0 0 1 1 −1/3 ∶ 1/3 0 0


𝑅2 ↔𝑅3
𝑅3 →𝑅3 +(4)𝑅1 [0 0 1/3 ∶ 2/3 1 0] → [ 0 −1 2/3 ∶ 4/3 0 1]

0 −1 2/3 ∶ 4/3 0 1 0 0 1/3 ∶ 2/3 1 0

𝑅2 →(−1)𝑅2 1 1 −1/3 ∶ 1/3 0 0 𝑅1 →𝑅1 +(−1)𝑅2 1 0 1/3 ∶ 5/3 0 1


𝑅3 →(3)𝑅3 [ 0 1 −2/3 ∶ −4/3 0 −1] 𝑅2 →𝑅2 +(2/3)𝑅3 [0 1 0 ∶ 0 2 −1]
→ →
0 0 1 ∶ 2 3 0 0 0 1 ∶ 2 3 0

1 0 0 ∶ 1 −1 1 1 −1 1
𝑅1 →𝑅1 +(−1/3)𝑅3
→ [ 0 1 0 ∶ 0 2 −1] ⇒ 𝐴−1 = [0 2 −1]
0 0 1 ∶ 2 3 0 2 3 0

Here are the MATLAB codes for this solution:


% Inverse of a Matrix by Gauss Jordan Elimination
A = [3 3 -1;
-2 -2 1;
-4 -5 2];
2
I = eye(3);
B = [A,I];

B(1,:) = (1/3) * B(1,:);


B(2,:) = B(2,:) + (2) * B(1,:);
B(3,:) = B(3,:) + (4) * B(1,:);
B([2 3],:) = B([3 2],:); % swap of rows 2 and 3
B(2,:) = (-1) * B(2,:);
B(1,:) = B(1,:) + (-1) * B(2,:);
B(3,:) = (3) * B(3,:);
B(2,:) = B(2,:) + (2/3) * B(3,:);
B(1,:) = B(1,:) + (-1/3) * B(3,:);
B
C = B(:,4:6); % This command takes the second half of our matrix
(i.e. rows 1 through 3 and columns 4 through 6) and assigns it to C.
C
D = inv(A);
D

Here the matrix D is also the inverse of matrix A. It is calculated by the MATLAB function, inv(). This
function calculates the inverse of a matrix. In our example, both matrix C and D are the same which shows that
we have calculated the inverse of matrix A correct. ☺
The generalized form of it:

% Inverse of a matrix by Gauss-Jordan Elimination Generalized Form


clear; clc;
A = [3 3 -1;
-2 -2 1;
-4 -5 2];
[m,n] = size(A);
I = eye(m);
B = [A,I];
[m,n] = size(B);
% All row operations
% Downwards
for i=1:m
B(i,:) = B(i,:)/B(i,i);
for j=(i+1):m
B(j,:) = B(j,:)+(-1)*B(j,i)*B(i,:);
end
end
%Upwards
for i=m:-1:1
for j=(i-1):-1:1
B(j,:) = B(j,:) + (-1)*B(j,i)*B(i,:);
end
end

3
%Inverse
C = B(:,(n/2)+1:n);
3. Inverse of a Matrix by Adjoint Matrix in MATLAB
This is how we get the inverse of a matrix by Adjoint Matrix:
1
𝐴−1 = . 𝑎𝑑𝑗(𝐴)
|𝐴|
Here |𝐴| is the determinant of matrix A and 𝑎𝑑𝑗(𝐴) is the adjoint matrix of A.
Since we will be using MATLAB function det( ) for calculating the determinant of matrix A, we will
not deal with how to get the determinant in this lab notes. You may check your class notes for that.
However, we will be dealing with how to calculate 𝑎𝑑𝑗(𝐴). It is calculated as following:
𝑎𝑑𝑗(𝐴) = 𝐶 𝑇 ,
where 𝐶 𝑇 is the transpose of the coefficients matrix of A. And 𝐶 𝑇 is calculated as such:
𝐶 = (−1)𝑖+𝑗 . 𝑀
where 𝑀 is the minors matrix. We calculate it as this:
𝑚𝑖𝑗 = |𝐵|
where 𝑚𝑖𝑗 is the elements of matrix 𝑀, and 𝐵 is the matrix that is obtained by deleting the ith row and jth
column from matrix 𝐴.
Let us try to find the inverse of the matrix in previous example.

3 3 −1 3 3 −1
𝐴 = [−2 −2 1] ⇒ |𝐴| = |−2 −2 1| = 1
−4 −5 2 −4 −5 2

−2 1 −2 1 −2 −2
𝑚11 = | |=1 𝑚12 = | |=0 𝑚13 = | |=2
−5 2 −4 2 −4 −5
3 −1 3 −1 3 3
𝑚21 =| |=1 𝑚22 =| |=2 𝑚23 = | | = −3
−5 2 −4 2 −4 −5
3 −1 3 −1 3 3
𝑚31 =| |=1 𝑚32 =| |=1 𝑚33 = | |=0
−2 1 −2 1 −2 −2

1 0 2
Hence, matrix of minors is 𝑀 = [1 2 −3].
1 1 0
Now we can calculate the matrix of coefficients by the formula 𝐶 = (−1)𝑖+𝑗 . 𝑀
1 0 2 1 −1 1
Here is the matrix 𝐶 = [−1 2 3]. Then the transpose of it is 𝐶 𝑇 = [0 2 −1] = 𝑎𝑑𝑗(𝐴).
1 −1 0 2 3 0
1
Now the only thing we need to do it is to put all these together in the formula 𝐴−1 = |𝐴| . 𝑎𝑑𝑗(𝐴).

4
1 1 −1 1 1 −1 1
𝐴−1 = . [0 2 −1] = [0 2 −1]
1
2 3 0 2 3 0
As you can tell, this solution has given us the same solution in the previous method’s example.
Here are the MATLAB codes for this solution:
A = [3 3 -1
-2 -2 1
-4 -5 2];

determinant = det(A);
M = []; %Minor Matrix
M(1,1) = det(A([2 3],[2 3])); % A([2 3],[2 3]) means that we only get
2nd and 3rd rows and 2nd and 3rd columns in matrix A
M(1,2) = det(A([2 3],[1 3]));
M(1,3) = det(A([2 3],[1 2]));
M(2,1) = det(A([1 3],[2 3]));
M(2,2) = det(A([1 3],[1 3]));
M(2,3) = det(A([1 3],[1 2]));
M(3,1) = det(A([1 2],[2 3]));
M(3,2) = det(A([1 2],[1 3]));
M(3,3) = det(A([1 2],[1 2]));
M
C = []; % Cofactor Matrix
for i=1:3
for j=1:3
C(i,j) = ((-1)^(i+j)) * M(i,j);
end
end
C
adjointA = transpose(C);
adjointA
B = (1/determinant) * adjointA;
B
inv(A)

Você também pode gostar