Sunteți pe pagina 1din 13

MATLAB INTRODUCTION

HANDS ON MATLAB PAGE 1


Hands on MATLAB Introduction
1. AIM AND OBJECTIVES
Aim
Learn how to use MATLAB to solve various engineering problems.

Objectives
On completion of this session, the students should be able to:
Use MATLAB to do basic data analysis.
Use MATLAB to solve algebraic equations.
Use MATLAB to solve differential equations.
2. THE PLOT FUNCTION
The plot function produces a 2-D plot of a y-vector versus a specified x-vector. The following
task let you to learn how to use the plot function.

TASK 1.
The following reaction data has been obtained from a simple decay reaction:
B A (1)
Time (Minutes) Concentration (mol/L)
0 100
1 80
3 65
6 55
9 49
12 45
15 42
18 41
21 38
Using MATLAB to plot the concentration of component A in mol/L against the reaction time, t
in minutes. Title the plot, label the axes and obtain elementary statistics for the data.

MATLAB INTRODUCTION

HANDS ON MATLAB PAGE 2
Solution.
1. Input data into MATLAB as two vectors, t the time and y the concentration by entering
the following commands:
t = [ 0 1 3: 3: 21] ;
y = [ 100 80 65 55 49 45 42 41 38] ;

2. Produce the plot by entering the command:
pl ot ( t , y) ;

3. The concentration of B can be obtained by formula C(B) =100 C(A).
x= 100 y;

4. Use the following command to add concentration of B.
hol d on;

pl ot ( t , x, - - ) ;

5. Title the plot by using the following command:
t i t l e( Concent r at i ons i n r eact i on ) ;
6. Label the axes as follows
xl abel ( t i me, mi nut es )
yl abel ( concent r at i on, mol es/ L )

7. Add legend by using the following command
l egend( A , B ) ;

8. To add statistics of data, click Tool manual and select Data Statistics. From the popup
window, select the mean and std. The final plot window should look like Figure 1.

MATLAB INTRODUCTION

HANDS ON MATLAB PAGE 3

Figure 1. Concentrations of reaction
Note, you can use commands max, min, mean, std, etc to calculate these statistics of data.
3. POLYNOMIALS
The ideal gas law can represent the pressure-volume-temperature (PVT) relationship pf
gases only at low (near atmospheric) pressures. For higher pressure more complex
equations of state should be used. The calculation of the molar volume and the
compressibility factor using complex equations of state typically requires a numerical
solution when the pressure and temperature are specified.

The van der Waals equation of state is given by
( ) RT b V
V
a
P =

+
2
(2)

MATLAB INTRODUCTION

HANDS ON MATLAB PAGE 4
where

=
c
c
P
T R
a
2 2
64
27
(3)
and
c
c
P
RT
b
8
= (4)
The variables are defined by
P Pressure in atm
V Molar volume in liters/g-mol
T Temperature in K
R Gas constant (=0.08206 atm.liter/g-mol.K)
c
T
Critical temperature (405.5 K for ammonia)
c
P
Criteical pressure (111.3 atm for ammonia)

TASK 2
Calculate the molar volume and compressibility factor for gaseous ammonia at pressure
56 = P atm and a temperature 450 = T K using the van der Waals equation of state.

Solution.
The van der Waals equation can be rearranged as a polynomial equation:
0 ) (
2 3
= + + ab aV V RT Pb PV (5)
For given P and T , the molar volume V can be obtained by solving the above equation. In
MATLAB, there are two ways to solve the above equation: r oot s and f zer o.

1. Use MATLAB editor to create the following function:
%f i l ename waal svol . m
f unct i on x=waal svol ( vol , pr ess, T, a, b)
R = 0. 08206;
x=pr ess*vol ^3- ( pr ess*b+R*T) *vol ^2+a*( vol - b) ;

2. Use the MATLAB editor to create the following script file:
Pc=111. 3;
Tc=405. 5;
R=0. 08206;
a=27/ 64*R^2^Tc^2/ Pc;

MATLAB INTRODUCTION

HANDS ON MATLAB PAGE 5
b=R*Tc/ ( 8*Pc) ;
T=450;
P=56;
f =@( vol ) waal svol ( vol , P, T, a, b) ;
vol _guess=R*T/ P;
vol =f zer o( f , vol _guess) ;
z=P*vol / ( R*T) ;
f pr i nt f ( mol ar vol ume=%g, compr essi bi l i t y f act or = %g\ n , vol , z) ;

3. Run the m-file, the following results will be displayed:
mol ar vol ume=0. 696782, compr essi bi l i t y f act or = 1. 05667

4. To use function r oot s, the polynomial equation is defined by its coefficients.
pol =[ P ( P*b+R*T) a a*b] ;

5. Then, the molar volume can be obtained using roots as follows:
x = r oot s( pol ) ;
vol 1 = x( 3) ;

6. Check vol1 and vol should almost the same.
4. CURVE FITTING
The following table presents data of vapour pressure versus temperature for benzene. Some
design calculations require these data to be accurately correlated by various algebraic
expressions which provide P in mm Hg as a function of T in C.
Temperature, T (C) Pressure, P (mm Hg)
-36.7 1
-19.6 5
-11.5 10
-2.6 20
7.6 40
15.4 60
26.1 100
42.2 200
60.6 400
80.1 760


MATLAB INTRODUCTION

HANDS ON MATLAB PAGE 6
A simple polynomial is often used as an empirical modelling equation. This can be written in
general form for this problem as
n
n
T a T a T a T a a P + + + + + = ...
3
3
2
2 1 0
(6)
where
n
a a ,...,
0
are the parameters (coefficients) to be determined by regression and n is the
degree of the polynomial. Typically, the degree of the polynomial is selected which gives the
best data representation when using a least-squares objective function.

TASK 3
Regress the data with polynomials having the form of Equation (6). Determine the degree of
polynomial which best represents the data.

Solution
Problem a) is solved by using the command polyfit.
1. Inter the data:
vp = [ 1 5 10 20 40 60 100 200 400 760] ;
T = [ - 36. 7 - 19. 6 - 11. 5 - 2. 6 7. 6 15. 4 26. 1 42. 2 60. 6 80. 1] ;

2. Set the degree of polynomial to 4
m= 4

3. Fit the polynomial
p=pol yf i t ( T, vp, m)

p =

0. 0000 0. 0004 0. 0360 1. 6062 24. 6788

4. Evaluate the polynomial for every T
z=pol yval ( p, T) ;

5. Plot results
pl ot ( T, z, or , T, vp, b )
t i t l e( Vapor Pr essur e wi t h m= 4 )
xl abel ( T ( \ ci r c C) )
yl abel ( vp ( mmHg) )


MATLAB INTRODUCTION

HANDS ON MATLAB PAGE 7

Figure 2. Polynomial fitting on vapor pressure data

5. LINEAR EQUATIONS


15% Xylene
25% Styrene
40% Toluene
20% Benzene
F=70 mol/min
D
B
D
1
B
1
D
2
B
2
{
{
{
{
7% Xylene
4% Styrene
54% Toluene
35% Benzene
18% Xylene
24% Styrene
42% Toluene
16% Benzene
15% Xylene
10% Styrene
54% Toluene
21% Benzene
24% Xylene
65% Styrene
10% Toluene
1% Benzene
#1
#2
#3

Figure 3. A separation train

MATLAB INTRODUCTION

HANDS ON MATLAB PAGE 8
Xylene, styrene, toluene and benzene are to be separated with the array of distillation
columns that is shown below where F, D, B, D1, B1, D2 and B2 are the molar flow rates in
mol/min.

Material balances on individual componenets on the overall separation train yield the
equation set:
Xylene: 0.07D1 +0.18B1 +0.15D2 +0.24B2 =0.15 x 70
Styrene: 0.04D1 +0.24B1 +0.10D2 +0.65B2 =0.25 x 70
Toluene: 0.54D1 +0.42B1 +0.54D2 +0.10B2 =0.40 x 70
Benzene: 0.35D1 +0.16B1 +0.21D2 +0.01B2 =0.20 x 70

Overall balances and individual componenet balances on column #2 can be used to
determine the molar flow rate and mole fractions from the equation of stream D from
Molar Flow Rates: D =D1 +B1
Xylene: X
Dx
D =0.07D1 +0.18B1
Styrene: X
Ds
D =0.04D1 +0.24B1
Toluene: X
Dt
D =0.54D1 +0.42B1
Benzene: X
Db
D =0.35D1 +0.16B1
where X
Dx
=mole fraction of Xylene, X
Ds
=mole fraction of Styrene, X
Dt
=mole fraction of
Toluene and X
Db
=mole fraction of Benzene.

Similarly, overall balances and individual component balances on column #3 can be used to
determine the molar flow rate and mole fractions of tream B from the equation set
Molar Flow Rates: B =D2 +B2
Xylene: X
Dx
B =0.15D2 +0.24B2
Styrene: X
Ds
B =0.10D2 +0.65B2
Toluene: X
Dt
B =0.54D2 +0.10B2
Benzene: X
Db
B =0.21D2 +0.01B2

TASK 4
a) Calculate the molar flow rates of streams D1, D2, B1 and B2
b) Determine the molar flow rates and compositions of streams B and D.


MATLAB INTRODUCTION

HANDS ON MATLAB PAGE 9
Solution.
1. The first equation set can be represented in MATLAB as Ax=b, then x is solved as x =
A\b.
A=[ 0. 07 0. 18 0. 15 0. 24
0. 04 0. 24 0. 10 0. 65
0. 54 0. 42 0. 54 0. 10
0. 35 0. 16 0. 21 0. 01] ;
b = [ 0. 15*70 0. 25*70 0. 40*70 0. 2*70] ;
di sp( Sol ut i on f or D1 B1 D2 B2 i s: )
x = A\ b

The solution is D1 =26.25, B1=17.50, D2=8.75, B2=17.50

2. The mole fraction for column 2 are solved directly from the 2
nd
set equations.
D1 = x( 1) ;
B1 = x( 2) ;
di sp ( Sol ve f or Col umn 2 )
D=D1+B1 %43. 75 mol / mi n
X_Dx=( 0. 07*D1+0. 18*B1) / D %0. 114 mol e f r act i on
X_Ds=( 0. 04*D1+0. 24*B1) / D %0. 120 mol e f r act i on
X_Dt =( 0. 54*D1+0. 42*B1) / D %0. 492 mol e f r act i on
X_Db=( 0. 35*D1+0. 16*B1) / D %0. 274 mol e f r act i on

3. The mole fractions for column 3 are solved from the 3
rd
equation set.
D2 = x( 3) ;
B2 = x( 4) ;
di sp( Sol ve f or Col umn 3 )
B=D2+B2 %26. 25 mol / mi n
X_Bx=( 0. 15*D2+0. 24*B2) / B %0. 2100 mol e f r act i on
X_Bs=( 0. 10*D2+0. 65*B2) / B %0. 4667 mol e f r act i on
X_Bt =( 0. 54*D2+0. 10*B2) / B %0. 2467 mol e f r act i on
X_Bb=( 0. 21*D2+0. 01*B2) / B %0. 0767 mol e f r act i on

6. NONLINEAR EQUATIONS
The following reactions are taking place in a constant volume, gas-phase batch reactor.
Z X A
Y X C B
D C B A
+
+ +
+ +

A system of algebraic equations describes the equilibrium of the above reactions. The
nonlinear equilibrium relationships utilize the thermodynamic equilibrium expressions, and
the linear relationships have been obtained from the stoichiometry of the reactions.

MATLAB INTRODUCTION

HANDS ON MATLAB PAGE 10
Z X Y
Y D C
Y D B B
Z D A A
X A
Z
C
C B
Y X
C
B A
D C
C
C C C
C C C
C C C C
C C C C
C C
C
K
C C
C C
K
C C
C C
K
+ =
=
=
=
=
=
=
0
0
3
2
1

In this equation set,
X D C B A
C C C C C , , , , and
Y
C are concentrations of various species at
equilibrium resulting from initial concentrations of only
0 A
C and
0 B
C . The equilibrium
constants
2 1
,
C C
K K and
3 C
K have known values.

TASK 5
Solve this system of equations when 5 . 1
0 0
= =
B A
C C , 06 . 1
1
=
C
K , 63 . 2
2
=
C
K and
5
3
=
C
K starting from three sets of initial estimations.
(a) 0 = = =
Z X D
C C C
(b) 1 = = =
Z X D
C C C
(c) 10 = = =
Z X D
C C C

Solution.
1. Create the following function
%f i l ename pr ob4. m
f unct i on f = pr ob4( cvect or , Cao, Cbo, Kci , Kci i , Kci i i )
%cvect or ar e t he concent r at i ons of t he seven speci es.
%cvect or ( 1) i s t he concent r at i on of speci es a,
%cvect or ( 2) i s t he concent r at i on of b et c.
f ( 1) = cvect or ( 3) *cvect or ( 4) - Kci *cvect or ( 1) *cvect or ( 2) ;
f ( 2) = cvect or ( 6) *cvect or ( 5) - Kci i *cvect or ( 2) *cvect or ( 3) ;
f ( 3) = cvect or ( 7) - Kci i i *cvect or ( 1) *cvect or ( 5) ;
f ( 4) = Cao - cvect or ( 1) - cvect or ( 4) - cvect or ( 7) ;
f ( 5) = Cbo - cvect or ( 2) - cvect or ( 4) - cvect or ( 6) ;
f ( 6) = cvect or ( 4) - cvect or ( 6) - cvect or ( 3) ;
f ( 7) = cvect or ( 6) - cvect or ( 5) - cvect or ( 7) ;

2. Define the function to be called by fsolve

MATLAB INTRODUCTION

HANDS ON MATLAB PAGE 11
Cao = 1. 5;
Cbo = 1. 5;
Kci = 1. 06;
Kci i = 2. 63;
Kci i i = 5;
f = @( x) pr ob4( x, Cao, Cbo, Kci , Kci i , Kci i i ) ;

3. Call f sol ve to find the solution.
cvect or = [ 1. 5 1. 5 0 0 0 0 0] ;
guess = cvect or
y = f sol ve( f , guess)

It gives the following solution:
y = 0. 4207 0. 2429 0. 1536 0. 7053 0. 1778 0. 5518 0. 3740

4. Change
(b) cvect or = [ - 0. 5 1. 5 - 1 1 1 2 1] ;
(c) cvect or = [ - 18. 5 28. 5 - 10 10 10 20 10] ;
Then repeat the above to find the solution. It should gives the same result.

7. DIFFERENTIAL EQUATIONS
T
0
=20
o
C
W
1
=100 kg/min
Steam
T
1
Steam
T
2
Steam
T
3
T
1
T
2
T
3

Figure 4. Three heating tanks
Three tanks in series are used to preheat a multi-component oil solution before it is fed to a
distillation column for separation as shown in Figure 3. Each tank is initially filled with 1000
kg of oil at 20 C. Saturated steam at a temperature of 250 C condenses within coils
immersed in each tank. The oil is fed into the first tank at the rate of 100 kg/min and
overflows into the second and the third tanks at the same flow rate. The temperature of the
oil fed into the first tank is 20 C. The tanks are well mixed so that the temperature inside the
tanks is uniform, and the outlet stream temperature is the temperature within the tank. The
heat capacity, Cp of the oil is 2.0 KJ /kg. For a particular tank, the rate at which heat is
transferred to the oil from the steam coil is given by the expression

MATLAB INTRODUCTION

HANDS ON MATLAB PAGE 12
) ( T T UA Q
steam
=
where UA =10 kJ /min-C is the product of the heat transfer coefficient and the area of the
coil for each, T=temperature of the oil in the tank in C, and Q =rate of heat transferred in
kJ /min.

Energy balances can be made on each of the individual tanks. In these balances, the mass
flow rate to each tank will remain at the same fixed value. Thus, W =W1 =W2 =W3. The
mass in each tank will be assumed constant as the tank volume and oil density are assumed
to be constant. Thus, M=M1=M2=M3. The energy balances of these three tanks can be
expressed as follows.
( ) ( ) [ ] ( )
( ) ( ) [ ] ( )
( ) ( ) [ ] ( )
p steam p
p steam p
p steam p
MC T T UA T T WC
dt
dT
MC T T UA T T WC
dt
dT
MC T T UA T T WC
dt
dT
/
/
/
3 3 0
3
21 21 1
2
1 1 0
1
+ =
+ =
+ =

TASK 6
Determine the steady state temperatures in all three tanks. What time interval will be
required for T3 to reach 99% of the steady state value during startup?

Solution
1. Create a function to present the tank series.
f unct i on dT_dt = t anks( t , T, W, UA, M, Cp, Tst eam, num_t anks, To)
dT_dt =zer os( num_t anks, 1) ;
f or j = 1: num_t anks
i f j ==1
dT_dt ( j ) = ( W*Cp*( To- T( j ) ) + UA*( Tst eam- T( j ) ) ) / ( M*Cp) ;
el se
dT_dt ( j ) = ( W*Cp*( T( j - 1) - T( j ) ) + UA*( Tst eam- T( j ) ) ) / ( M*Cp) ;
end
end

2. Define the function to be called by an ode solver.
num_t anks = 3
W= 100; %kg/ mi n
UA = 10; %kJ / mi n. C
M = 1000; %kg
Cp = 2. 0; %kJ / kg
Tst eam= 250; %C
To = 20; %C

MATLAB INTRODUCTION

HANDS ON MATLAB PAGE 13
f t anks = @( t , T) t anks( t , T, W, UA, M, Cp, Tst eam, num_t anks, To) ;

3. Call ode45 to solve the differential equations
T_i ni t i al = ones( 1, num_t anks) *To;
t _st ar t = 0; %mi n
t _f i nal = 90; %mi n
t span = [ t _st ar t t _f i nal ] ;
[ t , T] = ode45( f t anks, t span, T_i ni t i al ) ;

4. plot results
pl ot ( t , T)
t i t l e( Temper at ur e i n St i r r ed Tanks )
xl abel ( t i me ( mi n) )
yl abel ( T ( C) )

This will produce the following plot.

Figure 5. Temperature in heating tanks

S-ar putea să vă placă și