Lect-1 - P1 - Computer Applications
Lect-1 - P1 - Computer Applications
1
coding and simulation tools for the solution of
chemical and process engineering problems
including MATLAB coding
Introduction to MATLAB
o Matrices and Factors Operation
o Functions and Plots
o Iterations and Conditions
Identify how to write algorithms and Matlab codes to solve chemical
engineering related problems, and the required data and inputs to
simulators.
2
Introduction
MATLAB (MATrix LABoratory) is an interactive software system for
numerical computations and graphics.
4
Getting Started
Start Matlab by double clicking the icon on the desktop, or
from the start menu. To use Matlab you can simply enter
commands after the prompt (the >> is the Matlab prompt).
• Entering Matrices
You can enter matrices into MATLAB in several different ways:
• Enter an explicit list of elements.
• Load matrices from external data files.
• Generate matrices using built-in functions.
• Create matrices with your own functions in M-files.
A=12345 10
Example 9-1
11
Solution
Type the following command
12
Assessing the matrix element in the format of (row, column);
for example : C=B(1,4) would give 4 as an answer.
Example 9-2
Define Colum matrix
» B=[1 2 3 4 5]
Find:
C=B(1,4) 13
14
o Entering matrices into Matlab is the same as entering a vector,
except each row of elements is separated by a semicolon (;) or a
return:
o >>B = [1 2 3 4; 5 6 7 8; 9 10 11 12]
B=
1 2 3 4
5 6 7 8
9 10 11 12
15
o The special character prime ' denotes the transpose of a matrix e.g.
16
o Addition and subtraction of matrices are denoted by + and -. This operations
are defined whenever the matrices have the same dimensions.
For example: If A and B are matrices, then Matlab can compute A+B and A-B
when these operations are defined.
17
o Matrices can be joined together by treating them as elements of vectors:
18
o Matrix operations simply act identically on each element of an array. We have
already seen some vector operations, namely + and - , which are defined for
vectors the same as for matrices. But the operators * , / and ^ have different
matrix interpretations.
19
o Matrix division: To recognize how the two operator / and \ work
20
o Matrix Inverse: The function inv is used to compute the inverse of a matrix.
Let, for instance, the matrix A be defined as follows:
21
Maths operators
22
o Square root ( )
• >> r = sqrt(9)
value of r has been replaced by 3
23
One can use Matlab like a calculator without specifying
variables. Matlab has all the standard mathematical operations.
Try type:
Example 9-3
24
Solution:
25
Example 9-4
The volume of a circular cylinder of height h and radius r is given by
V = πr² h. A particular cylindrical tank is 15 m tall and has a radius of
8 m. We want to construct another cylindrical tank with a volume
20 percent greater (20% V)but having the same height. How large
must its radius be?
Solution :
26
Solution :
we can use the MATLAB
27
• Matrix Division and Linear Algebraic Equations
You can use the left division operator (\) in MATLAB to solve sets
of linear algebraic equations. For example, consider the set
To solve such sets in MATLAB you must create two arrays; we will call them A and
B. The array A has as many rows as there are equations, and as many columns as
there are variables. The rows of A must contain the coefficients of x, y, and z in
that order.
28
29
• The Matrix Inverse Method
30
HW#1
Use MATLAB to compute the following expressions.
1. 3
27 + 32(0.2) Ans: = 5
(2)
8
2. 4 − 12 − Ans: = 3
4×2
2
3. 8(3) + + 5 Ans: = 514.9
3
4. Ans. =
5. Ans. =
31
6. A= [1 2 3 7; 0 2 4 5; 10 1 2] B= [1 2 3 7; 0 2 4 5; 10 1 2]
Find:
(1) A +B
(2) X=A’
(3) C = X/B
(4) Z=B(2,3)
7. The ideal gas law relates the pressure P, volume V, absolute temperature
T, and amount of gas n. The law is where R is the gas constant. P V= nRT
Calculate molar volume of gas.
Given data:
Pressure P =2.2 atm
Temperature T=15 ᴼC
R= 0.082 Lit atm/mol K 32
8. Suppose x takes on the values x = 1, 1.2, 1.4, . . . , 5. Use
MATLAB to compute the array y that results from the function y
= 7 sin(4x). Use MATLAB to determine how many elements are
in the array y, and the value of the third element in the array y.
33