Proc Sim Komp 2005
-
Upload
kent-renember -
Category
Documents
-
view
216 -
download
0
Transcript of Proc Sim Komp 2005
-
8/20/2019 Proc Sim Komp 2005
1/94
1
Table of Contents
TABLE OF CONTENTS...........................................................................................................................1
PREFACE................................................................................................................................................... 3
1 DEVELOPMENT OF LUMPED MODELS..................................................................................4
1.1 I NTRODUCTION ..........................................................................................................................4 1.2 MASS BALANCES .......................................................................................................................4 1.3 SYSTEMS OF MASS BALANCES...................................................................................................8 1.4 E NERGY BALANCES .................................................................................................................10 1.5 MOMENTUM BALANCES...........................................................................................................11 1.6 SUMMARY................................................................................................................................ 12
2 STEADY-STATE SIMULATION OF LUMPED MODELS .....................................................13
2.1 I NTRODUCTION ........................................................................................................................13 2.2 METHOD FOR LINEAR EQUATIONS ........................................................................................... 13 2.3 METHODS FOR NONLINEAR EQUATIONS .................................................................................. 16 2.4 NEWTON METHODS .................................................................................................................18
2.5 SUMMARY................................................................................................................................ 21 3 DYNAMIC SIMULATION OF LUMPED MODELS ................................................................22
3.1 I NTRODUCTION ........................................................................................................................22 3.2 EXPLICIT O NE-STEP SOLVERS..................................................................................................22 3.3 IMPLICIT O NE-STEP METHODS.................................................................................................28 3.4 MULTI-STEP METHODS............................................................................................................30 3.5 DIFFERENTIAL-ALGEBRAIC EQUATION SOLVERS..................................................................... 31 3.6 ODE SIMULATION IN MATLAB..............................................................................................33 3.7 SUMMARY................................................................................................................................ 33
4 DEVELOPMENT OF DISTRIBUTED MODELS......................................................................35
4.1 I NTRODUCTION ........................................................................................................................35 4.2 MASS BALANCES ..................................................................................................................... 35
4.3 E NERGY BALANCES .................................................................................................................40 4.4 MOMENTUM BALANCES...........................................................................................................43 4.5 BOUNDARY CONDITIONS ......................................................................................................... 44 4.6 SUMMARY................................................................................................................................ 45
5 STEADY-STATE SIMULATION OF DISTRIBUTED SYSTEM............................................ 46
5.1 I NTRODUCTION ........................................................................................................................46 5.2 SHOOTING METHOD.................................................................................................................46 5.3 FINITE DIFFERENCE METHOD ..................................................................................................53 5.4 SUMMARY................................................................................................................................ 59
6 DYNAMIC SIMULATION OF DISTRIBUTED SYSTEMS..................................................... 60
6.1 I NTRODUCTION ........................................................................................................................60 6.2 METHOD OF LINES ................................................................................................................... 60
6.3 SPACE DISCRETIZATION...........................................................................................................61 6.4 SUMMARY................................................................................................................................ 69
7 COMPUTER AIDED PROCESS ENGINEERING.................................................................... 70
7.1 I NTRODUCTION ........................................................................................................................70 7.2 FLOWSHEETING........................................................................................................................71 7.3 COMPUTATIONAL FLUID DYNAMICS........................................................................................76 7.4 HYSYS – A FLOWSHEETING PROGRAM ....................................................................................79 7.5 SUMMARY................................................................................................................................ 80
-
8/20/2019 Proc Sim Komp 2005
2/94
2
8 PARAMETER ESTIMATION......................................................................................................81
8.1 I NTRODUCTION.........................................................................................................................81 8.2 LINEAR R EGRESSION ................................................................................................................81 8.3 NONLINEAR R EGRESSION .........................................................................................................85 8.4 R EGRESSION A NALYSIS............................................................................................................87 8.5 SUMMARY ................................................................................................................................92
9 INDEX..............................................................................................................................................93
-
8/20/2019 Proc Sim Komp 2005
3/94
3
PrefaceProcess Si mul at i on usi ng MATLAB is a set of lecture notes for the courseProcess Simulation at the Department of Chemical Engineering, Lund Institute of
Technology, Lund, Sweden. Current version contains eigth chapters on modelling,
simulation methods, simulation tools and on parameter estimation. There are
corresponding exercises for the chapters and they are found on the course home page at
the site.
ht t p: / / www. chemeng. l t h. se
The course is inspired by the textbook, Computational Methods for Process Simulation,
by Fred Ramirez. This book serves as a reference in the course.
Simulation techniques presented in the course are frequently solved using MATLAB.
The solutions to examples and exercises sometimes use predefined MATLAB functions,
while others are programmed by the user in MATLAB M-files. There is no need for a
deep knowledge about MATLAB before entering the course. There is no need for
additional toolboxes to solve the exercises in the course.
The course uses some Internet sources with MATLAB M-files.
• Ramirez library of M-files to his book is found on this pageht t p: / / opt i mal . col orado. edu/ ~r ami r ez/ chen4580. ht ml
• A general textbook written by Mathews, Numerical Methods, describes a large set ofsimple numerical routines and some of them are used in the course in order to
illustrate fundamental properties.ht t p: / / t i t an. f ul l er t on. edu/ ~mat hews/ sof t war e/ sof t war e. ht m
• In a selection of M-files developed by Kelley there is a set of nonlinear solvers thatare very interesting. One of them, nsol , is modified into newsol and usedfrequently through out the course.ht t p: / / www. si am. or g/ books/ kel l ey/ kel l code. ht m
These sources are also available on the computer network.
Lund, January 10th, 2005.
Bernt Nilsson
-
8/20/2019 Proc Sim Komp 2005
4/94
4
1 Development of Lumped Models
1.1 IntroductionThe basic properties in process systems are the conservation of mass, energy and
momentum. In process modelling this is formulised into the development of
mathematical expressions representing balance equations of mass, energy andmomentum. Conservation of a property follows the general balance expression below
In + Generation = Out + Consumption + Accumulation
Sometimes the generation and consumption terms are expressed as only one production
term as
In + Production = Out + Accumulation
This principle is illustrated in Figure 1.1 below. The definition of the system boundary
is important in order to develop the different terms in the balance equations. The system
boundary can be over a macroscopic object or over a microscopic fluid element.
mass
ener gy ener gy
mass
moment um
moment um
SYSTEM
In Out = + Accumulation
+Production
Figure 1.1: Illustration of the conservation principle.
The most convenient way of developing models is to ignore the spatial aspects of the
system that is going to be modelled. This means that all spatial properties are lumpedtogether and the model describes the integral averaged value of the properties. This is
the reason why this kind of models is called lumped models or models of lumped
systems. Models that describe spatial properties are called distributed models but they
are discussed in a later chapter.
1.2 Mass BalancesMass balances over a system can express the total mass in the system. A mass balance
can also be developed for a specific chemical component, a component mass balance or
just component balance. The only difference is that in chemical applications there is
never production of total mass while in reacting systems the component mass balance
can have production terms. In nonreacting systems there is no principle difference between total and component mass balances.
Example 1.1: Mixing Tank with two inlets
A tank has two inflows and one outflow and is seen in Figure 1.2. In to the tank
flows pure water from one inlet and pure ethanol from the other inlet. Out from
the mixing tank flows a mixture of the two components, the solution.
Model assumptions:
-
8/20/2019 Proc Sim Komp 2005
5/94
5
• Total mass in the system is m. No limits on the amount of mass in the tank. m can be infinite large and it can be negative.
• The mass flow rates are expressed as w. In this particular case the mass flowrates are independent of the mass inside the system.
The total mass balance over the control volume now becomes
sewsewwww
dt
dm
dt
dmwww −+=⇒+=++
+=+
0
AccOutProdIn
Controlvolume
Water Ethanol
100 kg/hr 40 kg/hr
Solution
120 kg/hr
Figure 1.2: A mixing tank with two inlets.
The mass balances can often be rewritten into other variables. Below follows two
examples of such transformations.
Example 1.2: Mixing Tank with liquid volumetric inflow
Assume that the mixing tank in Example 1.1, which is seen in Figure 1.2, has
volumetric inflows and outflow.
Additional model assumption:• The density in the different fluids is ρ . Assume that the density in the solution
is constant (practical assumption in this case).
The mass balance can now be rewritten into an equation expressing the change of
tank volume.
( )
se
s
ew
s
w
sseewws
qqqdt
dV
qqqdt
V d
−+=
−+=
ρ
ρ
ρ
ρ
ρ ρ ρ ρ
Example 1.3: Mixing Tank with vapour inflow
Assume that the mixing tank in Example 1.1 has vapour inflows and outflow.Additional model assumption:
• Assume that the vapour follows the ideal gas law.• Isothermal conditions and constant volume in the vapour mixing tank.• Constant molar weight in the mixture (a practical assumption).The mass balance can now be rewritten into an equation expressing the change of
tank pressure.
-
8/20/2019 Proc Sim Komp 2005
6/94
6
seww
sew
w
wwwdt
dp
RT
VM
wwwdt
RT
pVM d
−+=
−+=⎟
⎠
⎞⎜⎝
⎛
Component Mass Balances
It is possible to put up component balances, one for each chemical component. Note
that all component mass balances sum to the total mass balance. In order to set up a
consistent equation system it is recommended to take away one mass balance, often one
component mass balance, for instance the solvent balance. Sometimes this knowledge
can be used as redundant information to check the correctness of the model.
Example 1.4: Mixing Tank with component mass balances
Component mass balances over the mixing tank in Example 1.1, which is seen in
Figure 1.2 becomes as follows
Additional model assumptions:
• The inflows have pure chemical components. The solution has the massfraction of ethanol expressed as ω . This means that the mass fraction of water
is ω w=(1-ω ) because the sum of the fractions must be equal to 1.
( )
( )
dt
dmwwwtotal
dt
md wwwater
dt
md wwethanol
swe
sw
se
+=+
−+−=+
+=+
+=+
:
)1()1(0:
0:
AccOutProdIn
ω ω
ω ω
We see that the two component mass balances sum to form the total mass balance.
The ethanol component mass balance become
( )sse
wwdt
md ω
ω −=
The conservation of mass is valid for each chemical component. It is often more
convenient to express it in composition instead of mass. This means that the component
mass balance must be rewritten using the rule for the derivative of a product.
( )dt
dm
dt
d m
dt
md ω
ω ω +=
This manipulation is just pure mathematics. Remember that the left hand side is the
component balance and the second part of the second term on the right hand side is the
total mass balance. Let us rewrite it as
-
8/20/2019 Proc Sim Komp 2005
7/94
7
( )dt
dm
mdt
md
mdt
d ω ω ω −=
1
This means that both the total mass balance and the component balance are used to
derive an expression of the composition change.
Example 1.5: Mixing Tank with component mass balancesThe change in composition in mixing tank in Example 1.1, which is seen in
Figure 1.2, can easily be found using the above expansion.
Additional model assumptions:
• The mass must be nonzero.
The total mass balance from Example 1.1 and the component balance from
Example 1.4 are used together to derive the change of composition.
sewwww
dt
dm−+=
( ) sse wwdt md ω ω −=
These two balances are used in
( )dt
dm
dt
d m
dt
md ω
ω +=
to form a composition equation
( ) ( )
( ) ω ω
ω ω
ω
m
w
m
w
www
m
ww
mdt
d
we
sewse
−−=
−+−−=
1
1
Note that this expression is only valid when the total mass is nonzero (positive).
Component mass balances are often rewritten into component mole balances, using m =
M n. It can further be rewritten using concentrations, using m =M n = M V c. This
means that a component mass balance can be transformed into a component mole
balance just by dividing all terms with the molar weight of the chemical component.
Example 1.6 : Mixing Tank expressed in concentration changesA tank with constant volume has an inflow with a volumetric flow rate, q, and an
inflow concentration, cin. Develop a mathematical expression for the
concentration in the tank.
Model assumptions:
• Constant volume in the tank.• Ideal mixing and that the concentration in the tank is equal every where and
that the outflow concentration is the same as the tank concentration.
-
8/20/2019 Proc Sim Komp 2005
8/94
8
( )ccV
q
dt
dc
dt
dc MV qcqc
dt
MVcd c Mqc Mq
dt
dmww
in
out in
out out inin
out in
−=
⇒+=
+=
+=
+=
)(
AccOutIn
qVc
in c
out
Figure 1.3: A Mixing Tank
1.3 Systems of Mass BalancesModels of process systems with sets of subsystems generate equation systems of mass
balances. This means that the equation set is coupled and the equations must be solved
simultaneously.
Example 1.7 : Two Tanks in Series
Two well-mixed tanks are connected in series. They have constant but different
volumes. They are seen in the figure below.
Model assumptions:
1. Constant volumes and the same volumetric flow through the tank series.2. Well-mixed tanks, which means that the outlet concentration is the same as
the tank concentration.
Figure 1.4: Two-tank series .
Material balances over one chemical component, expressed in moles, in the two
tanks becomes
( )
( )
In Out Acc= +
= + ⇒ = −
= + ⇒ = −
qC qC V dC
dt
dC
dt
q
V C C
qC qC V dC
dt
dC
dt
q
V C C
in in1 1
1 1
1
1
1 2 2
2 2
2
1 2
and rewritten on state-space form using matrices it becomes
d
dt
C
C
q
V
q
V
q
V
C
C
q
V C in1
2
1
2 2
1
21
0
0
⎡
⎣⎢
⎤
⎦⎥ =
−
−
⎡
⎣
⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥
⎡
⎣⎢
⎤
⎦⎥ +
⎡
⎣
⎢⎢
⎤
⎦
⎥⎥
-
8/20/2019 Proc Sim Komp 2005
9/94
9
At steady state the accumulation terms are zero and in this case this is the same as
the concentration derivatives are zero. Instead we get the following algebraic
equation system (linear in this case). Note that the solution is trivial.
inC V
q
C
C
V
q
V
q
V
q
⎥⎥⎦
⎤
⎢⎢⎣
⎡
−=⎥⎦
⎤⎢⎣
⎡
⎥⎥⎥
⎥
⎦
⎤
⎢⎢⎢
⎢
⎣
⎡
−
−
0
0
12
1
22
1
Example 1.8: Six-Plate Absorption Column
An absorption column with six plates is used to absorb a chemical component in a
vapour into a liquid. The liquid, L, enters at the top and the vapour, V, enters at
the bottom. It is assumed that the liquid and vapour flow rates do not change
through the column.
1
2
3
4
5
6
L, x0
L, x6
V, y1
V, y7
Tray i
xi
yi
xi-1
yi+1
Figure 1.5: Six-plate adsorption column. left) column configuration, right) column tray
A steady-state component material balance over a tray givesIn Out=
+ +− + Lx Vy = Lx Vyi i i i1 1
Assume a linear equilibrium relation between liquid and vapour composition.
y = ax bi i
+
Let us put up the six component balances together with the equilibrium relation
In Out=
+ + + +
+ + + +
+ + + +
+ + + +
+ + + +
+ + +
1
2
3
4
5
6
0 2 1 1
1 3 2 2
2 4 3 3
3 5 4 4
4 6 5 5
5 7 6 6
: ( ) ( )
: ( ) ( )
: ( ) ( )
: ( ) ( )
: ( ) ( )
: ( )
Lx V ax b = Lx V ax b
Lx V ax b = Lx V ax b
Lx V ax b = Lx V ax b
Lx V ax b = Lx V ax b
Lx V ax b = Lx V ax b
Lx Vy = Lx V ax b
-
8/20/2019 Proc Sim Komp 2005
10/94
10
1.4 Energy BalancesIn process systems chemical components are transformed into other chemicals under
energy changes. Energy is released or consumed in the reaction step and it is often
consumed in the separation step. Energy balances are important parts of the process
model in analysis and design. Total energy can be decomposed into different energy
forms, similar to the decomposition of total mass into component masses. The most
common decomposition of total energy is into thermal energy and mechanical energy.There are other energy forms like electrical and magnetically energy.
A general total energy balance can be written as follows
( )( ) ( )( ) ( )( )mPK U dt
d QPK U W W QQPK U out out out out out pV Pininininin ++++++=++++++
+=+
ω ω
AccOutProdIn
U is the internal energy, K is the kinetic energy and P is the potential energy, all three
per mass unit. The three energy forms enter and leave the system in the process streams.
Energy can also be transferred into and from the system, Qin and Qout and it can be produced in the system through reaction and phase change, Q p. The work done by the
system on the surroundings is shaft work, W , and pressure/volume work, W pV . The most
common situation is when the internal energy dominates the total energy balance. Then
kinetic and potential energy are neglected and we get
( ) ( )
W QQQ p pU U
dt
mU d
dt
dmU QU W W QQU
out inPout
out
out
in
in
in
out out inin
out out out pV Pininin
−−++−+−=
⇒
++=−−++
+=+
ρ
ω
ρ
ω ω ω
ω ω
)(
AccOutProdIn
The enthalpy of a system can be expressed as the sum of the internal energy and the
pressure-volume
ρ
pU H +=
If we use this and assume that the change of the pressure-volume is neglectable the
internal energy balance can be expressed as an enthalpy balance
W QQQ H H dt
mH d
W QQQ p p
p
H
p
H dt
p H md
out inPout out inin
out inPout
out
out
in
in
in
out
out
in
in
−−++−=
−−++−+⎟⎟ ⎠
⎞
⎜⎜⎝
⎛
−−⎟⎟ ⎠
⎞
⎜⎜⎝
⎛
−=
⎟⎟ ⎠
⎞⎜⎜⎝
⎛ ⎟⎟ ⎠
⎞⎜⎜⎝
⎛ −
ω ω
ρ
ω
ρ
ω
ρ ω ρ ω
ρ
)(
Example 1.9: Mixing Tank with one inlet
A tank with constant volume has one inflow with a volumetric flow rate, q, and an
inflow temperature, Tin. Develop a mathematical expression for the enthalpy in
the tank. Model assumptions are constant volume in the tank and ideal mixing.
-
8/20/2019 Proc Sim Komp 2005
11/94
11
The enthalpy in the tank is equal every where and that the outflow enthalpy is the
same as the tank enthalpy. The density is constant.
( )hhV
q
dt
dh
dt
dhV qhqh
dt
hV d hqhq
in
in
out out out ininin
−=
⇒
+=
+=
+=
)(
AccOutIn
ρ ρ ρ
q VTin
Tout
Figure 1.6 : A Mixing Tank
Assume also that the enthalpy can be expressed as proportional to the temperature
difference from reference temperature as
T C H m p ∆= ,
The enthalpy balance can be rewritten into a temperature expression
( ))( ) ( )( )
( )T T V
q
dt
dT
T T C T T C V
q
dt
T T C d
in
m pininm p
m p
−=
−−−=−
0,0,
0,
This is only valid under the assumption that the heat capacity is (or almost)
constant.
Mechanical Energy BalancesThe neglected kinetic and potential energies in the total energy balance can sometimes
be expressed almost independent of the enthalpy balance. For incompressible flows this
is described in the famous Bernoulli equation, which is a mechanical energy balance.
Mechanical energy balances are not discussed here.
1.5 Momentum BalancesThe conservation of momentum is not so important in lumped model development of
process systems. The reason is that it is unusual to have moving lumped models.
Dynamic momentum balances also occur in mechanics. Steady-state momentum
balances are also called force balances.
( )dt
mvd vF v out out lossinin +=−
+=+
ω ω
AccOutProdIn
One major difference between mass and energy balances compared to momentum
balance is that momentum has direction. This means that we can put up one momentum
balance for each dimension in the modelling problem. Momentum balances becomes
-
8/20/2019 Proc Sim Komp 2005
12/94
12
much more important in distributed models and is the fundamental issue in
Computational Fluid Dynamics, which will be discussed more in a later chapter.
1.6 SummaryLumped models are based on the assumption that spatial effects can be neglected.
Lumped models are often composed of a set of mass, component and thermal energy balances. Under the assumption that we are studing steady-state behaviour the lumped
model results in a algebraic equation system, which is discussed in the next chapter.
Unsteady-state or transient behaviour studies results in a set of ordinary differential
equaitons in time. Simulation of unsteady-state models is discussed in chapter three.
-
8/20/2019 Proc Sim Komp 2005
13/94
13
2 Steady-State Simulation of Lumped Models
2.1 IntroductionA steady-state lumped model results in a set of algebraic equations. These algebraic equations
describe static relations between a set of variables. The equation system can be solved if
the number of equations is the same as the number of variables. For computationalreasons one often differs between linear and nonlinear equation systems. Nonlinear
steady-state lumped models can be written on a general form as
f (x , x , ..,x ) =
f (x , x , ..,x ) =
f (x , x , ..,x ) =
n
n
n n
1 1 2
2 1 2
1 2
0
0
0
...
or on a more compact representation on vector form
f x 0( ) =
If the algebraic equation system is linear it can have the following representation
a x a x a x u =
a x a x a x u =
a x a x a x u =
n n
n n
n n nn n n
11 1 12 2 1 1
21 1 22 2 2 2
1 1 2 2
0
0
0
+ + + −
+ + + −
+ + + −
...
...
...
...
or
A ux =
using matrix notation. First we discuss one numerical method for the solution of linear
algebraic equation system, namely Gaussian elimination, and then different nonlinear
equation solvers are discussed.
2.2 Method for Linear EquationsThe general method for simultaneously solving linear equation systems are based on
Gaussian elimination. This is done by performing a decomposition of the A-matrix into
one lower and one upper triangular matrix, a so called LU-decomposition.
LU-decomposition
In MATLAB LU-decomposition is done by the command l u( A) . The l u-commandreturns the two triangular matrices, L and U, and a partitioning matrix, P. Partitioning isdone if the diagonal element, the pivots, becomes zero or very small and it is done by
reordering the rows. To solve the linear equation system, A x = b, in MATLAB one can
write x = A\ b. Note the backslash notation on the left hand side which is interpreted
as left division of vector b by matrix A. This command implicitly makes a lu-
decomposition with pivoting.
-
8/20/2019 Proc Sim Komp 2005
14/94
14
Example 2.1: Six-Plate Absorption Column
An absorption column with six plates is used to absorb a chemical component in a
vapour into a liquid. The liquid, L, enters at the top and the vapour, V, enters at
the bottom. It is assumed that the liquid and vapour flow rates do not change
through the column.
1
2
3
4
5
6
L, x0
L, x6
V, y1
V, y7
Tray i
xi
yixi-1
yi+1
Figure 2.2: Six-plate adsorption column. left) column configuration, right) column tray
A steady-state component material balance over a tray gives
In Out=
+ +− + Lx Vy = Lx Vyi i i i1 1
Assume a linear equilibrium relation between liquid and vapour composition.
y = ax bi i +
Let us put up the six component balances together with the equilibrium relation
In Out=
+ + + +
+ + + +
+ + + +
+ + + +
+ + + +
+ + +
1
2
3
4
5
6
0 2 1 1
1 3 2 2
2 4 3 3
3 5 4 4
4 6 5 5
5 7 6 6
: ( ) ( )
: ( ) ( )
: ( ) ( )
: ( ) ( )
: ( ) ( )
: ( )
Lx V ax b = Lx V ax b
Lx V ax b = Lx V ax b
Lx V ax b = Lx V ax b
Lx V ax b = Lx V ax b
Lx V ax b = Lx V ax b
Lx Vy = Lx V ax b
This can now be rewritten with unknowns on the left and remember that x0 and y7
are known values.
-
8/20/2019 Proc Sim Komp 2005
15/94
15
( )
( )
( )
( )
( )
( )
− + + −
− + +
− + +
− + +
− + +
− + −
L Va x Vax = Lx
Lx L Va x Vax =
Lx L Va x Vax =
Lx L Va x Vax =
Lx L Va x Vax =
Lx L Va x = Vy
1 2 0
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7
0
0
0
0
This can now be put up on matrix form as A x = b.
Example 2.2: Six-Plate Absorption Column
A linear model of a six-plate absorption column is developed in Example 1.1
above. The mathematical problem becomes a linear equation system and it can be
solved by MATLAB in the following way:
a = 0. 72;b = 0;
V = 66. 7;L = 40. 8;x0 = 0;y7 = 0. 3;eyel ow = di ag( ones( 1, 5) , - 1) ;A = - ( L+V*a) *eye(6) + L*eyel ow+ V*a*eyel ow' ;u = [ - L*x0 0 0 0 0 - G*a*( y7- b) / a] ' ;x = A\ u
x =0. 09210. 17030. 23680. 29330. 3413
0. 3820
Note the creation of a matrix with ones on the lower off diagonal, eyel ow. It isalso used to create the elements in the upper off diagonal and this is done by
transposing the matrix, eyel ow' . Note also that the u-vector is defined as a row
but it is transposed at the end.
Analysis of Linear Equation
Analysis of the equation system gives insight regarding the error in the solution. There
are some tools available in MATLAB for this. The rank of a matrix is calculated by the
r ank-command. The rank measurement indicates if there are singularities in the problem formulation, like redundant or contradictory equations. This is not so
interesting in this stage of the problem solving. A more interesting measurement is a
numerical value of how close we are a singular equation system. The condition number
is a measurement of the quality of the solution. The reciprocal condition number is
found by the r cond-command. The rcond value is between 1 (good) and 0 (bad).Values of the r cond down to 0.0001 are common.
-
8/20/2019 Proc Sim Komp 2005
16/94
16
Example 2.3: Analysis of the Six-Plate Adsorption Column
The rank and the reciprocal condition number of the problem are calculated by the
following commands:
r = r ank( A)
r =6
c = rcond(A)
c =0. 0508
This condition number is satisfactory. Bad condition numbers are at least 3
magnitudes lower. Make it a good habit to always calculate the rcond-value for
linear problems.
2.3 Methods for Nonlinear EquationsIt is much easier to solve linear than nonlinear equations. In solving linear equationssystem there are one dominating method, Gaussian elimination. For nonlinear equations
there are a number of different methods that are good for different problems. A number
of different nonlinear solvers are discussed in Ramirez, section 2.3. In this text we
present three different methods. First we illustrate the use of a predefined equation
solver in MATLAB for one-dimensional problems called f zer o. For many nonlinear problems the Newton method are used. A multidimensional Newton solver, called
newsol , is presented .
One-dimensional Nonlinear Problems
There are a number of well known equation solvers for nonlinear one-dimensional
problems. Three similar methods are presented below. The first is the bisection method.The idea is very simple. Choose an interval where the function has different values.
Calculate the value of the function in the middle of the interval. The zero is now in the
interval where the function has different sign. The procedure can now be repeated until
the interval is small enough. The procedure is illustrated on the left in Figure 1.
a
b
c
(a)
(b)
(c)
a
b
c
(a)
(b)
Figure 2.2 - right) Bisection method, left) Regula Falsi method.
-
8/20/2019 Proc Sim Komp 2005
17/94
17
Another simple method is the Regula Falsi or linear inverse interpolation. The idea is to
make an interpolation between two function values with different sign. The zero of the
interpolation is now used as a new function value. The method is illustrated on the right
in Figure 1.
fzero
The fzero method in MATLAB can be use for nonlinear problems in one unknownvariable. It is a combination of bisection and secant methods (using inverse quadratic
interpolation). The secant method is a simplified Newton method.
Example 2.4: Heat Capacity for Carbon Dioxide
Calculate the temperature where the heat capacity for carbon dioxide is equal to a
given value, in this case C p = 1. The temperature dependency can be expressed
with the following equation (T in Kelvin, C p in kJ/kg K)
C T T
p = − −1716 4 257 10
15046
. . *.
Nonlinear problems in general are represented as functions in MATLAB. This
means the creation of a M-file. Our small example becomes a M-file with thesame name as the function name, Cpf un. This function requires one inputvariable, the unknown variable, in this case T. The function returns the residual,i.e., the deviation from zero.
f uncti on r es = Cpf un( T)r es = 0. 716 - 4. 257e- 6*T - 15. 04. / sqr t ( T) ;
The Cpf un can now be used in the nonlinear equation solver fzero. The firstinput variable to f zero is a string with name of the function (M-file). Thesecond is the starting guess of the unknown variable, in this case T.
T = fzero('Cpfun',600)
T =443. 5712
Note that fzero can only find the zero for a nonlinear function in one unknown.
In nonlinear problems with more than one unknown other types of solvers are needed.
One simple and often used method is direct substitution. It can be used in multi
dimensional problems but it is more used in problems with one unknown. The most
dominating method is a multidimensional version of the Newton method.
Direct substitutionA third simple method is called direct substitution and this is very common method.
The algebraic problem is rewritten as
x xg x f −== )()(0and put on the following form
-
8/20/2019 Proc Sim Komp 2005
18/94
18
The procedure starts with a guess of x0. The initial guess is used to calculate g(x0) and
the answer is a new value of x, i.e. x1, and the procedure can now be repeated until x
converges. Direct substitution has problems for equation systems with derivatives
greater than one, as seen in Figure 1.3. The problem on the right converges quite rapidly
while the problem on the left diverges.
x0x1x2
d i a g
o n a l
g(x0)
g(x1)g(x2)
x0 x1 x2
d i a g
o n a l
g(x0)
g(x1)
Figure 2.3 - right) Converging direct substitution , left) Diverging direct substitution.
2.4 Newton MethodsThe most common method for nonlinear systems is the Newton method or Newton-
Raphson method. The Newton method is based on Taylor expansion of the function
with the truncation of all terms with order higher than one, HOT.
HOT x x x f x f x f +−+= ))((')()( 0100
At the solution the function f is zero and let us through away higher order terms.
))((')(0 0100 x x x f x f −+=
The approximation can now be rewritten where a new x-value are calculated based on a
function evaluation, f , and its derivative, f' . The calculations are now repeated in order
to find a new even better value. A graphical interpretation is seen in Figure 2.4.
)('
)())((')(
0
0010100
x f
x f x x x x x f x f −=⇒−=−
x0x1x2
f'(x0)
Figure 2.4 – The Newton-Raphson method.
-
8/20/2019 Proc Sim Komp 2005
19/94
19
The Newton method converges to a solution if we start close enough. For some
problems it is hard to find good starting guesses in order to get a desired Newton
solution. Other problems can be slow converging rates and some times the derivative
goes to zero and the problem becomes singular.
Problems are very often multidimensional, i.e., contains more than one unknown.
Sometimes it is possible to rewrite multidimensional problems as a one-dimensional
problem by making symbolic manipulations. Even when it is possible it may beundesirable.
A multidimensional nonlinear problem is written as iteration of a linear equation
system. Define J as the Jacobian matrix, which is a matrix with the partial derivatives as
elements
f x J x' ( ) ( ) x x x
x x
f
x
f
x
f
x
f
x
0
0 0
0 0
0
1
1
1
2
2
1
2
2
= =
⎡
⎣
⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥
∂
∂
∂
∂
∂
∂
∂
∂
Note that the notation x1
and x2
is the definition of the two unknowns. Make amultidimensional Newton method and rewrite it as a linear equation system
)()()())(()( 000100100 xf xxJxxJxxxJxf −=⇒−=−
To find a new x-vector, x1, one have to solve A x1 = b where A = J and b = J x0 + f . In
other words the Newton method for nonlinear equation systems solves a linear equation
system in each iteration. Below are two different Newton methods briefly presented,
namely new2di m and newsol . The routine new2di m is a Newton solver fornonlinear problems in 2 unknowns and it is developed by Mathews. It requires one
function describing the nonlinear problem and one function describing the Jacobian.
The second method,newsol
, is described in the following subsection.
Example 2.5: Exothermic CSTR
An exothermic reaction occurs in a continuous stirred tank reactor, CSTR.
Assume constant volume, V , and flow rates, F . The CSTR is cooled by a coolant
and it is also assumed that the coolant temperature is constant, T C .
F, C A0
, T0 F, C A, T
TC
Figure 2.5 - Continuous stirred tank reactor.
A steady-state model with one component material balance and one energy
balance is as follows
-
8/20/2019 Proc Sim Komp 2005
20/94
20
( )
In Prod Out+ =
+ =
+ − = + −
FC VR FC
FC C H VR FC C kA T T
A A
p A p A C
0
0 ρ ρ ∆ ( )
Assume also a first-order reaction and an Arrhenius temperature dependency as
R k C e A
E
RT
a
= −−
0
The model can be rewritten in dimensionless form as follows
0 1
0
501
2
1
0 1
501
2
1
= e
= u ( ) ec
− − ⋅
− − − + ⋅
−
−
ξ ξ
η η η η ξ
η
η
( )
( )
( )
where ξ and η are the unknown dimensionless concentration and temperature.This is implemented in the following MATLAB functions, cstrss for the
problem and cstrssJ for the Jacobian. Note that the input variable is a vector, x
= [ ξ; η ] and the output variable is also a vector containing two values.
f uncti on [ res]=cst rss( x)% CSTRSS i s a di mensi onl ess st eady- st at e% l umped model of an exot her mi c cont i nuous% st i r r ed t ank r eact or . See Rami r ez ex. 2. 3
% par amet ers u1=2;nyc=1. 75;ny0=1. 75;% unknown var i abl esksi =x(1) ;ny=x( 2) ;% equat i ons P=ksi . *exp( 50*( 0. 5- 1. / ny)) ;Us=u1. *( ny- nyc) ;r es=[ 1- ksi - P; (ny0- ny)- Us+P];
f uncti on [ J ] =cst rss J (x)% CSTRSSJ cal cul at es t he J acobi an f or t he% di mensi onl ess st eady- st ate l umped model% f or an exot her mi c cont i nuous st i r r ed% t ank r eact or. See Rami r ez ex. 2. 3
% par amet ers
u1=2;nyc=1. 75;ny0=1. 75;% equat i ons ksi =x(1) ;ny=x( 2) ;% equat i ons Pksi =exp( 50*( 0. 5- 1. / ny)) ;Pny=( 50. / ny. 2̂) *ksi *exp( 50*( 0. 5- 1. / ny)) ;Usny=u1; J =[ - 1- Pksi , - Pny; Pksi , - ny- Usny+Pny] ;
-
8/20/2019 Proc Sim Komp 2005
21/94
21
This is now solved by the new2di mfunction
sol = new2dim('cstrss','cstrssJ',[1 1],1e-6,1e-6,40)
sol =0. 9676 1. 7608
newsol
Advanced Newton solvers have routines for numerical differentiation. The newsol MATLAB M-file is a method of this kind and it is a modification of a solver developed
by C.T.Kelley, called nsol . The user only have to specify the problem on residualform as r = f(x), i.e. a set of algebraic equations. The numerical Jacobian is calculated
by the diffjac function and it is possible to change the updating mechanism of the
numerical Jacobian. There is no Newton solver in the basic MATLAB program but
there is one solver in Optimization toolbox called f sol ve which is similar to
newsol .The newsol Newton solver is used in the following ways. The solver only needs thename of the user defined function and a column vector of initial guesses and it returns
the solution column vector as
sol = newsol ( ' cstr ss' , [ 1 1] ' ) ;
There are some optional input arguments to newsol which makes it possible to settolerances, t ol =[r t ol ato l ] (relative/absolute tolerance), and to specify the Jacobian
update mechanism, par am=[ 40 1 0 0] , for more information use the help command. It
is possible to parameterise the function with the mpar input argument. This means that it
is possible to set parameters from the outside in the user defined function. The method
returns the solution, sol , the solution history, sol hi st , and an error indicator for a
successful iteration, i er r . It is also possible to return variables from the user defined
function to the outside through a fourth output variable, like mvar . A complete function
call to newsol look as follows
[ sol , sol hi st, i err , mvar] =newsol ( ' cst rss' , [ 1 1] ' , t ol , par am, mpar ) ;
Example 2.6 : Exothermic CSTR, continue
The nonlinear problem in Example 2.5 can be solved by the Newton solver
newsol . The solver starts at [ 1 1] . Note that newsol only needs the function
cstrss.
sol = newsol ( ' cst rss ' , [ 1 1] ' )sol =
0. 96761. 7608
2.5 SummaryIn the course we solve linear equation systems with Gaussian elimination as x = A\ b.
Nonlinear equations can be solved by fzero, the predefined MATLAB function. Thenonlinear equation system solver, newsol , is used frequently throughout the course.The solver is available on the course home page on the web.
-
8/20/2019 Proc Sim Komp 2005
22/94
22
3 Dynamic Simulation of Lumped Models
3.1 IntroductionAn unsteady-state lumped model results in a set of ordinary differential equations, ODEs. These
equations describe time-dependent relations between a set of variables. Nonlinear
unsteady-state lumped models can often be written as ODEs on state-space form withderivatives of the states explicitly on the RHS like
dx
dt f x x u u
dx
dt f x x u u
n n
n
n n n
1
1 1 1
1 1
=
=
( ,..., , ,..., )
...
( ,..., , ,..., )
or on a more compact representation on vector form
d
dt
xf x u= ( , )
(the general form, F(d x /dt, x ,u) = 0, is not discussed in this course).
If the ODE system is linear (and has one input u) it has the following representation
dx
dt a x a x a x b u
dx
dt a x a x a x b u
n n
n
n n nn n n
1
11 1 12 2 1 1
1 1 2 2
= + + + +
= + + + +
...
...
...
or using matrix notation it becomes
d
dt A B
xx u= +
The difference between linear and nonlinear ODEs is not an important issue here as it
were in the steady-state case. Solvers of ODE systems are categorised into one-
step/multi-step methods and into explicit/implicit methods. The different methods and
their properties are briefly discussed in this chapter. First, explicit one-step solvers are
discussed, as they are simple and commonly used, followed by a short discussion about
implicit one-step solvers. Finally, the multi-step methods are discussed.
3.2 Explic it One-Step SolversThe simplest ODE solver uses the explicit Euler method. More accurate explicit
methods are called Runge-Kutta methods which is a family of methods. These methods
are the most common methods for ODE systems. A one-step method uses only one
previous step to calculate a new step. An explicit method uses an explicit formula to
-
8/20/2019 Proc Sim Komp 2005
23/94
23
calculate a new step, i.e. a direct calculation without internal iteration or equation
solving.
Euler method
The simplest ODE solver uses the Euler method. Assume an ODE expressed on state-
space form as,
dx
dt f x= ( )
Make a Taylor expansion of x(t+h) where h t t t i i= = −+∆ 1 and truncate theexpansion after the first derivative. The first derivative is equal to the function value.
x t h x t dx
dt t h t h x t h x t f x t h( ) ( ) ( ) ( ) ( ) ( ) ( ( ))+ = + + ⇒ + ≈ +φ 2
This approximation gives us the following explicit one-step method.
x x f x
x x
( ) ( ) ( ( ))
( )
t t h t
t
i i i+ = +
=1
0 0
One drawback with this method is that the error decreases proportional to the step-size.
This results in small step-sizes in order to keep the error low. A related problem is that
the solution can become unstable if the step-size is to large, see Ramirez section 3.1.
Assume a linear ODE written on state-space form and that it is stable, i.e. K > 0.d
dt
xKx= −
An Euler approximation gives the following expression
x x Kx K xi i i ih h+ = − = −1 1( )
The stability of this discrete system is determined by the size of the coefficient in front
of the state variable, x. This coefficient is the eigenvalue for this simple system. If the
coefficient is less than one then the solution decreases and the solution is stable and if it
is larger than one then the solution x increases for each calculation and the solution is
unstable.
1 1 1 0
1 1 1
1
1
− < ⇒ = − < ⇒ →
− > ⇒ = − > ⇒ → ∞
+ ∞
+ ∞
h x h x x x
h x h x x x
i i i
i i i
K K
K K
( )
( )
We get the following step-size criteria on h for the stability limit.
1 1 22
− = ⇒ − = − ⇒ =h h hK K K
-
8/20/2019 Proc Sim Komp 2005
24/94
24
The step-size for the Euler method for a linear ODE must be less than a given value in
order to give a stable solution.
h <2
K
Example 3.1: Concentration Dynamics in a Mixing Tank Using the Euler MethodA tank with constant volume, V=1, and inflow, q=1 and inflow concentration
cin= 1, has the initial concentration equal to zero.
( )
( )ccV
q
dt
dc
ccqdt
dcV
cqcqdt
Vcd
in
in
out out inin
−=
−=
−=)(
Figure 3.1: A Mixing Tank
This is expressed in the following M-file.
f uncti on [ dc] =onet ank(t , c)% ONETANK i s a model of t he concent r at i on var i at i on % i n a cont i nuous t ank. % par amet ers ci n = 1;q = 1;V = 1;% equat i ons dc = - q/ V*c + q/ V*ci n;
An explicit Euler method called eul er s , is found in Mathews, Numerical Methods. The one tank model can be simulated by the Euler method with the
following settings, from 0 to 10, with initial concentration 0 and with 20 steps:
[ t , y] =eul ers( ' onet ank' , 0, 10, 0, 20) ; TY = [ t ' y' ]
TY =0 0
0. 5000 0. 50001. 0000 0. 75001. 5000 0. 87502. 0000 0. 93752. 5000 0. 96883. 0000 0. 98443. 5000 0. 99224. 0000 0. 99614. 5000 0. 9980
5. 0000 0. 99905. 5000 0. 99956. 0000 0. 99986. 5000 0. 99997. 0000 0. 99997. 5000 1. 00008. 0000 1. 00008. 5000 1. 00009. 0000 1. 00009. 5000 1. 0000
10. 0000 1. 0000
-
8/20/2019 Proc Sim Komp 2005
25/94
25
0 2 4 6 8 10
0
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
2
time
c
o
n
c
e
n
tr
a
ti
o
n
Figure 3.2: Simulation of the concentration in a tank using Eulers method (5, 10, 20 and 100 steps).
The solution is shown in Figure 3.2. The plot illustrates the solutions with 5, 10, 20 and100 steps. Figure 3.2 indicates that the solutions depend on the choice of step-size. The
solution goes unstable for step-sizes larger then 2. The solution based on 100 steps
(step-size = 0.1) are close to the exact solution.
Runge-Kutta methods
To increase the step-size the derivative approximation must include higher order terms.
In Runge-Kutta methods this is done by taking the average of a number of function
derivatives in the current step. The local error decrease with the number of derivatives
used. A fourth order Runge-Kutta solver called rk4, found in Mathews Numerical
Methods, is used to simulate the mixing tank example below.
0 2 4 6 8 10
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
time
c
o
n
c
e
n
tr
a
ti
o
n
Figure 3.3: Simulation of a tank using Eulers method (1000 steps) and RK4 method (10 steps)
Example 3.2: Concentration Dynamics in a Continuous Tank, continue.
Two solutions of the mixing tank concentration dynamics are shown in Figure 3.3.
The solid line solution is based on the Euler method with 1000 steps and the
dashed line is based on 4-order Runge-Kutta, r k4, with 10 steps. As can be seen
-
8/20/2019 Proc Sim Komp 2005
26/94
26
in this case that the Runge-Kutta method performs very well. It uses only 1% of
the steps compared with Euler. On the other hand it uses 4 function evaluations in
each step, i.e. 40 function evaluations which is 4% compared to the 1000 function
evaluations in the Euler method used in this case. An Euler solution based on 10
steps is seen in Figure 3.2.
Below follows a brief presentation of a second order Runge-Kutta method. The basicRunge-Kutta formula is as follows.
x t h x t h x t e( ) ( ) ( ( ))+ = + φ
A new step is explicitly expressed as a function of the previous step. The choice of the
φ e
x t ( ( )) function is different for different RK methods. A second order RK method has
the following expression
φ = +ak bk 1 2
where the k -coefficients are different function evaluations
k f t xi i1 = ( , ) k f t ph x q x f t ph x qhk i i i i i2 1= + + = + +( , ) ( , )∆
Make a Taylor expansion of the second function evaluation, k 2. ( HOT represents higher
order terms.)
k f t x phf t x qhf t x f t x HOT i i t i i i i x i i2 = + + +( , ) ( , ) ( , ) ( , )
Now use the two function evaluation expressions in the Runge-Kutta formula
( ) x t h x t h ak bk ( ) ( )+ = + +1 2
and rewrite the expression as
( )
( )
x x h af t x bf t x
h bpf t x bqf t x f t x h
i i i i i i
t i i i i x i i
+ = + +
+ + +
1
2 3
( ) ( )
( ) ( ) ( ) ( )
, ,
, , , Ο
In order to find the parameters, a, b, p and q the RK formula is compared with a Taylor
expansion of the original ODE
( )
x t x t hf t x t
h f t x t f t x t f t x t h
i i i i
t i i x i i i i
( ) ( ) ( , ( ) )
( , ( ) ) ( , ( ) ) ( , ( ) ) ( )
+ = +
+ + +
1
2
3
2Ο
We now have two expressions, one is the RK approximation and the other is the Taylor
expansion of the original equation. If the different terms in the both expressions are
assumed to be equal then we get the following expressions
-
8/20/2019 Proc Sim Komp 2005
27/94
27
( )
( )
( )
x t x
f t x t a b f t x
f t x t f t x t f t x t
bpf t x t bqf t x t f t x t
i i
i i i i
t i i x i i i i
t i i x i i i i
( )
( , ( ) ) ( , )
( , ( ) ) ( , ( ) ) ( , ( ) )
( , ( ) ) ( , ( ) ) ( , ( ) )
+ =
= +
+ =
+
1
1
2
These expressions give
a b bp bq+ = = =11
2
1
2; ;
This means that three RK parameters are given when the fourth parameter is assumed. If
we choose b=0 then it gives the Euler method. If we choose b=1 instead then it gives
a=0 and p=q=0.5 which is a midpoint approximation. The choice b=0.5 results in
a=0.5 and p=q=1. This RK method uses two function evaluations, one on each side of
the interval.
The Runge-Kutta method discussed above is a second order method based on two
function evaluations on the interval. Other RK methods like fourth order RK4 methodsuses four function evaluations on the interval. The error decreases with the order, i.e.
the number of function evaluation on the interval.
Variable Step-Size
To keep the step-size fixed at a given value is not common practice. Instead almost all
professional methods have variable step-sizes based on some kind of step-size control.
The step-size control is often based on the required error tolerance of the problem.
Runge-Kutta solvers have error estimators included in the code. These estimators must
have higher order than the actual solver. Therefore a second-order Runge-Kutta is
equipped with a third-order error estimator and a solver like this is called rk23 or as in
MATLAB ode23. In MATLAB there is also a fourth-order Runge-Kutta solver called ode45. (In MATLAB 4 and older versions there are two kinds of ODE-solvers, ode23 and ode45. These are not used in this course. Instead there are new ode23 and
ode45 in the MATLAB ODE Suite. From a user point of view the only difference is
the set of input variables.)
RK methods in MATLAB
ODE-solvers in MATLAB have the following simplified syntax:
[ T, Y] = ODE23( ' F' , TSPAN, Y0) ;
TSPAN is a vector with the start and stop time, e.g. [0 10], and Y0 is the initial values.
Example 3.3: Concentration Dynamics in a Mixing Tank
The simple mixing tank is solved by the ode23 method in the following way. Notice that the solution has many steps in the beginning of the solution. At the
end there is only a few steps and in total 22 step with guaranteed error estimates.
The default tolerances are a relative error tolerance of 1e-3 and an absolute error
tolerance of 1e-6. The ode45 solver works exactly in the same way.
[ t , y]=ode23( ' onet ank' , [ 0 10] , 0) ;
-
8/20/2019 Proc Sim Komp 2005
28/94
28
TY=[ t y]
TY =0 0
0. 0001 0. 00010. 0005 0. 00050. 0025 0. 00250. 0125 0. 01240. 0625 0. 06060. 1791 0. 16400. 3485 0. 29430. 5668 0. 43280. 8350 0. 56631. 1574 0. 68601. 5431 0. 78682. 0077 0. 86642. 5790 0. 92513. 3139 0. 96494. 3139 0. 98835. 3139 0. 99616. 3139 0. 99877. 3139 0. 99968. 3139 0. 99999. 3139 1. 0000
10. 0000 1. 0000
3.3 Implici t One-Step Methods Explicit solvers uses current function evaluations to predict a new step, like
xi+1 = xi + h f(xi).
Implicit solvers are based on the idea to use the new step in the function evaluation to
generate the new step, like
xi+1 = xi + h f(xi+1),
which in turn generates an implicit equation system. This means that an implicit method
solves a nonlinear equation system in each step. This results in very good stability properties. In MATLAB, ode23s is an implicit second-order Rosenbrock solver.
Example 3.4: Two Tank Series and Stiff Problems
Two tanks connected in series are simulated with different volumes. The total
volume is constant. In the first case the volumes are the same and in the second
case the first tank has 0.1% of the volume of the second tank. This is clearly
illustrated in the figure below.
Figure 3.4: Two-tank series left) same volumes right) different volumes.
Material balances over one chemical component in the two tanks becomes
-
8/20/2019 Proc Sim Komp 2005
29/94
29
( )
( )
In Out Acc= +
= + ⇒ = −
= + ⇒ = −
qC qC V dC
dt
dC
dt
q
V C C
qC qC V dC
dt
dC
dt
q
V C C
in in1 1
1 1
1
1
1 2 2
2 2
2
1 2
and rewritten on state-space form using matrices it becomes
d
dt
C
C
q
V
q
V
q
V
C
C
q
V C in1
2
1
2 2
1
21
0
0
⎡
⎣⎢
⎤
⎦⎥ =
−
−
⎡
⎣
⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥
⎡
⎣⎢
⎤
⎦⎥ +
⎡
⎣
⎢⎢
⎤
⎦
⎥⎥
The first case solved by ode23 is seen on the left in Figure 3.5. The solution by
ode23s of the second case is seen on the right in the same figure.
f uncti on [ dc] =t wot anks(t , c)
% TWOTANKS i s a model of t he concent r at i on var i at i on % i n a t ank ser i es of 2 t anks.
% par amet ers c0 = 1;q = 1;V1 = 0. 5; Vt ot = 1;V2 = Vt ot - V1;% equat i ons dc = [ - q/ V1 0; q/ V2 - q/ V2] *c + [ q/ V1; 0] *c0;
Numerical statistics is clearly illustrated in the table below. In the first case the
two solvers take almost the same amount of steps but the nonlinear solver in the
implicit ode23s makes almost twice as much floating point operations, called flops, compared with the explicit Runge-Kutta ode23. In the second case theimplicit ode23s takes some more steps and require 20% more flops comparedwith the first case. The explicit ode23 needs almost 15 times more flops andneeds an enormous amount of steps.
Table 3.1: Number of steps and floting point operations during the solution of a two-tank problem
with stiffness properies.
V1 = 0.5 V1 =0.001
ode23 33 steps, 4968 flops 3995 steps, 63932 flops
ode23s 35 steps, 9453 flops 41 steps, 11143 flops
-
8/20/2019 Proc Sim Komp 2005
30/94
30
0 2 4 6 8 10
0
0 2
0 4
0.6
0.8
1
1.2
1 4
time 0 2 4 6 8 10
0
0 2
0.4
0.6
0 8
1
1 2
1.4
time Figure 3.5: Simulation of a two-tank series left) tanks with equal sizes
right)volume of first tank is 0.1% of the second
The implicit solver is superior in cases when fast dynamics die out and the interesting
dynamics is slow. Problems with these kinds of characteristics are called stiff problems. Note that a simulation of the second case between 0 and 0.01 is not a stiff problem.
3.4 Multi-Step MethodsTo increase the accuracy in a RK-method we have to increase the order of the method
which increases the number of function evaluations needed in each step. One way to
increase the order without increasing the function calls is to reuse previous steps. This
means using one new function evaluation together with a number of old ones. There are
both explicit and implicit multi-step methods.
Adams Methods
Explicit multi-step methods are often called Adams-Bashforth methods. These methodsare rarely used because they have very small stability regions and they require very
small step-sizes. Methods like this are often modified. The explicit multi-step formula is
used as a prediction of the new step. This predicted value is then used in an implicit
multi-step formula, which correct the predicted value. This method uses direct
substitution to solve the nonlinear equation. In this way we get a semi-implicit multi-
step method with good performance. Methods like this are called predictor-corrector
methods or Adams-Bashforth-Moulton methods or just Adams methods. These methods
can also include variable step-size and variable order, both controlled by error
tolerances. In MATLAB there is a variable step-size variable order multi-step method
(Adams-Bashforth-Moulton) called ode113.
Example 3.5: Two Tank Series and High Accuracy
If the error tolerances are decreased higher order methods are favourable. The
simple two tank problem from Example 3.4 illustrates this in the table below. If
the default tolerances are used the lowest order Runge-Kutta is the most economic
method. If the tolerances are halved the situation becomes the opposite. Adams
methods have good performance for non-stiff problems with high accuracy
demands.
-
8/20/2019 Proc Sim Komp 2005
31/94
31
Table 3.2: Number of steps and floting point operations during the solution of a two-tank problem
with high accuracy.
default tolerances halved tolerancesode23 33 steps, 6463 flops 251 steps, 40180 flopsode45 81 steps, 13539 flops 201 steps, 31539 flopsode113 46 steps, 13056 flops 95 steps, 25576 flops
Gear methods
True implicit multi-step methods are often based on backward difference formula, so
called BDF-methods. These methods were developed by Gear and are also called Gear
methods. The methods use a number of previous steps to approximate the derivatives,
backward differences. In MATLAB there is one BDF-method called ode15s. Thismethod is a variable order method between 1 and 5. BDF-methods with order higher
then 6 become unstable. ode15s has quasi-constant step-size control which means thatthe step-size is fixed as long as possible. When the step-size is changed the solver must
recompute a number of previous steps. The ode15s method is very good on stiff problems. Specially when the number of states increase then it performs better than
ode23s. (ode15s is actually a NDF-method which means numerical difference formulaand it uses numerical calculated derivatives instead of using previous function calls
direct.)
Example 3.6 : Two Tank Series and Multiple Components
The tank series in Example 3.4 requires stiff ODE solvers if the tank volumes
differs in magnitude. A comparison between the two stiff solvers ode23s and
ode15s is illustrated in the table below. If the tank series only has one chemicalcomponent then the ODE-system have 2 states, i.e. 2 differential equations. If the
tanks have 4 chemical components the ODE-system have 8 states. When the order
of the ODE-system increases the high order method becomes more efficient
compared to the low order method. This is seen below. For the tank series from
Example 4 the ode23s is slightly better then ode15s. When the same tank serieshas 4 chemical components ode15s is much more efficient. Note that ode15stakes more steps due to the quasi-constant step-size control.
Table 3.3: Number of steps and floting point operations during the solution of a multi component
tank problem.
2 state system 8 state systemode23s 35 steps, 9453 flops 41 steps, 170647 flopsode15s 49 steps, 10701 flops 65 steps, 59228 flops
3.5 Differential-Algebraic Equation SolversWhen fast dynamics becomes so fast compared to the dynamics of interest it can be
approximated to be instantaneous. Some physical phenomena can also be so fast that it
is hared to measure the dynamic behaviour. In chemical engineering we assume that the
phenomenon is at equilibrium. A differential-algebraic equation system, DAE, can be
expresses as
-
8/20/2019 Proc Sim Komp 2005
32/94
32
),(0
),(
21
211
x xg
x x f dt
dx
=
=
The problem is a set of differential equations, f , and a set of algebraic equations, g. The
algebraic equations have to be fulfilled in each time step that the differential equationsas solved. These kinds of problems can be solved by implicit ODE-methods with
modified step-size control. In MATLAB the methods ode23t and ode15s can solve
DAE problems.
)()(00
01
),(*0
),(*1
2122
2111
x f dt
dx M x f
dt
dx
x x f dt
dx
x x f dt
dx
=⇒=⎥⎦
⎤⎢⎣
⎡⇒
⎪⎭
⎪⎬
⎫
=
=
The matrix on the left hand side, M, is called the mass matrix. For DAE problems the
mass matrix is singular. To solve DAE problems in MATLAB the right hand side isdescribed in an m-file as usual and the mass matrix is defined in the options using
odeset .
M=[ . . . ] ;opt i ons=odeset ( ‘ Mass’ , M)[ t , y] =ode15s( @f unc, [ 0, 1] , x0, opt i ons) ;
Example 3.6 : Two Tank Series again
Two tanks connected in series are simulated with different volumes. The total
volume is constant, see axample 3.4. In this case we assume that the first tank is
so small that the dynamics is neglected.
Material balances over one chemical component in the two tanks becomes
( )
( )212
22221
11 0
AccOutIn
C C V
q
dt
dC
dt
dC V qC qC
C C qqC qC inin
−=⇒+=
−=⇒=
+=
and rewritten on state-space form using matrices it becomes
inC q
C
C
V
q
V
q
q
C
C
dt
d ⎥⎦
⎤⎢⎣
⎡+⎥
⎦
⎤⎢⎣
⎡⎥⎥
⎦
⎤
⎢⎢
⎣
⎡
−
−=⎥
⎦
⎤⎢⎣
⎡⎥⎦
⎤⎢⎣
⎡0
0
10
00
2
1
222
1
This problem is solved using ode15s as follows. Define the right hand side of the
DAE syetm, i.e., the differential and algebric equations.
f unct i on dx=DynTankSer i es( t , x)c1=x( 1) ;c2=x( 2) ;ci n=1;q=1;V2=1;
-
8/20/2019 Proc Sim Komp 2005
33/94
33
r es=q*( ci n- c1) ;dc2=q/ V2*( c1- c2) ;dx=[ r es; dc2] ;
Then define the mass matrix and call the DAE solver.
M=[ 0 0; 0 1] ;
opt i ons=odeset ( ' Mass' , M) ;t span=[ 0 10] ;xi ni t =[ 0; 0] ;[ t , x]=ode15s( @DynTankSer i es, t span, xi ni t , opt i ons) ;
Figure 3.6: Simulation of a two-tank series using a DAE solver.
The DAE solvers in MATLAB can not solve all kinds of DAE problems. They can only
solve index 1 problems. The index of a DAE problem is the number of timed the
equation system have to be differentiated in order to be a set of ODE:s. In the example
above the first equation only have to be differentiated once in order to be an ODE. The
derivative of the first concentration is equal to the derivative of the secondconcentration. In general this is not the case and the solvers fail to solve the problems
indicating in the error message that the index is higher than one.
3.6 ODE Simulation in MATLABAbove we have discussed five different methods that are available in MATLAB. There
are two additional ones, ode23t and ode23tb. These methods can handle moderate
stiff problems and stiff problems respectively. ode23t is an implementation of thetrapezoidal rule. ode23t b is an implementation of an implicit Runge-Kutta formulawith a first stage that is a trapezoidal rule step and a second stage that is a backward
differentiation formula of order two. All solvers have a number of options for setting
different solver parameters in order to increase solver performances. These option parameters are handles through odeset and odeget . MATLAB also advise the user
to write its ODE-file in a predefined way. This is well described in the help text of
odef i l e or in the manual Using MATLAB. When you become a more skilled user use
these guidelines to develop simulation problems in MATLAB.
3.7 Summary
-
8/20/2019 Proc Sim Komp 2005
34/94
34
The ODE solvers in MATLAB6 are categorised in the table below. In ordinary
simulation problems the low order methods can be used, ode23 and ode23s. In
process simulation, high order stiff problems often occur in which ode15s is preferable. These three methods are probably the ones that are used in the Process
Simulation course. The ode15s and ode23t can handle differential-algebraicequation systems, DAE, but this is not discussed futher in the course.
Table 3.4: Selection of ODE-methods in MATLAB.
Nonstiff Stiff DAELow order ode23 ode23s
ode23t bode23t
High order ode113,ode45
ode15s ode15s
High accuracy ode113 ( ode15s) ( ode15s)
-
8/20/2019 Proc Sim Komp 2005
35/94
35
4 Development of Distributed Models
4.1 IntroductionIn the previous chapters process models are based on balance equations of mass, energy
and momentum over a lumped system. Inside the system the properties are uniform with
no spatial change. Lumped systems are well mixed and they have no spatial dependent behaviour. In this chapter we are going to develop models for systems with spatial
change. The behaviour is distributed in the space dimension and therefore they are
called distributed systems or distributed parameter models. The development is similar
to lumped model development. The difference is that the balance equations are set up
for arbitrary small fluid elements or infinite small control volumes.
A general balance equation is expressed as follows
In + Production = Out + Accumulation
This is set up for the volume in Figure 4.1.
Figure 4.1: A general fluid element
Below we are discussing distributed models based on mass, energy and momentum
balances first in one space dimension, in this case x-dimension, and second very briefly
present balances in three dimensions.
4.2 Mass BalancesMass balances over a distributed system are very similar to mass balances over lumped
systems. The major difference is the special change. Assume that the fluid in the control
volume in Figure 4.1 has a given density, than the accumulation of mass is simply the
time change of the mass.
For simplicity let us assume a convective flow of mass only in the positive x-direction.
The inflow of mass can be expressed as the density times the volume flow rate. In this
case the element surface, ∆y∆z, times the velocity in x-dimension.
y z
∆x
∆y
∆z
x
( ) z y xt d
d ∆∆∆ ρ
∆ ∆ y z v x x( ) ρ
-
8/20/2019 Proc Sim Komp 2005
36/94
36
The outflow at x+∆ x becomes
There is no generation of mass and therefore the total mass balance is as follows
Assume that the volume boundaries are time independent. This means that the control
volume derivative is zero and it is possible to divide all terms with the volume. Then let
the volume size goes to zero, i.e. take the limit when ∆V goes to zero, and use thedefinition of derivation. In the simple example with flow only in the x-direction the
total mass balance becomes
An equation, like the one on the right above, with derivatives in more the one
independent variable is called a partial differential equation, shortly just called PDE.
This particular PDE, with first order derivative in space and time, is called convective
type PDE or first order hyperbolic PDE .
In the general case there is a flow field in all directions giving changes in all directions.
Convection in three dimensions is just the sum of the individual convection terms.
Note the velocity vector v and the divergence of the velocity, the nabla operator. Below
follows a tube example based on an alternative development of the total mass balance. It
is based in the integral form of the ideas presented above.
Example 4.1: Tube
A medium is flowing through a pipe. The pipe has cylindrical geometry with a
constant cross section. The accumulation of mass in the finite volume is the
volume integral of the density. Mass enters the finite volume at position z and
leaves at position z+dz.
Figure 4.1: Tube.
ρ L
z=0 z z+dz z=L
ρ 0
∆ ∆∆
y z v x x x( ) ρ +
( ) ( )Acc In Out= −
= −+
d x y z
d t y z v v x x x x x
ρ ρ ρ
∆ ∆ ∆∆ ∆
∆( ) ( )
( ) xv
t xvv
t d d x x x x x x
∂ ρ ∂
∂ ρ ∂ ρ ρ ρ −=⇒⎟⎟
⎠ ⎞⎜⎜
⎝ ⎛
∆−= ∆+
( )∂ρ
∂
∂
∂ ρ
∂
∂ ρ
∂
∂ ρ ρ t x v y v z v x y z= − + +
⎛
⎝ ⎜
⎞
⎠⎟ = −∇ ⋅ v
-
8/20/2019 Proc Sim Komp 2005
37/94
37
The accumulation of mass in the control volume is the time derivative of the
volume integral of the density.
∫+
=dz z
z
d Adt
d ζ ρ )(massof onAccumulati
The inlet and outlet of mass is in this case only convection dependent
z z Av ρ =massof Inlet
dz zdz z Av ++= ρ massof Outlet
The three terms are put together in a mass balance giving
∫
+
++ +=
+=+dz z
z
dz zdz z z z d A
dt
d Av Av ζ ρ ρ ρ )(
AccOutProdIn
The inlet and outlet terms are put on the same side and can be rewritten as the
primitive function of the integral of its derivative. We see that there is the same
integral on both sides of the equation.
[ ] ( )
∫∫+
+++
+
∂
∂−=−=−=
dz z
z
dz z
zdz zdz z z z
dz z
z
d Av
Av Av Avd Adt
d ζ
ζ
ρ ρ ρ ρ ζ ρ
ζ ζ
ζ ζ )(
If the boundaries, z and z+dz, are time independent then the time derivative in theaccumulation term can be moved inside the integral.
( )∫∫++
∂∂
−=∂∂ dz z
z
dz z
z
d v
Ad t
A ζ ζ
ρ ζ
ρ
Let us now remove the integral on the both sides and the result is a partial
differential equation.
( ) z
v
t ∂∂
−=∂∂ ρ ρ
We see that the development in Example 4.1 results in the same equation.
Diffusion
In systems with molecular diffusion effects there will be additional terms in the mass
balance. Often one can assume that the molecular flux is proportional to the
concentration gradient described in Fick's first law.
-
8/20/2019 Proc Sim Komp 2005
38/94
38
This expression assumes a binary mixture where component A diffuse in a solvent B.
Component Mass BalancesIt is possible to put up component balances, one for each chemical component. Note
that all component mass balances sum to the total mass balance as in the lumped
parameter case. In order to set up a consistent equation system it is recommended to
take away one mass balance, often one component mass balance, for instance the
solvent balance. Sometimes this knowledge can be used as redundant information to
check the correctness of the model.
Let us derive a component balance with only molecular diffusion as diving force for the
fluid control volume in Figure 4.1. The mass of component A in the control volume is
the product of mole weight and concentration.
The inflow of component A becomes
and the outflow
Let us now put these three terms together in a component mass balance of component
A.
Divide all terms with the control volume and the mole weight. Let the volume size go to
zero.
J Ddc
dx A AB A= −
( ) z y xc M t d
d A A ∆∆∆
( ) x
A AB A x A A
dx
dc D M z y J M z y ⎟
⎠
⎞⎜⎝
⎛ −∆∆=∆∆
( ) x x
A AB A x x A A
dx
dc D M z y J M z y
∆+∆+ ⎟
⎠
⎞⎜⎝
⎛ −∆∆=∆∆
( ) ( ) ( ) x x A A x A A A A
J M z y J M z y z y xc M t d
d ∆+∆∆−∆∆=∆∆∆
( ) ( )( ) x
J
t
c J J
xt d
dc A A
x x A x A A
∂∂
−=∂
∂⇒−
∆= ∆+
1
-
8/20/2019 Proc Sim Komp 2005
39/94
39
The flux J can be expressed as Fick's first law which results in Fick's second law.
A partial differential equation with a second order derivative in space and first order in
time is called a parabolic PDE . It is also some times called diffusion type PDE or heat
equation.
Dispersion
In flow systems there appear a phenomenon called dispersion. This occurs in problems
with turbulent flow, flow in complicated passages such as packed beds or porous media.
Dispersion is mathematically expressed similar to diffusion.
The major difference is that the dispersion coefficient, Dax, is flow dependent while the
diffusion coefficient is not. Below follows an example with dispersion in a tubular
reactor.
Example 4.2: Tubular reactor with dispersion
A medium is flowing through a tubular reactor. A chemical component has the
concentration c. The reactor has cylindrical geometry with a constant diameter.
The accumulation of mass in the finite volume is the volume integral of the
density. Mass enters the finite volume at position z and leaves at position z+dz.
Figure 4.2: Tubular reactor.
∫+
=dz z
z
d Acdt d ζ )(moleof onAccumulati
z
ax z zd
dc A D Acv
ζ −=moleof Inlet
c L
z=0 z z+dz z=L
c0
2
2
x
c D
x
c D
x x
J
t
c A AB
A AB
A A
∂∂
=⎟⎟ ⎠
⎞⎜⎜⎝
⎛
∂∂
−∂∂
−=∂
∂−=
∂∂
dx
dc D J Aaxax −=
2
2
x
c D
t
c A AB
A
∂
∂=
∂
∂
-
8/20/2019 Proc Sim Komp 2005
40/94
40
dzz
axdz zdz zd
dc A D Acv
+
++ −=ζ
moleof Outlet
( )∫+
=dz z
z
d rA ζ moleof Production
The three terms are put together in a mass balance
( ) ∫∫+
+
++
+
++=++
+=+dz z
zdzz
axdz zdz z
dz z
z z
ax z z d Acdt
d
d
dc A D Acvd rA
d
dc A D Acv ζ
ζ ζ
ζ )(
AccOutProdIn
Rearrage the component mass balance
( )
[ ] ( )
( )( )∫∫∫
∫∫∫
+++
+++
+
+
++
+
+∂
⎟⎟ ⎠
⎞⎜⎜⎝
⎛ ∂∂
∂
+∂
∂−=
=+⎥⎦
⎤⎢⎣
⎡
∂∂
+−=
=+
∂
∂+−
∂
∂−=
∂
∂
dz z
z
dz z
z
axdz z
z
dz z
z
dz z
z
ax
dz z
z
dz z
zdzz
axdz zdz z
z
ax z z
dz z
z
d rAd
c A D
d Acv
d rAc
A D Acv
d rAc
A D Acvc
A D Acvd Ac
t
ζ ζ ζ
ζ ζ
ζ
ζ ζ
ζ
ζ ζ
ζ
ζ ζ
ζ ζ
)(
Remove all integrals and we get a partial differential equation with a second order
space derivative term.
( )r
z
c D
z
vc
t
cax +∂
∂+
∂∂
−=∂∂
2
2
This kind of partial differential equation has one convective term (hyperbolic),
one dispersion term (parabolic) and one reactive term. It is a hyperbolic-parabolic
PDE.
4.3 Energy BalancesThe development of distributed energy balance is identical to the development of
distributed mass balance. In microscopic energy balances there occur different termscompared to lumped models.
Assume that the total energy is composed of internal energy and kinetic energy.
Accumulation of potential energy is neglected together with other energy forms.
⎟⎟ ⎠
⎞⎜⎜⎝
⎛ ∆∆∆⎟⎟
⎠
⎞⎜⎜⎝
⎛ + z y x
vU
dt
d
2
2
ρ ρ
-
8/20/2019 Proc Sim Komp 2005
41/94
41
U represents the internal energy per mass unit. The difference between the convection
of energy in the x-direction is
The energy conduction terms are
Work done by the fluid element against the surroundings consists of work against
volume forces (gravity) and work against surface forces, such as pressure
and viscous forces.
Put all terms together and taking the limit of the volume that goes to zero gives the
following energy balance
This is a general distributed energy balance in on space dimension. Some special cases
are discussed below before the general