Vous êtes sur la page 1sur 77

Table of Contents

I. ...............................................................................................................INTRODUCTION
1
The term linear applies to a branch of mathematics that studies vectors, matrices, vector
spaces, and systems of linear equations which means collections of two or more linear
equations with the same set of unknowns. ................................................................................. 1
II. ................................................................................... THEORETICAL BACKGROUND
2
III.NUMERICAL ANALYSIS ............................................................................................ 9
A. ................................................................................................... Computational method
21
1. ....................................................................................................... Gauss Elimination
21
2. ........................................................................................... Gauss Jordan Elimination
22
3. ......................................................................................................... LU Factorization
23
4. ................................................................................................................ Gauss Seidel
25
B. ............................................................................................................ Matlab Approach
26
1. ....................................................................................................... Gauss Elimination
27
2. ........................................................................................... Gauss Jordan Elimination
31
.................................................................................................................................................. 34
3. ......................................................................................................... LU Factorization
35
V.................................................................................................................... REFERENCES
38
I. INTRODUCTION

The term linear applies to a branch of mathematics that studies vectors, matrices,
vector spaces, and systems of linear equations which means collections of two or
more linear equations with the same set of unknowns.

A general system of m linear equations with n unknowns can be written as:

a11x1  a12 x2    a1n xn  b1


a21x1  a22 x2    a2 n xn  b2
 
am1 x1  am 2 x2    amn xn  bm

 a11 a12  a1n 


a a22  a21 
Where A =  21 = coefficient matrix
     
 
am1 am 2  amn 

 x1 
x 
x =  2  = vector of unknowns

 
 xn 

 b1 
b 
b=  2  = right-hand-side vectors
 
 
bm 

 a11 a12  a1n b1 


 
a21 a22  a21 b2 
[A][b]=  = augmented matrix
     
 
am1 am 2  amn bm 

Expressing the systems of linear equations as

Ax = b

1
The matrix A displayed in detail earlier is called an m*n matrix because it has m
rows and n columns. The rows are the horizontal arrays inside A and the columns are
the vertical arrays inside A.

II. THEORETICAL BACKGROUND

Adopting the Gauss Elimination method will find the solution of linear
simultaneous equations arising in engineering problems. In this method, equations are
solved by elimination procedure of the unknowns successively.

To solve a general set of n equations, the designed approach:

a11 x1 a12 x2  a13 x3 ..........  a1n xn  b1      (1)

a21 x1 a22 x2  a23 x3 ..........  a2n xn  b2      (2)

an1 x1 an 2 x2  an3 x3 ..........  ann xn  bn      (n)

The technique for n equations consists of two phases: elimination of the unknowns
and solution through back substitution.

2
In forward elimination of unknowns, the first phase is designed to reduce the set
of equations to an upper triangular system (Fig. 9.3a). The initial step will be to
eliminate the first unknown x1 from the second through the nth equations. To do this,
multiply Eq. (9.8a) by a21/a11 to give

a21 a a a
a21 x1  a12 x2  21 a13 x3  ...  21 a1n xn  21 b1
a11 a11 a11 a11

Subtracting it from Eq. (9.8b) will give

 a   a  a
 a22  21 a12  x2  ...   a2 n  21 a1n  xn  b2  21 b1
 a11   a11  a11

Or

a'22 x2  ...  a'2n xn  b'2

where the prime indicates that the elements have been changed from their original
values. The procedure is then repeated for the remaining equations.. Repeating the
procedure for the remaining equations results in the following modified system:

a11x1a12 x2  a13 x3 ..........  a1n xn  b1

a'22 x2  ...  a'2n xn  b'2

a'32 x2  a'33 x3  ...  a'3n xn  b'3

a'n 2 x2  a'n3 x3  ...  a'nn xn  b' n

For the foregoing steps, Eq. (9.8a) is called the pivot equation and a11 is called the
pivot element. Note that the process of multiplying the first row by a21/a11 is
equivalent to dividing it by a11 and multiplying it by a21. Normalization is the division
operation. We make this distinction because a zero pivot element can interfere with

3
normalization by causing a division by zero. We will return to this important issue
after we complete our description of naive Gauss elimination. To eliminate x2 from
a '32
Eq. (9.10c) through (9.10d), multiply Eq. (9.10b) by and subtract the result from
a ' 22
Eq. (9.10c). Perform a similar elimination for the remaining equations to yield

a11x1a12 x2  a13 x3 ..........  a1n xn  b1

a'22 x2  a'23 x3  ...  a'2n xn  b'2

a' '33 x3  ...  a' '3n xn  b' '3

a' 'n3 x3  ...  a' 'nn xn  b' 'n

where the double prime indicates that the elements have been modified twice. The
procedure can be continued using the remaining pivot equations. The final
manipulation in the sequence is to use the (n−1)th equation to eliminate the xn−1 term
from the nth equation. At this point, the system will have been transformed to an
upper triangular system:

a11x1a12 x2  a13 x3 ..........  a1n xn  b1

a'22 x2  a'23 x3  ...  a'2n xn  b'2

a' '33 x3  ...  a' '3n xn  b' '3

( n 1)
a nn x n  bn( n 1)

4
Solving for xn, Equation (9.1d), using back substitution

bn( n 1)
xn  ( n 1)
a nn

This result can be back-substituted into the (n−1)th equation to solve for xn−1. The
procedure, which is repeated to evaluate the remaining x’s, can be represented by the
following formula:

n
bi(i 1)   aij(i  j ) x j
j i 1
xi  (i  j ) for i=n-1, n-2,…1
a ii

Because division by zero is possible during both the elimination and back
substitution phases, the forgoing technique is unnecessary. Problems may also arise
when the pivot element is close, rather than exactly equal to zero because if the
magnitude of the pivot element is small compared to the other elements, then round-
off errors can be introduced. Therefore, before each row is normalized, it is
advantageous to determine the coefficient with the largest absolute value in the
column below the pivot element. This is called partial pivoting and is used to control
rounding errors.

Gauss Jordan method is commonly used to find the solution of linear simultaneous
equations. Consider the following system of linear equations:

a11 x1 a12 x2  a13 x3 ..........  a1n xn  b1      (1)

a21 x1 a22 x2  a23 x3 ..........  a2n xn  b2      (2)

a31 x1 a32 x2  a33 x3 ..........  a3n xn  b3      (3)

an1 x1 an 2 x2  an3 x3 ..........  ann xn  bn      (n)

5
Representing the numbers of linear equations using the general form:

Ax  B

Where, A is the matrix of coefficients of variables, x is the matrix of variable and


B is the matrix of constants on right hand side of linear equations.

 a 11 a 12  a 1n   x1   b1 
a a 22  a 2n  x  b 
A   21 , x   , b   2
2

      
     
a n 1 a n 2  a nn   n
x b n 

Expressing these matrices in the following form:

 a 11 a 12  a 1n b1 
 
a 21 a 22  a 2n b2 
     
 
a n 1 a n2  a nn bn 

Perform row operations and reduce the above matrix into echelon form as given
below:

1 0  0 s1 
 
0 1  0 s2 
    
 
0 0  1 sn 

s1, s2… and sn are the solution of equation.

LU factorization is very similar with Gaussian elimination with a form:

Ax  b

6
It involves the two steps of Gaussian elimination, which is the forward
substitution, and back substitution and it requires pivoting to avoid division by zero.

LU factorization refers to the factorization of A, letting A be a square matrix, with


proper row and/or column ordering or permutations, into two factors, a lower
triangular matrix L and an upper triangular matrix U,

A  LU

In the lower triangular matrix, all elements above the diagonal are zero; in the

upper triangular matrix, all the elements below the diagonal are zero.

 a11 a12 a13  l11 0 0  u11 u12 u13 


a a22 a23   l 21 l 22 0   0 u 22 u 23 
 21
a31 a32 a33  l31 l32 l33   0 0 u33 

Or

 a11 a12 a13  l11 0 0 u11 u12 u13 


A  a21 a22 a23  L  l21 l22 0  U   0 u22 u23 

a31 a32 a33  l31 l32 l33   0 0 u33 

The Lower triangular matrix is obtained by Forward substitution and the Upper
triangular matrix is obtained by Back substitution.

[L] and [U] are used to determine a solution {x} for a right-hand side {b}.
[ L]{d }  {b} is used to generate an intermediate vector {d} by forward substitution.

Then, the result is substituted into [U ]{ x}  {d } which can be solved by back


substitution for {x}.

The Gauss-Seidel method is the most commonly used iterative method for solving
linear algebraic equations. Assume that we are given a set of n equations:

Ax  b
Suppose that for conciseness we limit ourselves to a 3 × 3 set of equations. If the
diagonal elements are all nonzero, the first equation can be solved for x 1, the second
for x2, and the third for x3 to yield

7
b1  a12 x2j 1  a13 x3j 1
x1j  (1)
a11

b2  a21 x1j  a23 x3j 1


x2j  (2)
a22

b3  a31 x1j  a32 x2j


x 
3
j
(3)
a33

Where j and j −1 are the present and previous iterations.

To start the solution process, initial guesses must be made for the x’s. A simple
approach is to assume that they are all zero. These zeros can be substituted into Eq.
(1), which can be used to calculate a new value for x1 =b1/a11. Then we substitute this
new value of x1 along with the previous guess of zero for x3 into Eq. (2) to compute a
new value for x2. The process is repeated for Eq. (3) to calculate a new estimate for
x3. Then we return to the first equation and repeat the entire procedure until our
solution converges closely enough to the true values. Convergence can be checked
using the criterion that for all i,

xij  xij 1
 a ,i  100%   s
xij

As each new x value is computed for the Gauss-Seidel method, it is immediately

used in the next equation to determine another x value. Thus, if the solution is

converging, the best available estimates will be employed. An alternative approach,

called Jacobi iteration, utilizes a somewhat different tactic. Rather than using the

latest available x’s, this technique uses the previous equations to compute a set of new

x’s on the basis of a set of old x’s. Thus, as new values are generated, they are not

immediately used but rather are retained for the next iteration. The difference between

the Gauss-Seidel method and Jacobi iteration is depicted in Fig.12.1. Although there

are certain cases where the Jacobi method is useful, Gauss-Seidel’s utilization of the

best available estimate usually makes it the method of preference.

8
III. NUMERICAL ANALYSIS
A. GAUSS ELIMINATION
1. GAUSS ELIMINATION WITHOUT PIVOTING
a. Algorithm
Step 1. Start
Step 2. Input A, b
Step3. Declare the variables and read the order of the matrix, n.
Take the coefficients of the linear equation as:
Do for i = 1 to n.
Do for j = 1 to n+1.
Read aij.
End for j.
End for i.

Step 4. Do for k = 1 to n-1. Loop over all rows in matrix except last.

Do for i = k+1 to n. Loop over all rows below the diagonal


position.

9
Do for j = k+1 to n. Loop over all columns to the right of the
diagonal position.
Update coefficient for row (i) and column (j) using:

aij = aij –[(aik /akk)* akj].

Update right hand side for row (i) and column (j) using:

bi = bi –[(aik /akk)* bk].


End for j.
End for i.
End for k.

Step 5. Compute last unknown, xn= an(n+1)/ann. Note: an(n+1) =bn.

Step 6. Do for k = n-1 to 1. Loop backwards over all rows except last.

sum = 0

Do for j = k+1 to n. Loop over all columns to the right of current


row.

sum = sum + akj * xj

End for j.
Compute kth unknown.

xk = (1/akk)* (ak(n+1) – sum). Note ak(n+1) = bk.


End for k.

Step 7. Display the result xk.

Step 8. End.

10
b. Flowchart
START

INPUT A, b

For k=1, n-1

For i=k+1,n

mik=aik/akk

For j=k+1, n

aij=aij [mik*akj]

bi = bi – [mik* bk]

xn = an(n+1)/ann

For k = n-1, 1

sum = 0

For j = k+1, n

sum = sum + (akj * xj)

xk = (1/akk)* (ak(n+1) – sum)

Display output x

E
ND
11
To start with, input the coefficients of matrix A and values of the right-hand side vectors,
b. Next is to read the size of the matrix by letting m as the number of rows and n as the
number of columns. Note that if the given is a square matrix, it is reasonable that m should be
equal to n. In the flowchart presented above, n is also for the number of rows for a square
matrix is assumed.

To get with the gauss elimination proper, forward elimination is employed. With this,
looping is done from the first element of the diagonal position k up to the elements below it.
The forward elimination process zeroes out the elements of i or rows below the diagonal.
After that, the last vector of unknown xn is computed then backward substitution starts form
this point. Vector of unknowns are computed and backsubtituted up to the first row to find all
unknowns. Lastly the solution set is displayed.

c. M-file

function x = GaussNaive(A,b) % GaussNaive: naive


Gauss elimination
% x = GaussNaive(A,b):
Gauss elimination without pivoting.
% input:
A = [ 130 -30 0; 90 -90 0; 40 60 -120 ]; % A =
d. M-file:
coefficient matrix
b = [ 200; 0; -500 ]; % b = right hand side vector
% Determining size of A
[m,n] = size(A); % m = number of rows; n
= number of columns
if m~=n, error('Matrix A must be square');
end

nb = n+1; % nb = column for


matrix b
Aug = [A b]; % matrix A with added
column (matrix b)

% forward elimination
for k = 1:n-1 % loop forward
elimination to elements from 1st(pivot element) to (n-1)th row of matrix A
for i = k+1:n % loop forward
elimination to elements from 2nd to nth row of matrix A
factor = Aug(i,k)/Aug(k,k); % factor = scalar
quantity to be multiplied to all elements in a given row
Aug(i,k:nb) = Aug(i,k:nb)-factor*Aug(k,k:nb); % update elements in
ith row and kth to (n+1)th column
end
end

% back substitution
x = zeros(n,1); % elements below the
diagonal should be equal to zero
x(n) = Aug(n,nb)/Aug(n,n); % getting nth unknown
% output:
% x = solution vector
for i = n-1:-1:1
x(i) = (Aug(i,nb)-Aug(i,i+1:n)*x(i+1:n))/Aug(i,i); % backsubstitute x(n)'s
starting from (n-1) row
end

12
2. GAUSS ELIMINATION WITH PIVOTING
a. Algorithm
Step 1. Start.
Step 2. Input A, b.
Step 3. Declare the variables and read the order of the matrix, n.
Step 4. Take the coefficients of the linear equation as:
Do for i = 1 to n.
Do for j = 1 to n+1.
Read aij.
End for j.
End for i.
Step 5. Determine the maximum absolute value in the column below the pivot
element and make it as the new pivot element.
Step 6. Do for i = 1 to n.
Do for k = 1 to n.
Do for j = k+1 to n.
Switch rows if aij is equal to zero.
Step 7. Do for k = 1 to n-1. Loop over all columns in matrix except last.
Do for i = k+1 to n. Loop over all rows below the diagonal
position.
Do for j = k+1 to n. Loop over all columns to the right of the diagonal
position.
Update coefficient for row (i) and column (j) using: aij = aij –[(aik /akk)*
akj].
Step 8. Update right hand side for row (i) and column (j) using: bi = bi –[(aik
/akk)* bk].
End for j.
End for i.
End for k.
Step 9. Compute last unknown, xn= an(n+1)/ann. Note: an(n+1) = bn.
Step 10. Do for k = n-1 to 1. Loop backwards over all rows except last.
sum = 0
Do for j = k+1 to n. Loop over all columns to the right of current row.
sum = sum + akj * xj
13
End for j.
Compute kth unknown. xk = (1/akk)* (ak(n+1) – sum). Note ak(n+1) = bk.
End for k.
Step 11. Display the result xk.
Step 12. End.

14
b. Flowchart
STA
RT

Input A, b

Determine element with


maximum absolute value

For i = 1 to n

For k = 1 to n

aik = N
0 O
Y
ES
Switch i

For k = 1, n-1

For i = k +1, n

mik = aik /akk

For j = k+ 1, n

aij = aij [mik * akj]

bi = bi – [mik* bk]

xn = an(n+1)/ann

A
15
A

For k = n-1, 1

sum = 0

For j = k+1, n

sum = sum + (akj * xj)

xk = (1/akk)* (ak(n+1) – sum)

xk = (1/akk)* (ak(n+1) – sum)

E
ND

In gauss elimination with pivoting, before forward elimination is employed, zeros within
the diagonal position is relocated within the matrix such that null values are either below or at
the right-hand position of the diagonal. Upon detecting zero(s) in the diagonal, rows are being
switched. When all the diagonal elements are of scalar quantity, forward elimination starts.

16
c. M-file

function x = GaussPivot(A,b) % GaussPivot: Gauss


elimination pivoting
% x = GaussPivot(A,b): Gauss
elimination with pivoting.
% input:
A = (‘Enter the coefficient matrix’); % A = coefficient
matrix
b = [ ‘Enter right hand side vector’); % b = right
hand side vector
% Determining size of A
[m,n]=size(A); % m = number of rows; n =
number of columns
if m~=n, error('Matrix A must be square');
end

nb = n+1; % nb = column for matrix b


Aug = [A b]; % matrix A with added column
(matrix b)

% forward elimination
for k = 1:n-1 % loop forward elimination
to elements from 1st(pivot element) to (n-1)th row of matrix A
% partial pivoting
[big,i]=max(abs(Aug(k:n,k))); % find for the largest
absolute value in the column below the pivot element and make it as the new
pivot element
ipr=i+k-1;
if ipr~=k % value of element in the
diagonal is nearly zero or equal to zero
Aug([k,ipr],:)=Aug([ipr,k],:); % switch rows
end
for i = k+1:n % loop forward elimination
to elements from 2nd to nth row of matrix A
factor=Aug(i,k)/Aug(k,k); % factor = scalar quantity
to be multiplied to all elements in a given row
Aug(i,k:nb)=Aug(i,k:nb)-factor*Aug(k,k:nb); % update elements in ith row
and kth to (n+1)th column
end
end

% back substitution
x = zeros(n,1); % elements below the
diagonal should be equal to zero
x(n)= Aug(n,nb)/Aug(n,n); % getting nth unknown
% output:
% x = solution vector
for i = n-1:-1:1
x(i) = (Aug(i,nb)-Aug(i,i+1:n)*x(i+1:n))/Aug(i,i); % backsubstitute x(n)'s
starting from (n-1) row
end

17
B. GAUSS JORDAN ELIMINATION

1. Gauss Jordan Without Pivoting


a. Algorithm
Step 1. Start
Step 2. Read the order of the matrix ‘n’ and read the coefficients of the
linear equations.
Step 3. Do for k=1 to n
Do for l=k+1 to n+1
a[k][l] = a[k][l] / a[k][k]
End for l
Set a[k][k] = 1
Do for i=1 to n
if (i not equal to k) then,
Do for j=k+1 to n+1
a[i][j] = a[i][j] – (a[k][j] * a[i][k])
End for j
End for i
End for k
Step 4. Do for m=1 to n
x[m] = a[m][n+1]
Display x[m]
End for m
Step 5. End

18
b. Flowchart

St
art

For i = 0, n-1

For j = 0, n

INPUT a[i] [j]

END for j

END for i

If i<> j

t=a[i][j] / a[j][i]

For k = 0, n

a[i][k] = -a[j][k]*t

END for k

For i = 0, n-1

19
A

For j = 0, n

PRINT a [i] [j]

END for i

END for i

For i = 0, n-1

PRINT i+1, a [i] [n]/a[i]

END for i

E
ND

To start with, input the coefficients of matrix A and values of the right-hand side vectors,
b. Next is to read the size of the matrix by letting m as the number of rows and n as the
number of columns. Note that if the given is a square matrix, it is reasonable that m should be
equal to n. In the flowchart presented above, n is also for the number of rows for a square
matrix is assumed.

To get with the Gauss Jordan elimination proper, forward elimination is employed. With
this, looping is done throughout the rows i and columns j. The forward elimination process
zeroes out the elements of i or rows below the diagonal. This process is seen on the flowchart
with the loop. While backward substitution process zeroes out the elements of j or columns at
the right of the diagonal position. The final matrix will have ones at the diagonal position and
zero elements everywhere else. From this values of the vector of unknowns x’s will be
computed by using the elementary operation, division, with the corresponding value of b.

20
a. M-file

function [x,err]=gauss_jordan_elim_pivot(A,b)
A = (‘Enter the coefficient matrix’); % A = coefficient
matrix
b = (‘Enter right hand side vector’); % intput for matrix B
[m,n]=size(A); % finding the size of matrix A
err =0; % calculation of error
x=zeros(m,1); % calling fuction zero
if m ~= n
disp('error: m~=n'); % displaying error if found
err = 1;
end % end of the scope of if
if length(b) ~= m % finding the legth of matrix B
disp('error: wrong size of b'); % displaying error, if found
err = 2;
else
if size(b,2) ~= 1
b=b';
end % end of the scope of if-else
if size(b,2) ~= 1
disp('error: b is a matrix'); % displaying erron in matrix B
err = 3;
end
end
if err == 0
Aa=[A,b];
for i=1:m
[Aa(i:m,i:m+1),err]=gauss_pivot(Aa(i:m,i:m+1));
if err == 0
Aa(1:m,i:m+1)=gauss_jordan_step(Aa(1:m,i:m+1),i);
end
end
x=Aa(:,m+1);
end
A=0;

21
function A1=gauss_jordan_step(A,i) % calling of fuction function

[m,n]=size(A); % determination of size of matrix A


A1=A; % assigning A to A1
s=A1(i,1);
A1(i,:) = A(i,:)/s;
k=[[1:i-1],[i+1:m]];
for j=k
s=A1(j,1);
A1(j,:)=A1(j,:)-A1(i,:)*s;
end % end of for loop
function [A1,err]=gauss_pivot(A) % calling of fucntion
[m,n]=size(A); % finding the size of matrix A
A1=A; % process of assigning
err = 0; % error flag
if A1(1,1) == 0
check = logical(1); % logical(1) - TRUE
i = 1;
while check
i = i + 1;
if i > m
disp('error: matrix is singular');
err = 1;
check = logical(0);
else
if A(i,1) ~= 0 & check
check = logical(0);
b=A1(i,:); % process to change row 1 to i
A1(i,:)=A1(1,:);
A1(1,:)=b;
end
end
end
end

22
2. GAUSS JORDAN WITH PIVOTING

a. Algorithm
Step 1. Start
Step 2. Read the order of the matrix ‘n’ and read the coefficients of the linear
equations.
Step 3. Do for k=1 to n
Do for l=k+1 to n+1
a[k][l] = a[k][l] / a[k][k]
End for l
Set a[k][k] = 1
Do for i=1 to n
if (i not equal to k) then,
Do for j=k+1 to n+1
a[i][j] = a[i][j] – (a[k][j] * a[i][k])
End for j
End for i
End for k
Step 4. Do for m=1 to n
x[m] = a[m][n+1]
Display x[m]
End for m
Step 5. Determine the maximum absolute value in the column below the pivot
element and make it as the new pivot element.
Step 6. Do for i = 1 to n.
Do for k = 1 to n.
Do for j = k+1 to n.Switch rows if aij is equal to zero.
Step 7. End

23
b. Flowchart

St
art

For i = 0, n-1

For j = 0, n

INPUT a[i] [j]

END for j

END for i

Determine element with


maximum absolute value

For i = 1 to n

For k = 1 to n

24
A

No
aik = 0

Yes

Switch i

No
If i<> j
Yes

t=a[i][j] / a[j][i]

For k = 0, n

a[i][k] = -a[j][k]*t

END for k

For i = 0, n-1

In Gauss Jordan elimination with pivoting, rows that have zeros within the diagonal
position are relocated within the matrix such that null values are either below or at the right-
hand position of the diagonal. Upon detecting zero(s) within the diagonal, rows are being
switched. When all the diagonal elements are of scalar quantity, Gauss elimination proper is
done.

25
c. M-file

function [x,err]=gauss_jordan_elim_2(A,b)
A = (‘Enter the coefficient matrix’); % A =
coefficient matrix
b = (‘Enter right hand side vector’); % intput for matrix B
[m,n]=size(A); % finding the size of matrix A
err =0; % calculation of error
x=zeros(m,1); % calling fuction zero
if m ~= n
disp('error: m~=n'); % displaying error if found
err = 1;
end % end of the scope of if
if length(b) ~= m % finding the legth of matrix B
disp('error: wrong size of b'); % displaying error, if found
err = 2;
else
if size(b,2) ~= 1
b=b';
end % end of the scope of if-else
if size(b,2) ~= 1
disp('error: b is a matrix'); % displaying erron in matrix B
err = 3;
end
end
if err == 0
Aa=[A,b];
for i=1:m
if err == 0
Aa(1:m,i:m+1)=gauss_jordan_step(Aa(1:m,i:m+1),i);
end
end
x=Aa(:,m+1);
end
A=0;function A1=gauss_jordan_step(A,i) % calling of fuction function

[m,n]=size(A); % determination of size of matrix A


A1=A; % assigning A to A1
s=A1(i,1);
A1(i,:) = A(i,:)/s;
k=[[1:i-1],[i+1:m]];
for j=k
s=A1(j,1);
A1(j,:)=A1(j,:)-A1(i,:)*s;
end % end of for loop
end
end

26
C. LU FACTORIZATION

1. LU FACTORIZATIION WITHOUT PIVOTING


a. Algorithm:

INPUT dimension n; the entries aij ,1  i, j  n of A;

the diagonal l11  ...  lnn  1 of L or

the diagonal u11  ...  unn  1 of U .

OUTPUT the entries lij ,1  j  i, 1  i  n of L and the entries,

uij , i  j  n, 1  i  n of U

Step 1. Select l11 and u11 satisfying l11u11= a11


If l11u11= 0 then OUTPUT (‘Factorization impossible’)
STOP
Step 2. For j = 2,…,n set u1j = a1j/l11 (First row of U)
Lj1 = aj1/u11 (First column of L)
Step 3. For i = 2,…,n-1 do Steps 4 and 5:

Step 4. Select lii and uii satisfying liiuii  aii  k 1likuki


i 1

If l11u11= 0 the OUTPUT (‘Factorization impossible’)


STOP
Step 5. For j = i+1,…,n

Set uij 
1
lii
aij   k 1 l ik ukj
j 1
 (ith row of U)

lij 
1
uii
a ji   k 1 l jk uki
i 1
 (ith column of L)

Step 6. Select lnn and unn satisfying lnnunn  ann  k 1lnkukn


n 1

(Note: If lnnunn= 0, then A=LU but A is singular)


Step 7. OUTPUT (lij for j=1,…,i and i=1,…,n)
OUTPUT (uij for j=1,…,n and i=1,…,n)
STOP

27
a. Flowchart

Sta
rt

Get the elements of Matrix


into Arrays A and b

Calculate element of L and


U

Print the element of L and


U

Find v by solving LY = b
by Forward substitution

Find X by solving UX = Y
by backward substitution

Print Array X as
solution

E
nd

28
To start with, input the coefficient of matrix A and the values of right hand side vector b.
Next, read whether the m as the number of rows and n as the column is a square matrix. Then
we can get the value of L and U by decomposing or factorizing the A. After getting the
values of L and U, given b, we can determine the value of d by [L]{d}={b}, then using
[U]{x}={d} and by back substitution to solve for x.

b. M-file

function x = LU (A,b)
A = (‘Enter the coefficient matrix’); % A = coefficient
matrix
b = (‘Enter right hand side vector’);
[m,n]=size(A);
[L,U] = lu(A);
d = L\b;
x = U\d;
disp('The Lower Triangular Matrix is')
L
disp('The Upper Triangular Matrix is')
U
disp('The Final Iteration is')
x

29
2. LU Factorization with Pivoting

a. Flowchart

Start

Get the elements of


Matrix
into Arrays A and b

Determine element with


maximum absolute value

For i = 1 to n

For k = 1 to n

aik = 0 N
o
Yes

Switch i

Calculate element of L and U

Print the element of L and U

Find v by solving LY = b by
Forward substitution

Find X by solving UX = Y by
backward substitution

Print Array X as solution

END
30
In LU factorization with pivoting, a permutation matrix, which is similar to an
identity matrix, is multiplied to the right-hand side vectors. Rows that have zeros within the
diagonal position are relocated within the matrix such that null values are either below or at
the right-hand position of the diagonal. Upon detecting zero(s) within the diagonal, rows are
being switched. At the same time, the coefficients of the permutation matrix are also switched
to jive with the order of the switches rows of A. When all the diagonal elements are of scalar
quantity, matrix A is decomposed into its upper and lower triangular matrices. The values of
d’\s and x’s are computed and the solution set is displayed.

b. M-file

function x = LU (A,b)
A =(‘Enter the coefficient matrix’); % A =
coefficient matrix
b = (‘Enter right hand side vector’);
[m,n]=size(A);
function [A1,err]=lu_pivot(A) % calling of fucntion
[m,n]=size(A); % finding the size of matrix A
A1=A; % process of assigning
err = 0; % error flag
if A1(1,1) == 0
check = logical(1); % logical(1) - TRUE
i = 1;
while check
i = i + 1;
if i > m
disp('error: matrix is singular');
err = 1;
check = logical(0);
else
if A(i,1) ~= 0 & check
check = logical(0);
b=A1(i,:); % process to change row 1 to i
A1(i,:)=A1(1,:);
A1(1,:)=b;
end
end
end
end
end
P=eye(m);
[L,U] = lu(A);
d = L\(P*b);
x = U\d;
P
disp('The matrix to be decomposed is')
A
disp('The Lower Triangular Matrix is')
L
disp('The Upper Triangular Matrix is')
U
disp('The Final Iteration is')
x
end

31
D. GAUSS SEIDEL AND JACOBI ELIMINATION
a. Algorithm
Step1. Start
Step2. Declare the variables and read the order of the matrix n
Step3. Read the stopping criteria er
Step4. Read the coefficients aim as
Do for i=1 to n
Do for j=1 to n
Read a[i][j]
Repeat for j
Repeat for i
Step 5. Read the coefficients b[i] for i=1 to n
Step 6. Initialize x0[i] = 0 for i=1 to n
Step7. Set key=0
Step 8. For i=1 to n
Set sum = b[i]
For j=1 to n
If (j not equal to i)
Set sum = sum – a[i][j] * x0[j]
Repeat j
x[i] = sum/a[i][i]
If absolute value of ((x[i] – x0[i]) / x[i]) > er, then
Set key = 1
Set x0[i] = x[i]
Repeat i
Step 9. If key = 1, then
Goto step 6
Otherwise print results
Step 10. End

32
b. Flowchart

STAR
T

Input n

For i = 1, n
For j = 1, n +
1

Input
a(i,j)

For i = 1,
n

x(i) =
0

y(i) =
0

33
A

Itr = 0

Itr = itr + 1

For i = 1, n

x(i) = a(I, n + 1)

For j = 1, n

If i = j

x(i) = x(i) –a(i,j)*x(j)

x(i) = x(i)/a(i,i)

C
B

34
C
B

For k = 1, n

N
IfAbs (x(k)-y(k))>0.0001
o
Y
es
Print itr

For i =, n

y(i) = x(i)

Print x(i)

End

To start with, input the coefficients of matrix A and values of the right-hand side vectors,
b. Next is to read the size of the matrix by letting m as the number of rows and n as the
number of columns. Note that if the given is a square matrix, it is reasonable that m should be
equal to n. In the flowchart presented above, n is also for the number of rows for a square
matrix is assumed.

In Gauss Seidel elimination method, N number of iterations are made until the true value
of the unknown of vectors are found. On the first set of iteration, the unknown of vectors
except the first x are assumed to be zero. The second x is computed by substituting the
computed first x and assuming the other x’s to be zero of value. This process is done until all
x’s have scalar values. Another set of iteration is done using the value of old x’s. The whole
process is just repeated until the error between iterations is below one percent. Then the
values of x’s are displayed.

35
c. M-file

function x = GaussSeidel(A,b,es,maxit)
A = (‘Enter the coefficient matrix’); % A = coefficient
matrix
b = (‘Enter right hand side vector’);
es = .0000000000001
maxit = 150
if nargin<4||isempty(maxit),maxit=150;end
if nargin<3|isempty(es),es=.0000000000001;end
[m,n] = size(A);
if m~=n, error('Matrix A must be square'); end
C = A;
for i = 1:n
C(i,i) = 0;
x(i) = 0;
end
x = x';
for i = 1:n
C(i,1:n) = C(i,1:n)/A(i,i);
end
for i = 1:n
d(i) = b(i)/A(i,i);
end
iter = 0;
while (1)
xold = x;
for i = 1:n
x(i) = d(i)-C(i,:)*x;
if x(i) ~= 0
ea(i) = abs((x(i) - xold(i))/x(i)) * 100;
end
end
iter = iter+1;
if max(ea)<=es | iter >= maxit, break, end
end
end

IV. CONCLUSION
1. PROBLEM
The figure below shows three reactors linked by pipes. As indicated, the rate of
transfer of chemicals through each pipe is equal to a flow rate (Q, with units of cubic
meters per second) multiplied by the concentration of the reactor from which the flow
originates (c, with units of milligrams per cubic meter). If the system is at a steady state,
the transfer into each reactor will balance the transfer out. Develop mass-balance
equations for the reactors and solve the three simultaneous linear algebraic equations for
their concentrations.

36
Q33 = 120 Q23 = 60
Q13 = 40 Q21 = 30
Q12 = 90

Q Q
200
13c1 33c3
mg/s 1 3
Q Q
12c1 23c2
500 mg/s
Q
21c2 2
Mass-balance equations:

For reactor 1:
200  Q21c2  Q13c1  Q12c1

200  30c2  40c1  90c1

130c1  30c 2  200 ---------------------------- eq.1

For reactor 2:
Q12 c1  Q 23 c 2  Q 21c 2

90c1  60c 2  30c 2

90c1  90c 2  0 -------------------------------- eq.2

For reactor 3:
Q13 c1  Q 23 c 2  500  Q 33 c 3

40c1  60c 2  500  120c 3

40c1  60c 2  120c 3  500 ----------------- eq.3

A. Computational method
1. Gauss Elimination
130  30 0 200 
 
 90  90 0 0 
 40 60  120  500

1
130  30 0 200  130  30 0 200 
  R2  R2 130 R1  
90

 90  90 0      0 90
0 13 0 1800
13 
 40 60  120  500  40 60  120  500

130  30
40 0 200 
   
R3  R3 
 0 130
R1

 13
90
0 1800
13 
 0 900
 120 7300 
 13 13 

 900  1800
x2 
13 13

x2  2

900 7300
x2  120 x3  
13 13

900  7300
(2)  120 x3 
13 13

 7300 1800
 120 x3  
13 13

 9100
 120 x3  
13

 700
x3 
 120

x3  5.8333

130x1  30x2  200


130x1  30(2)  200
130x1  200  160
130x1  360
x1  2

2. Gauss Jordan Elimination


The augmented matrix of the system is:
130  30 0 200 
 
 90  90 0 0 
 40 60  120  500

2
Performing the row operations:

130  30 0 200  300 0 0 600 


   
 90  90 0 0   90  90 0 0 
 40 60  120  500 3R1 R 2  40 60  120  500

300 0 0 600 
3R 2R  
3
 
 90  90
2
0 0 
 40 0  360  1500

300 0 0 600 
R R  
3 
 
 90  90
1
0 0 
 0 0  360  2100

300 0 0 600 
10R 2 3R1
   0  900 0  1800 


 0 0  360  2100
1
R1 1 0 0 2 
 
300   
0  900 0  1800 
0 0  360  2100
1
R2 1 0 0 2 
 
900  
0 1 0 2 
0 0  360  2100

1
R3 1 0 0 2 
360
  
0 1 0 2 
0 0 1 5.8333

The solutions are:


C1 = 2
C2 = 2
C3 = 5.833

3. LU Factorization

130  30 0 200 
 
Ab   90  90 0 0 
 40 60  120  500

3
Calculating L and U

a11 = u11

a12 = u12

a13 = u13

u11 = u12=- u13=0


130 30

90 40
l21u11=90 l21  l21=0.6 l31u11=40 l31  l31=0.3
130 923 130
077

l21u12+u22=-90 u22=-90 + (0.6923)(30) u22=-


69.231
l21u13+u23=0 u23=0 + (0.6923)(0) u23=0

60  (0.3077)(30)
l31u12+l32u22=60 l32  l32=-1
 69.231

l31u13+l32u23+u33=-120 u33=-120 + (0.3077)(0) + (-1)(0) u33=-


120
Determining the d-values:

i 1
bi   l jj d j
j 1 b1 200
di  d1   d1 =
lii l11 1
200

b2  (l 21 )( d 1) 0  (0.6923)( 200)
d2   d2 =-
l11 1 138.46

b3  (l 31 )(d 1)  (l 32 )(d 2 )  500  (0.3077)(200)  (1)(138.46)


d3   d2 =-
l 33 1
700
Determining the x-values:

n
di  u
j i 1
ij xj
xi 
uii

4
d 3  700 d2  138.46
x3   x3=5 x2   x2=-
u 33  120 u 22  69.2308
.833 2.0

d 1  (30)( x 2 ) 200  30(2.0)


x1   x1 =
u11 130
2.0
4. Gauss Seidel

130  30 0 200 
 
 90  90 0 0 
 40 60  120  500

130 x1  30x2  200

90x1  90x2  0

40 x1  60 x2  120 x3  500

First, solve each of the equations for its unknown on the diagonal:

200  30 x2
x1  (E.1)
130

 90 x1
x2   x1 (E.2)
 90

 500  40 x1  60 x2
x3  (E.3)
 120

First iteration: Using initial guesses of x2 = x3 = 0, we can solve for x1:

200  0
x1   1.53846
130

Since x2 = x3

x2  1.53846

The first iteration is completed by substituting the calculated values for x 1 and x2
into Eq. (E.3) to yield.

5
 500  40(1.53846)  60(1.53846)
x3   5.4487
 120

At this point, we could compute estimated errors with the criterion

xij  xij 1
 a ,i  100%   s .
xij

However, since we started with assumed values of zero, the errors for both
variables will be 100%.

Second iteration: Using the same procedure as for the first iteration, the second
iteration yields

200  30(1.53846)
x1   1.89349
130

1.89349  1.53846
 a1   100  18.75%
1.89349

x2  1.89349

1.89349  1.53846
 a2   100  18.75%
1.89349

 500  40(1.89349)  60(1.89349)


x3   5.744575
 120

5.744575  5.4487
 a3   100  5.1505%
5.744575

At this point, the errors estimated did not meet the 1% stopping criterion. Hence,
we must implement another iteration.

Third Iteration

200  30(1.89349)
x1   1.975420
130

1.975420  1.89349
 a1   100  4.1475%
1.975420

6
x2  1.975420

1.975420  1.89349
 a2   100  4.1475%
1.975420

 500  40(1.975420)  60(1.975420)


x3   5.81285
 120

5.81285  5.744575
 a3   100  1.1746%
5.81285

Since the third unknown only meets the 1% stopping criterion, we must
implement iteration again.

Fourth Iteration

200  30(1.975420)
x1   1.994327692
130

1.994327692  1.975420
 a1   100  0.94808%
1.994327692

x2  1.994327692

1.994327692  1.975420
 a2   100  0.94808%
1.994327692

 500  40(1.994327692)  60(1.994327692)


x3   5.82860641
 120

5.82860641  5.81285
 a3   100  0.270329%
5.82860641

At this point, we can terminate the computation because both error estimates have
fallen below the 1% stopping criterion. The results at this juncture, x1  1.994327692 ,

x2  1.994327692 and x3  5.82860641 are converging on the exact solution of

x1  x2  2 and x3  5.83.

7
B. Matlab approach
Using

130  30 0   200 
  
A   90  90 0  b 0 
 40 60  120  500
 
as the input data for every method.

1. Gauss Elimination
a) Gauss Elimination Without Pivoting
(1) Mfiles
Imputing the data of the given problem in the Mfile of Gauss elimination
without pivoting.
function x = GaussNaive(A,b) % GaussNaive: naive Gauss
elimination
% x = GaussNaive(A,b): Gauss elimination without pivoting.
% input:
A = [130 -30 0; 90 -90 0; 40 60 -120]; % A = coefficient
matrix
b = [200 ; 0; -500]; % b = right hand side vector
% Determining size of A
[m,n] = size(A); % m = number of rows; n = number
of columns
if m~=n, error('Matrix A must be square');
end

nb = n+1; % nb = column for matrix b


Aug = [A b]; % matrix A with added column
(matrix b)

% forward elimination
for k = 1:n-1 % loop forward
elimination to elements from 1st(pivot element) to (n-1)th row of
matrix A
for i = k+1:n % loop forward
elimination to elements from 2nd to nth row of matrix A
factor = Aug(i,k)/Aug(k,k); % factor = scalar
quantity to be multiplied to all elements in a given row
Aug(i,k:nb) = Aug(i,k:nb)-factor*Aug(k,k:nb); % update elements
in ith row and kth to (n+1)th column
end
end

% back substitution
x = zeros(n,1); % elements below the diagonal should be
equal to zero
x(n) = Aug(n,nb)/Aug(n,n); % getting nth
unknown
% output:
% x = solution vector
for i = n-1:-1:1
x(i) = (Aug(i,nb)-Aug(i,i+1:n)*x(i+1:n))/Aug(i,i); % backsubstitute
x(n)'s starting from (n-1) row
end 8
A
b
end
(2) Output

9
b) Gauss Elimination With Pivoting
(1) Mfiles
Inputing the data of the given problem in the Mfile of Gauss elimination
with pivoting.

function x = GaussPivot(A,b) % GaussPivot: Gauss elimination


pivoting
% x = GaussPivot(A,b): Gauss elimination with pivoting.
% input:
A = [130 -30 0; 90 -90 0; 40 60 -120]; % A = coefficient matrix
b = [200 ; 0; -500]; % b = right hand side vector
% Determining size of A
[m,n]=size(A); % m = number of rows; n =
number of columns
if m~=n, error('Matrix A must be square');
end

nb = n+1; % nb = column for matrix b


Aug = [A b]; % matrix A with added
column (matrix b)

% forward elimination
for k = 1:n-1 % loop forward elimination
to elements from 1st(pivot element) to (n-1)th row of matrix A
% partial pivoting
[big,i]=max(abs(Aug(k:n,k))); % find for the largest
absolute value in the column below the pivot element and make it as the new
pivot element
ipr=i+k-1;
if ipr~=k % value of element in the
diagonal is nearly zero or equal to zero
Aug([k,ipr],:)=Aug([ipr,k],:); % switch rows
end
for i = k+1:n % loop forward elimination
to elements from 2nd to nth row of matrix A
factor=Aug(i,k)/Aug(k,k); % factor = scalar quantity
to be multiplied to all elements in a given row
Aug(i,k:nb)=Aug(i,k:nb)-factor*Aug(k,k:nb); % update elements in ith
row and kth to (n+1)th column
end
end

% back substitution
x = zeros(n,1); % elements below the
diagonal should be equal to zero
x(n)= Aug(n,nb)/Aug(n,n); % getting nth unknown
% output:
% x = solution vector
for i = n-1:-1:1
x(i) = (Aug(i,nb)-Aug(i,i+1:n)*x(i+1:n))/Aug(i,i); % backsubstitute
x(n)'s starting from (n-1) row
end
A
b
end

10
(2) Output

11
2. Gauss Jordan Elimination
a) Gauss Jordan Without Pivoting
(1) Mfiles
Imputing the data of the given problem in the Mfile of Gauss Jordan
elimination without pivoting.

function [x,err]=gauss_jordan_elim_2(A,b)
A = [130 -30 0; 90 -90 0; 40 60 -120]; % A = coefficient matrix
b = [200 ; 0; -500]; % intput for matrix B
[m,n]=size(A); % finding the size of matrix A
err =0; % calculation of error
x=zeros(m,1); % calling fuction zero
if m ~= n
disp('error: m~=n'); % displaying error if found
err = 1;
end% end of the scope of if
if length(b) ~= m % finding the legth of matrix B
disp('error: wrong size of b'); % displaying error, if found
err = 2;
else
if size(b,2) ~= 1
b=b';
end% end of the scope of if-else
if size(b,2) ~= 1
disp('error: b is a matrix'); % displaying error in matrix B
err = 3;
end
end
if err == 0
Aa=[A,b];
for i=1:m
if err == 0
Aa(1:m,i:m+1)=gauss_jordan_step(Aa(1:m,i:m+1),i);
end
end
x=Aa(:,m+1);
end
function A1=gauss_jordan_step(A,i) % calling of function

[m,n]=size(A); % determination of size of matrix A


A1=A; % assigning A to A1
s=A1(i,1);
A1(i,:) = A(i,:)/s;
k=[[1:i-1],[i+1:m]];
for j=k
s=A1(j,1);
A1(j,:)=A1(j,:)-A1(i,:)*s;
end% end of for loop
end
A
b
end

12
(2) Output

13
b) Gauss Jordan With Pivoting
(1) Mfiles
Imputing the data of the given problem in the Mfile of Gauss Jordan
elimination without pivoting.
function x = GaussPivot(A,b) % GaussPivot: Gauss
elimination pivoting
% x = GaussPivot(A,b): Gauss elimination with pivoting.
% input:
A = [ 130 -30 0; 90 -90 0; 40 60 -120 ]; % A =
coefficient matrix
b = [200 ; 0; -500 ]; % b = right hand side vector
% Determining size of A
[m,n]=size(A); % m = number of rows; n =
number of columns
if m~=n, error('Matrix A must be square');
end

nb = n+1; % nb = column for matrix


b
Aug = [A b]; % matrix A with added
column (matrix b)

% forward elimination
for k = 1:n-1 % loop forward
elimination to elements from 1st(pivot element) to (n-1)th row of matrix A
% partial pivoting
[big,i]=max(abs(Aug(k:n,k))); % find for the largest
absolute value in the column below the pivot element and make it as the
new pivot element
ipr=i+k-1;
if ipr~=k % value of element in the
diagonal is nearly zero or equal to zero
Aug([k,ipr],:)=Aug([ipr,k],:); % switch rows
end
for i = k+1:n % loop forward
elimination to elements from 2nd to nth row of matrix A
factor=Aug(i,k)/Aug(k,k); % factor = scalar
quantity to be multiplied to all elements in a given row
Aug(i,k:nb)=Aug(i,k:nb)-factor*Aug(k,k:nb); % update elements in ith
row and kth to (n+1)th column
end
end
% back substitution
x = zeros(n,1); % elements below the
diagonal should be equal to zero
x(n)= Aug(n,nb)/Aug(n,n); % getting nth unknown
% output:
% x = solution vector
for i = n-1:-1:1
x(i) = (Aug(i,nb)-Aug(i,i+1:n)*x(i+1:n))/Aug(i,i); % backsubstitute
x(n)'s starting from (n-1) row
end
A
b

14
(2) Output

15
3. LU Factorization
a) LU Factorization Without Pivoting
(1) Mfiles
Imputing the data of the given problem in the Mfile of LU Factorization
without pivoting.
function[L,U,X]=LU_Crout(A,b)
%Function to find solution of AX = b by crout's
%LU Factorization method.
%Input=A,b
%Output=L,U,X
%%
A = [130 -30 0; 90 -90 0; 40 60 -120]; % A = coefficient matrix
b = [200 ; 0; -500];
[m,n]=size(A);
if m~=n
disp('Matrix must be square')
beep
return;
end
U=zeros(m);
L=eye(m);
for j=1:m
L(j,j)=1;
end
for j=1:m
U(1,j)=A(1,j);
end
for i=2:m
for j=1:m
for k=1:i-1
s1=0;
if k==1
s1=0;
else
for p=1:k-1
s1=s1+L(i,p)*U(p,k);
end
end
L(i,k)=(A(i,k)-s1)/U(k,k);
end
for k=i:m
s2=0;
for p=1:i-1
s2=s2+L(i,p)*U(p,k);
end
U(i,k)=A(i,k)-s2;
end
end
end
Y=L\b; %Z=inv(L)*b

x=U\Y; %X=inv(U)*Z
disp('The Lower Triangular Matrix is')
L
disp('The Upper Triangular Matrix is')
U
disp('The Final Iteration is')
x
end 16
(2) Output

17
b) LU Factorization With Pivoting
(1) Mfiles
Imputing the data of the given problem in the Mfile of LU Factorization
with pivoting.
function[L,U,X]=LU_Crout(A,b)
%Function to find solution of AX = b by crout's
%LU Factorization method.
%Input=A,b
%Output=L,U,X
%%
A = [130 -30 0; 90 -90 0; 40 60 -120]; % A = coefficient matrix
b = [200 ; 0; -500];
[m,n]=size(A);
if m~=n
disp('Matrix must be square')
beep
return;
end
U=zeros(m);
L=eye(m);
for j=1:m
L(j,j)=1;
end
for j=1:m
U(1,j)=A(1,j);
end
for i=2:m
for j=1:m
for k=1:i-1
s1=0;
if k==1
s1=0;
else
for p=1:k-1
s1=s1+L(i,p)*U(p,k);
end
end
L(i,k)=(A(i,k)-s1)/U(k,k);
end
for k=i:m
s2=0;
for p=1:i-1
s2=s2+L(i,p)*U(p,k);
end
U(i,k)=A(i,k)-s2;
end
end
end
Y=L\b; %Z=inv(L)*b

x=U\Y; %X=inv(U)*Z
disp('The Lower Triangular Matrix is')
L
disp('The Upper Triangular Matrix is')
U
disp('The Final Iteration is')
(2) Output
x
end

18
19
2. Problem
The Lower Colorado River consists of a series of four reservoirs as shown in the
figure below.
Mass balances can be written for each reservoir, and the following set of simultaneous
linear algebraic equations results:

 13.422 0 0 0 
 13.422 12.252 0 0 

 0  12.252 12.377 0 
 
 0 0  12.377 11.797

 c1  750.5
c   300 
 
x=  2
 =  
c 3   102 

c 4 
 
 30 

where the right-hand-side vector consists of the loadings of chloride to each of the
four lakes and c1, c2, c3, and c4 is the resulting chloride concentrations for Lakes
Powell, Mead, Mohave, and Havasu, respectively.

Upper
Colorado
River

C
1
La C
ke 21
C
Powell Lak
3
e Mead
Lake
C
Mohave 4
Lake
Hava
su

20
 13.422 0 0 0   c1  750.5
 13.422 12.252 0  c   300 
 
x=  2
 0
 =  
 0  12.252 12.377 0  c 3   102 
  
 0 0  12.377 11.797 
c 4 
  30 

A. Computational method
1. Gauss Elimination

 13.422 0 0 0 750.5
 
 13.422 12.252 0 0 300 
 0  12.252 12.377 0 102 
 
 0 0  12.377 11.797 30 

13.422x1  750.5

55.9157  x1

 13.422x1  12.252x2  300

 13.422(55.9157)  12.252x2  300

x2  85.744

 12.252 x2  12.377 x3  102

 12.252(85.7411)  12.377 x3  102

x3  93.1163

 12.377 x3  11.79 x4  30

 11.377(93.1163)  11.797 x4  30

x4  100.2373

21
2. Gauss Jordan Elimination

The augmented matrix of the system is,

 13.422 0 0 0 750.5
 
 13.422 12.252 0 0 300 
 0  12.252 12.377 0 102 
 
 0 0  12.377 11.797 30 

 13.422 0 0 0 750.5 13.422 0 0 0 750.5


   
 13.422 12.252 0 0 300  R2  R1  0

12.252 0 0 300 
 0  12.252 12.377 0 102   0  12.252 12.377 0 102 
   
 0 0  12.377 11.797 30   0 0  12.377 11.797 30 

13.422 0 0 0 750.5 
 
R3  R2  0 12.252 0 0 1050.5

 0 0 12.377 0 1152.5
 
 0 0  12.377 11.797 30 

13.422 0 0 0 750.5 
 
R3  R2  0 12.252 0 0 1050.5

 0 0 12.377 0 1152.5
 
 0 0 0 11.797 1182.5

1 0 0 0 55.9157

1 
0 12.252 0 0 1050.5 

R1
 
13.422
0 0 12.377 0 1152.5 
 
0 0 0 11.797 1182.5 

22
1 0 0 0 55.9157

1 
0 1 0 0 85.7411

R2
 
12.252
0 0 12.377 0 1152.5 
 
0 0 0 11.797 1182.5 

1 0 0 055.9157
1  
0 1 0 085.7411

R3
 
12.377
0 0 1 093.1163
 
0 0 0 11.797 1182.5 

1 0 0 0 55.9157 
 1 
0 1 0 0 85.7411 

R4
  11.797
0 0 1 0 93.1163 
 
0 0 0 1 100.2373

The solutions are:

c1  55.9157

c2  85.7411

c3  93.1163

c4  100.2373

3. LU Factorization

 13.422 0 0 0 750.5
 
 13.422 12.252 0 0 300 
Ab  
 0  12.252 12.377 0 102 
 
 0 0 12.377 11.797 30 

Calculating L and U

a11 = u11

a12 = u12

a13 = u13

23
a14 = u14

u11 u12=0 u13=0 u14=0


=13.422

 13.422 0
l21u11=-13.422 l 21  l21 l31u12=0 l 31  l31=0
13.422 0
=-1

0
l41u13=0 l 41  L41=0
0

l21u12+u22=12.252 u22=12.252 + (-1)(0) u22=12.25


2

l21u13+u23=0 u23=0 + (-1)(0) u23=0

l21u14+u24=0 u23=0 + (-1)(0) u24=0

 12.252  (0)(0)
l31u12+l32u22=-12.252 l 32  l32=-1
12.252

0  (0)(0)
l41u12+l42u22=0 l 32  l42=0
12.252

l31u13+l32u23+u33=12.377 u33=12.377+ (0)(0) + (-1)(0) u33=12.37


7

l31u14+l32u24+u34=0 u34=0+ (0)(0) + (-1)(0) u34=0

 12.377  (0)(0)  (0)(0)


l31u13+l42u23+l43u33=-12.377 l 43  l43=-1
12.377

l41u14+l42u24+l43u34+u44=11.797 u44=11.797 + (0)(0) + (0)(0) + (-1)(0) u44=11.


797
24
Determining the d-values:

i 1
bi   l jj d j
j 1 b1 750.5
di  d1   d1 = 750.5
lii l11 1

b2  (l 21 )( d 1) 300  (0)(750.5)
d2   d2
l11 1
=1050.5
b3  (l 32 )(d 2 ) 102  (1)(1050.5)
d3   d3=1152
l 33 1 .5

b3  (l 32 )(d 2 ) 30  (1)(1152.5)
d3   d4
l 33 1
=1182.5

Determining the x-values:

n
di  u
j i 1
ij xj
xi 
uii

d1 750.5 d2 1050.5
x1   x1 x2   x2=85.
u11 13.422 u 22 12.2520
=55.9157 74
d3 1152.5 d 4 1182.5
x3   x3=93.1 x4   x4=100.23
u 33 12.3770 163 u 44 11.797
73

4. Gauss Seidel

 13.422 0 0 0 750.5
 
 13.422 12.252 0 0 300 
 0  12.252 12.377 0 102 
 
 0 0  12.377 11.797 30 

13.422x1  750.5

 13.422x1  12.252x2  300

25
 12.252 x2  12.377 x3  102

 12.377 x3  11.797 x4  30

750.5
x1   55.91566
13.422

300  13.422 x1 300  13.422(55.91566)


x2    85.74110
12.252 12.252

102  12.252 x2 102  12.252(85.74110)


x3    93.11626
12.377 12.377

30  12.377 x3 30  12.377(93.11626)
x4    100.2373
11.797 11.797

B. Matlab Approach

Using
 13.422 0 0 0  750.5
 13.422 12.252 0   300 
0  
A b 
 0  12.252 12.377 0   102 
   30 
 0 0  12.377 11.797
As the input data for every method.

26
1. Gauss Elimination

a) Gauss Elimination without Pivoting

1) Mfiles
Imputing the data of the given problem in the Mfile of Gauss
elimination without pivoting.

function x = GaussNaive(A,b) % GaussNaive: naive Gauss


elimination
% x = GaussNaive(A,b): Gauss elimination without pivoting.
% input:
A = [13.422 0 0 0; -13.422 12.252 0 0; 0 -12.252 12.377 0; 0 0 -
12.377 11.797]; % A = coefficient matrix
b = [750.5; 300; 102; 30]; % b = right hand side
vector
% Determining size of A
[m,n] = size(A); % m = number of rows; n = number of
columns
if m~=n, error('Matrix A must be square');
end

nb = n+1; % nb = column for matrix b


Aug = [A b]; % matrix A with added column (matrix b)

% forward elimination
for k = 1:n-1 % loop forward
elimination to elements from 1st(pivot element) to (n-1)th row of
matrix A
for i = k+1:n % loop forward
elimination to elements from 2nd to nth row of matrix A
factor = Aug(i,k)/Aug(k,k); % factor = scalar
quantity to be multiplied to all elements in a given row
Aug(i,k:nb) = Aug(i,k:nb)-factor*Aug(k,k:nb); % update elements
in ith row and kth to (n+1)th column
end
end

% back substitution
x = zeros(n,1); % elements below
the diagonal should be equal to zero
x(n) = Aug(n,nb)/Aug(n,n); % getting nth
unknown
% output:
% x = solution vector
for i = n-1:-1:1
x(i) = (Aug(i,nb)-Aug(i,i+1:n)*x(i+1:n))/Aug(i,i); % backsubstitute
x(n)'s starting from (n-1) row
end
A
b
end

27
(1) Output

28
b) Gauss Elimination with Pivoting

(1) Mfiles
Imputing the data of the given problem in the Mfile of Gauss
elimination with pivoting.

function x = GaussPivot(A,b) % GaussPivot: Gauss


elimination pivoting
% x = GaussPivot(A,b): Gauss elimination with pivoting.
% input:
A = [13.422 0 0 0; -13.422 12.252 0 0; 0 -12.252 12.377 0; 0 0 -
12.377 11.797]; % A = coefficient matrix
b = [750.5; 300; 102; 30]; % b = right hand side
vector
% Determining size of A
[m,n]=size(A); % m = number of rows;
n = number of columns
if m~=n, error('Matrix A must be square');
end

nb = n+1; % nb = column for


matrix b
Aug = [A b]; % matrix A with added
column (matrix b)

% forward elimination
for k = 1:n-1 % loop forward
elimination to elements from 1st(pivot element) to (n-1)th row of
matrix A
% partial pivoting
[big,i]=max(abs(Aug(k:n,k))); % find for the largest
absolute value in the column below the pivot element and make it as the
new pivot element
ipr=i+k-1;
if ipr~=k % value of element in
the diagonal is nearly zero or equal to zero
Aug([k,ipr],:)=Aug([ipr,k],:); % switch rows
end
for i = k+1:n % loop forward
elimination to elements from 2nd to nth row of matrix A
factor=Aug(i,k)/Aug(k,k); % factor = scalar
quantity to be multiplied to all elements in a given row
Aug(i,k:nb)=Aug(i,k:nb)-factor*Aug(k,k:nb); % update elements in
ith row and kth to (n+1)th column
end
end

% back substitution
x = zeros(n,1); % elements below the
diagonal should be equal to zero
x(n)= Aug(n,nb)/Aug(n,n); % getting nth unknown
% output:
% x = solution vector
for i = n-1:-1:1
x(i) = (Aug(i,nb)-Aug(i,i+1:n)*x(i+1:n))/Aug(i,i); % backsubstitute
x(n)'s starting from (n-1) row
end
A
b
end 29
(2) Output

30
2. Gauss Jordan Elimination

a) Gauss Jordan Without Pivoting

(1) Mfiles
Imputing the data of the given problem in the Mfile of Gauss
Jordan elimination without pivoting.

function [x,err]=gauss_jordan_elim_2(A,b)
A = [13.422 0 0 0; -13.422 12.252 0 0; 0 -12.252 12.377 0; 0 0 -12.377
11.797]; % A = coefficient matrix
b = [750.5; 300; 102; 30]; % intput for matrix B
[m,n]=size(A); % finding the size of matrix A
err =0; % calculation of error
x=zeros(m,1); % calling fuction zero
if m ~= n
disp('error: m~=n'); % displaying error if found
err = 1;
end% end of the scope of if
if length(b) ~= m % finding the legth of matrix B
disp('error: wrong size of b'); % displaying error, if found
err = 2;
else
if size(b,2) ~= 1
b=b';
end% end of the scope of if-else
if size(b,2) ~= 1
disp('error: b is a matrix'); % displaying error in matrix B
err = 3;
end
end
if err == 0
Aa=[A,b];
for i=1:m
if err == 0
Aa(1:m,i:m+1)=gauss_jordan_step(Aa(1:m,i:m+1),i);
end
end
x=Aa(:,m+1);
end
function A1=gauss_jordan_step(A,i) % calling of function

[m,n]=size(A); % determination of size of matrix A


A1=A; % assigning A to A1
s=A1(i,1);
A1(i,:) = A(i,:)/s;
k=[[1:i-1],[i+1:m]];
for j=k
s=A1(j,1);
A1(j,:)=A1(j,:)-A1(i,:)*s;
end% end of for loop
end
A
b
end

31
(2) Output

32
b) Gauss Jordan with pivoting

(1) Mfiles
Imputing the data of the given problem in the Mfile of Gauss
Jordan with pivoting.

function x = GaussPivot(A,b) % GaussPivot: Gauss


elimination pivoting
% x = GaussPivot(A,b): Gauss elimination with pivoting.
% input:
A = [13.422 0 0 0; -13.422 12.252 0 0; 0 -12.252 12.377 0; 0 0 -12.377
11.797]; % A = coefficient matrix
b = [750.5; 300; 102; 30]; % b = right hand side
vector
% Determining size of A
[m,n]=size(A); % m = number of rows; n =
number of columns
if m~=n, error('Matrix A must be square');
end

nb = n+1; % nb = column for matrix b


Aug = [A b]; % matrix A with added
column (matrix b)

% forward elimination
for k = 1:n-1 % loop forward elimination
to elements from 1st(pivot element) to (n-1)th row of matrix A
% partial pivoting
[big,i]=max(abs(Aug(k:n,k))); % find for the largest
absolute value in the column below the pivot element and make it as the new
pivot element
ipr=i+k-1;
if ipr~=k % value of element in the
diagonal is nearly zero or equal to zero
Aug([k,ipr],:)=Aug([ipr,k],:); % switch rows
end
for i = k+1:n % loop forward elimination
to elements from 2nd to nth row of matrix A
factor=Aug(i,k)/Aug(k,k); % factor = scalar quantity
to be multiplied to all elements in a given row
Aug(i,k:nb)=Aug(i,k:nb)-factor*Aug(k,k:nb); % update elements in ith
row and kth to (n+1)th column
end
end
% back substitution
x = zeros(n,1); % elements below the
diagonal should be equal to zero
x(n)= Aug(n,nb)/Aug(n,n); % getting nth unknown
% output:
% x = solution vector
for i = n-1:-1:1
x(i) = (Aug(i,nb)-Aug(i,i+1:n)*x(i+1:n))/Aug(i,i); % backsubstitute
x(n)'s starting from (n-1) row
end
A
b

33
(2) Output

34
3. LU Factorization

a) LU Factorization Without Pivoting

(1) Mfiles

function[L,U,X]=LU_Crout(A,b)
%Function to find solution of AX = b by crout's
%LU Factorization method.
%Input=A,b
%Output=L,U,X
%%
A = [13.422 0 0 0; -13.422 12.252 0 0; 0 -12.252 12.377 0; 0 0 -12.377
11.797]; % A = coefficient matrix
b = [750.5; 300; 102; 30];
[m,n]=size(A);
if m~=n
disp('Matrix must be square')
beep
return;
end
U=zeros(m);
L=eye(m);
for j=1:m
L(j,j)=1;
end
for j=1:m
U(1,j)=A(1,j);
end
for i=2:m
for j=1:m
for k=1:i-1
s1=0;
if k==1
s1=0;
else
for p=1:k-1
s1=s1+L(i,p)*U(p,k);
end
end
L(i,k)=(A(i,k)-s1)/U(k,k);
end
for k=i:m
s2=0;
for p=1:i-1
s2=s2+L(i,p)*U(p,k);
end
U(i,k)=A(i,k)-s2;
end
end
end
Y=L\b; %Z=inv(L)*b

x=U\Y; %X=inv(U)*Z
disp('The Lower Triangular Matrix is')
L
disp('The Upper Triangular Matrix is')
U
disp('The Final Iteration is')
x 35
end
(2) Output

36
b) LU Factorization With Pivoting

(1) Mfiles

function x = LU (A,b)
A = [13.422 0 0 0; -13.422 12.252 0 0; 0 -12.252 12.377 0; 0 0 -
12.377 11.797]; % A = coefficient matrix
b = [750.5; 300; 102; 30];
[m,n]=size(A);
function [A1,err]=lu_pivot(A) % calling of fucntion
[m,n]=size(A); % finding the size of matrix A
A1=A; % process of assigning
err = 0; % error flag
if A1(1,1) == 0
check = logical(1); % logical(1) - TRUE
i = 1;
while check
i = i + 1;
if i > m
disp('error: matrix is singular');
err = 1;
check = logical(0);
else
if A(i,1) ~= 0 & check
check = logical(0);
b=A1(i,:); % process to change row 1 to i
A1(i,:)=A1(1,:);
A1(1,:)=b;
end
end
end
end
end
P=eye(m);
[L,U] = lu(A);
d = L\(P*b);
x = U\d;
disp('The Lower Triangular Matrix is')
L
disp('The Upper Triangular Matrix is')
U
disp('The Final Iteration is')
x
end

37
(2) Output

V. REFERENCES
[1] Chapra, S. C. (2012). Applied Numerical Methods using Matlab for Engineers and
Scientists (third ed.). 1221 Avenue of Americas, New York, NY 10020: McGraw-
Hill.

[2] Stapel, E. (n.d.). Retrieved April 12, 2017, from PurpleMath:


http://www.purplemath.com/modules/systlin6.htm

[3] Suriya Gharib, S. R. (2015). System of Linear Equations, Gaussian Elimination.

[4] The Jacobi and Gauss-Seidel Iterative Methods . (n.d.).

38
39

Vous aimerez peut-être aussi