Académique Documents
Professionnel Documents
Culture Documents
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.
x1
x
x = 2 = vector of unknowns
xn
b1
b
b= 2 = right-hand-side vectors
bm
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.
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.
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
a a a
a22 21 a12 x2 ... a2 n 21 a1n xn b2 21 b1
a11 a11 a11
Or
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:
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
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:
( 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:
5
Representing the numbers of linear equations using the general form:
Ax B
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
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
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.
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.
Or
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.
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:
Ax 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
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
used in the next equation to determine another x value. Thus, if the solution is
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
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.
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:
Update right hand side for row (i) and column (j) using:
Step 6. Do for k = n-1 to 1. Loop backwards over all rows except last.
sum = 0
End for j.
Compute kth unknown.
Step 8. End.
10
b. Flowchart
START
INPUT A, b
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
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
% 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
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
For j = k+ 1, n
bi = bi – [mik* bk]
xn = an(n+1)/ann
A
15
A
For k = n-1, 1
sum = 0
For j = k+1, n
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
% 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
18
b. Flowchart
St
art
For i = 0, n-1
For j = 0, n
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
END for i
END for i
For i = 0, n-1
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
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
END for j
END for i
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
26
C. LU FACTORIZATION
uij , i j n, 1 i n of U
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)
27
a. Flowchart
Sta
rt
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
For i = 1 to n
For k = 1 to n
aik = 0 N
o
Yes
Switch i
Find v by solving LY = b by
Forward substitution
Find X by solving UX = Y by
backward substitution
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,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
For reactor 2:
Q12 c1 Q 23 c 2 Q 21c 2
For reactor 3:
Q13 c1 Q 23 c 2 500 Q 33 c 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
2
Performing the row operations:
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
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
90 40
l21u11=90 l21 l21=0.6 l31u11=40 l31 l31=0.3
130 923 130
077
60 (0.3077)(30)
l31u12+l32u22=60 l32 l32=-1
69.231
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
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
130 30 0 200
90 90 0 0
40 60 120 500
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
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
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
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
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
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 ,
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
% 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.
% 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
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
% 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
x2 85.744
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
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
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
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
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
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
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
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
25
12.252 x2 12.377 x3 102
12.377 x3 11.797 x4 30
750.5
x1 55.91566
13.422
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
1) Mfiles
Imputing the data of the given problem in the Mfile of Gauss
elimination without pivoting.
% 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.
% 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
(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
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.
% 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
(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.
38
39