Principles of Matlab (Fall 07) Workout #1
Principles of Matlab (Fall 07) Workout #1
Principles of Matlab (Fall 07) Workout #1
WORKOUT #1
2. Use the MATLAB diary command to save the following in a file named session1:
1) Remove all variables from the workspace
2) Format all numbers with a floating point format with 5 digits (scientific notation)
3) Clear the screen, so that the MATLAB prompt appears at the top left of the screen
4) Let a = 112 ; b = -12 ; c = 3π and d = 6 - 12 i (Suppress the appearance of these
variables on the MATLAB command window)
5) Find the value of the variable "Number" if it equals 2a3/ c (ans :9.1527 x 105)
6) Restore the default format for displaying numbers
7) Find the value of the variable "ValueofAngle", if it equals Number/9.1527x105
(ans : 1.0000)
8) Find the arcsine of ValueofAngle, in degrees (ans : 89.8951)
9) Find the inverse cosine of the one sixth (i.e. 1/6) of the real part of the variable "d"
(ans : 0)
10) Get the quotient (division) of the variables "a" and "b" using left division, and put
the result in a variable named "Quotient" (ans : -9.3333)
11) Round the absolute value of Quotient to the nearest integer, and calculate its
exponential, put the result in a variable named "expquot" (ans : 8.1031 x 103)
12) Get a list of the current variables available in the MATLAB workspace
13) Save the current session in a file named firstime.
• Turn the diary command off, exit MATLAB and look for the file named session1,
open it and make sure all steps were recorded
• Enter MATLAB again, load the file firstime and make sure that all the variables
are now in the workspace
PRINCIPLES OF MATLAB (FALL 07)
WORKOUT #2
1. Write a script file called moles.m that calculates the number of moles, n, in kmol,
of 120 liters of hydrogen at 2.5 bars and 50°C. The file should contain one or two
comment lines describing what the script file is calculating. (Remember: PV =
nRT) (Ans: 0.0123 kmol)
RT αa
3. The SRK equation of state is given as: P = - ;
(V - b)
V(V + b)
where α, a and b are system dependent parameters: Tc and Pc are the critical temperature
a = 0.42747 R2Tc2/Pc and pressure, respectively
b = 0.08664RTc/Pc
α = [1 + m(1- T / Tc )]2
where m = 0.48508 + 0.55171w - 0.15613w2
(w is the Pitzer acentric factor)
Five kmol N2, at 75°C occupy 10 m3. The critical temperature and pressure of
nitrogen are 126.2 K and 33.5 atm, respectively. The acentric factor is 0.04.
Write a script file called n2press.m that calculates the pressure of nitrogen.
(Ans: 14.3124 bars)
The results of executing the script file should appear as follows, using compact
format:
The data for nitrogen are:
V=
n=
T=
The parameters are:
a=
b=
w=
alpha =
The pressure of nitrogen is:
P=
Note : Leave 5 blank lines before printing the parameters, and five before
the pressure.
4. The following expression approximates the factorial of N ( N!), where N is an
integer greater than or equal zero:
N
⎛N⎞
N! = 2πN ⎜ ⎟ , where e = 2.718282
⎝e⎠
Write a function called fact.m that will calculate N!. Use this function to find the
value of the variable Divresult, if it equals the factorial of 12 divided by 4.7569x108
(Ans: 1)
1. Use the matrix A, to construct the two matrices B and C, such that B is formed
from the first three rows and first three columns of A, and the matrix C is
composed of the rows 2 to 4 and the columns 2 to 4 of the matrix A.
⎡7 1 1 1⎤
A= ⎢2 11 1 - 1⎥ Get the sizes of all three matrices
⎢1 2 7 2⎥
⎣⎢1 - 1 - 1 8 ⎦⎥
2. Find the product of the B and C, the product of their corresponding elements.
7. Find the quotient of the matrix composed of the 0.9383 0.0731 0.1607 0.1250
-0.2760 0.9659 0.1250 -0.1250
upper triangle of A and the matrix composed of Ans -0.1364 -0.1656 1.0357 0.2500
the lower triangle of A. -0.1818 0.0649 0.1429 1.0000
8. From what follows find the values of the elements of the array x
⎡4 0 0 0 2 ⎤ ⎡ x1 ⎤ ⎡ 14 ⎤
⎢1 - 3 1 0 0 ⎥ ⎢ x 2⎥ ⎢− 2 ⎥
⎢1 - 1 5 1 - 1 ⎥ ⎢ x 3 ⎥ = ⎢ 13 ⎥
⎢- 3 0 0 4 0 ⎥ ⎢ x 4 ⎥ ⎢ 13 ⎥
⎢⎣0 2 2 0 - 6⎥⎦ ⎢⎣ x 5 ⎥⎦ ⎢⎣− 20⎥⎦
(Ans: x1 = 1, x2 = 2, x3 = 3, x4 = 4 and x5 =5)
PRINCIPLES OF MATLAB (FALL 07)
WORKOUT # 4
2. Create a vector X containing 30 elements from 0 to 2π. Find the corresponding values of the
vector Y if Y = sin(X). Plot Y vs. X.
3. Using the vector X of problem 2, create a vector Z, such that Z = cos(X ). Plot both, sin(X) and
cos(X) versus X on the same plot. Use a circle symbol (o) for the sine function and a cross symbol
(x) for the cosine. All lines should appear as solid lines.
4. The two variables x and y are related through the expression: y = 7.52x0.75. Generate a vector for x
from 10 to 100 in steps of 5. Find the corresponding y's. Plot y vs. x on a log-log scale. Customize
your plot by labeling the axes and giving a title to the plot. Use gridlines.
5. The concentration of two components, A and B, varies with time according to the following table:
Create a plot showing the variation of CA and CB with time. Make sure you label all axes properly.
6. The diffusivity, D (cm2/s), of a gas varies with the temperature T(K) according to Arrhenius Law:
D = Do exp (-E/RT); where, Do is the preexponential factor, E is the activation energy for
diffusion and R =1.987 cal/mol.K. Diffusivities of SO2 in fluorosilicone tube are measured at
several temperatures, with the following results:
Use a semilogarithmic plot to show the variation of the diffusivity with temperature. (Hint: plot D vs.
1/T on a semi-log scale)
7. Use the subplot command to plot four relations (2x2) on the same graphics window. The top left
plot is that of problem 3, top right is the plot of problem 4, bottom left is the plot of problem 5 and
in the bottom right should appear the plot of problem 6.
8. Use the mesh and/or meshz command to show the effect of the speed and the salt concentration on
the rate of corrosion of zinc, according to the following table. (Label all axes properly)
3 cos( w )
iii) z = (Ans: 3*cos(w)/sin(2*w/(w+1))
⎛ 2w ⎞
sin ⎜ ⎟
⎝ w +1 ⎠
3 sin( t ) − cos( t2 )⎤ A=
iv) A = ⎡⎢ [3*sin(t),-cos(t^2)]
⎣cos(2 t ) − sin( t ) ⎥⎦ [cos(2*t), -sin(t)]
3s 2 + 2s + 1
v) p = (Ans: (3*s^2+2*s+1)/(4*s-2)) vi) r = e-2t (Ans: exp(-2*t))
4s − 2
3. Find the result of adding, subtracting and multiplying the two functions f and f ' of
problem1
(Ans: a*x^2+b*x+c+1/2/(3*x^2+2*x+5)^(1/2)*(6*x+2)
a*x^2+b*x+c-1/2/(3*x^2+2*x+5)^(1/2)*(6*x+2)
1/2*(a*x^2+b*x+c)/(3*x^2+2*x+5)^(1/2)*(6*x+2) )
n
4. Find: i) ∑ (2n − 1)2
1
(Ans: 11/3*n+8/3-4*(n+1)^2+4/3*(n+1)^3)
∞
1
ii) ∑ (2n − 1)2
1
(Ans: 1/8*pi^2)
6. Find the functional inverse of the two functions f(x) given in problem 5
(Ans: i) Warning: finverse(sqrt(1-x^2)) is not unique -(1-x^2)^(1/2)
ii) 1/x^3)
7. Use the determ and the eval functions to find the determinant of the matrix A,
given in problem 1, if t = π/6. (Ans: -0.2687 )
PRINCIPLES OF MATLAB (FALL 07)
WORKOUT # 6
2π ∞ 1 ∞
dx sin x ⎛ ex + 1 ⎞
v) ∫0 1 − 2a cos x + a2 ∫0 x
vi) dx
0
vii) ∫ ln x ln(1 + x )dx
viii) ∫ ln⎜ x
0 ⎝ e −1⎠
⎟dx
6. Plot all four functions given in problem 1, in one graphics windows. For the
function f Assume c = 3 and n = 2. Make sure you label all axes, and fully
customize the plots.
7. Plot the two derivatives obtained from problem 2, in one graphics window
PRINCIPLES OF MATLAB (FALL 07)
WORKOUT # 7
3. If D = Do exp(-E/RT); find the value of E for the following case: (Ans: 2.0000e+003 J/mol)
T = 393 K; D = 7.065 x 10-7 m2/h; Do = 1.303 x 10-6 m2/h; R = 8.314 J/mol.K
5. Find the temperatures (in °C) at which the vapor pressure of a substance are 1319.9 and
1821.4 mmHg. The vapor pressure is given by the following formula:
P*(T) = exp[A - B/(T+C)]
Where: T is absolute temperature; A = 15.6782; B = 2154.9; C = -34.42
(Ans: 15.1499°C and 25.1506°C)
1. Consider
⎡− 5 1 0⎤ ⎡0⎤
⎢ ⎥ ⎢ ⎥
A = ⎢0 - 2 1⎥ , b = ⎢0⎥ , c = [ - 1 1 0]
⎢⎣0 0 1⎥⎦ ⎢⎣1 ⎥⎦
i) Suppose Ax = b, find x.(Ans: x' = [0.1000 0.5000 1.0000]
ii) Suppose yA = c, find y. (Ans: 0.2000 -0.4000 0.4000]
2. Solve the following set of linear equations. (Use a script file to find the solution)
Ax = B
x= 2.5683
⎡7 1 1 1 ⎤ ⎡ x1⎤ ⎡19 ⎤
⎢2 11 1- 1⎥ ⎢ x2⎥ = ⎢- 5 ⎥ -1.3724
⎢1 2 72 ⎥ ⎢ x3⎥ ⎢23 ⎥ 3.6775
⎣⎢1 - 1 -18 ⎦⎥ ⎣⎢ x4⎦⎥ ⎣⎢- 10⎦⎥ -1.2829
4.. A mass balance on a system of coupled reactors (5 reactors) gives the following results
6c1 - c3 = 50 Ans:
-3c1 + 3c2 = 0 c=
- c2 - 9c3 = 160 5.2727
- c2 - 8c3 + 11c4 - 2c5 = 0 5.2727
-3c1 - c2 + 4c5 = 0 -18.3636
-11.9174
Find the value of ci’s . 5.2727
6. The specific gravity of aqueous nitric acid solutions is given in the following table at 20°C
% 10 15 20 25 30
Sp. Gr. 1.0543 1.0842 1.1150 1.1469 1.1800
Find the specific gravity of an 18% solution and a 32% solution. (Ans: 1.1027, ????)
7. The specific enthalpy (kJ/kg) of superheated steam is shown in the following table as a
function of pressure and temperature
P (bar) 250°C 300°C 350°C
1.0 2975 3074 3176
5.0 2961 3065 3168
10.0 2943 3052 3159
Write one single m-file named enthalpy.m in which you perform the following:
a) Find the specific enthalpy of superheated steam at 250°C and 3.0 bars (Ans: 2968 kJ/kg)
b) Find the specific enthalpy of superheated steam at 320°C and 5.0 bars (Ans: 3106.2 kJ/kg)
c) Find the specific enthalpy of superheated steam at 275°C and 7.0 bars (Ans: 3006.8 kJ/kg)
(Plot the variation of the specific enthalpy with temperature at different pressures)
PRINCIPLES OF MATLAB (FALL 07)
WORKOUT # 9
Solve each of the following problems in a separate script file. Plot the original data
using circles and the fitted relations as continuous lines.
t (min) 10 20 30 40 50 60
CA (g/l) 8.1 4.3 3.0 2.2 1.8 1.5
3. Fit a power equation to the following data points, and determine the constants of the relation.
4. Values of the viscosity μ of water (centipoises) have been measured at various temperatures T (°C).
Find the constants which fit an equation of the form:
1
= k1 + k2T + k3T2
μ
T (°C) 10 20 30 40 50 60 70
μ (cp) 1.308 1.005 0.801 0.656 0.549 0.469 0.406
(Ans: k3 = 0.0001; k2 = 0.0205 ; k1 = 0.5481)
Construct an array, called RESULT, made of three columns: the first one contains r , the second
one contains calculated values for r and the third one contains the difference between r and the
calculated value of r. Print the array. (Ans: KR = 5.0264 ; KA = 19.9392)
RESULT =
1.0e-004 *
0.3400 0.3421 -0.0021
0.3600 0.3569 0.0031
0.3700 0.3722 -0.0022
0.3900 0.3873 0.0027
PRINCIPLES OF MATLAB (FALL 07)
WORKOUT # 10
1. Estimate the following integrals using the quad function and symbolically:
0.4 3π / 20
a)
∫0
x exp(− x )dx b)
∫
0
sin(5x + 1)dx
2. Suppose the relationship between Cp and T for a given substance is expressed as:
Cp = 0.235 exp(0.0473T1/2)
and you wish to evaluate:
150
(cal / g) =
ΔH
∫
1800
CpdT
3. The specific heat of carbon monoxide can be obtained by the following formula:
Cp (J/mol.K) = 28.95 + 0.4110x10-2T + 0.3548x10-5T2 - 2.22x10-9T3 (T in °C)
The mean heat capacity of carbon monoxide as it goes from 25°C to 1500°C can be
defined as :
1500
∫ Cp dT
Cp (25 → 1500) = 25 (Ans: 32.8851 J/mol.K)
1500 - 25
1.0e+003 *
a) Calculate the mean heat capacity C p for carbon monoxide. 0.1000
0.2000
0.0292
0.0295
b) Generate an array of two columns and 10 rows, in which 0.3000
0.4000
0.0297
0.0300
the first column is the temperature, from 100°C to 1000°C, 0.5000
0.6000
0.0303
0.0306
and the second column is the corresponding mean heat 0.7000
0.8000
0.0308
0.0311
capacities. 0.9000
1.0000
0.0314
0.0317
4. The behavior of a batch reactor for a second order irreversible reaction can be
described by the relation:
x
∫
1 1 + εx
kt = dx
Co (1 − x) 2
0
where: x is the fractional conversion
k is the reaction rate constant
Co is the initial concentration of reacting component
ε is the fractional change in volume of the reactor between zero
conversion and complete conversion
t is the time of the reaction
For k=6.2 x 10-4, Co = 10 and ε = 0.2, find the time necessary to reach a fractional
conversion, x, of 0.4. (Ans: 112.5558 )