Chemical Engineering
Laboratory III
LAB #1
LAB #1
• Introduction to MATLAB as a technical computing tool.
• Defining and managing variables.
MATLAB
A computer program that provides the user
to perform many types of calculations. In
particular, tool to implement numerical
methods.
The Environment
Workspace:
shows a list of
variables created
by MATLAB.
Command window: Type your
instructions here and press
ENTER to execute them.
The way to operate MATLAB
• is by entering the commands (Calculations and
Creating plots) one at a time in the command
window.
• Alternatively, such commands can be used to
create MATLAB programs (by using edit windows).
MATLAB uses three primary windows
Command window Graphics window Edit window
Used to enter commands and data. Used to display plots and graphs. Used to create and edit M-files.
MATLAB uses three primary windows (Example)
Command window Graphics window Edit window
Used to enter commands and data. Used to display plots and graphs. Used to create and edit M-files.
After starting MATLAB
the command window will be opened and the
command prompt is displayed.
>>
For each command, you get a result. Thus, you
can think of it as operating like a very fancy
calculator. i.e.:
>> 55 - 16
• Note: MATLAB has automatically assigned the
answer to a variable, ans. (refer to workspace)
Scalars
The Assignment
How we assign values to variable names. Arrays,
Vectors
This results in the storage of the values in and
Matrices
the memory location corresponding to the
variable name.
Colon
Operator
Try it yourself !!!
• >> a = 4
Scalars • >> A = 6; Function
• >> b = 3, pi; x = 1;
• >> x = 2+i*4
Character
Strings
Arrays, Vectors and Matrices
is a collection of values that are represented
by a single variable name.
1D arrays are called vectors. 2D are called matrices.
Note: The scalars used are actually matrices with one row and one column.
Challenge
Create a matrix (2x3)
𝟑 𝟏. 𝟓 𝟐
𝒙=
𝟎. 𝟏 𝟕 𝟏
Colon Operator, : Aside from creating
series of numbers, it
is a powerful tool for creating and
manipulating arrays. can be used to select
Starting value Final value
the individual rows
x = [xi ∶ dx ∶ xf ]
or/and columns of a
increment
matrix.
Functions
MATLAB defined fcn
1.linspace
2.logspace
3.rand
4.magic
5.ones
6.zeros
7.eye
8.and many more …
Character Strings
Aside from numbers, alphanumeric information can be represented by
enclosing the strings within single quotation marks
Each character in a string is one element in an array. Thus we can
combine them as in:
Challenge
The density of freshwater can be computed as a function of
temperature with the following cubic equation:
𝜌 = 5.5289 × 10−5 𝑇𝑐 − 8.5016 × 10−6 𝑇𝑐2
+ 6.5622 × 10−5 𝑇𝑐 + 0.99987
where ρ = density (g/cm3) and T = temperature (°C).
C
Q1: Use MATLAB to generate a vector of density ranging from
32 °F to 93.2 °F using increments of 3.6 °F.
Q2: Determine the density at T = 60.8 °F
LAB #1
• Creating and manipulating vector and matrix
Mathematical Operations
Mathematical Mathematical Array
+ Addition + Addition
- Subtraction - Subtraction
* Multiplication .* Multiplication
/ Division ./ Division
^ Power .^ Power
Try it Yourself !!!
>> 2*pi Operations with scalar quantities are handled in a
straightforward manner.
>> 2 + 9
>> a=1,b=3, c=2*a/b
>> y1 = -6^2 Exponentiation has higher priority then negation,
thus 6^2 = 36 and final answer is -36
>> y2 = (-6)^2 Parentheses can override the exponentiation term,
thus (-6)^2 = 36
The real power of MATLAB is illustrated in its
ability to carry out vector-matrix calculations
To further illustrate vector-matrix multiplication, first redefine a and b:
>> a = [1 2 3]; b = [4 5 6];
>> x1 = 2*a Mixed operations with scalars variable
>> x2 = 2/b
>> c = 2.3; x3 = c*b
>> a*b Matrix, a Matrix, b
1 2 3 × 3 4 5
𝑎(1 × 3) 𝑏(1 × 3)
3 𝑐𝑜𝑙𝑢𝑚𝑛𝑠 ≠ 1 𝑅𝑜𝑤𝑠
Note: Matrices cannot be multiplied if the
inner dimensions are unequal
Matrix, B
Try it yourself !!! 4
Matrix, a
>> A = [1 2 3]; B = [4; 5; 6]; 1 2 3 × 5
6
>> X1 = A*B 𝑎(1 × 3) 𝐵(3 × 1)
3 𝑐𝑜𝑙𝑢𝑚𝑛𝑠 = 3 𝑅𝑜𝑤𝑠
>> X2 = B*A 1 𝑟𝑜𝑤𝑠 1 𝑐𝑜𝑙𝑢𝑚𝑛𝑠
The size of product Matrix
1×1
Challenge
Question: Compute 𝑌1, 𝑌2 and 𝑌3 if the value for 𝑥1, 𝑥2 and 𝑥3 are 1.3,
2.7 and 1.02 respectively.
𝑌1 = −2𝑥1 + 3𝑥2 + 𝑥3 --- Eq.1
𝑌2 = −𝑥1 + 3𝑥2 + 3𝑥3 --- Eq.2
𝑌3 = 2𝑥1 − 𝑥2 + 𝑥3 --- Eq.3
Hint: Create MATRIX equation by using Eq1, Eq2 and Eq3.
𝑌1 −2 3 1 𝑥1
𝑌2 = −1 3 3 𝑥2
𝑌3 2 −1 1 𝑥3
(3x1) (3x3) (3x1)
𝑌 =𝐴∗𝑋
What if you want to square each element of M?
2 3 7 22 32 72 2∗2 3∗3 7∗7
𝑀= 0 8 3 𝑁 = 02 82 32 OR 𝑀 = 0∗0 8∗8 3∗3
82 42 12 8∗8 4∗4 1∗1
8 4 1
Answer:
>> M = [2 3 7;0 8 3;8 4 1], N = M.^2 >> M = [2 3 7;0 8 3;8 4 1], N = M.*M
Note: The . preceding the ^ operator signifies that the
operation is to be carried out element by element.
Also known as “array operations” which referred as
element-by-element operations.
Use of Built-In Functions
>> help elfun
Question
Compute values of x, and y for t = 0 to 6π with t = π.
𝑥 = 𝑡 cos(6𝑡)
𝑦 = 𝑡 sin(6𝑡)
Try it yourself !!!
>> dt = pi ,t = 0:dt:6*pi
>> x = t.*cos(6*t)
>> y = t.*sin(6*t)
Challenge
The following equation can be used to compute values of y as a function of x:
𝑦 = 𝑏𝑒 −𝑎𝑥 sin(𝑏𝑥)(0.012𝑥 4 − 0.15𝑥 3 + 0.075𝑥 2 + 2.5𝑥)
where a and b are parameters. Write the equation for implementation with
MATLAB, where a = 2, b = 5, and x is a vector holding values from 0 to π/2 in
increments of x = π/40. In addition, compute the vector where each element
holds the square of each element of y. Combine x, y, and z into a matrix w,
where each column holds one of the variables, and display w (short format).
End of LAB #1