Professional Documents
Culture Documents
CHEP 530D1
COMPUTER APPLICATIONS
IN
CHEMICAL ENGINEERING
Crispulo G. Maranan
Instructor
Laboratory Exercise
November 9, 2013
Laboratory Exercise
November 9, 2013
8.Evaluate the results after pressing the enter key using colon (:) in addressing arrays.
9. Identify the different built-in functions for handling array and indicate its description and give an
example.
10.Evaluate the results after pressing the enter key that involves strings and strings as variables.
11. Evaluate the results after pressing the enter key that involves the operations of matrices.
12.Evaluate the values of x, y and z of the three equations three unknowns :
4x 2y + 6z = 8
2x + 8y + 2z = 4
6x + 10y + 3z = 0
13.Evaluate the results after pressing the enter key that involves element-element operations.
14.Identify the different built-in functions for analyzing arrays and indicate its description and give an
example.
Course:
Group No.:
Bautista, Keziah Lynn S.
No. 1
Familiarization with Matlab Environment,
Built-in Functions, Matrices and Plotting
Group Members:
2.Figure Window
3.Editor Window
4.Help Window
5.Launch Pad Window
6.Command History
7.Workspace Window
8.Current Directory
Purpose
Inputs the commands to be use. It is used to
execute commands or aliases directly in the
Visual
Studio
integrated
development
environment (IDE). You can execute both menu
commands and commands that do not appear on
any menu. To display the Command window,
choose Other Windows from the View menu, and
select Command Window.
It is directed to a window that is separate from
the Command Window. This window is referred
to as a figure. The characteristics of this window
are controlled by your computer's windowing
system and MATLAB figure properties.
It is to create your own custom editor window
that can float free or be docked as a tab, just like
the native windows in the Unity interface. Editor
windows are typically opened using a menu item.
Helps you about the commands and other terms
to be used.
Access help, tools, demos and documentation
Compiles the history of the commands
It consist of common mathematical figures,
graphs, etc.
The directory (folder) that MATLAB is currently
working in. This is where anything you save will
go by default, and it will also influence what files
MATLAB can see. You won't be able to run a
script that you saved that you saved in a di
erent directory.
2.
Bautista, Keziah Lynn S.
No. 1
Familiarization with Matlab Environment,
Built-in Functions, Matrices and Plotting
Laboratory Exercise
November 9, 2013
Symbol
>>
;
%
clc
Purpose
Relational operators perform element-byelement comparisons between two arrays. They
return a logical array of the same size, with
elements set to logical 1 (true) where the
relation is true, and elements set to logical
0(false) where it is not.
Used inside brackets to end rows. sssUsed
after an expression or statement to suppress
printing or to separate statements.
Percentage. Used to indicate that the number
preceding it should be understood as a
proportion multiplied by 100
It clears all input and output from the Command
Window display, giving you a "clean screen."
After using clc, you cannot use the scroll bar to
see the history of functions, but you still can use
the up arrow key, , to recall statements from
the command history.
3.
Mathematical Expression
>> 8 + 5/9
>> (8 + 5)/9
>> 8^5/9
>>29^1/5 + 35^0.7
Result
8.5556
1.4444
3.6409e+03
17.8461
4.
Built-in Function
>>sqrt(144)
>>exp(7)
>>abs(-99)
>>log(100000)
>>log10(100000)
>>factorial(10)
>>sin(pi/4)
>>round(19/6)
>>rem(16,5)
>>sign(-19)
Result
12
1.0966e+03
99
11.5129
5
3658800
0.7071
3
1
-1
5.
>>x= 10
>>x=4*x -15
>>a = 10
>>B= 9
>>C= (a B) +50 a/B *16
Bautista, Keziah Lynn S.
No. 1
Familiarization with Matlab Environment,
Built-in Functions, Matrices and Plotting
10
25
10
9
33.2222
Laboratory Exercise
November 9, 2013
33.2222
0.9900
0.6750
yr =
Columns 1 through 2
2001
2002
Columns 3 through 4
2003
2004
Column 5
2005
>>yr = [ 256; 299; 350; 402; 503]
yr =
>>y = [1:2:15]
256
299
350
402
503
y=
Columns 1 through 5
1
Columns 6 through 8
>>y = [1.5:0.1;2.0]
>>y=[-5:15]
11 13 15
2
y=
Columns 1 through 5
-5 -4 -3 -2 -1
Columns 6 through 10
0
Columns 11 through 15
Bautista, Keziah Lynn S.
No. 1
Familiarization with Matlab Environment,
Built-in Functions, Matrices and Plotting
Laboratory Exercise
November 9, 2013
Columns 16 through 20
10 11 12 13 14
Column 21
>>b = [21:-3:6]
15
b=
Columns 1 through 5
21 18 15 12
Column 6
>>a = linspace(0,8,6)
6
a=
Columns 1 through 3
0 1.6000 3.2000
Columns 4 through 6
>>b=linspace(30,10,11)
>>c=linspace(49.5,0.5)
10
c=
Columns 1 through 3
Laboratory Exercise
November 9, 2013
Laboratory Exercise
November 9, 2013
Columns 37 through 39
31.6818 31.1869 30.6919
Columns 40 through 42
30.1970 29.7020 29.2071
Columns 43 through 45
28.7121 28.2172 27.7222
Columns 46 through 48
27.2273 26.7323 26.2374
Columns 49 through 51
25.7424 25.2475 24.7525
Columns 52 through 54
24.2576 23.7626 23.2677
Columns 55 through 57
22.7727 22.2778 21.7828
Columns 58 through 60
21.2879 20.7929 20.2980
Columns 61 through 63
19.8030 19.3081 18.8131
Columns 64 through 66
18.3182 17.8232 17.3283
Columns 67 through 69
16.8333 16.3384 15.8434
Bautista, Keziah Lynn S.
No. 1
Familiarization with Matlab Environment,
Built-in Functions, Matrices and Plotting
Laboratory Exercise
November 9, 2013
Columns 70 through 72
15.3485 14.8535 14.3586
Columns 73 through 75
13.8636 13.3687 12.8737
Columns 76 through 78
12.3788 11.8838 11.3889
Columns 79 through 81
10.8939 10.3990 9.9040
Columns 82 through 84
9.4091 8.9141 8.4192
Columns 85 through 87
7.9242 7.4293 6.9343
Columns 88 through 90
6.4394 5.9444 5.4495
Columns 91 through 93
4.9545 4.4596 3.9646
Columns 94 through 96
3.4697 2.9747 2.4798
Columns 97 through 99
1.9848 1.4899 0.9949
Column 100
0.5000
Laboratory Exercise
November 9, 2013
10
7.
>>a = [2 35 6;5 67 88;22 56 89]
>>b = [23 56 78 73 68
35 98 54 32 15
99 34 23 12 2]
>>cd = 9 ;e 6;h=8;
>>Ram=[e, cd*h,cos(pi/3);h^2,sqrt(h*h/cd),15]
>>Z= [1:2:11;0.0:5:25;linspace(10,60,6)]
a=
2 35 6
5 67 88
22 56 89
b=
23 56
35 98
99 34
Ram =
6.0000
64.0000
Z=
78 73 68
54 32 15
23 12 2
72.0000 0.5000
2.6667 15.0000
Columns 1 through 5
1 3 5 7 9
0 5 10 15 20
10 20 30 40 50
Column 6
>>zr=zeros(4,6)
11
25
60
zr =
Columns 1 through 5
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Column 6
0
Bautista, Keziah Lynn S.
No. 1
Familiarization with Matlab Environment,
Built-in Functions, Matrices and Plotting
Laboratory Exercise
November 9, 2013
11
>>on=ones(3,4)
0
0
0
on =
1
1
1
1
1
1
>>we=eye(5)
1 1
1 1
1 1
we =
1
0
0
0
0
aa =
0
1
0
0
0
0
0
1
0
0
0
0
0
1
0
4
bb =
4
B=
3
8
2
C=
6
7
7
7
6
9
8
4
3
3
8
2
6
7
7
7
6
9
8
4
3
>>aa=[4 8 9]
>>bb= aa
>>B=[3 6 7 8; 8 7 6 4;2 7 9 3]
>>C=B
>>D=[ 3 5 6 8 23 67]
>>E=D(3)
0
0
0
0
1
D=
Columns 1 through 5
3
8 23
Column 6
67
E=
>>D(2)=69
Bautista, Keziah Lynn S.
No. 1
Familiarization with Matlab Environment,
Built-in Functions, Matrices and Plotting
6
D=
Laboratory Exercise
November 9, 2013
12
Columns 1 through 5
3 69
8 23
Column 6
>>D(2) + D(5)
>>D(3)^3 + D(4)^4
>>M=[3 11 6 5;4 7 10 2;13 9 0 8]
67
92
4312
M=
>>M(2,3)=18
3 11 6
4 7 10
13 9 0
M=
>>M(3,2)-M(4,1)
3 11 6 5
4 7 18 2
13 9 0 8
Index exceeds matrix dimensions.
8.
>> v=[23 56 34 45 67 54 23 12 21]
>>w=v(2:6)
5
2
8
v=
Columns 1 through 5
23 56 34 45 67
Columns 6 through 9
54 23 12 21
w=
>>Q=[1 3 4 5 6 8 ;4 6 7 8 2 1;1 1 4 6 8 9;
23 56 7 8 34 2; 21 45 67 83 2 3]
56 34 45 67 54
Q=
Columns 1 through 5
1
4
1
23
21
3
6
1
56
45
4
7
4
5
8
6
6
2
8
7 8 34
67 83 2
Laboratory Exercise
November 9, 2013
13
Column 6
>>R=Q(:,3)
8
1
9
2
3
R=
>>S=Q(2,:)
4
7
4
7
67
S=
Columns 1 through 5
4
Column 6
>>T=Q(2:4,:)
1
T=
Columns 1 through 5
4 6 7 8 2
1 1 4 6 8
23 56 7 8 34
Column 6
>>U=Q(1:3,2:4)
1
9
2
U=
>>V=4:3:34
3
6
1
V=
4
7
4
5
8
6
Columns 1 through 5
Bautista, Keziah Lynn S.
No. 1
Familiarization with Matlab Environment,
Built-in Functions, Matrices and Plotting
Laboratory Exercise
November 9, 2013
14
7 10 13 16
Columns 6 through 10
19 22 25 28 31
Column 11
>>A=[10:-1:4;ones(1,7);2:2:14;zeros(1,7)]
34
A=
Columns 1 through 5
10 9 8 7 6
1 1 1 1 1
2 4 6 8 10
0 0 0 0 0
Columns 6 through 7
5
1
12
0
B=
>>B=A([1,3],[1,3,5:7])
4
1
14
0
10 8 6 5 4
2 6 10 12 14
9.
Function
length(A)
Description
Designate the number
column
of
size(A)
reshape(A,m,n)
Example
10 9 8 7 6 5 4
1 1 1 1 1 1 1
2 4 6 8 10 12 14
0 0 0 0 0 0 0
Ans
=7
10 9 8 7 6 5 4
1 1 1 1 1 1 1
2 4 6 8 10 12 14
0 0 0 0 0 0 0
Ans
=4x7
Reshape a 3-by-4 matrix into a
2-by-6 matrix:
Laboratory Exercise
November 9, 2013
15
diag(v)
Diag(A)
B=
1 3 5 7 9 11
2 4 6 8 10 12
Diagonal
matrices
and 23 0 0 0 0 0 0 0
diagonals of a matrix.
0
0 56 0 0 0 0 0 0
0
0 0 34 0 0 0 0 0
0
0 0 0 45 0 0 0 0
0
0 0 0 0 67 0 0 0 0
0 0 0 0 0 54 0 0
0
0 0 0 0 0 0 23 0
0
0 0 0 0 0 0 0 12
0
0 0 0 0 0 0 0 0
21
Diagonal of matrices and 10 9 8 7 6 5 4
diagonal of matrix.
1 1 1 1 1 1 1
2 4 6 8 10 12 14
0 0 0 0 0 0 0
Ans
= 10
1
6
0
where: A is a matrix and v is a vector
10.
>> b = Matlab Programming
>>c= My name is Richard Schooling
>>c(5)
>>c(12:18)
No result
No result
47.5202
Columns 1 through 5
44.0556 43.5606 43.0657 42.5707 42.0758
Columns 6 through 7
Laboratory Exercise
November 9, 2013
16
>>Info=char(Student
Schooling,Grade:,A+)
11.
>>VecA=[ 8 6 7];VecB=[2 3 6];
>>VecC= VecA + VecB
41.5808 41.0859
Name:,Richard No result
>>A=[2 3 4; 5 4 7; 3 6 9; 5 3 1];
>>B=[3 4 ; 3 2 ; 7 8];
>>C=A*B
>>D=B*A
>>F=[6 7; 4 3]; G=[1 2; 4 5];
>>H=F*G
>>I=G*F
VecC =
10
C=
9 13
1 -9
6 6
D=
2
1
5 -1 12
8 10 5
C=
43 46
76 84
90 96
31 34
Inner matrix dimensions must agree.
H=
34 47
16 23
I=
14 13
44 43
>>AV=[ 2 5 7];BV=[3;4;1];
>>AV*BV
>>BV*AV
>>A*b
33
ans =
6 15 21
8 20 28
2 5 7
ans =
4 12 14 18
6 4 2 8
8 12 6 2
ans =
Laboratory Exercise
November 9, 2013
17
>>D=5*A
4 12 14 18
6 4 2 8
8 12 6 2
D=
10 30 35 45
15 10 5 20
20 30 15 5
B=
>>A*B
>>A*A^-1
1.0000
0 0.0000
-0.0000 1.0000
0
-0.0000 0.0000 1.0000
ans =
1.0000
0 0.0000
-0.0000 1.0000
0
-0.0000 0.0000 1.0000
12.
>>A = [4 -2 6;2 8 2;6 10 3];
>>B= [8;4;0];
>>X = A\B
X=
>>Xb=inv(A)*B
-1.8049
0.2927
2.6341
Xb =
-1.8049
0.2927
2.6341
C=
2 6
8 10
2 3
>>D=[8 4 0]
4
-2
6
D=
>>Xc=D/C
8
Xc =
Laboratory Exercise
November 9, 2013
18
13.
>>A=[3 6 8; 3 5 6]
A=
6
5
8
6
>>B=[2 4 3; 6 3 4]
3
3
B=
4
3
3
4
>>C=A.*B
2
6
C=
>>D=A./B
6 24 24
18 15 24
D=
>>E=B.^B
256
27
Column 3
>>F=A*B
>>x=[1:8]
27
256
Inner matrix dimensions must agree.
x=
Columns 1 through 5
1
Columns 6 through 8
6
>>y=x.^2 + 5*x
y=
Columns 1 through 5
6 14 24 36 50
Columns 6 through 8
Laboratory Exercise
November 9, 2013
19
>>x=[1:2:15]
66 84 104
x=
Columns 1 through 5
1
Columns 6 through 8
>>y=(x.^3 + 5*x)./(4*x.^2 10)
11 13 15
y=
Columns 1 through 3
-1.0000 1.6154 1.6667
Columns 4 through 6
2.0323 2.4650 2.9241
Columns 7 through 8
>>
3.3964 3.8764
x=
Columns 1 through 3
0 0.5236 1.0472
Columns 4 through 6
1.5708 2.0944 2.6180
Column 7
>>y=cos(x)
3.1416
y=
Columns 1 through 3
1.0000 0.8660 0.5000
Columns 4 through 6
Laboratory Exercise
November 9, 2013
20
Description
Example
It returns the mean values of A = [1 2 3; 3 3 6; 4 6 8; 4 7 7];
the elements along different
dimensions of an array.
mean(A)
ans =
3.0000 4.500 6.0000
mean(A,2)
ans =
2.0000
4.0000
6.0000
6.0000
C=max(A)
Laboratory Exercise
November 9, 2013
21
ans =
5
7
(d,n)=max(A)
min(A)
8
5
5
9
(d,n)=min(A)
sum(A)
sort(A)
5
3
4
5
Laboratory Exercise
November 9, 2013
22
the
ascending order.
median(A)
sort(A)
ans =
5 6 10 23 45 78 100
Define a 4-by-3 matrix.
A = [0 1 1; 2 3 2; 1 3 2; 4 2 2]
A=
0 1 1
2 3 2
1 3 2
4 2 2
Find the median value of each
column.
M = median(A)
M=
1.5000 2.5000 2.0000
For each column, the median
value is the mean of the middle
two numbers in sorted order.
For matrix X
X=
1 5 9
7 15 22
s = std(X,0,1)
s=
4.2426 7.0711 9.1924
s = std(X,0,2)
s=
4.000
7.5056
std(A)
It is a function of X, where X is a
vector, returns the standard
deviation using (1) above. The
result s is the square root of an
unbiased estimator of the
variance of the population from
which X is drawn, as long as X
consists
of
independent,
identically distributed samples.
det(A)
Laboratory Exercise
November 9, 2013
23
cross(a,b)
November 9, 2013
24
inv(A)
d = dot(a,b)
d=
32
It returns the inverse of the Here
is
an
example
square matrix X. A warning demonstrating the difference
message is printed if X is badly between solving a linear system
scaled or nearly singular.
by inverting the matrix with
inv(A)*b and solving it directly
with A\b. A random matrix A of
order 500 is constructed so that
its condition number, cond(A), is
1.e10, and its norm, norm(A), is1.
The exact solution x is a random
vector of length 500 and the
right-hand side is b = A*x. Thus
the system of linear equations is
badly conditioned, but consistent.
On a 300 MHz, laptop computer
the statements
n = 500;
Q = orth(randn(n,n));
d = logspace(0,-10,n);
A = Q*diag(d)*Q';
x = randn(n,1);
b = A*x;
tic, y = inv(A)*b; toc
err = norm(y-x)
res = norm(A*y-b)
produce
elapsed_time =
1.4320
err =
7.3260e-006
res =
4.7511e-007
while the statements
tic, z = A\b, toc
err = norm(z-x)
res = norm(A*z-b)
produce
elapsed_time =
0.6410
err =
7.1209e-006
res =
Laboratory Exercise
November 9, 2013
25
4.4509e-015
It takes almost two and one half
times as long to compute the
solution with y = inv(A)*b as with
z = A\b. Both produce computed
solutions with about the same
error, 1.e-6, reflecting the
condition number of the matrix.
But the size of the residuals,
obtained by plugging the
computed solution back into the
original equations, differs by
several orders of magnitude. The
direct solution produces residuals
on the order of the machine
accuracy, even though the
system is badly conditioned.
The behavior of this example is
typical. Using A\b instead of
inv(A)*b is two to three times as
fast and produces residuals on
the order of machine accuracy,
relative to the magnitude of the
data.
7. Conclusion:
I therefore conclude that Matlab can be used as an engineering devise in solving common mathematical
problems such as mathematical computations, modelings, graphs, etc.
8. Further Readings:
Ferraris, G. and Manenti, F. (2010). Interpolation and regression models for the chemical engineer:
solving numerical problems. Germany: Wiley-VCH Verlag
Filo, O. (2010). Information processing by biochemical systems: neural network type
configurations.
New Jersey: Wiley.
Gopal, S. (2009). Bioinformatics: a computing perspective. India: McGraw-Hill Science/Engineering
Math.
Jaluria, Y. (2012). Computer methods for engineering with MATLAB applications (2nd ed.). Boca,
Raton,Florida: CRC Press.
Knopf, F. C. (2012). Modeling, analysis and optimization of process and energy systems.Hoboken,
New Jersey: John Wiley and Sons.
Velten, K. (2009). Mathematical modeling and simulation: introduction for scientists and engineers.
Singapore: Wiley-VCH.
Bautista, Keziah Lynn S.
No. 1
Familiarization with Matlab Environment,
Built-in Functions, Matrices and Plotting
Laboratory Exercise
November 9, 2013
26
Laboratory Exercise
November 9, 2013
27
engineering
problems.
3. Recognize
the benefits
and
constraints
of modern
engineering
tools.
Evaluated by:
problems.
Does not
recognize the
benefits and
constraints of
modern
engineering
tools.
Recognizes
some
benefits and
constraints of
modern
engineering
tools.
Recognizes the
benefits and
constraints of
modern
engineering
tools and
shows intention
to apply them
for engineering
practice.
Total Score
Mean Score = (Total Score / 3)
Percentage Rating = (Total Score / 12) x 100%
______________________________________
_______________
Printed Name and Signature of Faculty Member
Date
Laboratory Exercise
November 9, 2013
28