Académique Documents
Professionnel Documents
Culture Documents
TABLE OF CONTENTS........................................................................................................................... 1
PREFACE................................................................................................................................................... 3
1
INTRODUCTION ........................................................................................................................ 46
SHOOTING METHOD ................................................................................................................. 46
FINITE DIFFERENCE METHOD .................................................................................................. 53
SUMMARY ................................................................................................................................ 59
INTRODUCTION ........................................................................................................................ 35
MASS BALANCES ..................................................................................................................... 35
ENERGY BALANCES ................................................................................................................. 40
MOMENTUM BALANCES........................................................................................................... 43
BOUNDARY CONDITIONS ......................................................................................................... 44
SUMMARY ................................................................................................................................ 45
INTRODUCTION ........................................................................................................................ 22
EXPLICIT ONE-STEP SOLVERS.................................................................................................. 22
IMPLICIT ONE-STEP METHODS ................................................................................................. 28
MULTI-STEP METHODS ............................................................................................................ 30
DIFFERENTIAL-ALGEBRAIC EQUATION SOLVERS..................................................................... 31
ODE SIMULATION IN MATLAB.............................................................................................. 33
SUMMARY ................................................................................................................................ 33
INTRODUCTION ........................................................................................................................ 13
METHOD FOR LINEAR EQUATIONS ........................................................................................... 13
METHODS FOR NONLINEAR EQUATIONS .................................................................................. 16
NEWTON METHODS ................................................................................................................. 18
SUMMARY ................................................................................................................................ 21
INTRODUCTION .......................................................................................................................... 4
MASS BALANCES ....................................................................................................................... 4
SYSTEMS OF MASS BALANCES ................................................................................................... 8
ENERGY BALANCES ................................................................................................................. 10
MOMENTUM BALANCES........................................................................................................... 11
SUMMARY ................................................................................................................................ 12
INTRODUCTION ........................................................................................................................ 60
METHOD OF LINES ................................................................................................................... 60
SPACE DISCRETIZATION ........................................................................................................... 61
SUMMARY ................................................................................................................................ 69
INTRODUCTION ........................................................................................................................ 70
FLOWSHEETING........................................................................................................................ 71
COMPUTATIONAL FLUID DYNAMICS ........................................................................................ 76
HYSYS A FLOWSHEETING PROGRAM .................................................................................... 79
SUMMARY ................................................................................................................................ 80
INTRODUCTION .........................................................................................................................81
LINEAR REGRESSION ................................................................................................................81
NONLINEAR REGRESSION .........................................................................................................85
REGRESSION ANALYSIS ............................................................................................................87
SUMMARY ................................................................................................................................92
INDEX..............................................................................................................................................93
Preface
Process Simulation using MATLAB is a set of lecture notes for the course
Process 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.
http://www.chemeng.lth.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 page
http://optimal.colorado.edu/~ramirez/chen4580.html
A general textbook written by Mathews, Numerical Methods, describes a large set of
simple numerical routines and some of them are used in the course in order to
illustrate fundamental properties.
http://titan.fullerton.edu/~mathews/software/software.htm
In a selection of M-files developed by Kelley there is a set of nonlinear solvers that
are very interesting. One of them, nsol, is modified into newsol and used
frequently through out the course.
http://www.siam.org/books/kelley/kellcode.htm
SYSTEM
momentum
In +
=
+ Out
Production
Accumulation
energy
mass
momentum
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 lumped
together 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.
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 flow
rates are independent of the mass inside the system.
The total mass balance over the control volume now becomes
+ Prod = Out + Acc
dm
dm
ww + we + 0
= ws +
= ww + we ws
dt
dt
In
Water
100 kg/hr
Ethanol
40 kg/hr
Control
volume
Solution
120 kg/hr
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.
d ( sV )
= w q w + e qe s q s
dt
dV
= w q w + e qe q s
dt
s
s
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.
pVM w
d
RT = w + w w
w
e
s
dt
VM w dp
= ww + we ws
RT dt
In + Prod = Out
ethanol : we + 0
water : ww + 0
total : we + ww
+ Acc
d (m )
= ws
+
dt
d (m(1 ) )
= ws (1 ) +
dt
dm
= ws
+
dt
We see that the two component mass balances sum to form the total mass balance.
The ethanol component mass balance become
d (m )
= we ws s
dt
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.
d (m )
d
dm
= m
+
dt
dt
dt
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
d 1 d (m ) dm
=
dt m dt
m dt
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 balances
The 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.
dm
= ww + we ws
dt
d (m )
= we ws s
dt
These two balances are used in
d (m )
d
dm
= m
+
dt
dt
dt
d 1
= (we ws ) (ww + we ws )
dt m
m
we
ww
(1 )
=
m
m
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 changes
A 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.
+ Acc
dm
= wout
+
win
dt
d ( MVc)
Mqin cin = Mqout cout +
dt
dc
= qcout
+ MV
qcin
dt
dc q
= (cin c )
dt V
In
= Out
q
c in
cout
Material balances over one chemical component, expressed in moles, in the two
tanks becomes
In = Out + Acc
dC1
dC1 q
= (C C1 )
dt
dt V1 in
dC
dC2 q
qC1 = qC2 + V2 2
= (C1 C2 )
dt
dt
V2
and rewritten on state-space form using matrices it becomes
qCin = qC1 + V1
C
d 1 V1
=
dt C2 q
V2
0
q
C1
+ V1 Cin
q C
2 0
V2
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.
q
V
1
q
V2
0
q
C
1 = V1 Cin
q
C2
0
V2
V, y1
1
2
3
xi-1
yi
xi
yi+1
Tray i
4
5
6
V, y7
L, x6
Figure 1.5: Six-plate adsorption column. left) column configuration, right) column tray
yi= axi + b
Let us put up the six component balances together with the equilibrium relation
In
= Out
1: Lx0 + V (ax2 + b)= Lx1 + V (ax1 + b)
2: Lx1 + V (ax3 + b)= Lx2 + V (ax2 + b)
3: Lx2 + V ( ax4 + b)= Lx3 + V (ax3 + b)
4: Lx3 + V (ax5 + b)= Lx4 + V (ax4 + b)
5: Lx4 + V ( ax6 + b)= Lx5 + V (ax5 + b)
6: Lx5 + Vy7
= Lx6 + V (ax6 + b)
In
= Out
+ Acc
d
+ Pout ) + Qout ) + ((U + K + P )m )
dt
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, Qp. The work done by the
system on the surroundings is shaft work, W, and pressure/volume work, WpV. The most
common situation is when the internal energy dominates the total energy balance. Then
kinetic and potential energy are neglected and we get
In
+ Prod
= Out
+ Acc
+ Qout ) +
dmU
dt
d (mU )
= inU in out U out + in p in out p out + Q P + Qin Qout W
in
out
dt
The enthalpy of a system can be expressed as the sum of the internal energy and the
pressure-volume
p
H =U +
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
p
d m H
p
p
= in H out H + in p in out p out + Q P + Qin Qout W
dt
in
out in
out
d (mH )
= in H in out H out + Q P + Qin Qout W
dt
10
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.
In
= Out
+ Acc
d (Vh)
dt
dh
+V
dt
= qh
q
Tin
dh q
= (hin h )
dt V
Tout
Assume also that the enthalpy can be expressed as proportional to the temperature
difference from reference temperature as
H = C p ,m T
q
= (C p , m (Tin T0 )in C p , m (T T0 ))
dt
V
dT q
= (Tin T )
dt V
This is only valid under the assumption that the heat capacity is (or almost)
constant.
Mechanical Energy Balances
The 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.
+ Prod= Out
in vin Floss
+ Acc
d (mv )
= out vout +
dt
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
11
1.6 Summary
Lumped 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.
12
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 computational
reasons one often differs between linear and nonlinear equation systems. Nonlinear
steady-state lumped models can be written on a general form as
f 1(x1 , x 2 , ..,x n ) = 0
f 2 (x1 , x 2 , ..,x n ) = 0
...
f n (x1 , x 2 , ..,x n ) = 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
a11 x1 + a12 x2 +...+ a1n xn u1= 0
a21 x1 + a22 x2 +...+ a2 n xn u2= 0
...
an1 x1 + an 2 x2 +...+ ann xn un= 0
or
Ax = u
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.
LU-decomposition
In MATLAB LU-decomposition is done by the command lu(A). The lu-command
returns the two triangular matrices, L and U, and a partitioning matrix, P. Partitioning is
done 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, Ax = 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 ludecomposition with pivoting.
13
V, y1
1
2
3
xi-1
yi
xi
yi+1
Tray i
4
5
6
V, y7
L, x6
Figure 2.2: Six-plate adsorption column. left) column configuration, right) column tray
In
= Out
Lxi 1 + Vyi +1= Lxi + Vyi
Assume a linear equilibrium relation between liquid and vapour composition.
yi= axi + b
Let us put up the six component balances together with the equilibrium relation
In
= Out
1: Lx0 + V (ax2 + b)= Lx1 + V (ax1 + b)
2: Lx1 + V (ax3 + b)= Lx2 + V (ax2 + b)
3: Lx2 + V (ax4 + b)= Lx3 + V (ax3 + b)
4: Lx3 + V (ax5 + b)= Lx4 + V (ax4 + b)
5: Lx4 + V ( ax6 + b)= Lx5 + V (ax5 + b)
6: Lx5 + Vy7
= Lx6 + V (ax6 + b)
This can now be rewritten with unknowns on the left and remember that x0 and y7
are known values.
14
( L + Va ) x1 + Vax2= Lx0
Lx1 ( L + Va ) x2 + Vax3= 0
Lx2 ( L + Va ) x3 + Vax4= 0
Lx3 ( L + Va ) x4 + Vax5= 0
Lx4 ( L + Va ) x5 + Vax6= 0
Lx5 ( L + Va ) x6
= Vy7
Note the creation of a matrix with ones on the lower off diagonal, eyelow. It is
also used to create the elements in the upper off diagonal and this is done by
transposing the matrix, eyelow'. 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
rank-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 rcond-command. The rcond value is between 1 (good) and 0 (bad).
Values of the rcond down to 0.0001 are common.
15
f(b)
a
f(a)
f(c)
f(a)
16
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 unknown
variable. 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 Cp = 1. The temperature dependency can be expressed
with the following equation (T in Kelvin, Cp in kJ/kg K)
15.04
C p = 1716
.
4.257 * 10 6 T
T
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 the
same name as the function name, Cpfun. This function requires one input
variable, the unknown variable, in this case T. The function returns the residual,
i.e., the deviation from zero.
function res = Cpfun(T)
res = 0.716 - 4.257e-6*T - 15.04./sqrt(T);
The Cpfun can now be used in the nonlinear equation solver fzero. The first
input variable to fzero is a string with name of the function (M-file). The
second 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 substitution
A third simple method is called direct substitution and this is very common method.
The algebraic problem is rewritten as
0 = f ( x) = g ( x) x
and put on the following form
17
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.
g(x0)
di
di
ag
ag
on
al
on
al
g(x1)
g(x0)
g(x1)
g(x2)
x2
x1
x0
x0
x1
x2
Figure 2.3 - right) Converging direct substitution, left) Diverging direct substitution.
At the solution the function f is zero and let us through away higher order terms.
0 = f ( x 0 ) + f ' ( x 0 )( x1 x 0 )
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.
f ( x 0 ) = f ' ( x 0 )( x1 x 0 )
x1 = x 0
x1
x0
f'(x0)
x2
18
f ( x0 )
f ' ( x0 )
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 be
undesirable.
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 1
f 1
x 1
x 2 x0
x0
f ' ( x ) x0 = J ( x 0 ) =
f 2
f 2
x 1
x 2 x0
x0
Note that the notation x1 and x2 is the definition of the two unknowns. Make a
multidimensional Newton method and rewrite it as a linear equation system
f ( x 0 ) = J ( x 0 )( x 1 x 0 ) J ( x 0 ) x 1 = J ( x 0 ) x 0 f ( x 0 )
F, CA, T
TC
A steady-state model with one component material balance and one energy
balance is as follows
19
In
+ Prod
FC A 0
= Out
VR = FC A
FC p C A 0 + ( H )VR = FC p C A + kA(T TC )
Assume also a first-order reaction and an Arrhenius temperature dependency as
Ea
R = k 0C A e RT
1 1
50 ( )
2
0=(0 ) u1( c ) + e
1 1
50 ( )
2
20
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 residual
form 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 fsolve which is similar to
newsol.
The newsol Newton solver is used in the following ways. The solver only needs the
name of the user defined function and a column vector of initial guesses and it returns
the solution column vector as
sol = newsol('cstrss',[1 1]');
There are some optional input arguments to newsol which makes it possible to set
tolerances, tol=[rtol atol] (relative/absolute tolerance), and to specify the Jacobian
update mechanism, param=[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, solhist, and an error indicator for a
successful iteration, ierr. 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,solhist,ierr,mvar]=newsol('cstrss',[1 1]',tol,param,mpar);
2.5 Summary
In 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. The
nonlinear equation system solver, newsol, is used frequently throughout the course.
The solver is available on the course home page on the web.
21
dx
= Ax + Bu
dt
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 onestep/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.
22
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 statespace form as,
dx
= f ( x)
dt
dx
( t ) h + ( t ) h 2 x ( t + h) x ( t ) + f ( x ( t )) h
dt
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.
dx
= Kx
dt
x i + 1 = x i h Kx i = (1 h K ) x i
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 h K < 1 x i + 1 = (1 h K ) x i < x i x 0
1 h K > 1 x i + 1 = (1 h K ) x i > x i x
1 hK = 1 hK = 2 h =
2
K
23
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 Method
A tank with constant volume, V=1, and inflow, q=1 and inflow concentration
cin= 1, has the initial concentration equal to zero.
d (Vc )
= q in cin q out c out
dt
dc
= q(c in c )
V
dt
dc q
= (c in c )
dt V
24
0
0.5000
0.7500
0.8750
0.9375
0.9688
0.9844
0.9922
0.9961
0.9980
0.9990
0.9995
0.9998
0.9999
0.9999
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.8
1.6
c
o
n
c
e
n
tr
a
ti
o
n
1.4
1.2
0.8
0.6
0.4
0.2
0
0
10
time
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 and
100 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.
1
0.9
0.8
c
o
n
c
e
n
tr
a
ti
o
n
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
0
10
time
Figure 3.3: Simulation of a tank using Eulers method (1000 steps) and RK4 method (10 steps)
25
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 basic
Runge-Kutta formula is as follows.
x ( t + h ) = x ( t ) + h e ( x ( t ))
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 1 + bk 2
where the k-coefficients are different function evaluations
k1 = f (ti , xi )
k 2 = f ( t i + ph , x i + q x i ) = f ( t i + ph , x i + qhk 1 )
Make a Taylor expansion of the second function evaluation, k2. (HOT represents higher
order terms.)
k 2 = f (t i , xi ) + phf t (t i , xi ) + qhf (t i , xi ) f x (t i , xi ) + HOT
Now use the two function evaluation expressions in the Runge-Kutta formula
x (t + h) = x (t ) + h(ak1 + bk2 )
xi +1 = xi + h af ( t i , xi ) + bf ( t i , xi )
+ h 2 bpf t ( ti , xi ) + bqf ( t i , xi ) f x ( ti , xi ) + ( h 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 ( ti +1 ) = x ( ti ) + hf ( ti , x ( t ) i )
+
h2
( f ( t , x ( t ) i ) + f x ( ti , x ( t ) i ) f ( ti , x ( t ) i ) ) + ( h 3 )
2 t i
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
26
x ( ti +1 ) = xi
f ( ti , x ( t ) i ) = ( a + b) f ( ti , xi )
1
( f ( t , x ( t ) i ) + f x ( t i , x ( t ) i ) f ( ti , x ( t ) i ) ) =
2 t i
( ti , x ( t ) i ) f ( ti , x ( t ) i ) )
a + b = 1 ; bp =
1
1
; bq =
2
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 methods
uses 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.
27
TY=[t y]
TY =
0
0.0001
0.0005
0.0025
0.0125
0.0625
0.1791
0.3485
0.5668
0.8350
1.1574
1.5431
2.0077
2.5790
3.3139
4.3139
5.3139
6.3139
7.3139
8.3139
9.3139
10.0000
0
0.0001
0.0005
0.0025
0.0124
0.0606
0.1640
0.2943
0.4328
0.5663
0.6860
0.7868
0.8664
0.9251
0.9649
0.9883
0.9961
0.9987
0.9996
0.9999
1.0000
1.0000
Figure 3.4: Two-tank series left) same volumes right) different volumes.
Material balances over one chemical component in the two tanks becomes
28
= Out + Acc
dC
qCin = qC1 + V1 1
dt
dC
qC1 = qC2 + V2 2
dt
In
dC1 q
= (C C1 )
dt V1 in
dC2 q
= (C1 C2 )
dt
V2
0
q
C1
d C1 V1
=
q C2 + V1 Cin
dt C2 q
0
V2
V2
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.
function [dc]=twotanks(t,c)
% TWOTANKS is a model of the concentration variation
%
in a tank series of 2 tanks.
% parameters
c0 = 1;
q = 1;
V1 = 0.5;
Vtot = 1;
V2 = Vtot - V1;
% equations
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 the
implicit ode23s takes some more steps and require 20% more flops compared
with the first case. The explicit ode23 needs almost 15 times more flops and
needs 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.
ode23
ode23s
V1 = 0.5
33 steps, 4968 flops
35 steps, 9453 flops
V1 =0.001
3995 steps, 63932 flops
41 steps, 11143 flops
29
14
1.4
1.2
12
0.8
08
0.6
0.6
04
0.4
02
02
0
0
time
10
10
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.
30
Table 3.2: Number of steps and floting point operations during the solution of a two-tank problem
with high accuracy.
ode23
ode45
ode113
default tolerances
33 steps, 6463 flops
81 steps, 13539 flops
46 steps, 13056 flops
halved tolerances
251 steps, 40180 flops
201 steps, 31539 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. This
method 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 that
the 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 formula
and it uses numerical calculated derivatives instead of using previous function calls
direct.)
ode23s
ode15s
2 state system
35 steps, 9453 flops
49 steps, 10701 flops
8 state system
41 steps, 170647 flops
65 steps, 59228 flops
31
dx1
= f ( x1 , x 2 )
dt
0 = g ( x1 , x 2 )
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 equations
as 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.
dx1
= f1 ( x1 , x 2 )
dt
dx 2
0*
= f 2 ( x1 , x 2 )
dt
1*
1 0 dx
dx
0 0 dt = f ( x) M dt = f ( x)
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 is
described in an m-file as usual and the mass matrix is defined in the options using
odeset.
M=[. . .];
options=odeset(Mass,M)
[t,y]=ode15s(@func,[0,1],x0,options);
In = Out + Acc
qC in = qC1
qC1 = qC 2 + V2
0 = q (C in C1 )
dC 2
dC 2
q
(C1 C 2 )
=
dt
dt
V2
2
V2 2
V2
This problem is solved using ode15s as follows. Define the right hand side of the
DAE syetm, i.e., the differential and algebric equations.
function dx=DynTankSeries(t,x)
c1=x(1);
c2=x(2);
cin=1;
q=1;
V2=1;
32
res=q*(cin-c1);
dc2=q/V2*(c1-c2);
dx=[res;dc2];
Then define the mass matrix and call the DAE solver.
M=[0 0;0 1];
options=odeset('Mass',M);
tspan=[0 10];
xinit=[0;0];
[t,x]=ode15s(@DynTankSeries,tspan,xinit,options);
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 second
concentration. 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.7 Summary
33
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-algebraic
equation systems, DAE, but this is not discussed futher in the course.
Table 3.4: Selection of ODE-methods in MATLAB.
Low order
High order
High accuracy
34
Nonstiff
ode23
ode113,
ode45
ode113
Stiff
ode23s
ode23tb
ode15s
ode15s
(ode15s)
(ode15s)
DAE
ode23t
z
y
y
x
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.
35
There is no generation of mass and therefore the total mass balance is as follows
Acc
Out
In
d ( xyz )
= yz ( v x ) x ( v x ) x + x
dt
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 the
definition of derivation. In the simple example with flow only in the x-direction the
total mass balance becomes
d v x x vx
=
x
d t
x + x
(vx )
=
t
x
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.
=
v x +
v y +
vz = ( v )
t
y
z
x
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.
L
z=0
z+dz
36
z=L
The accumulation of mass in the control volume is the time derivative of the
volume integral of the density.
Accumulation of mass =
d
dt
z + dz
( A )d
z
The inlet and outlet of mass is in this case only convection dependent
Inlet of mass = v z A z
Outlet of mass = v z + dz A z + dz
In + Prod = Out
v z A
+ Acc
d
= v z + dz A z + dz +
dt
z + dz
( A ) d
z
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.
d
dt
z + dz
( A )d = vz A z vz + dz A
z + dz = v A
z + dz
z
z + dz
(v A )
If the boundaries, z and z+dz, are time independent then the time derivative in the
accumulation term can be moved inside the integral.
z + dz
d = A
t
z + dz
(v )
d
Let us now remove the integral on the both sides and the result is a partial
differential equation.
(v )
=
t
z
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.
37
dc A
dx
This expression assumes a binary mixture where component A diffuse in a solvent B.
J A = DAB
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.
d
(M Ac A xyz )
dt
The inflow of component A becomes
dc
yz (M A J A )x = yz M A DAB A
dx x
dc
yz (M A J A )x + x = yz M A DAB A
dx x + x
Let us now put these three terms together in a component mass balance of component
A.
d
(M Ac A xyz ) = yz (M A J A )x yz (M A J A )x+ x
dt
Divide all terms with the control volume and the mole weight. Let the volume size go to
zero.
1
dc A
((J A )x (J A )x +x ) c A = J A
=
x
x
t
dt
38
The flux J can be expressed as Fick's first law which results in Fick's second law.
cA
J
c
DAB A
= A =
t
x
x
x
2 cA
= DAB
x2
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.
cA
2 cA
= D AB
t
x2
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.
J ax = Dax
dc A
dx
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.
cL
c0
z=0
z+dz
z=L
Accumulation of mole =
d
dt
z + dz
( Ac )d
dc
d
39
dc
d
+ dzz
z + dz
Production of mole =
(rA) d
z
In
v z Ac z + Dax A
dc
+
d z
= Out
+ Acc
z + dz
(rA) d
= v z + dz Ac z + dz + Dax A
dc
d
+
+ dzz
d
dt
z + dz
( Ac )d
z
z + dz
( Ac )d = vz Ac z Dax A
z
= v Ac
z + dz
c
c
vz + dz Ac z + dz + Dax A
z
z + dz
z + dz
z
c
+ Dax A
z
(v Ac )
d +
z + dz
z + dz
+
+ dzz
(rA) d =
z
z + dz
(rA) d =
z
c
Dax A
z + dz
d + (rA) d
Remove all integrals and we get a partial differential equation with a second order
space derivative term.
c
(vc )
2c
=
+ Dax 2 + r
t
z
z
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.
d
v2
U + xyz
2
dt
40
U represents the internal energy per mass unit. The difference between the convection
of energy in the x-direction is
v2
v2
ec = v x U + z y x v x U + z y x + x
2
2
eq = q x z y x q x z y x + x
Work done by the fluid element against the surroundings consists of work against
volume forces (gravity) and work against surface forces, such as pressure
ew = xyz (v x g x ) + pv x y x x pv x y x x + x
ev = yz ( xx v x ) x yz ( xx v x ) x + x
Put all terms together and taking the limit of the volume that goes to zero gives the
following energy balance
v2
v2 qx
vx U +
pv x )
+ v x g x
U + =
(
( v )
2
2 x
t
x
x
x xx x
This is a general distributed energy balance in on space dimension. Some special cases
are discussed below before the general case in three dimensions is presented.
(U ) = vx (U ) qx ( pvx ) xx vx
x x
x
t
x
Energy balance for convection of liquid
A second special case is the assumption of a liquid fluid with no pressure-volume work
and where the internal energy can be approximated with the enthalpy. Under these
assumptions it is common to assume that velocity gradients can be neglected. These
three assumptions, together with no conduction and constant density, result in the f
following
H
H
= v x
t
x
which is just convection of enthalpy.
41
Conduction
A third case is with no convection and only conduction. Let us assume that the heat
conduction follows the Fourie's law, where the heat flux is proportional to the
temperature gradient.
dT
dx
Using the general energy balance from above it beconmes
q x =
q
H
T
2T
=
= x =
t
x
x
x
x2
Heat equation
For small temperature changes we can assume constant heat capacity, Cp,m, resulting in
the following energy balance expressed in temperature
T
2T
=
t C p ,m x 2
Mathematically this is identical to the diffusion example discussed in the component
mass balance section. This is a parabolic PDE and it is also called the heat equation for
obvious reasons.
v2
v2
U + = v U + q + ( v g) ( pv ) ( v ) : v
2
2
t
d ( HxA)
= A H v x H v x + x + xLs Q
dt
d C pT
dt
C p (T Tr )v x C p (T Tr )v x + x
x
LQ
T
T
= v
+ s
t
x Cp A
42
Ls Q
A
Use the knowledge of constant heat capacity and let the element size go to zero
and we get the last equation with a accumulation term on the left and a negative
convection term and one constant heat source term.
T
T kLs
(Ts T )
= v
+
t
x C p A
d
( xyzvx )
dt
The flux of momentum form convection and stress can be expressed as
f m = mv + = yz v x v x x v x v x
x + x
) + yz(
xx x
xx
x + x
Forces on the control volume is composed of pressure forces and gravity forces
f f = Ap + mg = yz p x p x + x + g x xyz
These terms can be put together to form the distributed momentum balance
d vx vx vx x vx vx
=
x
dt
x + x
xx x xx
x
x + x
p x p x + x
x
+ gx
Once again take the limit of the volume that goes to zero and the one-dimensional
momentum balance becomes
43
( vx )
v x v x xx p
=
+ gx
t
x
x x
In three space dimensions it becomes
( v )
= vv p + g
t
where v and g are vectors. Note also that vv is a dyad (matrix variable) and is a tensor
(matrix variable).
Newtonian viscosity
Assume that the stress is proportional to the velocity gradient. This is called the
Newton's law and fluid is a Newtonian fluid
xx =
vx
x
Navier-Stokes Equation
Assume a Newtonian fluid and constant density, i.e. incompressible fluid. Then the
general momentum balance becomes the famous Navier-Stokes equation. Below it is
derived for the one-dimensional case, i.e. for the x-dimension (no gravity)
vx
v v
2 vx p
= x x +
t
x
x2 x
v
= vv + 2 v p + g
t
44
4.6 Summary
Distributed models are derived from balance equaitons on infinite small fluid elements
resulting in descriptions with spatial behaviour. Equations with special behaviour are
called partial differential equations. Here we focus upon models with one dominating
space dimension. Steady-state assumption results in differential equaitons with
boundary condition, so called bounday value problem, BVP, which are presented in the
next chapter. In chapter six simulations of unsteady-state distributed models are
discussed.
45
equations describe static relations between a set of variables and how these variables
change in space. Properties are often known on the border of the space. For instance in a
tube the inlet conditions are often known. These properties on the border are called
boundary conditions or boundary values. A steady-state distributed system simulation is
also called a boundary value problem, BVP.
A steady-state distributed system in one space dimension, z, can be expressed as (a set
of ODEs)
dx
= f(z,x)
dz
where the space dimension is between a<z<b. The corresponding boundary conditions,
called BCs, are
A x(a) + B x(b) = C
where A, B and C are matrices (if the BCs are linear). The number of boundary
conditions, BCs, is the same as the number of states (derivatives) in the ODEs. A split
boundary value problem means that the BCs are described in separate equations, e.g.,
x(a) and x(b) do not occur in the same equation. For a tube the description of the inlet
conditions are described in one set of BC equations and the condition of the outlet in
other BC equations
Two different types of methods for simulation of steady-state distributed systems are
presented here; the first is the shooting method and the second is the finite difference
method, FDM. The shooting method is limited to ODEs and the FDM can be applied to
both ODEs and PDEs.
Initial-Value Problem
Special cases of BVPs are problems with BCs only at the first boundary. This means
that the solution is found by just integration of the ODEs in space using the BCs as
initial values. The BVP becomes an IVP (initial-value problem).
Example 5.1: Tubular Reactor without Dispersion
In an ideal isothermal tubular reactor a first-order reaction A B take place. The
velocity profile is assumed to be plug flow, that is, vz is constant. A material
balance results in the following ordinary differential equation
dc
v z A = kc A
dz
46
At the inlet the concentration is known to be c0 which means that the boundary
condition is
c A ( z = 0) = c0
Assume a case with v=1 and k = 10. The inlet concentration is c0 = 1 and the
reactor length is L = 0.35.
[dc]=tubex1(t,c)
is an ideal tubular reactor
first-order kinetics
* c;
The reactor profile can now be found with the following commands:
[t,y]=ode23('tubex1',[0 0.35],1);
plot(t,y)
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
0
0.05
0.1
0.15
0.2
0.25
0.3
0.35
reactor length
47
The previous case where BCs are known only at the inlet results in an IVP. This is a
special case of BVP. In the general case we have a mixture of BCs at the beginning and
at the end of the interval. The idea of shooting is very simple and straightforward.
1. Guess boundary conditions at the first border, z=a.
2. Solve the ODEs with an ordinary ODE solver from z=a to z=b.
3. Check the result at z=b with the known BCs. If they are not the same go back to the
first step.
The question is how to make clever guesses in step 1. The procedure 1-3 can be viewed
as a nonlinear problem where a guess results in a residual. The general shooting
technique is just a nonlinear equation problem from this point of view. In each iteration
the equation solver calculates a new guess using the residual. This residual is calculated
by an ODE solver that integrates the ODE between a and b.
Two shooting problems are special. The first is when all boundary conditions are at one
side as discussed above. The second is when the ODE is linear. For linear ODEs
superposition is valid. This means that one can shoot the problem with one BC at the
time and add them together afterwards.
fzero needs many guesses to find this value (more than 20). The profiles form
the different guesses are illustrated in on the left in Figure 5.3.
The problem is also solved by the Newton solver newsol.
c0n = newsol('funcex2',1)
c0n =
1.6664
The Newton solver only needs two guesses, the initial guess and the right guess.
The two profiles are found on the right in Figure 5.3.
48
1.8
1.8
1.6
1.6
1.4
1.4
1.2
1.2
1
1
0.8
0.8
0.6
0.6
0.4
0.4
0.2
0.2
0
0
0.05
0.1
0.15
0.2
0.25
0.3
0.35
0.05
0.1
0.15
0.2
0.25
0.3
0.35
reactor length
reactor length
Figure 5.3: Reactor profiles from shooting methods using left) fzero and right) newsol.
d2y
dy
+ a2 y = b
2 + a1
dz
dz
dy
d 2 y dx1
=
x
=
Introduce
. The second-order
1 . The second-order derivative becomes
dz
dz 2
dz
ODE can now be rewritten as
dy
= x1
dz
dx1
= a1 x1 a2 y + b
dz
Example 5.3: Tubular Reactor with Dispersion
A tubular reactor with dispersion is described in Ramirez, section 7.1. The reactor
has a plug flow velocity profile, axial dispersion and a first order reaction take
place. Dispersion describes the microscopic mixing of small volume elements
caused by small scale turbulent effects. The dispersion term is modelled as a
second order derivative of the concentration gradient and scaled by the dispersion
coefficent, D. A steady-state material balance in the reactor axial space dimension
becomes
v
dc
d 2c
= D 2 kc
dz
dz
49
dc
= v (c cin ) at z = 0
dz
dc
=0
at z = L
dz
To solve the second order ODE it has to be expressed on state-space form as two
first order ODEs. This is done by defining a new state as the derivative of the
concentration.
dc
= x1
dz
dx1 v
k
= x1 + c
dz D
D
v
(c cin ) at z = 0
D
x1 = 0
at z = L
x1 =
The newsol uses funcex3 to find the right derivative, x1, of the concentration
at that inlet that gives a derivative at the outlet that is zero.
x0 = newsol('funcex3', 1)
x0 =
0.6182
50
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
0
0.05
0.1
0.15
0.2
0.25
0.3
0.35
reactor length
51
function [dx]=tubex4(t,x)
% TUBex4b is a dispersion tubular reactor
%
with second-order kinetics in backward coordinates
x1 = x(1,1); c = x(2,1);
v = 1; k = 10; D = 0.1;
dx1= -v/D * x1 - k/D * c^2;
dc = -x1;
dx = [dx1; dc];
The initial condition is x1 = 0 and c N gets a stating value. At the end point (the
inlet, zz = L) the boundary condition must be fulfilled. The residual function
becomes
function [res]=funcex4(cN)
[z,x]=ode23('tubex4',[0 0.35],[0; cN]);
x10 = x(max(size(z)),1);
c0 = x(max(size(z)),2);
res = x10 - 1/0.1*(c0 - 1);
0.75
0.7
0.65
0.6
0.55
0.5
0.45
0.4
0.35
0.3
0
0.05
0.1
0.15
0.2
0.25
0.3
0.35
Here x10 is the value of the derivative at the end point, inlet in this case, and c0
is the concentration at the inlet.
x10 = newsol('funcex4', 0.35)
x10 =
0.3018
The concentration profile for the backward direction of the tubular reactor with
dispersion and second order reaction is seen in Figure 5.5.
52
2h
dz
3. Apply the approximation to the ODE for each mesh point. This results in N
equations. Note that points outside the interval, like x-1 and xN+2, can occur in the
equations.
4. Apply the approximations also on the BC. Derive equations for the points outside the
interval. This is called method of fictitious points.
5. Solve the algebraic equation system. If the ODE and BCs are linear the equation
system also is linear and can be solved by Gaussian elimination. If the ODE and/or
BC are nonlinear the equation system is also nonlinear and requires a nonlinear
solver like a Newton method.
Centered-Difference Approximations
The most common finite difference approxiamtion is the centered-difference method.
Derivatives are approximated by a relation between grid point values around the point
where the derivatives are calculated. The simplest centered-difference method is of
second order. Different finite difference approximations can be derived using Taylor
expansions. First order approximations are based on Euler approximations as (forward
or backward)
dx xi +1 xi
dx xi xi 1
or
dz
h
dz
h
Second order approximation of a first order derivatives can be approximated by the
mean of two simple differences
dx
dz
xi +1 xi xi xi 1
+
x xi + xi xi 1 xi +1 xi 1
h
h
= i +1
=
2
2h
2h
The second order derivative can be approximated by the difference between two simple
derivative approximations as
53
d x
dz 2
dx dx
dz i dz i 1
h
xi +1 xi xi xi 1
x 2 xi + xi 1
h
h
=
= i +1
h
h2
x
L/3
x
L/3
L/3
Figure 5.6: Simple grid of the tubular reactor (2 internal grid points).
2h
dz
dz 2
h2
Step 3, apply the discritization on equation: Replace the derivatives with these
approximations. The ODE
dc
d2x
= D 2 kc
dz
dz
ci +1 ci 1
c 2ci + ci 1
= D i +1
kci
2h
h2
It can be rewritten as
ci +1 2ci + ci 1
c c
v i +1 i 1 kci = 0
2
2h
h
54
0
0 c0
1
0
0
2 1
1 2 1
1 0
0 c1
1
D
v
2
1 2 1 c2 2h 0 1 0
h 0
0
1 2 c3
0 1
0
0
0 c0
1
0 c1
k
0
1 c2
0 c3
0
0 0 0 c0 b0
1 0 0 c1 0
+ = 0
0 1 0 c2 0
0 0 1 c3 b3
Step 4, apply discritization on BC:s: At the boundaries the BCs must be fulfilled.
dc
= v (c cin ) at z = 0
dz
dc
=0
at z = L
dz
2hv
(c c )
D 0 in
c4 = c2
Apply this on matrix form and we get a system of four linear equations
hv
2 1 + D
D
1
h2
0
2hv
0 c0
D
c
2 1
0 1 v 1
c 2 h 0
1 2 1 2
c
0
0
2 2 3
1
0 0 c0
c
0 1 0 1 k 0
0
1 0 1 c2
0
0 0 0 c3
2
0
1
0
0
0
0
1
0
0 c0 d 0
0 c1 0
=
0 c2 0
1 c3 0
D v 2hv
d0 = 2 +
c
h
2h D in
55
Step 5, solve the algebraic system: Now include everything in a linear equation
system, which of course can be solved by Gaussian elimination.
D hv v 2hv
2 h 2 1 + D 2h D k
D v
h 2 2h
D v
h 2 2h
D
2 2 k
h
D v
+
h 2 2h
2
0
D v
h 2 2h
D
2 2 k
h
D
2 2
h
2v v 2
c0 cin
c h D
0
1 =
0
D v c2
h 2 2h c3
2 2 k
h
This matrix is useful in order to create the other ones needed. The matrix below is
the discretization of the second derivative.
A2 = -2*eye(N+2) + eyelow + eyelow'
A2 =
-2
1
0
0
1
-2
1
0
0
1
-2
1
0
0
1
-2
0
0
1
0
0
0
1
0
0
0
0
1
The boundary conditions are just added to the matrices defined above.
A2(1,1) = -2*(1+h*v/D);
A2(1,2) = 2;
A2(N+2,N+1) = 2;
56
A1(1,1) = 2*h*v/D;
A1(1,2) = 0;
A1(N+2,N+1) = 0;
Aode = D/h^2 * A2 - v/2/h * A1 - k * A0
Aode =
-51.8367
11.6327
0
0
14.6939
-24.6939
11.6327
0
0
3.0612
-24.6939
14.6939
0
0
3.0612
-24.6939
The matrix Aode is the final discretization matrix for this problem. The boundary
conditions also result in a b-vector
b = (-2*v/h - v^2/D)*[1;zeros(N+1,1)] * cin
b =
-27.1429
0
0
0
The problem is a linear equation system and it is easy to solve with Gaussian
elimination using the backward division in MATLAB.
c=Aode\b
c =
0.6107
0.3070
0.1561
0.0929
The solution is seen in Figure 5.7 and it is based on 2 internal points and the two
boundary points. A solution based on 100 internal points is also seen in the plot
below.
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
0
0.05
0.1
0.15
0.2
0.25
0.3
0.35
reactor length
Figure 5.7: Tubular reactor with dispersion with N=2 and N=100 internal grid points.
57
The nonlinear equation system can now be solved by a Newton solver, like
newsol. In this case an initial guess is made as a line between 1 and 0.3.
c0 = (1:(0.3-1)/11:0.3)';
c = newsol('eqex6', c0)
c =
0.7068
0.6240
0.5567
0.5012
0.4550
0.4162
0.3836
0.3565
0.3343
0.3172
0.3057
0.3011
The solution is seen in Figure 5.8. Note that we do not have problems with the
unstable ODE and there is no requirement on rewriting the problem.
58
0.75
0.7
0.65
0.6
0.55
0.5
0.45
0.4
0.35
0.3
0
0.05
0.1
0.15
0.2
0.25
0.3
0.35
reactor length
Figure 5.9: Nonlinear tubular reactor solved with finite differences and a Newton solver
(10 internal grid points).
5.4 Summary
Steady-state distributed systems in one space dimension have been solved by two
different methods, namely the shooting method and the finite difference method. Both
methods have advantages and drawbacks. For linear systems the shooting method
solution becomes simple. If the system is nonlinear the choice is not so obvious. If the
ODE is unstable the shooting method requires that the problem have to be rewritten to a
stable one. The advantages with finite difference methods are that they can be used in
two and three space dimensions. It can also be used in the method of lines, MOL, which
is used to solve unsteady-state distributed system.
59
y (t , z)
2 y (t , z)
y (t , z)
=A
+B
+ Cy (t , z )
2
t
z
z
with initial and boundary conditions. It is first order in time and second in space.
Wave-type PDE or hyperbolic PDE can be described as
2 y (t , z)
2 y (t , z )
y (t , z)
=
+B
+ Cy (t , z )
A
2
2
t
z
z
with initial and boundary conditions. It is second order in both time and space.
A special type is the convection-type PDE or first-order hyperbolic PDE, which is firstorder both in time and space.
y (t , z)
y (t , z)
=B
+ Cy (t , z )
t
z
The dominating technique for numerically solving parabolic and hyperbolic PDEs is the
method of lines, MOL. The idea of MOL is to discretize the PDE in space, which results
in a set of ODEs in time. These ODEs are then solved by an ordinary ODE solver
discussed earlier in the course. There are different space discretization methods and
here we are going to use finite difference approximation, which has also been discussed
earlier in the course.
60
The basic steps for a finite difference approximation in space are as follows:
1. Choose a mesh on the interval [a , b] , a = x0 < x1 < x2 <... < x N < x N +1 = b . The
mesh has N internal grid points. It is often convenient to choose a fixed mesh grid,
ba
and that xi +1 = xi + h .
which means that h =
N +1
2. Approximate the derivatives by a difference method. Approximations of
convection and diffusion terms are discussed later.
3. Apply the derivative approximations on the PDE for each mesh point. This results
in N ODEs for the internal mesh points.
4. a) Neumann BC: Apply the approximation on the PDE at the boundary and on the
BC. Derive equations for the points outside the interval. This is called method of
fictitious
points.
b) Dirichlet BC: The value on the boundary is known and no equation is needed.
5. Put up the ODE system of N+Nb ODEs, where N is the number of internal points
and Nb the number of Neumann BCs.
6. Solve the ODEs. It is almost always stiff and requires ode15s and it is also often
sparse.
x xi xi 1
h
z
Diffusion terms
The second order diffusion term can be approximated by the same methods as in steadystate problems. Centered-difference approximation looks like
2 x xi +1 2 xi + xi 1
z2
h2
Note that diffusion terms can be of first-order if the discretization is done in cylindrical
or spherical coordinates. Diffusion term in a sphere is approximated below
1 2 x 2 x 2 x xi +1 2 xi + xi 1 2 xi +1 xi 1
r
=
+
+
r2 r r r2 r r
h2
ih
2h
61
with T = T0 for all t>0 as the boundary condition at the inlet and with T = T(z) at
t=0 as the initial condition.
Ts
Step 1, selection of grid: Assume a uniform fixed grid on the interval [0,L], from
L0
.
the inlet to the outlet. The mesh-size becomes h =
N +1
Step 2, selection of discritization method: The convection term is discritized with
backward difference
T Ti Ti 1
h
z
Step 3, apply on equation: Replace the space derivative with this approximation
and the PDE becomes a set of ODEs
dTi
T Ti 1
4U
= v i
+
(T T )
Cpd s i
dt
h
This means that we have done step 1, 2 and 3 in the procedure presented above.
Step 4, apply on BC: The boundary condition is a known value at the inlet, T0,
which means that step 4 is a direct application of the ODE above.
Step 5, put up the ODE system: The heat exchanger is simulated with the
following parameters:velocity, v = 10; heat transfer, U = 2500; density,
= 3000 ; heat capacity, C p = 4.18 ; tube diameter, d = 0.1; tune length, L = 0.5;
62
% grid
N = 100;
h = L/(N+1);
% convection term discretization
A1 = eye(N+1) - [zeros(1,N+1); eye(N) zeros(N,1)];
% boundary condition
b = [1; zeros(N,1)]*T0;
% PDE
dT = -v/h * A1*T + 4*U/(rho*Cp*D)*(Ts - T) + v/h*b;
0
1
-1
0
0
0
0
0
0
0
0
0
0
1
-1
0
0
0
0
0
0
0
0
0
0
1
-1
0
0
0
0
0
0
0
0
0
0
1
-1
0
0
0
0
0
0
0
0
0
0
1
-1
0
0
0
0
0
0
0
0
0
0
1
-1
0
0
0
0
0
0
0
0
0
0
1
-1
0
0
0
0
0
0
0
0
0
0
1
-1
0
0
0
0
0
0
0
0
0
0
1
-1
0
0
0
0
0
0
0
0
0
0
1
Step 6, solve the ODE system: The function can be simulated by the following
command
[t,y]=ode15s('shex',[0 0.1],25*ones(11,1));
Two solutions with different gridding are seen in the figure below. The one on the
left is based on 10 grid points and the on on the right is based on 100 grid points.
We see that the profiles are similar in the two cases.
Grid point temperature transients (10 internal grid points)
90
100
80
90
80
70
temperature
temperature
70
60
50
60
50
40
40
30
20
30
0.01
0.02
0.03
0.04
0.05
time
0.06
0.07
0.08
0.09
0.1
20
0.01
0.02
0.03
0.04
0.05
time
0.06
0.07
0.08
0.09
0.1
Figure 6.2 - Temperature transients at grid points; left) N = 10, right) N = 100
The temperature profiles at specific times in Figure 6.3 is plotted with the
following command and
plot((1:11),[y(1,:) y(6,:) y(10,:) y(14,:) y(18,:) y(31,:)])
63
80
time = 0.1000
temperature
70
time = 0.0240
60
time = 0.0142
50
time = 0.0072
40
time = 0.0025
30
time = 0
20
6
7
tube lenght
10
11
2 T
T
=
z2
t
with the initial condition T(x,0) = T0 and the boundary conditions
T
= T Tin at z = 0
z
T
=0
z
Tin x
at z = L
In step 1 we define the mesh. The BCs means that the values on the boundaries
are unknown and they become variable. Define an uniform grid of N internal grid
points together with two boundary points, 0 and N+1, and make a centereddifference approximation of the diffusion term.
Step 2 is the definition of the discretization matrix, A2. To apply the discretization
is straightforward in this case (step 3). Note that there are 7 grid points, 5 internal
grid points and one boundary grid point at each side. In the definition below we
first define a help matrix, eyelow.
64
N = 5;
Nn = N + 2;
eyelow = [zeros(1,Nn); eye(Nn-1) zeros(Nn-1,1)]
A2 = -2*eye(Nn) + eyelow + eyelow'
eyelow =
0
1
0
0
0
0
0
A2 =
-2
1
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
1
-2
1
0
0
0
0
0
1
-2
1
0
0
0
0
0
1
-2
1
0
0
0
0
0
1
-2
1
0
0
0
0
0
1
-2
1
0
0
0
0
0
1
-2
T T
T
2h
= T Tin 1 1 = T0 Tin T1 = T1 (T0 Tin )
z
2h
2h
T1 2T0 + T1 ( T0 Tin )
dT0
T 2T0 + T1
dT0
= 1
=
2
2
dt
dt
h
h
h
2T1 2 1 + T0
dT0
2
=
+ Tin
2
dt
h
h
The BC at z = N+1 is a homogenous Neumann and it is discretized as
T
T TN
= 0 N +2
= 0 TN + 2 = TN
z
2h
It is used in the ODE for boundary grid point at z = N+1
dTN +1
T
dT
T 2TN +1 + TN
dT
2T 2T
2TN +1 + TN
= N +2
N +1 = N
N +1 = N 2 N +1
2
2
dt
dt
dt
h
h
h
In step 5 we put together the boundary condition equations and the discretization
matrix.
L = 0.5;
alpha = 1e-3;
Tin = 25;
65
h = L/(N+1);
A2(1,1) = -2*(1 + h/alpha);
A2(1,2) = 2;
A2(Nn,Nn-1) = 2
b = 2/h*[1; zeros(Nn-1,1)]*Tin
A2 =
-168.6667 2.0000
1.0000 -2.0000
0
1.0000
0
0
0
0
0
0
0
0
0
1.0000
-2.0000
1.0000
0
0
0
0
0
0
0
1.0000
0
-2.0000 1.0000
1.0000 -2.0000
0
1.0000
0
0
0
0
0
0
1.0000
-2.0000
2.0000
0
0
0
0
0
1.0000
-2.0000
b =
600
0
0
0
0
0
0
The ODE system is described in the hotrod MATLAB function below. It is now
easy to recognize the different statements.
function [dT]=hotrod(t,T)
% HOTROD is a model of heat conduction in a
%
one dimensional rod. MOL with centered-difference.
% parameters
L = 0.5; alpha = 1e-3; Tin = 50;
% gridding
N = 5;
Nn = N + 2;
h = L/(N+1);
% discretization
eyelow = [zeros(1,Nn); eye(Nn-1) zeros(Nn-1,1)];
A2 = -2*eye(Nn) + eyelow + eyelow';
% boundary conditions
A2(1,1) = -2*(1 + h/alpha);
A2(1,2) = 2;
A2(Nn,Nn-1) = 2;
b = 2/h*[1; zeros(Nn-1,1)]*Tin;
% ODE system
dT = alpha/h^2*A2*T + b;
The rod can now be solved by ode15s and plotted by the commands below.
66
Te m p e r a tu re s in th e R o d (5 in te rn a l g r id p o in ts )
50
temperature
45
40
35
30
25
0
50
100
tim e
150
200
Figure6.5: Temperature transients in hot rod (5 internal and 2 boundary grid points).
The temperature transients in the different grid points are seen in Figure 6.5. Note
that the first grid point is very fast compared to the rest. The last grid point is also
quite fast because it follows the second last grid point closely.
[t,y]=ode15s('hotrod',[0 200],25*ones(7,1));
plot(t,y)
A 3-D plot of the transients in time and profiles in space is seen in Figure 6.6
below. It is easy to make plots like this in MATLAB with the mesh and surf
comands.
Transient temperature profiles in the Rod (20 grid points)
50
temperature
45
40
35
30
200
25
0
150
5
100
10
15
50
20
25
time
rod length
Figure 6.6: 3-D plot of the temperature profile transients in the hot rod example
67
c 1 2 T
r D
=
t r2 r
r
with the initial condition c(r,0) = c0 and the boundary conditions
c
=0
at r = 0
r
c
= k (c cb ) at r = R
D
r
The component balance above can be rewritten in the following way.
2 c 2 c
c 1 2 c 1 2 2 c
c
r D = 2 D r
2+
= 2
D
+
r
=
2
r r
t r r
r r r2
r
r
r r
h2
ih
2h
r
ci
c 2ci + ci 1 2 ci +1 ci 1
= D i +1
+
t
2h
h2
ih
The boundary condition at r = R can be discretized as follows
D
2hk
cN +2 cN
= k (cN +1 cb ) cN + 2 = c N +
(c c )
2h
D N +1 b
This results in an expression for the fictuous point N+2 as a function of the point
on the boundary N+1 and an internal point N. The PDE has a singularity at r = 0.
The equation must be rewritten for this grid point. It is a direct application of
l'Hospitals rule, see Persson and Biers "Analys i en variabel" page 392. When r
goes to zero the expression becomes a second order term.
c
lim 2 c
r r
2 c
=2 2
=2
r0 r r
r
(r)
r
then the PDE becomes for the grid point in the centre
68
c0
c 2c0 + c1
c 2c0 + c1
= D 1
+2 1
t
h
h2
The boundary condition at zero gives the following relation for the fictitious point
1
c
c c
= 0 1 1 = 0 c1 = c1
r
2h
This completes the MOL approximation of diffusion in a particle. The
approximation together with the boundary conditions is as follows, where the grid
point 0 is the value at the origin and N+1 is the value on the surface.
dc0
c 2c0 + c1
= D 3 1
dt
h2
dci
c 2ci + ci 1 2 ci +1 ci 1
= D i +1
+
dt
ih
2h
h2
d c N +1
dt
2hk
2hk
(c N +1 cb ) 2c N +1 + c N
(c N +1 cb ) c N
cN +
cN +
2
D
D
= D
+
2
R
2h
h
6.4 Summary
Unsteady-state distributed systems have been solved by the method of lines, MOL. It is
based on finite difference approximations in the space dimension. This is the most
common way to solve parabolic PDEs in one space dimension. The PDE is discretized
in space by the user and it results in a set of ODEs. This ODE system is solved by an
ordinary ode-solver like ode15s.
69
70
view the 3D structure of polymers and biomolecules. This class of tools will not be
discussed future in this course.
7.2 Flowsheeting
The major chemical engineering tools are called flowsheeting programs. The first
flowsheeting programs appeared all ready in the late fifties and were FORTRAN
subroutines that calculated mass and energy balances over different kinds of unit
operations. Flowsheeting programs of today contain a large set of process equipment,
physical property descriptions and physical parameter databases. The actual simulation
code is a small fraction of the programs and sometimes these flowsheeting programs are
used as databases for simulations in other software.
By tradition flowsheeting programs solve steady-state lumped systems and a common
definition of flowsheeting is
"The use of computer aids to perform steady-state heat and material balancing,
and sizing and costing calculations for a chemical process".
This definition is changing and the dominating flowsheeting programs today can
include dynamics and sometimes also distributed parameter systems, i.e. PDEs. There
are also special flowsheeting programs that can perform process synthesis, i.e. optimal
process design.
Steady-State Flowsheeting
All programs that are called flowsheeting programs can solve steady-state material and
energy balances. Most flowsheeting programs contain a library of modules that solves
different unit operations. It also contains a library of different thermodynamic models
together with a database for physical properties of common chemical components. The
user needs to specify data for the different unit operation modules, chemical
components in the process, and how the modules are connected, a so-called flowsheet.
The solver can now solve the problem, by calling different libraries of unit operation
modules and thermodynamic models. The mathematical models for the process
equipment are predefined in the programming code of the modules. The principle
structure is found in Figure 7.1 below.
User Defined
Data
Solver
Unit
Operation
Modules
Physical and
Thermodynamic
Models and Data
Reactor
Enthalpy
Dest.
kolonn
Density
Mixer
Vapour
Pressure
VLE
...
...
Heater
Output
Data
user
= defined
routines
71
72
ASPEN PLUS
is a product from ASPEN Tech. This is one of the most used and
advanced flowsheeting programs. There are a lot of add-on modules
for special purposes and it can handle solids and electrolytes. It is
possible to add on user defined unit operations and user-defined
components and their physical data. It is used in research at the
department of Chemical Engineering I. ASPEN is quite expensive.
http://www.aspentec.com/
CHEMCAD III
PRO/II
Dynamic Flowsheeting
By tradition flowsheeting programs handle steady-state problems which means that
dynamic simulation is quite new in these programs. One of the first dynamic simulation
tools was SPEEDUP, developed at Imperial Collage, London, and this is now a product
at ASPEN Tech. The dynamic simulation problems are solved using general ODE
solvers. In general there are explicit methods, like Euler and Runge-Kutta methods, and
there are implicit solves, like Gears method.
73
Below follows a list of some commercial dynamic flowsheeting programs. The list is
not complete. HYSYS is used in our under graduate courses and ASPEN Dynamics is
used in our research projects.
ASPEN Dynamics
gPROMS
HYSYS Plant
http://www.psenterprise.com/
http://www.hyprotech.com/
SPEEDUP
Process Synthesis
In some special process design situations is possible to make simultaneous process
optimisation and process design. This is sometimes called process synthesis. This
means that the structure of the flowsheet is manipulated through the optimisation
procedure. One such situation is the optimal design of a heat recovery plant section of
connected heat exchangers. It is possible to find simultaneously the structure of
connected heat exchanges together with the optimal sizing and operation conditions.
This is called pinch technology. One such program is Super Target from Linnhoff
March, which is used at the department.
Other synthesis situations under development are sequences of distillation columns,
particular in petrochemical industry and optimal structure of tubular and tank reactors in
series and parallel.
ASPEN
One of the dominating flowsheeting programs on the market today is ASPEN. It is a
family of different modules build around the steady state solver ASPEN PLUS. It was
first developed at MIT, Cambrige, USA, in the seventies. ASPEN has a large database
and can simulate many unit operations under different conditions. The interface to an
ASPEN PLUS simulation is seen in Figure 2. A resent developed module is the ASPEN
Dynamics. It is possible to convert an ASPEN PLUS simulation into an ASPEN
Dynamics problem and simulate the dynamic behaviour. The interface to ASPEN
Dynamics together with the corresponding simulation to the previous figure is
illustrated in Figure 3.
74
Figure 7.2: The interface to ASPEN PLUS. In this case a simulation problem with two distillation
columns.
Figure 7.3: The interface to ASPEN Dynamics. Two distillation column problem with control system.
75
Relaxation methods are iteration methods for linear equations. These are good for
very large equation systems.
Multigrid technique solves first the system with a few finite volumes (or elements).
This solution is used as the initial guess of a finer grid and so on.
Gaussian elimination, sparse or not, solves the equation system exactly and calculates
every variable. These iteration methods give an approximated solution and for large
problems the iteration converge quite rapidly compared with an exact solution of every
variable.
Dynamic CFD problems
Discretized partial differential equation systems become large and stiff. For cases like
this ODE solvers based on implicit techniques are used. There are often different
versions of the Gear methods. They can be modified with iteration method of the linear
equation solver inside the Newton algorithm.
76
CFD programs
Below follows a list of some commercial CFD programs. The list is not complete.
CFX
FIDAP
Flow-3D
FLUENT
FEMLAB
FIDAP
One CFD program is Fidap and it is based on the finite element method. In CFD
problems it is important to define the geometry in 2 or 3 dimensions. This requires good
graphical tools and a high-level geometry language to facilitate the problem definition.
When the geometry is specified the mesh of grid points is defined. It often requires
some physical and numerical insight to define an efficient grid. Some advanced CFD
programs make automatic grid generation. In Figure 4 the geometry and mesh of a flow
problem is illustrated. The interface to Fidap is illustrated in Figure 5. The solution of
the flow problem in Figure 4 is seen in a colourful plot. The actual flow problem is
defined on a user oriented description language. The user fills in a form with different
check button for the problem specification.
77
78
streams, work independent and the total solution is build up gradually. There is
no sequential calculation order that the solver must follow.
HYSYS Environments
The HYSYS environments allow you to access and input information in a certain area
or "environment" of the program, while the other areas are put on "hold" until you are
finished working in the area of interest. The relations between the environments are
seen in Figure 6. The five environments are:
Basis Definition and modification of the Fluid Packages is done in Basis. The
Fluid Packages are definitions of the chemical components present in the
simulation and selection of proper thermodynamic property package.
Oil Characterisation Definitions of assays and blends of petroleum fluids.
Optional.
Main Flowsheet Definition of the topology of the main simulation flowsheet.
The main flowsheet, called PFD (process flowsheet diagram), contains unit
operation objects and can also contain sub-flowsheets and columns.
Sub-Flowsheet A part of the PFD can be a sub-flowsheet, connected to the main
flowsheet through streams.
Column Special sub-flowsheets for column descriptions.
The user has to define at least a Basis environment in a form of a Fluid Package. After
that the user has to define the flowsheet in the PFD in the Main Flowsheet environment.
79
Basis
Environment
Column
Environment
Main Flowsheet
Environment
Oil
Envirnment
Sub-Flowsheet
Environment
7.5 Summary
Computer tools for modelling, analysis and design for chemical process engineering are
vital for solving today's industrial needs. Flowsheeting programs, that can be used to
describe and solve process configurations on the unit operation level, are special
computer tool developed in process industry. In the course we are using HYSYS as an
example of a flowsheeting tool. More detailed studies on microscopic level require the
use of tool from computational fluid dynamics, CFD. These tools are becoming to be
common in process engineering in the near future.
80
8 Parameter Estimation
8.1 Introduction
Development and numerical solutions of models have been discussed in great detail in
the previous chapters. Before we can do anything with our models we have to validate
the models against data from the real system. Here we will discuss estimation of
parameters in models derived from first principle, i.e. in the way presented previously.
There are other ways of parameter estimation like black-box identification, where the
structure of the model is unimportant as long as the model predicts the behaviour.
Let us assume that we have a set of experimental responses, y, measured at a set of
conditions, x. We also assume that there is a relationship, f, between y and x, which is
parameterised by a set of parameters, . The measurement is not perfect so it has a
measurement error .
y = f ( , x) +
Regression is the procedure, which finds the unknown parameter vector, , which
minimizes . If the relationship between y and is linear then the procedure is called
linear regression and the problem becomes a linear equation system. If the relationship
is nonlinear in the parameters the procedure to find becomes iterative and this
procedure is called nonlinear regression. Below we will also discuss the statistical
analysis of the resulted regression.
This means that there are two parameters, k and l, in the relationship (slope and
intercept). Assume that we have N number of measurements. Put up all measurements
in a matrix representation as indicated below.
1
x1
y1
x
y
2 = k 2 + l + 2 =
...
...
...
N
xN
yN
1
x1 1
x 1 k
2 + 2
... ... l ...
N
xN 1
Y = X +
81
Under certain conditions, is normalised distributed with zero mean, the optimal
regression is performed by minimization the square error of the differences between
measurements and model responses, the residual R.
Q = ri =R T R where
R = Y Y = Y X
The Q is minimised when this derivative is zero. The parameter derivative of R is just XT .
Q
R
=2
R=0
2 X T Y X = 0
Multiply the last expression with XT and derive an expression for the parameter vector
.
X T Y X T X = 0
X T X = X T Y
This last expression is the linear regression based on least square. Note that in linear
regression the linearity is in the parameters and not in the x and y relationship, which is
the meaning of linearity in the previous chapters.
= (X T X ) X T Y
1
x
0
1
2
3
4
5
6
y
1.1832
1.9539
2.6738
3.2787
3.9681
4.8931
5.5011
6
5.5
5
4.5
4
3.5
3
2.5
2
1.5
1
82
XTy
The data in table 8.1 can now be put into the matrix formulation and the estimated
parameters becomes, k = 0.7188 and l = 1.1939. The MATLAB commands are as
follows
X=[x 1];
b=(X'*X)\(X'*y);
Polynomial estimation
Traditional polynomials are linear in the parameters and can directly be represented as
y = an x n + an 1 x n 1 + ... + a1 x + a0
Measurements can now be put into matrix form where the x and powers of x forms the
X-matrix
x1n
n
x2
n 1
n
yi = an xi + an 1 xi + ... + a1 xi + a0 + i = ...
n
x N 1
xn
N
n 1
x1
n 1
x2
...
...
x1
x2
...
... ...
n 1
xN 1 ... x N 1
n 1
xN
... x N
1 an 1
1 an 1 2
... ... + ...
1 a1 N 1
1 a0 N
X=[x.^2 x 1];
b=(X'*X)\(X'*y);
83
Another useful method is polyval for the evaluation of the polynomial at specific
points. In Statistic toolbox there is a graphical interface to these functions called
polytool. Try it!
1.2166
y = k1e k2 x ln y = ln k1 xk 2
y = k1 x k2
1
1 1 k 1
1 1 k1
kx
= + 2
= 1
y= 1
k2 + x
y k1 k1 x
y x k 2
k1
As we see becomes all these nonlinear expressions just simple linear regression
problems after transformation. The drawback is that the transformation also transforms
the measurement error. This means that the error is unevenly weight in the actual
regression making that result slightly disturbed.
q=
84
qmax c
Kd + c
1
K
c
= d +
q qmax c qmax c
Kd
1 1 qmax
=
1
q c 1
qmax
c
0.0203
0.0498
0.3106
0.5325
0.7612
0.9304
1.1123
1.4304
1.7798
2.2330
90
q
9.1898
27.9678
37.4374
47.9383
57.7252
69.4346
80.2992
85.3483
88.5122
86.8252
80
70
60
50
40
30
20
10
0
0.5
1.5
2.5
Kd
q 0.0123
q 81.1
max =
max =
1 0.0019
K d 0.150
qmax
f ( X , i +1 ) f ( X , i ) +
f ( X , )
i +1 i
The partial derivative is the parameter Jacobian, which we denote J. Assume that we
still would like to minimize the least square of the residuals we get the following.
Q
R
R
=2
R=0 2
R = 0 2 J T Y f ( X , ) = 0
If we use both these expressions by putting the linearization into the least square
expression we get
))
)
2 J iT Y f ( X , i ) J i i +1 i = 0
J T J = J T Y f ( X , )
i
i +1
i +1 = i + (J J i ) J Y f ( X , i )
T
i
T
i
85
A more convenient expression is to recognize that the last term is the residual with the
i:th parameter guess and only express the parameter step, .
i = J iT J i
J iT Ri
The similarity with linear regression is obvious. The Jacobian corresponds to the Xmatrix and the residual vector to the Y-vector. The nonlinear regression procedure
begins with an initial guess of the parameter vector. This makes it possible to calculate
the model response and the corresponding residual. It is also possible to calculate the
Jacobian, which makes it possible to calculate a parameter step. The new parameter
vector can now be used in the procedure until the parameter step is small and the
iteration can be stopped. This is called the Gauss-Newton method. In other words the
Gauss-Newton method solves a linear regression problem in each iteration step.
nonlinreg
We have developed a simple MATLAB function that performs a nonlinear regression. It
is called nonlinreg and has four input arguments, the independent x vector,
measurement vector y, the model function name and the initial parameter guess, b0.
b=nonlinreg(x,y,'model',b0)
The model must have the following input arguments, ym=model(b,x).
b=nonlinreg(c,q,'Langmuir',[90; 0.5])
The result becomes qmax = 115.2695 and Kd = 0.6055. The prediction of the
estimated model is seen in the figure below, Figure 8.3.
86
100
90
80
70
60
50
40
30
20
10
0
0.5
1.5
2.5
[]
E =
and
[]
1
Var = 2 (X T X )
If the regression is nonlinear then X can be replaced by J, the Jacobian, in the last
expression. The last expression is the covariance matrix of the parameter estimation.
The variance is estimated using the sum of squares of the residuals
2 =
Q
N P
and N is the number of measurements and P the number of parameters that is estimated.
The difference, N-P, is called the degree of freedom.
linreg
To facilitate the use of regression in the course there is a M-file, called linreg, which
makes a linear regression and computes some regression properties. The function has
only two input arguments, the X-matrix and the measurement vector, y. It returns the
parameter vector b.
b = linreg(X,y)
87
Additional outputs are the estimated variance, s2, and the covariance matrix, cov.
[b,s2,cov] = linreg(X,y)
nonlinreg
This function for nonlinear regression, presented earlier, also has additional input and
output arguments. The complete function call is as follows.
[b,r,J,s2,cov] = nonlinreg(x,y,'model',b0,nlopt)
There is an additional input argument, nlopt, which makes it possible to change the
method parameters, like the parameter error tolerance and maximal number of
iterations. For more detail read the help file. The new output arguments are the resulting
residual, r, the parameter Jacobian, J, estimated variance, s2, and the parameter
covariance matrix, cov.
si = 2 X T X
ii
i s Tinv 1
i
, N P
2
0.35
0.3
0.25
0.2
0.15
95 %
0.1
0.05
0
-3
-2
-1
Figure 8.4: The Student T distribution for three different degrees of freedom.
The confidence interval discussed above is only for valid for one individual parameter.
A better measure is the joint 1- marginal confidence region. This gives an estimation
of the simultaneous variation of the parameter. The simultaneous parameter distribution
is F distributed. The joint variation has the following 1- marginal confidence region
88
2 2 1 +
Finv ( , P, N P )
N P
Finv is the inverse of the F cumulative distribution. In the general nonlinear case this is
hard to compute. For the linear case it is possible to compute the joint variation as an
ellipsoid.
( ) X X ( ) P F
T
inv
( , P, N P )
and the correlation and joint confidence region for the parameters specified in jpc.
[bci,corr,bjcr]=confpar(XorJ,y,b,s2,cov,alpha,jpc)
The second command, confparplot, uses confpar and makes a plot with the
individual confidence intervals as a square and the joint confidence region as an
ellipsoid. Note that they can be used for both linreg and nonlinreg results.
Example 8.6: Confidence Analysis of Linear Regression
Assume four different data sets similar to the linear example in Example 8.1. All
four data sets are generated from the theoretical model y = 0.7 x + 1.3. The data
sets differ in the amplitude of the normal distributed error, 0, 0.05, 0.1 and 0.2.
The parameter estimation with prediction is seen in Figure 8.5. We see that the
estimation becomes less accurate when the error increases. The parameter
confidence interval and the simultaneous joint parameter region for the four cases
are seen in Figure 8.6. We see that the interval is increasing with increasing
x p x p s xTp ( X T X ) 1 x p Tinv 1 , N P
2
89
6
1.3
0.7
5
4
6
1.28
0.719
1.26
0.738
1.29
0.71
Figure 8.5: Four different data sets with different errors and corresponding
parameter estimates. See Example 8.6.
1
0.9
0.9
0.8
0.8
0.7
0.7
0.6
0.6
0.5
0.5
1.5
0.5
0.5
0.9
0.9
0.8
0.8
0.7
0.7
0.6
0.6
0.5
0.5
1.5
0.5
0.5
1.5
1.5
Figure 8.6: Parameter confidence intervals, the squares, and joint parameter
confidence region, the ellipsoid. See Example 8.6
Here s is the standard error, xp is the independent variable at the prediction. In the
nonlinear case X can be replaced by J. A more interesting result is the confidence band
for the response function at any x. Note that the joint distribution is F distributed.
90
y x s xT ( X T X ) 1 x PFinv ( , P, N P )
In KATtools there is a confpred function calculating the confidence band of a
prediction. There is also an additional function, confpredplot, for plotting of the
prediction and of the confidence band. The function call looks as follows
ypredci=confpred(XorJ,y,b,s2,ypred,xpred,alpha)
ypred and xpred are the dependent and independent variables of the prediction. The
output variable, ypredci, is a vector with three columns with the lower and upper
confidence bands. Additional input arguments make it possible to calculate the
confidence interval for a specific point (points).
[ypredci,ypointci]=confpred(XorJ,y,b,s2,ypred,xpred,
alpha,ypoint,xpoint)
Example 8.7: Confidence band for a linear regression
The confidence band of the prediction of the models estimated in Example 8.6 is
seen in Figure 8.7. We see that the confidence band becomes bigger when the
measurement error increases.
6
4
4
3
2
2
1
Figure 8.7: Prediction confidence regions for Example 8.7. Note different scales.
91
100
80
60
40
20
0.5
1.5
2.5
Figure 8.8: Prediction with 95% confidence band on nonlinear estimation from Example 8.5
1.4
1.2
1
0.8
0.6
0.4
0.2
0
Kd
-0.2
70
80
90
100
110
120
130
140
150
160
qmax
Figure 8.9: Parameter confidence interval and joint parameter confidence region for the
nonlinear estimation from Example 8.5
8.5 Summary
Modelling and simulation relay strongly on its validity to the real process it tries to
predict. If the model is badly validated to data the study has low changes to be a
success. The parameter estimation and model validation is important. This is composed
of three different tasks:
1. Numerical methods that do the parameter estimation,
2. Statistical theory that gives insight on the quality of the estimation
3. Model validation that help you to select a proper model.
92
9 Index
A
Adams methods ...............................................30
Adams-Bashforth methods .........................30
Adams-Bashforth-Moulton methods ..........30
algebraic equations..........................................13
ASPEN ...........................................................75
ASPEN Dynamics..........................................74
ASPEN PLUS ................................................73
B
backslash notation...........................................14
backward Euler approximation .......................61
Backward Shooting .........................................51
BDF-methods ..................................................31
bisection method .............................................16
boundary conditions........................................46
Boundary Conditions ......................................44
boundary values ..............................................46
C
CAD................................................................70
CAPE ..............................................................70
centered-difference approximation .................53
Centered-Difference Approximations .............53
CFD ..........................................................70, 77
CFX ................................................................78
CHEMCAD III..............................................73
Component Mass Balance.................................6
Component Mass Balances .............................38
Computational fluid dynamics ........................77
Computer Aided Engineering .........................70
condition number ......................................15, 16
Conduction.....................................................42
confpar........................................................90
confpred .....................................................92
Conservation .....................................................4
convective type PDE .......................................36
covariance matrix ............................................89
D
DAE ................................................................32
DAE solvers ....................................................33
derivative of a product ......................................6
difference approximation
centered ....................................53, 60, 61, 64
differential-algebraic equation system ............32
Diffusion .........................................................37
Diffusion-type PDE .........................................60
direct substitution......................................17, 73
Dirichlet ..........................................................45
Dispersion ................................................39, 49
distributed parameter models .........................35
divergence .......................................................36
E
Energy Balance ...............................................10
93
MOL ............................................................... 60
Momentum Balances ................................ 11, 43
Multigrid technique ........................................ 77
Multi-Step Methods........................................ 30
N
nabla operator ................................................. 36
Navier-Stokes Equation ............................... 44
newsol..................................... 19, 21, 48, 50, 58
Newton method ............................................... 18
convergens ................................................. 19
multi dimensional....................................... 19
Newtonian viscosity ...................................... 44
nonlinear regression....................................... 82
Nonlinear Regression ..................................... 86
nonlinear solvers............................................. 16
nonlinreg............................................. 87, 89
O
ode113 ............................................................ 30
ode15s............................................................. 31
ode23 ............................................................ 28
ode23s.......................................................... 28
ode23t.......................................................... 33
ode23tb ....................................................... 33
ODEs .............................................................. 22
ordinary differential equation ........................ 47
ordinary differential equations........................ 22
P
parabolic PDE.......................................... 39, 60
Parameter confidence regions ..................... 89
Parameter Estimation...................................... 82
partial differential equation............................ 36
partial differential equations .......................... 60
PDE ................................................................ 36
pinch technology............................................. 74
pivots............................................................... 13
polyfit ....................................................... 84
Polynomial fit ................................................. 84
polytool ..................................................... 85
polyval ....................................................... 85
Prediction confidence regions...................... 90
predictor-corrector methods .......................... 30
PRO/II ........................................................... 74
process synthesis ............................................ 74
R
rank ................................................................. 16
rcond ............................................................ 15
Regression ...................................................... 82
94