Professional Documents
Culture Documents
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 = /aij/mxn
A = [ 1 2 3 4 ….. n ] 1xn
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
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
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
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 * 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
A-1 = adj AT
|A|
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
Ex.
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
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,
Ax b
after decomposition
LU 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.
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,
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.
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
>> [L,U]=lu(A)
L=
1.0000 0 0
-0.3333 1.0000 0
-0.5000 -0.6923 1.0000
U=
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=
>> X=V*Y
X=
1.0000
4.0000
3.0000
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
>> 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
>> 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:________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
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
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
[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
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
GAUSS-SEIDEL METHOD
% Gauss-Seidel method
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