You are on page 1of 7

ChE 121 Engineering Computation

S 2013 - Lab 2 (Due:May 27, 2013)


1. Learning Objectives:
By completing the Lab, you should be competent in the following:

Using the iskeyword command to list protected MATLAB keywords


Modifying the number format in MATLAB
Creating predefined matrices and arrays using MATLAB commands
Understanding the different matrix operations available
Writing MATLAB script files and executing them

2. Instructions:
You should perform the Lab on an individual basis. You may, however, seek assistance
from TAs when you cant figure things out. To do so just raise your hand and a TA will
assist you.
Passing the Lab will require that you answer all the questions in the Tasks and Exercise
sections .

3. Protected Keywords iskeyword:


MATLAB contains several keywords that may not be used as variable names. An
example of this is the else keyword, since this is already used by MATLAB in ifstatement structures.
Usage:
iskeyword(string)
iskeyword string
iskeyword
The first two commands will return 1 if the string matches a protected MATLAB
keyword, while the third command will display a list of all MATLAB keywords.
Task 1:
Type:
iskeyword(while)

Then, use the third iskeyword command to familiarize yourself with other protected
keywords. Finally, attempt the following:
while = 3

Note the error message, so you can identify this problem in the future. Notice how
while turns blue once you type it, this indicates that it is a keyword that MATLAB
recognizes.

4. Number Format
MATLAB uses its own code to compute its operations; however, the results can be
displayed on the Command Window or in a script file in different formats using
MATLABs built-in function format. It is important to remember that the format
command is only used to format the output of a calculation. For more information about
this command, type: help format in the MATLAB Command Window.
Task 2(a):
Objective: Evaluate the expression e-2ln(5) and express the result in the formats available
in MATLAB. To do this, follow these instructions:
>> format short % changing the output format to short
>> a=exp(-2)
a =
0.1353
>> b = log(5)
b =
1.6094
>> a*b
ans =
0.2178
>>format long %changing the format to long
>> a*b
ans =
0.21781373573101

Task 2(b):
Type: help format on the MATLAB Command Window and choose 8 formats among the
list. Introduce the following matrix into MATLAB and display it in those formats.
A = [e-20

ln(10)

e2]

5. Predefined Matrix Types


MATLAB has several built-in functions for quickly making commonly used matrix
types.
Task 3:
Try using the following commands:
>> ones(3,2)
>> zeros(4,4)
>> diag([1 2 3])
>> eye(3)
>> eye(3,2)

6. Matrix Operations
MATLAB is capable of doing element by element mathematical operations as well as
standard matrix operations (determinants, inversion, dot products, etc.). There are also
built-in MATLAB functions to retrieve specific elements from a matrix as well as to
determine the size of a matrix.
Task 4:
Perform the following commands to familiarize yourself with matrix operations.
>> A = [1 2; 3 4];
>> B = [1 3; 2 6];
>> C= [8 9];

Once you have defined matrices A, B and C try the following operations.
>>A+B
>>A+C
>> A.*B
>>A*B
>>C.^2
>>C^2
>>inv(A)
>>A
>>det(B)

>>size(A)
>>A(1,2)

What is the difference between A*B and A.*B? The first command performs a row-bycolumn multiplication while the second one performs an element by element
multiplication. It is very important to understand the difference between these two
operations!

7. Creating scripts in the MATLABs Editor window:


We will create a file with the name Lab3_script.m Type in the following MATLAB code
inside the editor and save the file. To open the MATLAB editor, go to File New
M-file.
%Laboratory 2: Script
%
%Name:
David Williams put your name instead
%UserID:
202XXXXX
put your UserID
%Description:
This program calculates SIN(X) from 0 to pi
%
%Clear the screen first
clc % Command to clear the MATLAB screen
r = 4;
x = [0 pi/3 pi/2 2*pi/3 pi]
y = sin(x)
y
Save the file as Lab2_script.m in the directory that you have set in the MATLAB search
path. (File Save as)
Task 5:
Answer the following questions:
a) Before running the script file Lab2_script.m, type:
>> clear
Then, use the command who to find out what variables are stored so far in your
workspace.
b) Now, in the command window type:
>> a = 5;
>> b = [2,3,4];
>> c = [1 2 3];
4

>> d =Hello There;


>> r = 1;
Use the appropriate command to find out both the variables stored in your workspace and
their size and type.
c) What is the value of the variable r stored in your workspace at this moment?
d) Run your script Lab2_script.m
What did your screen display?
e) Answer the following questions:
- What is the value of r?
- What is the who function in MATLAB?
- Can running a script alter the value of a workspace variable?
- What is the function of a semicolon?
f) To get information about the program, you can try typing:
>> help Lab2_script
Which part of the script file shows up? Where does the help line terminate?

8. Exercises
1. You create the following array:
t = 0:0.5:8
Using linspace command, what will you do to obtain the same result?
2. Given the following matrices, perform the operations requested below and express it
in short format:
A=[2 3 8; 6 8 12; 5 6 12]
B=[9 14 -6; 8 24 5; 11 6 5]
C=[24, 23, 3; 5 9 8; 6 -3 15]
a)
b)
c)
d)
e)
f)
g)
h)
i)
j)

14A+9B-3C+15*B*(A*2C)
Inverse of A + Inverse of C + Transpose of (Transpose of B)
Transpose of C + Inverse of (B*A)
(Determinant of (Transpose of(Inverse of (Transpose of A))))^2
Determinant of A + (Determinant of (Transpose of B))^2
A*(Inverse of (B*C))
B*((Transpose of B)*B)
C*((Inverse of C)^(-1))+A*(Transpose of A)
3A+2sin(pi/2)B-exp(-9)*C
Inverse of (A*(Transpose of (B))+Transpose of C 15*(B*C)

Hint: the angles are expressed in radians and perform matrix operations, not element by
element operations.
3. Generate a script file to calculate the surface area and volume of a rectangular pyramid
with height of 0.5 m that prompts for dimensional user inputs. Test your script file for the
following cases:
a) Base is a square with side length=8 cm
b) Base is rectangular with length= 24 cm, width=5 cm
Formulas to be used:
LW h
P h
S P = B + L BW B ; V P = b b
3
2

Where SP= Pyramid surface area


PB = Base perimeter
VP=Pyramid Volume
LB= Length of the pyramid base
WB= Width of the pyramid base
h= Pyramid height
6

4. The behavior of a non-ideal gas can be represented by the following Van der Waals
equation of state (EOS):
RT
a
P=
2
V b V
Where,
a = 9.2548e6;
b = 90.3393;
R = 82.06;

(cm3.atm/mol2)
(cm3/mol)
(cm3.atm/mol.K)

Create a script that calculates the pressure from the above EOS using the
following data:
Volume (cm3/mole)
1000
1500
2000
2500
3000

Temperature (K)
250
300
350
400
450

5. A student records time vs. concentration of component B (in mol/L) obtained in a


reactor where a consecutive reaction A B C takes place and the data is shown
the following table:
Time(min)
Concentration of B (mol/L)
1
0.02
2.5
0.028
4
0.034
6
0.024
9
0.021
Write a single MATLAB script file where you define an array variable TIMCON that
holds time and concentration data, and using the array variable TIMCON find the
maximum concentration of B obtained in the reactor (assign it to a variable
MAXCON) and the corresponding time at which it occurs (assign it to a variable
MAXTIME) and further should return the following statement when executed:
The maximum concentration is 0.034 mol/L and occurs at 4
minutes.
(note: use disp and num2str for the last part of the program)

You might also like