Lecture 2 2024

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

CHE 358

Numerical Methods for


Chemical Engineers
M. MENSAH
Department of Chemical Engineering
KNUST

1
COURSE OUTLINE
• Modelling, Computers & Error Analysis.
• Linear Algebraic & Non-linear Equations.
• Interpolation.
• Numerical differentiation and integration.
• Numerical solution of ordinary differential equations.
• Numerical solution of partial differential equations.

2
Learning objectives
• At the end of the lecture, students must be able to carry out
mathematical operations using the following methods under
solutions to systems of linear equations:
• Direct Method
1. Cramer’s rule
2. Gauss Elimination
3. Gauss Jordan
4. LU decomposition
5. Cholesky decomposition
6. Matrix inversion

• Iterative method
1. Gauss-Seidel 3
2. Jacobi
Linear Algebraic Equations
• The mathematical analysis of linear physicochemical systems often
results in models consisting of sets of linear algebraic equations

• Many of the fundamental equations of engineering are based on


conservation laws e.g. mass, energy, momentum.

Fig. 1 Two types of systems that can be modeled using


linear algebraic equations: (a) lumped variable system
that involves coupled finite components and (b)
distributed variable system that involves a continuum.

4
Linear Algebraic Equations
• Single-component systems result in a single equation.

• Multicomponent systems result in a coupled set of mathematical


equations.

• The equations are coupled because the individual parts of the system
are influenced by other parts.

• Consequently, its response is dependent on the quantity of chemical


in these other reactors.
5
Linear Algebraic Equations
• When these dependencies are expressed mathematically, the resulting
equations are often of the linear algebraic form:
𝑎11 𝑥1 + 𝑎12 𝑥2 + 𝑎13 𝑥3 ⋯ + 𝑎1𝑛 𝑥𝑛 = 𝑏1 ⋯ (2.1𝑎)

𝑎21 𝑥1 + 𝑎21 𝑥2 + 𝑎23 𝑥3 ⋯ + 𝑎2𝑛 𝑥𝑛 = 𝑏2 ⋯ (2.1𝑏)


⋮ ⋮

𝑎𝑛1 𝑥1 + 𝑎𝑛2 𝑥2 + 𝑎𝑛3 𝑥3 ⋯ + 𝑎𝑛𝑛 𝑥𝑛 = 𝑏𝑛 ⋯ (2.1𝑛)

Where a’s – flow rates of mass between the reactors (properties or


characteristics), b’s – feed rates (forcing functions) and x’s – magnitudes of
the responses of the individual components.
6
REVIEW OF SELECTED MATRIX OPERATIONS

Try (5 mins)
• Compute the determinant of the matrix A below:

3 −0.1 0.2
0.1 7 −0.3
0.3 0.2 10

7
REVIEW OF SELECTED MATRIX OPERATIONS

• A matrix is a rectangular array of elements arranged in rows and columns.

𝑎11 𝑎12 … 𝑎1𝑛


𝑎21 𝑎22 … 𝑎2𝑛
𝑨= .......... (1.2)
𝑎𝑚1 𝑎𝑚2 . . . 𝑎𝑚𝑛

• Matrices with row dimension n=1, such as


is called a row vector.
• Matrices where m = n are called square matrices.

8
MATRIX OPERATING RULES
• Matrix division is not a defined operation. However, if a matrix
[A] is square and nonsingular, there is another matrix [A]−1, called
the inverse of [A], for which
𝐴 𝐴 −1 = 𝐴 −1 𝐴=𝐼

• The inverse of a 2 × 2 matrix can be represented simply by

−1
1 𝑎22 −𝑎12
𝐴 =
𝑎11 𝑎22 − 𝑎12 𝑎21 −𝑎21 𝑎11

9
SYSTEMS OF EQUATIONS
Consider the set of simultaneous linear algebraic equations represented by:

𝑎11 𝑥1 + 𝑎12 𝑥2 + 𝑎13 𝑥3 ⋯ + 𝑎1𝑛 𝑥𝑛 = 𝑏1 ⋯ (2.1𝑎) 𝑎11 𝑎12 𝑎13 … 𝑎1𝑛 𝑥1 𝑏1


𝑎21 𝑥1 + 𝑎21 𝑥2 + 𝑎23 𝑥3 ⋯ + 𝑎2𝑛 𝑥𝑛 = 𝑏2 ⋯ (2.1𝑏) 𝑎21 𝑎21 𝑎23 … 𝑎2𝑛 𝑥2 𝑏
⋮ ⋮ = 2
⋮ ⋮ ⋮ ⋮ ⋮ ⋮
… 𝑥𝑛
𝑎𝑛1 𝑎𝑛2 𝑎𝑛3 𝑎𝑛𝑛 𝑏𝑛
𝑎𝑛1 𝑥1 + 𝑎𝑛2 𝑥2 + 𝑎𝑛3 𝑥3 ⋯ + 𝑎𝑛𝑛 𝑥𝑛 = 𝑏𝑛 ⋯ (2.1𝑛)

The coefficient matrix is A


The vector of unknowns is x
The vector of constants is b

The augmented matrix A, is defined as the matrix resulting from joining the vector b to the
columns of matrix A as shown below:
𝑎11 𝑎12 𝑎13 … 𝑎1𝑛 𝑏1
𝑎21 𝑎21 𝑎23 … 𝑎2𝑛 𝑏2
⋮ ⋮ ⋮ ⋮ ተ ⋮

𝑎𝑛1 𝑎𝑛2 𝑎𝑛3 𝑎𝑛𝑛 𝑏𝑛 10
Direct Method
1. CRAMER’S RULE
𝑎11 𝑎12 𝑎13 … 𝑎1𝑛 𝑥1 𝑏1
𝑎21 𝑎21 𝑎23 … 𝑎2𝑛 𝑥2 𝑏
⋮ ⋮ = 2
⋮ ⋮ ⋮ ⋮
… 𝑥𝑛
𝑎𝑛1 𝑎𝑛2 𝑎𝑛3 𝑎𝑛𝑛 𝑏𝑛
• This rule states that each unknown in a system of linear algebraic equations may be
expressed as a fraction of two determinants with denominator D and with the
numerator obtained from D by replacing the column of coefficients of the unknown
in question by the constants b1, b2, . . . , bn .

• For example, for three equations, x1 would be computed as

• For a system of n equations, Cramer's rule evaluates (n + 1) determinants and


performs n divisions.
11
CRAMER’S RULE
Example 1.1
Use Cramer’s rule to solve the following

12
CRAMER’S RULE
Solution to Example 1.1
The determinant D can be evaluated as

The solution can be calculated as:

13
2. GAUSS ELIMINATION
• The most widely used method for solution of simultaneous linear algebraic
equations is the Gauss elimination method
• The approach is designed to solve a general set of n equations:
𝑎11 𝑥1 + 𝑎12 𝑥2 + 𝑎13 𝑥3 ⋯ + 𝑎1𝑛 𝑥𝑛 = 𝑏1 ⋯ (2.1𝑎)

𝑎21 𝑥1 + 𝑎21 𝑥2 + 𝑎23 𝑥3 ⋯ + 𝑎2𝑛 𝑥𝑛 = 𝑏2 ⋯ (2.1𝑏)


⋮ ⋮

𝑎𝑛1 𝑥1 + 𝑎𝑛2 𝑥2 + 𝑎𝑛3 𝑥3 ⋯ + 𝑎𝑛𝑛 𝑥𝑛 = 𝑏𝑛 ⋯ (2.1𝑛)

𝑎11 𝑎12 𝑎13 … 𝑎1𝑛 𝑏1


𝑎11 𝑎12 𝑎13 … 𝑎1𝑛 𝑥1 𝑏1
𝑎21 𝑎21 𝑎23 … 𝑎2𝑛
𝑎21 𝑎21 𝑎23 … 𝑎2𝑛 𝑏2
⋮ ተ
𝑥2 𝑏
= 2 ⋮ ⋮ ⋮
⋮ ⋮ ⋮

⋮ ⋮ ⋮ … ⋮
𝑎𝑛1 𝑎𝑛2 𝑎𝑛3 𝑎𝑛𝑛 𝑥𝑛 𝑏𝑛 𝑎𝑛1 𝑎𝑛2 𝑎𝑛3 𝑎𝑛𝑛 𝑏𝑛
𝑨 𝒙 = 𝒃 (2.2) Augmented matrix 14
GAUSS ELIMINATION

1. Forward Elimination of Unknowns:

• The first phase is designed to reduce the set of equations to an upper triangular system

• The initial step will be to eliminate the first unknown x1 from the second through the nth equations.

• This equation can be subtracted from R2 to give

Or

where the prime indicates that the elements have been changed from their original values 15
GAUSS ELIMINATION
The procedure is then repeated for the remaining rows/equations. For instance, R 1 can be
multiplied by a31/a11 and the result subtracted from R3 to give:

𝑎11 𝑥1 + 𝑎12 𝑥2 + 𝑎13 𝑥3 ⋯ + 𝑎1𝑛 𝑥𝑛 = 𝑏1 ⋯ (2.1𝑎)

𝑎′22 𝑥2 + 𝑎′23 𝑥3 ⋯ + 𝑎′ 2𝑛 𝑥𝑛 = 𝑏′ 2 ⋯ 2.1𝑏′

𝑎′32 𝑥2 + 𝑎′33 𝑥3 ⋯ + 𝑎′ 𝑛 𝑥𝑛 = 𝑏′ 3 ⋯ 2.1𝑐′


⋮ ⋮

𝑎′𝑛2 𝑥2 + 𝑎′𝑛3 𝑥3 ⋯ + 𝑎′ 𝑛𝑛 𝑥𝑛 = 𝑏′ 𝑛 ⋯ (2.1𝑛′)

16
GAUSS ELIMINATION
• The next step is to eliminate x2 from R2 through Row n. To do this, multiply R2’ by
a’32/a’22 and subtract the result from R3’ to give:

𝑎11 𝑥1 + 𝑎12 𝑥2 + 𝑎13 𝑥3 = 𝑏1 ⋯ (2.1𝑎)

𝑎′22 𝑥2 + 𝑎′23 𝑥3 = 𝑏 ′ 2 ⋯ 2.1𝑏′

𝑎′′33 𝑥3 = 𝑏 ′′ 3 ⋯ 2.1𝑐′′

17
GAUSS ELIMINATION
2. Back Substitution.
• Equation (2.1n) can then be solved for xn:

• This result can be back-substituted into the (n − 1)th


equation to solve for xn−1.

The two phases of Gauss elimination:


(a) forward elimination and (b) back
substitution.

18
GAUSS ELIMINATION
Example 2.1
Use Gauss elimination to solve

19
GAUSS ELIMINATION

The Augmented matrix can be written from the system of equations as

3 −0.1 −0.2 7.85


0.1 7 −0.3 ቮ−19.3
0.3 −0.2 10 71.4

Multiply R1 by (0.1)/3 and subtract the result from R2 to give

3 −0.1 −0.2 7.85


0 7.00333 −0.293333 ቮ−19.5617
0.3 −0.2 10 71.4
20
Solution continued
3 −0.1 −0.2 7.85
0 7.00333 −0.293333 ቮ−19.5617
0.3 −0.2 10 71.4
Then multiply R1 by (0.3)/3 and subtract it from R3 to give

3 −0.1 −0.2 7.85


0 7.00333 −0.293333 ቮ−19.5617
0 −0.190000 10.0200 70.6150
multiply R2’ by -0.190000/7.00333 and subtract the result from R3’ to give

3 −0.1 −0.2 7.85


0 7.00333 −0.293333 ቮ−19.5617
0 0 10.0120 70.0843
We can now solve these equations by back substitution

21
3. Gauss-Jordan Reduction Method

• The Gauss-Jordan reduction method is an extension of the Gauss


elimination method.
• It reduces a set of n equations from its canonical form of 𝑨𝒙 = 𝒃 to the
diagonal set of the form 𝑰𝒙 = 𝒃′ where I is the unit matrix.

1 0 0 𝑥1 𝑏′1
0 1 0 𝑥2 = 𝑏′2
0 0 1 𝑥3 𝑏′3

• The Gauss-Jordan reduction method applies the same series of elementary


operations that are used by the Gauss elimination method.
• It applies these operations both below and above the diagonal in order to
reduce all the off-diagonal elements of the matrix to zero.
22
Try
Five reactors linked by pipes are shown below. The rate of mass flow
through each pipe is computed as the product of flow (Q) and
concentration (c). At steady state, the mass flow into and out of each
reactor must be equal.
For example, for the first reactor, a mass balance can be written as
Q01c01 + Q31c3 = Q15c1 + Q12c1
Write mass balances for the remaining reactors and express the
equations in matrix form. Then use MATLAB to solve for the
concentrations in each reactor.

23
Try:

24

You might also like