You are on page 1of 25

Name:__________________________________ Date:_____________________

Course & Section:_________________________ Program & Year:____________

Experiment 2
Solving a Systems of Linear Equations

Objectives
By the end of the experiment, the student should be able to
 understand the matrix operation.
 know how to solve linear equations using MATLAB.
 know how to apply the direct method in evaluating linear equations using
MATLAB.
 understand the iterative method in solving linear equations using MATLAB.

Tools Required
 Personal Computer/Desktop Computer/Computer Workstation
 MATLAB software

MATRIX
A matrix consists of a rectangular array of elements represented by a single symbol.

A = a11 a12 a13


a21 a22 a23
a31 a32 a33 3x3

A = /aij/mxn

where: aij = element of matrix A


mxn = size of order of matrix
m = number of row matrix
n = number of column matrix

Special Type of Matrices

Row Vector Matrix


A matrix that contain only one row and have a several columns.
Ex.

A = [ 1 2 3 4 ….. n ] 1xn

Column Vector Matrix


A matrix that contain only one column and have a several rows.
Ex.

A= 1
2
3
.
.
.
n m x1

Square Matrix
A matrix where elements in the rows and columns were equal.
Ex.
A = 1 4 3
4 4 5
2 5 4

Null or Zero Matrix


A square matrix with elements at the rows and columns were all zeros.

Ex.
A = 0 0
0 0

Diagonal Matrix
A square matrix wherein the only values lie in the main diagonal and the rest were all
zeros.
Ex.
A = 3 0 0
0 4 0
0 0 3

Unity or Identity Matrix


A square matrix whose elements in the main diagonal are all 1’s and the rest were all
zeros.
Ex.
A = 1 0 0
0 1 0
0 0 1

Symmetric Matrix
A square matrix whose element Aixj is equal to the element Ajxi or the elements of the
rows corresponds to that of the column.
Ex.
A = 1 -5 6
-5 7 2
6 2 3

Skew Matrix
A square matrix whose element Aixj is equal to the negative of the element Ajxi.
Ex.
A = 1 5 -6
-5 7 2
6 -2 3

Singular Matrix
A square matrix whose determinant value is equivalent to 0.
Ex.
A = 1 4
2 8

Non-singular Matrix
A square matrix whose determinant value is not equivalent to 0.
Ex.
A = 1 4 3 determinant value
4 4 5 /A/ = 3
2 5 4

MATRIX MATHEMATICAL OPERATION


In matrix operation, only addition, subtraction, and multiplication are defined. Division is
done by a different technique.

Addition / Subtraction of a Matrix


-two matrices may be added/subtracted, if and only if the size of the two matrices are
equal.

Amxn + Bmxn = Cmxn

Example.
A= 1 4 7 B= 2 5 1
2 5 8 1 6 4
3 6 9 3x3 0 3 7 3x3

A+B=C= 1 4 7 2 5 1
2 5 8 + 1 6 4
3 6 9 0 3 7
C= 3 9 8
3 11 12
3 9 16

Multiplication of a Matrix

By scalar
-each elements of a matrix will be multiplied to a scalar value.
Ex.
A =1 0 1
3 -4 3
4 5 2

Ax5=5 0 5
15 -20 15
20 20 10

By another matrix
-two matrices can be multiplied if the number of column (left hand) of the first matrix is
equal to the number of row (right hand) of the second matrix.

A = a11 a12 a13 B = b11 b12


a21 a22 a23 b21 b22
a31 a32 a33 3x3 b31 b32 3x2

A * B = (a11b11+a12b21+a13b31) (a11b12+a12b22+a13b32)
C 11 C 12
(a21b11+a22b21+a23b31) (a21b12+a22b22+a23b32)
C 21 C 22
(a31b11+a32b21+a33b31) (a31b12+a32b22+a33b32)
C 31 C 32

Division
Division of a matrix was restricted to square matrix only. The denominator matrix will be
converted to its equivalent inverse matrix then it will be multiplied to the numerator
(either scalar value or another matrix) not conformable to matrices of unequal rows &
columns. Hence, this operation is restricted to square matrices only.
Inverse of a matrix

B = 1 = A-1
A

where A-1 inverse of matrix A

A-1 = adj AT
|A|

where: |A| = determinant value of a matrix


AT = transpose of a matrix
adj = adjoint of a matrix

Transpose of a matrix ( AT )
-to determine the transpose of a matrix, interchange the corresponding rows and
columns of the given determinant.

Ex.

A= 1 4 3
4 4 5
2 5 4

AT = 1 4 2
4 4 5
3 5 4

Adjoint of a Matrix ( adj.)


-to obtain the adjoint of any matrix, replace each element by its corresponding co-factor.

Ex.
A= 1 4 3
4 4 5
2 5 4

adj A = (+) 4 5 (-) 4 5 (+) 4 4


5 4 2 4 2 5

(-) 4 3 (+) 1 3 (-) 1 4


5 4 2 4 2 5

(+) 4 3 (-) 1 3 (+) 1 4


4 5 4 5 4 4

Ex. Solve for the inverse matrix.


A = 1 4 3
4 4 5
2 5 4

A-1 = adj AT
|A|

|A| = 3

AT = 1 4 2
4 4 5
3 5 4

adj AT = (+) 4 5 (-) 4 5 (+) 4 4


5 4 3 4 3 5

(-) 4 2 (+) 1 2 (-) 1 4


5 4 3 4 3 5

(+) 4 2 (-) 1 2 (+) 1 4


4 5 4 5 4 4

A-1 = -3 -1/3 8/3


-2 -2/3 7/3
4 1 -4

Techniques in evaluating system of linear equations


Simultaneous linear equations can be represented in a single matrix form and can be
subject for different matrix operations, to find the unknown variables. Listed below the
different methods:

I. Direct Methods
* Conventional Methods
- Cramer's Rule (using determinants)
- Gauss-Jordan Elimination
- Gauss-Jordan Reduction
* Matrix Decomposition (LU decomposition)
- Crout's/Cholesky's Method
- Doolittle Method
Matrix Decomposition (LU decomposition)
LU decomposition methods separate the time-consuming elimination of the system
matrix [A] from the manipulations of the right-hand side, constant matrix, [b]. Thus, once
the system matrix has been “factored” or “decomposed”, multiple right-hand-side
vectors can be evaluated in an efficient manner.

from,
Ax  b
after decomposition
LU x  b
let
[ y ]  U x  equation (1)
then,
L y   b equation (2)

From the matrix equation (2) evaluate for the variables of matrix [y] by using forward
substitution. Substitute the values of the matrix [y] to the initial assumption in equation
(1) and determine the values of the unknown variables in matrix [x] by reverse
substitution.

Crout’s / Cholesky’s Method


One of the most popular approaches involves Cholesky method (also called Cholesky
decomposition). This method is based on the fact that a symmetric matrix can be
decomposed,

 a11 a12 a13 


A  a 21 a 22 a 23
a 31 a 32 a 33

A  L U 
 L11 0 0 1 U 12 U 13 
L   L 21 L32 0 
 U   0 1 U 23
 L31 L32 L33 0 0 1 

Doolittle Method
Similar with Cholesky method and this method is also based on a symmetric matrix that
can be decomposed,

 a11 a12 a13 


A  a 21 a 22 a 23
a 31 a 32 a 33

A  L U 
1 0 0 U11 U 12 U 13 
L   L 21 1 0
 U    0 U 22 U 23
 L31 L32 1  0 0 U 33 

MATLAB COMMAND
LU DECOMPOSITION
lu lu factorization.
[L,U] = lu(A) returns an upper triangular matrix in U and a permuted
lower triangular matrix in L, such that A = L*U. The input matrix A can
be full or sparse.

[L,U,P] = lu(A) returns unit lower triangular matrix L, upper


triangular matrix U, and permutation matrix P such that P*A = L*U.

[L,U,P] = lu(A, outputForm) returns P in the form specified by


outputForm:
'matrix' - (default) P is a matrix such that P*A = L*U.
'vector' - P is a vector such that A(P,:) = L*U.

The following syntaxes *only* apply for a sparse input matrix A:

[L,U,P,Q] = lu(A) returns unit lower triangular matrix L, upper


triangular matrix U, and row/column permutation matrices P and Q such
that P*A*Q = L*U. For sparse matrices this is significantly more time
and memory efficient than the three-output syntax.

[L,U,P,Q,D] = lu(A) also returns a diagonal scaling matrix D such that


P*(D\A)*Q = L*U for sparse A. Typically, the row-scaling leads to a
sparser and more stable factorization. Note that this is the
factorization used by sparse MLDIVIDE.

[___] = lu(A,THRESH) specifies the pivoting strategy employed by lu.


THRESH is a scalar or two-element vector with values in [0 1].
Increasing the value of THRESH tends to lead to higher accuracy, but
typically at a greater cost in time and memory. Depending on the number
of output arguments specified, the default value and requirements for
the thresh input are different:
* If 3 or fewer outputs are specified, then THRESH is a scalar. The
default value is 1.0.
* If 4 or more outputs are specified, then THRESH is a two-element
vector. The default value is [0.1 0.001].

[___] = lu(___, outputForm) specifies the output form of P and Q. Use


this option to return P and Q as vectors instead of matrices. P and Q
satisfy different identities depending on the number of outputs
specified and whether they are matrices or vectors:
With 4 outputs:
'matrix' - (default) P is a matrix such that P*A*Q = L*U.
'vector' - P is a vector such that A(P,Q) = L*U.
With 5 outputs:
'matrix' - (default) P is a matrix such that P*(D\A)*Q = L*U.
'vector' - P is a vector such that D(:,P)\A(:,Q) = L*U.

Example:
Determine the unknown mesh currents using different direct method in solving linear equation
using MATLAB.
-11 = 6 I1–2 I2–3 I3
12 = –2 I1+ 5 I2–2 I3
13 = –3 I1–2 I2+ 8 I3

>> A=[ 6 -2 -3; -2 5 -2; -3 -2 8 ]

A=

6 -2 -3
-2 5 -2
-3 -2 8

>> [L,U]=lu(A)

L=

1.0000 0 0
-0.3333 1.0000 0
-0.5000 -0.6923 1.0000

U=

6.0000 -2.0000 -3.0000


0 4.3333 -3.0000
0 0 4.4231
>> M=inv(L)

M=

1.0000 0 0
0.3333 1.0000 0
0.7308 0.6923 1.0000

[Y]=(inv[L])([C])
>> Y=M*C

Y=

-11.0000
8.3333
13.2692

[X]=(inv[U])([Y])
>> V=inv(U)

V=

0.1667 0.0769 0.1652


0 0.2308 0.1565
0 0 0.2261

>> X=V*Y

X=

1.0000
4.0000
3.0000

II. Iterative Methods


- Gauss-Jacobi Method
- Gauss-Siedel Method
- Relaxation Method

Iterative Methods
Iterative or approximate methods provide an alternative to the elimination methods.
Such approaches are similar to the techniques developed to obtain the roots of a single
equation. This approaches consisted of guessing a value and then using a systematic
method to obtain a refined estimate of the root.
Gauss-Jacobi Method
The Gauss-Jacobi method is the most commonly used iterative method for solving
linear algebraic equations. To start the solution process, initial guesses must be made
for the x's. A simple approach is to assume that they were all zero. These zeros can be
substituted to the formed equations with respect to the unknown variables, which can be
used to calculate a new value for each variables iteratively. Once the cycle of each
iteration were done, then it will return to the first equation and repeat the entire
procedure until the solution converges closely enough to the true values. Convergence
can be checked using the criterion that set by the permissible error.

Gauss-Seidel Method
The iterative process was similar to Gauss-Jacobi technique. The process were differ
only in the introduction of new values in the succeeding iteration (substitution process).
In order to forcefully accelerate convergence, the technique introduces the newly
computed variable to the next computed variable rather than the next iteration. Thereby,
improving the set of computed variables for the next iteration.
Example:
Determine the unknown mesh currents using different iterative method in solving linear
equation using MATLAB.
-11 = 6 I1–2 I2–3 I3
12 = –2 I1+ 5 I2–2 I3
13 = –3 I1–2 I2+ 8 I3

Gauss-Jacobi Method

>> I1=0;I2=0;I3=0;
>> I1a=(-11+2*I2+3*I3)/6

I1a =

-1.8333

>> I2a=(12+2*I1+2*I3)/5

I2a =

2.4000

>> I3a=(13+3*I1+2*I2)/8

I3a =

1.6250

>> Error1=abs(I1a-I1)

Error1 =

1.8333

>> Error2=abs(I2a-I2)

Error2 =

2.4000

>> Error3=abs(I3a-I3)

Error3 =
1.6250

.......up to 53th iterations

>> I1=0.999999;I2=3.999999;I3=2.999999;
>> I1a=(-11+2*I2+3*I3)/6

I1a =

1.0000

>> I2a=(12+2*I1+2*I3)/5

I2a =

4.0000

>> I3a=(13+3*I1+2*I2)/8

I3a =

3.0000

>> Error1=abs(I1a-I1)

Error1 =

1.6667e-07

>> Error2=abs(I2a-I2)

Error2 =

2.0000e-07

>> Error3=abs(I3a-I3)

Error3 =

3.7500e-07

Gauss-Seidel Method

>> I1=0;I2=0;I3=0;
>> I1a=(-11+2*I2+3*I3)/6
I1a =

-1.8333

>> I2a=(12+2*I1a+2*I3)/5

I2a =

1.6667

>> I3a=(13+3*I1a+2*I2a)/8

I3a =

1.3542

>> Error1=abs(I1a-I1)

Error1 =

1.8333

>> Error2=abs(I2a-I2)

Error2 =

1.6667

>> Error3=abs(I3a-I3)

Error3 =

1.3542

......up to 28th iteration

>> I1=0.999999;I2=3.999999;I3=3;
>> I1a=(-11+2*I2+3*I3)/6

I1a =

1.0000

>> I2a=(12+2*I1a+2*I3)/5
I2a =

4.0000

>> I3a=(13+3*I1a+2*I2a)/8

I3a =

3.0000

>> Error1=abs(I1a-I1)

Error1 =

6.6667e-07

>> Error2=abs(I2a-I2)

Error2 =

8.6667e-07

>> Error3=abs(I3a-I3)

Error3 =

1.5833e-07

Exercises:

1. Determine the unknown variable by using LU decomposition. Use additional sheet for the
syntax.
(a).
3v + 6x - 3y + 3z = 18
-3v + 6x + 6y - 3z = 9
3v + 6x - 3y + 6z = 24
6v - 3x + 6y + 6z = 42

Answer:_______________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
Syntax:________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________

(b).
4X1 – 3X2 + X3 – X4 + 2X5 – X6 = 8
-X1 – 3X2 + X3 + X4 – 2X5 + X6 = -7
-3X1 – 2X2 + X3 – 3X4 + X5 – 4X6 = 4
2X1 + X2 – 2X3 – 2X4 + X5 + 4X6 = 9
X1 + X2 – 3X3 + X4 – 2X5 + 5X6 = 2
-X1 – 4X2 + 5X3 + X4 + 4X5 + 5X6 = 9

Answer:_______________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________

Syntax:________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________

2. Determine the unknown variable by applying Gauss-Jacobi Method and Gauss-Seidel


Method, compare the results of the two methods. Use additional sheet for the syntax.
(a).
3v + 6x - 3y + 3z = 18
-3v + 6x + 6y - 3z = 9
3v + 6x - 3y + 6z = 24
6v - 3x + 6y + 6z = 42

Gauss- Jacobi Method


Answer:_______________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________

Syntax:________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
Gauss-Seidel Method
Answer:_______________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________

Syntax:________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
(b).
4X1 – 3X2 + X3 – X4 + 2X5 – X6 = 8
-X1 – 3X2 + X3 + X4 – 2X5 + X6 = -7
-3X1 – 2X2 + X3 – 3X4 + X5 – 4X6 = 4
2X1 + X2 – 2X3 – 2X4 + X5 + 4X6 = 9
X1 + X2 – 3X3 + X4 – 2X5 + 5X6 = 2
-X1 – 4X2 + 5X3 + X4 + 4X5 + 5X6 = 9

Gauss- Jacobi Method


Answer:_______________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________

Syntax:________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________

Gauss-Seidel Method
Answer:_______________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________

Syntax:________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________

3. Create and run the following M-files, using the given linear equations at problem 1(a)
and 1(b). Compare the results and explain
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________

LU DECOMPOSITION
% LU Factorization

A=input('Enter matrix, A: ');


b=input('Enter matrix, b: ');

[L U]=lu(A)
x = U\(L\b)

Create and run the following M-files, using the given linear equations at problem 2(a)
and 2(b). Compare the results and explain
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
GAUSS-JACOBI METHOD
% Jacobi method1

n=input('Enter number of equations, n: ');


B=input('Enter matrix, A: ');
A = zeros(n,n+1);
x1 = zeros(n);
x2 = zeros(n);
tol = input('Enter the tolerance, tol: ');
m = input('Enter maximum number of iterations, m:
');

A=B;
x1=[0 0 0 0 0 0 0 0 0 0];

k = 1;
while k <= m
err = 0;
for i = 1 : n
s = 0;
for j = 1 : n
s = s-A(i,j)*x1(j);
end
s = (s+A(i,n+1))/A(i,i);
if abs(s) > err
err = abs(s);
end
x2(i) = x1(i)+s;
end

if err <= tol


break;
else
k = k+1;
for i = 1 : n
x1(i) = x2(i);
end
end
end

fprintf('Solution vector after %d iterations is


:\n',k-1);
for i = 1 : n
fprintf(' %11.8f \n', x2(i));
end
Create and run the following M-files, using the given linear equations at problem 2(a)
and 2(b). Compare the results and explain
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________

GAUSS-SEIDEL METHOD
% Gauss-Seidel method

n=input('Enter number of equations, n: ');


B=input('Enter matrix, A: ');
A = zeros(n,n+1);
x1 = zeros(n);
tol = input('Enter the tolerance, tol: ');
m = input('Enter maximum number of iterations, m:
');

A=B;
x1=[0 0 0];

k = 1;
while k <= m
err = 0;
for i = 1 : n
s = 0;
for j = 1 : n
s = s-A(i,j)*x1(j);
end
s = (s+A(i,n+1))/A(i,i);
if abs(s) > err
err = abs(s);
end
x1(i) = x1(i) + s;
end

if err <= tol


break;
else
k = k+1;
end
end

fprintf('Solution vector after %d iterations is


:\n', k-1);
for i = 1 : n
fprintf(' %11.8f \n', x1(i));
end

You might also like