Vous êtes sur la page 1sur 94

Table of Contents

TABLE OF CONTENTS........................................................................................................................... 1
PREFACE................................................................................................................................................... 3
1

DEVELOPMENT OF LUMPED MODELS.................................................................................. 4


1.1
1.2
1.3
1.4
1.5
1.6

STEADY-STATE SIMULATION OF LUMPED MODELS ..................................................... 13


2.1
2.2
2.3
2.4
2.5

INTRODUCTION ........................................................................................................................ 46
SHOOTING METHOD ................................................................................................................. 46
FINITE DIFFERENCE METHOD .................................................................................................. 53
SUMMARY ................................................................................................................................ 59

DYNAMIC SIMULATION OF DISTRIBUTED SYSTEMS..................................................... 60


6.1
6.2
6.3
6.4

INTRODUCTION ........................................................................................................................ 35
MASS BALANCES ..................................................................................................................... 35
ENERGY BALANCES ................................................................................................................. 40
MOMENTUM BALANCES........................................................................................................... 43
BOUNDARY CONDITIONS ......................................................................................................... 44
SUMMARY ................................................................................................................................ 45

STEADY-STATE SIMULATION OF DISTRIBUTED SYSTEM ............................................ 46


5.1
5.2
5.3
5.4

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

DEVELOPMENT OF DISTRIBUTED MODELS...................................................................... 35


4.1
4.2
4.3
4.4
4.5
4.6

INTRODUCTION ........................................................................................................................ 13
METHOD FOR LINEAR EQUATIONS ........................................................................................... 13
METHODS FOR NONLINEAR EQUATIONS .................................................................................. 16
NEWTON METHODS ................................................................................................................. 18
SUMMARY ................................................................................................................................ 21

DYNAMIC SIMULATION OF LUMPED MODELS ................................................................ 22


3.1
3.2
3.3
3.4
3.5
3.6
3.7

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

COMPUTER AIDED PROCESS ENGINEERING.................................................................... 70


7.1
7.2
7.3
7.4
7.5

INTRODUCTION ........................................................................................................................ 70
FLOWSHEETING........................................................................................................................ 71
COMPUTATIONAL FLUID DYNAMICS ........................................................................................ 76
HYSYS A FLOWSHEETING PROGRAM .................................................................................... 79
SUMMARY ................................................................................................................................ 80

PARAMETER ESTIMATION ......................................................................................................81


8.1
8.2
8.3
8.4
8.5

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

These sources are also available on the computer network.

Lund, January 10th, 2005.


Bernt Nilsson

1 Development of Lumped Models


1.1 Introduction
The basic properties in process systems are the conservation of mass, energy and
momentum. In process modelling this is formulised into the development of
mathematical expressions representing balance equations of mass, energy and
momentum. Conservation of a property follows the general balance expression below
In + Generation = Out + Consumption + Accumulation
Sometimes the generation and consumption terms are expressed as only one production
term as
In + Production = Out + Accumulation
This principle is illustrated in Figure 1.1 below. The definition of the system boundary
is important in order to develop the different terms in the balance equations. The system
boundary can be over a macroscopic object or over a microscopic fluid element.
mass
energy

SYSTEM

momentum

In +
=
+ Out
Production
Accumulation

energy
mass

momentum

Figure 1.1: Illustration of the conservation principle.

The most convenient way of developing models is to ignore the spatial aspects of the
system that is going to be modelled. This means that all spatial properties are 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.

1.2 Mass Balances


Mass balances over a system can express the total mass in the system. A mass balance
can also be developed for a specific chemical component, a component mass balance or
just component balance. The only difference is that in chemical applications there is
never production of total mass while in reacting systems the component mass balance
can have production terms. In nonreacting systems there is no principle difference
between total and component mass balances.
Example 1.1: Mixing Tank with two inlets
A tank has two inflows and one outflow and is seen in Figure 1.2. In to the tank
flows pure water from one inlet and pure ethanol from the other inlet. Out from
the mixing tank flows a mixture of the two components, the solution.
Model assumptions:

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

Figure 1.2: A mixing tank with two inlets.

The mass balances can often be rewritten into other variables. Below follows two
examples of such transformations.
Example 1.2: Mixing Tank with liquid volumetric inflow
Assume that the mixing tank in Example 1.1, which is seen in Figure 1.2, has
volumetric inflows and outflow.
Additional model assumption:
The density in the different fluids is . Assume that the density in the solution
is constant (practical assumption in this case).
The mass balance can now be rewritten into an equation expressing the change of
tank volume.
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

Component Mass Balances


It is possible to put up component balances, one for each chemical component. Note
that all component mass balances sum to the total mass balance. In order to set up a
consistent equation system it is recommended to take away one mass balance, often one
component mass balance, for instance the solvent balance. Sometimes this knowledge
can be used as redundant information to check the correctness of the model.
Example 1.4: Mixing Tank with component mass balances
Component mass balances over the mixing tank in Example 1.1, which is seen in
Figure 1.2 becomes as follows
Additional model assumptions:
The inflows have pure chemical components. The solution has the mass
fraction of ethanol expressed as . This means that the mass fraction of water
is w=(1-) because the sum of the fractions must be equal to 1.

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

to form a composition equation

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

Figure 1.3: A Mixing Tank

1.3 Systems of Mass Balances


Models of process systems with sets of subsystems generate equation systems of mass
balances. This means that the equation set is coupled and the equations must be solved
simultaneously.
Example 1.7: Two Tanks in Series
Two well-mixed tanks are connected in series. They have constant but different
volumes. They are seen in the figure below.
Model assumptions:
1. Constant volumes and the same volumetric flow through the tank series.
2. Well-mixed tanks, which means that the outlet concentration is the same as
the tank concentration.

Figure 1.4: Two-tank series.

Material balances over one chemical component, expressed in moles, in the two
tanks becomes
In = Out + Acc

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

Example 1.8: Six-Plate Absorption Column


An absorption column with six plates is used to absorb a chemical component in a
vapour into a liquid. The liquid, L, enters at the top and the vapour, V, enters at
the bottom. It is assumed that the liquid and vapour flow rates do not change
through the column.
L, x0

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

A steady-state component material balance over a tray gives


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)

1.4 Energy Balances


In process systems chemical components are transformed into other chemicals under
energy changes. Energy is released or consumed in the reaction step and it is often
consumed in the separation step. Energy balances are important parts of the process
model in analysis and design. Total energy can be decomposed into different energy
forms, similar to the decomposition of total mass into component masses. The most
common decomposition of total energy is into thermal energy and mechanical energy.
There are other energy forms like electrical and magnetically energy.
A general total energy balance can be written as follows
+ Prod

In

= Out

( in (U in + K in + Pin ) + Qin ) + QP + W + W pV = ( out (U out + K 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

( inU in + Qin ) + Q P W W pV = ( outU 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

Example 1.9: Mixing Tank with one inlet


A tank with constant volume has one inflow with a volumetric flow rate, q, and an
inflow temperature, Tin. Develop a mathematical expression for the enthalpy in
the tank. Model assumptions are constant volume in the tank and ideal mixing.

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

q in in hin = q out out hout +


qhin

= qh

q
Tin

dh q
= (hin h )
dt V

Tout

Figure 1.6: A Mixing Tank

Assume also that the enthalpy can be expressed as proportional to the temperature
difference from reference temperature as
H = C p ,m T

The enthalpy balance can be rewritten into a temperature expression


d (C p , m (T T0 ))

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.

1.5 Momentum Balances


The conservation of momentum is not so important in lumped model development of
process systems. The reason is that it is unusual to have moving lumped models.
Dynamic momentum balances also occur in mechanics. Steady-state momentum
balances are also called force balances.
In

+ 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

much more important in distributed models and is the fundamental issue in


Computational Fluid Dynamics, which will be discussed more in a later chapter.

1.6 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

2 Steady-State Simulation of Lumped Models


2.1 Introduction
A steady-state lumped model results in a set of algebraic equations. These algebraic equations

describe static relations between a set of variables. The equation system can be solved if
the number of equations is the same as the number of variables. For 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.

2.2 Method for Linear Equations


The general method for simultaneously solving linear equation systems are based on
Gaussian elimination. This is done by performing a decomposition of the A-matrix into
one lower and one upper triangular matrix, a so called LU-decomposition.

LU-decomposition
In MATLAB LU-decomposition is done by the command 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

Example 2.1: Six-Plate Absorption Column


An absorption column with six plates is used to absorb a chemical component in a
vapour into a liquid. The liquid, L, enters at the top and the vapour, V, enters at
the bottom. It is assumed that the liquid and vapour flow rates do not change
through the column.
L, x0

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

A steady-state component material balance over a tray gives

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

This can now be put up on matrix form as Ax = b.


Example 2.2: Six-Plate Absorption Column
A linear model of a six-plate absorption column is developed in Example 1.1
above. The mathematical problem becomes a linear equation system and it can be
solved by MATLAB in the following way:
a = 0.72;
b = 0;
V = 66.7;
L = 40.8;
x0 = 0;
y7 = 0.3;
eyelow = diag(ones(1,5),-1);
A = -(L+V*a)*eye(6) + L*eyelow+ V*a*eyelow';
u = [-L*x0 0 0 0 0 -G*a*(y7-b)/a]';
x = A\u
x =
0.0921
0.1703
0.2368
0.2933
0.3413
0.3820

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

Example 2.3: Analysis of the Six-Plate Adsorption Column


The rank and the reciprocal condition number of the problem are calculated by the
following commands:
r = rank(A)
r =
6
c = rcond(A)
c =
0.0508

This condition number is satisfactory. Bad condition numbers are at least 3


magnitudes lower. Make it a good habit to always calculate the rcond-value for
linear problems.

2.3 Methods for Nonlinear Equations


It is much easier to solve linear than nonlinear equations. In solving linear equations
system there are one dominating method, Gaussian elimination. For nonlinear equations
there are a number of different methods that are good for different problems. A number
of different nonlinear solvers are discussed in Ramirez, section 2.3. In this text we
present three different methods. First we illustrate the use of a predefined equation
solver in MATLAB for one-dimensional problems called fzero. For many nonlinear
problems the Newton method are used. A multidimensional Newton solver, called
newsol, is presented .
One-dimensional Nonlinear Problems
There are a number of well known equation solvers for nonlinear one-dimensional
problems. Three similar methods are presented below. The first is the bisection method.
The idea is very simple. Choose an interval where the function has different values.
Calculate the value of the function in the middle of the interval. The zero is now in the
interval where the function has different sign. The procedure can now be repeated until
the interval is small enough. The procedure is illustrated on the left in Figure 1.
f(b)

f(b)

a
f(a)
f(c)

f(a)

Figure 2.2 - right) Bisection method, left) Regula Falsi method.

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.

2.4 Newton Methods


The most common method for nonlinear systems is the Newton method or NewtonRaphson method. The Newton method is based on Taylor expansion of the function
with the truncation of all terms with order higher than one, HOT.
f ( x ) = f ( x 0 ) + f ' ( x 0 )( x1 x 0 ) + HOT

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

Figure 2.4 The Newton-Raphson method.

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 )

To find a new x-vector, x1, one have to solve A x1 = b where A = J and b = J x0 + f. In


other words the Newton method for nonlinear equation systems solves a linear equation
system in each iteration. Below are two different Newton methods briefly presented,
namely new2dim and newsol. The routine new2dim is a Newton solver for
nonlinear problems in 2 unknowns and it is developed by Mathews. It requires one
function describing the nonlinear problem and one function describing the Jacobian.
The second method, newsol, is described in the following subsection.
Example 2.5: Exothermic CSTR
An exothermic reaction occurs in a continuous stirred tank reactor, CSTR.
Assume constant volume, V, and flow rates, F. The CSTR is cooled by a coolant
and it is also assumed that the coolant temperature is constant, TC.
F, CA0, T0

F, CA, T

TC

Figure 2.5 - Continuous stirred tank reactor.

A steady-state model with one component material balance and one energy
balance is as follows

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

The model can be rewritten in dimensionless form as follows


0=1 e

1 1
50 ( )
2

0=(0 ) u1( c ) + e

1 1
50 ( )
2

where and are the unknown dimensionless concentration and temperature.


This is implemented in the following MATLAB functions, cstrss for the
problem and cstrssJ for the Jacobian. Note that the input variable is a vector, x
= [ ; ] and the output variable is also a vector containing two values.
function [res]=cstrss(x)
% CSTRSS is a dimensionless steady-state
%
lumped model of an exothermic continuous
%
stirred tank reactor. See Ramirez ex. 2.3
% parameters
u1=2;
nyc=1.75;
ny0=1.75;
% unknown variables
ksi=x(1);
ny=x(2);
% equations
P=ksi.*exp(50*(0.5-1./ny));
Us=u1.*(ny-nyc);
res=[1-ksi-P; (ny0-ny)-Us+P];
function [J]=cstrssJ(x)
% CSTRSSJ calculates the Jacobian for the
%
dimensionless steady-state lumped model
%
for an exothermic continuous stirred
%
tank reactor. See Ramirez ex. 2.3
% parameters
u1=2;
nyc=1.75;
ny0=1.75;
% equations
ksi=x(1);
ny=x(2);
% equations
Pksi=exp(50*(0.5-1./ny));
Pny=(50./ny.^2)*ksi*exp(50*(0.5-1./ny));
Usny=u1;
J=[-1-Pksi, -Pny; Pksi, -ny-Usny+Pny];

20

This is now solved by the new2dim function


sol = new2dim('cstrss','cstrssJ',[1 1],1e-6,1e-6,40)
sol =
0.9676
1.7608

newsol
Advanced Newton solvers have routines for numerical differentiation. The newsol
MATLAB M-file is a method of this kind and it is a modification of a solver developed
by C.T.Kelley, called nsol. The user only have to specify the problem on 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);

Example 2.6: Exothermic CSTR, continue


The nonlinear problem in Example 2.5 can be solved by the Newton solver
newsol. The solver starts at [1 1]. Note that newsol only needs the function
cstrss.
sol = newsol('cstrss',[1 1]')
sol =
0.9676
1.7608

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

3 Dynamic Simulation of Lumped Models


3.1 Introduction
An unsteady-state lumped model results in a set of ordinary differential equations, ODEs. These

equations describe time-dependent relations between a set of variables. Nonlinear


unsteady-state lumped models can often be written as ODEs on state-space form with
derivatives of the states explicitly on the RHS like
dx1
= f 1 ( x1 ,..., xn , u1 ,..., un )
dt
...
dxn
= f n ( x1 ,..., xn , u1 ,..., un )
dt

or on a more compact representation on vector form


dx
= f ( x, u )
dt
(the general form, F(dx/dt,x,u) = 0, is not discussed in this course).
If the ODE system is linear (and has one input u) it has the following representation
dx1
= a11 x1 + a12 x2 +...+ a1n xn + b1u
dt
...
dxn
= an1 x1 + an 2 x2 +...+ ann xn + bn u
dt

or using matrix notation it becomes

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.

3.2 Explicit One-Step Solvers


The simplest ODE solver uses the explicit Euler method. More accurate explicit
methods are called Runge-Kutta methods which is a family of methods. These methods
are the most common methods for ODE systems. A one-step method uses only one
previous step to calculate a new step. An explicit method uses an explicit formula to

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

Make a Taylor expansion of x(t+h) where h = t = t i + 1 t i and truncate the


expansion after the first derivative. The first derivative is equal to the function value.
x (t + h) = x (t ) +

dx
( t ) h + ( t ) h 2 x ( t + h) x ( t ) + f ( x ( t )) h
dt

This approximation gives us the following explicit one-step method.


x(ti +1 ) = x(ti ) + hf (x(ti ))
x( t 0 ) = x 0

One drawback with this method is that the error decreases proportional to the step-size.
This results in small step-sizes in order to keep the error low. A related problem is that
the solution can become unstable if the step-size is to large, see Ramirez section 3.1.
Assume a linear ODE written on state-space form and that it is stable, i.e. K > 0.
dx
= Kx
dt

An Euler approximation gives the following expression

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

We get the following step-size criteria on h for the stability limit.

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

Figure 3.1: A Mixing Tank

This is expressed in the following M-file.


function [dc]=onetank(t,c)
% ONETANK is a model of the concentration variation
%
in a continuous tank.
% parameters
cin = 1;
q = 1;
V = 1;
% equations
dc = -q/V*c + q/V*cin;

An explicit Euler method called eulers, is found in Mathews, Numerical


Methods. The one tank model can be simulated by the Euler method with the
following settings, from 0 to 10, with initial concentration 0 and with 20 steps:
[t,y]=eulers('onetank',0,10,0,20);
TY = [t' y']
TY =
0
0.5000
1.0000
1.5000
2.0000
2.5000
3.0000
3.5000
4.0000
4.5000
5.0000
5.5000
6.0000
6.5000
7.0000
7.5000
8.0000
8.5000
9.0000
9.5000
10.0000

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)

Example 3.2: Concentration Dynamics in a Continuous Tank, continue.


Two solutions of the mixing tank concentration dynamics are shown in Figure 3.3.
The solid line solution is based on the Euler method with 1000 steps and the
dashed line is based on 4-order Runge-Kutta, rk4, with 10 steps. As can be seen

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 )

and rewrite the expression as

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

(bpf (t , x(t ) ) + bqf


t

( ti , x ( t ) i ) f ( ti , x ( t ) i ) )

These expressions give

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.

Example 3.3: Concentration Dynamics in a Mixing Tank


The simple mixing tank is solved by the ode23 method in the following way.
Notice that the solution has many steps in the beginning of the solution. At the
end there is only a few steps and in total 22 step with guaranteed error estimates.
The default tolerances are a relative error tolerance of 1e-3 and an absolute error
tolerance of 1e-6. The ode45 solver works exactly in the same way.
[t,y]=ode23('onetank',[0 10],0);

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

3.3 Implicit One-Step Methods


Explicit solvers uses current function evaluations to predict a new step, like
xi+1 = xi + h f(xi).
Implicit solvers are based on the idea to use the new step in the function evaluation to
generate the new step, like
xi+1 = xi + h f(xi+1),
which in turn generates an implicit equation system. This means that an implicit method
solves a nonlinear equation system in each step. This results in very good stability
properties. In MATLAB, ode23s is an implicit second-order Rosenbrock solver.

Example 3.4: Two Tank Series and Stiff Problems


Two tanks connected in series are simulated with different volumes. The total
volume is constant. In the first case the volumes are the same and in the second
case the first tank has 0.1% of the volume of the second tank. This is clearly
illustrated in the figure below.

Figure 3.4: Two-tank series left) same volumes right) different volumes.

Material balances over one chemical component in the two tanks becomes

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

and rewritten on state-space form using matrices it becomes


q

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.

3.4 Multi-Step Methods


To increase the accuracy in a RK-method we have to increase the order of the method
which increases the number of function evaluations needed in each step. One way to
increase the order without increasing the function calls is to reuse previous steps. This
means using one new function evaluation together with a number of old ones. There are
both explicit and implicit multi-step methods.
Adams Methods
Explicit multi-step methods are often called Adams-Bashforth methods. These methods
are rarely used because they have very small stability regions and they require very
small step-sizes. Methods like this are often modified. The explicit multi-step formula is
used as a prediction of the new step. This predicted value is then used in an implicit
multi-step formula, which correct the predicted value. This method uses direct
substitution to solve the nonlinear equation. In this way we get a semi-implicit multistep method with good performance. Methods like this are called predictor-corrector
methods or Adams-Bashforth-Moulton methods or just Adams methods. These methods
can also include variable step-size and variable order, both controlled by error
tolerances. In MATLAB there is a variable step-size variable order multi-step method
(Adams-Bashforth-Moulton) called ode113.

Example 3.5: Two Tank Series and High Accuracy


If the error tolerances are decreased higher order methods are favourable. The
simple two tank problem from Example 3.4 illustrates this in the table below. If
the default tolerances are used the lowest order Runge-Kutta is the most economic
method. If the tolerances are halved the situation becomes the opposite. Adams
methods have good performance for non-stiff problems with high accuracy
demands.

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.)

Example 3.6: Two Tank Series and Multiple Components


The tank series in Example 3.4 requires stiff ODE solvers if the tank volumes
differs in magnitude. A comparison between the two stiff solvers ode23s and
ode15s is illustrated in the table below. If the tank series only has one chemical
component then the ODE-system have 2 states, i.e. 2 differential equations. If the
tanks have 4 chemical components the ODE-system have 8 states. When the order
of the ODE-system increases the high order method becomes more efficient
compared to the low order method. This is seen below. For the tank series from
Example 4 the ode23s is slightly better then ode15s. When the same tank series
has 4 chemical components ode15s is much more efficient. Note that ode15s
takes more steps due to the quasi-constant step-size control.
Table 3.3: Number of steps and floting point operations during the solution of a multi component
tank problem.

ode23s
ode15s

2 state system
35 steps, 9453 flops
49 steps, 10701 flops

8 state system
41 steps, 170647 flops
65 steps, 59228 flops

3.5 Differential-Algebraic Equation Solvers


When fast dynamics becomes so fast compared to the dynamics of interest it can be
approximated to be instantaneous. Some physical phenomena can also be so fast that it
is hared to measure the dynamic behaviour. In chemical engineering we assume that the
phenomenon is at equilibrium. A differential-algebraic equation system, DAE, can be
expresses as

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);

Example 3.6: Two Tank Series again


Two tanks connected in series are simulated with different volumes. The total
volume is constant, see axample 3.4. In this case we assume that the first tank is
so small that the dynamics is neglected.
Material balances over one chemical component in the two tanks becomes

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

and rewritten on state-space form using matrices it becomes


0
q
C
0 0 d C1
1 q
0 1 dt C = q q C + 0 C in

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);

Figure 3.6: Simulation of a two-tank series using a DAE solver.

The DAE solvers in MATLAB can not solve all kinds of DAE problems. They can only
solve index 1 problems. The index of a DAE problem is the number of timed the
equation system have to be differentiated in order to be a set of ODE:s. In the example
above the first equation only have to be differentiated once in order to be an ODE. The
derivative of the first concentration is equal to the derivative of the 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.6 ODE Simulation in MATLAB


Above we have discussed five different methods that are available in MATLAB. There
are two additional ones, ode23t and ode23tb. These methods can handle moderate
stiff problems and stiff problems respectively. ode23t is an implementation of the
trapezoidal rule. ode23tb is an implementation of an implicit Runge-Kutta formula
with a first stage that is a trapezoidal rule step and a second stage that is a backward
differentiation formula of order two. All solvers have a number of options for setting
different solver parameters in order to increase solver performances. These option
parameters are handles through odeset and odeget. MATLAB also advise the user
to write its ODE-file in a predefined way. This is well described in the help text of
odefile or in the manual Using MATLAB. When you become a more skilled user use
these guidelines to develop simulation problems in MATLAB.

3.7 Summary
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

4 Development of Distributed Models


4.1 Introduction
In the previous chapters process models are based on balance equations of mass, energy
and momentum over a lumped system. Inside the system the properties are uniform with
no spatial change. Lumped systems are well mixed and they have no spatial dependent
behaviour. In this chapter we are going to develop models for systems with spatial
change. The behaviour is distributed in the space dimension and therefore they are
called distributed systems or distributed parameter models. The development is similar
to lumped model development. The difference is that the balance equations are set up
for arbitrary small fluid elements or infinite small control volumes.
A general balance equation is expressed as follows

In + Production = Out + Accumulation


This is set up for the volume in Figure 4.1.

z
y

y
x

Figure 4.1: A general fluid element

Below we are discussing distributed models based on mass, energy and momentum
balances first in one space dimension, in this case x-dimension, and second very briefly
present balances in three dimensions.

4.2 Mass Balances


Mass balances over a distributed system are very similar to mass balances over lumped
systems. The major difference is the special change. Assume that the fluid in the control
volume in Figure 4.1 has a given density, than the accumulation of mass is simply the
time change of the mass.
d
( xyz )
dt
For simplicity let us assume a convective flow of mass only in the positive x-direction.
The inflow of mass can be expressed as the density times the volume flow rate. In this
case the element surface, yz, times the velocity in x-dimension.
yz ( v x ) x

35

The outflow at x+x becomes


yz ( v x ) x + x

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.

Example 4.1: Tube


A medium is flowing through a pipe. The pipe has cylindrical geometry with a
constant cross section. The accumulation of mass in the finite volume is the
volume integral of the density. Mass enters the finite volume at position z and
leaves at position z+dz.

L
z=0

z+dz

Figure 4.1: Tube.

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

The three terms are put together in a mass balance giving

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

Component Mass Balances


It is possible to put up component balances, one for each chemical component. Note
that all component mass balances sum to the total mass balance as in the lumped
parameter case. In order to set up a consistent equation system it is recommended to
take away one mass balance, often one component mass balance, for instance the
solvent balance. Sometimes this knowledge can be used as redundant information to
check the correctness of the model.

Let us derive a component balance with only molecular diffusion as diving force for the
fluid control volume in Figure 4.1. The mass of component A in the control volume is
the product of mole weight and concentration.

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

and the outflow

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

Figure 4.2: Tubular reactor.

Accumulation of mole =

d
dt

z + dz

( Ac )d

Inlet of mole = vz Ac z Dax A

dc
d

39

Outlet of mole = vz + dz Acz + dz Dax A

dc
d

+ dzz

z + dz

Production of mole =

(rA) d
z

The three terms are put together in a mass balance


+ Prod

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

Rearrage the component mass balance

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.

4.3 Energy Balances


The development of distributed energy balance is identical to the development of
distributed mass balance. In microscopic energy balances there occur different terms
compared to lumped models.
Assume that the total energy is composed of internal energy and kinetic energy.
Accumulation of potential energy is neglected together with other energy forms.

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

The energy conduction terms are

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

and viscous forces.

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.

Energy balance for fluid with low velocity


A special case is when it is possible to neglect the change of kinetic energy, no gravity
term (x-direction) and neglect viscous work. This is the case for ordinary liquids in
ordinary velocity fields.

(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.

General form of energy balance


The general form in one dimension above can be expressed in three space dimensions
based on the notation of a vector velocity v and vector heat flux q. The equation below
uses the nabla operator. The two last terms as different. The variable is the stress
tensor and is a matrix variable.


v2
v2
U + = v U + q + ( v g) ( pv ) ( v ) : v
2
2
t

Example 4.3: Tube with convection and heating


Assume that a liquid is flowing through a tube. The axial dimension is called z.
The tube length is unity and the density, ,and heat capacity, Cp,are assumed to be
constant. The tube has a constant cross section, A, and a characteristic heat
transfer length (Ls=Atrans/L). The tube is heated with a constant flux, Q. Neglect
kinetic energy change and approximate internal energy to be equal to enthalpy.
Put up an energy balance for a small tube element.

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.

Example 4.4: Tube with heat transfer


Let us go back to the previous Example 4.3 with a fluid flowing in the x-direction.
In this case we assume that there is a surrounding medium with a constant
temperature Ts. An overall heat transfer relation express the heat flux as
proportional to the temperature difference between the outside and the inside.
q x = k (Ts T )

Put this in the energy balance in Example 4.3 and we get

T
T kLs
(Ts T )
= v
+
t
x C p A

4.4 Momentum Balances


Distributed momentum balances are quite different compared to mass and energy
balances. The reason is that momentum is a vector because momentum is the product of
mass and velocity. The accumulation of momentum is

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

The parameter is the viscosity coefficient.

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

In three dimensions it becomes

v
= vv + 2 v p + g
t

4.5 Boundary Conditions


In the sections above we have discussed the developmenyt of balance equaitons on
infinite small fluid elements. This results in equaitons with spatial behaviour. To solve
these equations we need to the behaviour at the system boundaries. These system
boundary descriptions or models are called boundary conditions, BC. There are three
common boundary conditions
Dirichlet BC: The dependent variable has a given given value at the boundary.
von Neuman BC: The derivative has a given value at the boundary.
Robin BC (general von Neuman): There is a given (linear) relation between
the value and the derivative at the boundary. This is also called a flux model of
the boundary condition.

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

5 Steady-State Simulation of Distributed System


5.1 Introduction
A steady-state distributed model results in a set of differential equations. Systems in one space
dimension become a set of ordinary differential equations, ODEs, and systems in two or more
space dimensions become a set of partial differential equations, PDEs. These differential

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.

5.2 Shooting Method


A number of different shooting techniques are discussed in Ramirez book. Here we are
only using the general shooting method.

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.

Figure 5.1: A turbular reactor.

The ODE is implemented in the following function:


function
% TUBex1
%
with
v = 1;
k = 10;
dc= -k/v

[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)

The plot is seen in Figure 5.2.


1

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

Figure 5.2: Concentration profile in tubular reactor

General Shooting Method

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.

Example 5.2: Tubular Reactor without Dispersion


Assume that the tubular reactor in Example 5.1 has a boundary condition on the
outlet instead of the inlet. Assume cL = 0.05 . A function that calculates the
residual between the assumed outlet concentration and the calculated is listed
below.
function [res]=funcex2(c0)
cL = 0.05;
[z,c]=ode23('tubex1',[0 0.35],c0);
cN = c(max(size(c)),1);
res = cL - cN;

The problem can be solved by using the fzero function in MATLAB.


c0=fzero('funcex2',1)
c0 =
1.6664

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.

Second order ODEs


Second order ODEs must be rewritten on state-space form, i.e. as two first order ODEs,
in order to be solve by the shooting method. The simplest way to transform a second
order ODE to two first-order ODEs is to introduce a new state, which is the derivative
of the first state. Assume a second-order ODE

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

The reactor is assumed to have the following boundary conditions.

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

with the following BCs

v
(c cin ) at z = 0
D
x1 = 0
at z = L
x1 =

The reactor model is implemented in the MATLAB function below


function [dx]=tubex3(t,x)
% TUBex3 is a dispersion tubular reactor
%
with first-order kinetics
x1 = x(1,1); c = x(2,1);
v = 1; k = 10; D = 0.1;
dx1= v/D * x1 + k/D * c;
dc = x1;
dx = [dx1; dc];

and the function is used in


function [res]=funcex3(c0)
x10 = 1/0.1*(c0 -1);
[z,x]=ode23('tubex3',[0 0.35],[x10; c0]);
xN = x(max(size(z)),1);
res = xN;

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

The profile is seen in Figure 5.4.

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

Figure 5.4: Tubular reactor with dispersion

Unstable ODEs and Backward Shooting


The problem in Example 5.3 contains an ODE which is unstable and unstable ODEs are
hard to solve in the general case. The ODE in Example 5.3 is linear and it was possible
for the Newton solver to find the right answer very fast. This is not the case in general.
Nonlinear ODEs must be stable in order to make reliable shooting solutions. It is hard
for the nonlinear equation solver to guess values that will not give exploding ODE
solutions. It is often possible to rewrite the ODE and change the direction of shooting.
The new opposite direction is often stable.
Example 5.4: Tubular Reactor with Dispersion
The tubular reactor with dispersion from Example 5.3 is assumed to have a
second-order reaction, r = kc 2 . The ODE is unstable if it is integrated from 0 to
L and the technique presented above will fail. One-way to make it stable is to
integrate backwards from L to 0. This is done by making a variable
dc
dc
=
and so on. The
transformation. Set zz = L - z. This means that
dz
dzz
transformed reactor model becomes
dc
d 2c
2
v
=D
2 kc
dzz
dzz

The boundary conditions are also modified.


dc
= 0 at zz = 0
dzz
dc
D
= v (c cin ) at zz = L
dzz

The MATLAB function for backward shooting is listed below

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

reactor lenght (backward)

Figure 5.5: Nonlinear tubular reactor with dispersion solved backwards.

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

5.3 Finite Difference Method


Another way to solve BVPs is to divide the space dimension into a set of mesh points
and to approximate the derivatives as differences between successive mesh points.
These approximations are put into the ODE and we get one equation for each mesh
point. This results in a large algebraic equation system. The basic steps for a finite
difference method 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 which
ba
means that h =
and that xi +1 = xi + h .
N +1
2. Approximate the derivatives by a difference methods. The most common method is
the centered-difference approximation. For a first order derivative it is:
dx xi +1 xi 1
.

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

Example 5.5: Tubular Reactor with Dispersion again


Step 1, selection of grid: Assume an uniform fixed grid on the interval [0, L] ,
L0
from the inlet to the outlet (L = 0.35). The mesh-size becomes h =
and if
N +1
we choose 2 internal points it becomes h=0.35/3.

x
L/3

x
L/3

L/3

Figure 5.6: Simple grid of the tubular reactor (2 internal grid points).

Step 2, selection of discritization method: Centered-difference of the first


derivative and of the second derivative becomes
dx xi +1 xi 1
d 2 x xi +1 2 xi + xi 1
and

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

becomes with centered-difference approximations

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

which can be expressed on matrix form

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

where the b-vector elements are


D v
b0 = 2 + c1
h
2h
D v
b3 = 2 c4
h
2h

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

Replace the derivatives


c1 c1
= v (c0 cin ) at z = 0
2h
c4 c2
at z = L
=0
2h

The BCs can now be rewritten as equations for c1 and cN +2 .


c1 = c1

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

with one element on the right hand side

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

The tubular reactor example is solved by MATLAB below


v = 1; k = 10; D = 0.1; L=0.35; cin = 1;
N = 2;
h = L/(N+1);
eyelow = [zeros(1,N+2);eye(N+1) zeros(N+1,1)]
eyelow =
0
0
0
0
1
0
0
0
0
1
0
0
0
0
1
0

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

Centered-difference of the first derivative is expressed as


A1 = eyelow' - eyelow
A1 =
0
1
0
-1
0
1
0
-1
0
0
0
-1

0
0
1
0

We also need an identity matrix


A0 = eye(N+2)
A0 =
1
0
0
1
0
0
0
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

Example 5.6: Nonlinear Tubular Reactor with Dispersion


The tubular reactor from Example 5.4 can also be solved by finite difference
method. The major difference compared to Example 5.5 is that here the equations
must be expressed on residual form and solved by a nonlinear solver. The function
eqex6 below contains similar calculations as in Example 5.5 but expressed in an
M-file. The difference is the nonlinear term and the residual calculation at the end.
function [res]=eqex6(c)
% EQex6 is a finite difference approx of the tubular
%
reactor with dispersin and 2-order kinetics
v = 1; k = 10; D = 0.1; L=0.35; cin = 1;
N = 10;
h = L/(N+1);
eyelow = [zeros(1,N+2);eye(N+1) zeros(N+1,1)];
A2 = -2*eye(N+2) + eyelow + eyelow';
A1 = eyelow' - eyelow;
%boundary conditions
A2(1,1) = -2*(1+h*v/D);
A2(1,2) = 2;
A2(N+2,N+1) = 2;
A1(1,1) = 2*h*v/D;
A1(1,2) = 0;
A1(N+2,N+1) = 0;
%ODE approx
Aodem = D/h^2 * A2 - v/2/h * A1;
b = (-2*v/h - v^2/D)*[1;zeros(N+1,1)] * cin;
% equation on residual form
res = Aodem*c - k*c.^2 - b;

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

6 Dynamic Simulation of Distributed Systems


6.1 Introduction
An unsteady-state distributed model results in a set of partial differential equations.
There are two kinds of unsteady-state PDEs, namely parabolic and hyperbolic.
Parabolic PDEs are also called diffusion-type PDEs and hyperbolic for wave-type
PDEs. The behaviour of these two kinds of PDEs are indicated by the names. Diffusiontype PDEs smooth out the initial conditions in space while wave-type PDEs instead just
let the initial condition travel in space as a wave. In process applications conduction,
viscosity and dispersion are like diffusion in material momentum and energy balances.
Convection in material, momentum and energy balances creates wave behaviour. Note
that PDEs are equations that describe a variable in two or more dimentions. A variable
in two dimensions is a surface and the PDE describes how this surface is changing in
the two coordinates.
Diffusion-type PDE or parabolic PDE can be described as

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.

6.2 Method of Lines


The discretization of the space dimension into a set of points results in a large set of
ordinary differential equations in time. The discretization can be done in different ways.
In this course we discuss only finite difference techniques. Two others very commom
techniques are finite volume and finite element methods, but they are not discussed
here. Finite difference approximations in MOL are the same as for the steady-state case.

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.

6.3 Space Discretization


The space dimension in the PDEs is discretized with the use of finite differences. It is
similar to the approximations of steady-state models presented in the previous chapter.
The space derivatives are approximated with differences between grid points in space.
The major difference is the discretization of convection terms.
Convection terms
The convection term can not be treated in the same way as first order derivatives as in
Finite Difference Methods used in static PDE problems. Instead the convection term is
discretized upwind, which means that it is approximated in the opposite flow direction.
A first order derivative is approximated using a backward Euler approximation.

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

Both parts in the diffusion term are approximated by centered-difference.


Example 6.1: Steam Heat Exchanger
A steam-heated plug-flow heat exchanger has the following energy balance
4U
T
T
= v
+
(T T )
t
z Cpd s

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

Figure 6.1: Steam heated heat exchanger.

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;

steam temperature, Ts = 120 and finally the inlet temperature, T0 = 25 . The


MATLAB function becomes
function [dT]=shex(t,T)
% SHEX is a steam-heated plug-flow heat exchanger
%
approximated with finite differences
v = 10; U = 2500;
rho = 1000; Cp = 4.18; D = 0.1; L = 0.5;
Ts = 120; T0 = 25;

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;

Note the discritization matrix with N = 10 becomes as follows.


A1 = eye(N+1) - [zeros(1,N+1); eye(N) zeros(N,1)]
A1 =
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
-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)

Grid point temperature transients (100 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

Temperature profiles at different time (10 internal grid points)


90

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

Figure 6.3: Temperature profiles in the heat exhanger at different times.

Example 6.2: Heat Conduction in a Rod


The unsteady-state heat conduction in a long rod can be modelled by an energy
balance in one space dimension.

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

Figure 6.4: Heated rod with 5 internal grid points.

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

In step 4 we have to discretize the boundary conditions. The BC at z = 0 is a


generalized Neumann (or Robin) and it is discretized as

T T
T
2h
= T Tin 1 1 = T0 Tin T1 = T1 (T0 Tin )
z
2h

It is used in the ODE for boundary grid point at z = 0

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

Example 6.3: Diffusion in a sphere


The unsteady-state diffusion in a sphere can be modelled by a component mass
balance in one space dimension the sphere radius.

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

This parabolic PDE can be discretized using centered-difference as discussed


before.
2 c 2 c ci +1 2ci + ci 1 2 ci +1 ci 1
2+

+

r r
h2
ih
2h
r

and in the complete PDE

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

7 Computer Aided Process Engineering


7.1 Introduction
Computer Aided Engineering, CAE, in general and Computer Aided Process
Engineering, CAPE, in particular are fields that change rapidly. The development in
computer technology makes it possible to solve larger and larger problems. The
software development makes it easier to model and describe advanced problems. This
trend will continue. The most advanced chemical engineering problems that was solved
for hours with large mainframe computers twenty years ago and it took one day to
describe the problem are solved today in a few seconds and it take only five minutes
describe the problem. This is done on cheap computer almost available for every
engineer. We can only imagine what kind of problems that can be solved in ten years.
Commercial programs for CAPE can be divided into four main categories.
1. General engineering programs for technical computing, statistical analysis,
curve drawing etc. are of course important tools.
2. CAD programs are often general Computer Aided Design programs that are
used for drawing of Process and Instrumentation Diagrams, P/I-D, and for
construction of processing equipments.
3. Flowsheeting programs for calculation of process streams, equipment
dimensions and process economics. These are used for evaluation of different
process designs.
4. CFD programs are special programs for Computational Fluid Dynamics and
can simulate static, and sometimes dynamic, fluid behaviour in two and three
dimensions.
General Engineering Programs
There are a lot of different programs around for mathematical, numerical and statistical
problem solving. There are general tools for technical computing, like MATLAB,
MathCAD, Maple and Mathematica. Excel is also an important tool because it is
available everywhere. There are also more special designed tools for particular
problems like curve drawing, Carisma for example, curve fitting to data etc.
Tools like are available for almost everybody but they require that the user specify his
or her problem in the right way. There are often no special libraries for chemical
engineering problems.
Computer Aided Design or CAD
There are a lot of different CAD tools available on the market. These tools are
developed for the mechanical industry. The tools are advanced drawing tools, often with
3D capability. They can include databases of different graphical objects and this makes
it possible to put together mechanical constructions virtual in the computer. If a new
mechanical part must be constructed, all dimensions and shapes of the objects are
determined in the CAD program.
There is no special CAD tools for just chemical and process engineering. Examples of
different CAD program used in process industries are AutoCAD, Catia, and
PRO/Engineer. There is special CAD a program for molecule construction and
visualisation. Particular for large and macro molecular chemistry it is interesting to

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

Figure 7.1: The principle structure of a flowsheeting program.

71

The different parts of a flowsheeting program is as follows:


User Defined Data. The problem is specified in a graphical user interface where
you describe your flowsheet by "drag and drop" of modules from the library. Data
are entered in specialised pop up forms and pull down menus.
Unit Operations. The majority of commonly used unit operations are often
available in the program. Often one kind of modules can be used in a number of
different ways, which increases the number of available unit operations. Some
programs allow the user to specify their own unit operations modules.
Database. In the major programs there are a lot of data for a large number of
chemical components. More advanced programs can handle solids and
electrolytes which are quite complicated.
Solve. The actual solver is only a marginal part of the software. The solver can
work with different solution strategies, i.e. sequential and/or simultaneous.
Output Data. Some programs generate a solution report in a predefined style.
There are programs that interactively calculate and present results in the graphical
user interface.
Solution strategies for Steady-State Calculations
The actual calculation of mass and heat balances can be done in a number of ways.

1. Sequential Modular Solution Strategy.


Each unit operation is solved in a sequence described by the flowsheet. The first unit
operation is solved first and this may include iteration. Then the second is solved in
the same way. When all units are calculated the simulation is done. This procedure
has problems with recycle loops. Therefore a recycle must be specified in a special
way and given an initial value. The procedure above can now be used and when all
units are calculated the recycle loop is given the new value and the calculation can
be done all over again until it converge and then it stops.
2. Simultaneous Solution Strategy.
All equations are put together and sorted. The large equation system is solved
simultaneously by an equation solver, for instance a Newton solver. This method
does not have any problem with recycle loops and it can utilise advanced general
equation solving techniques. The drawback is the equation sorting and manipulation
before the actual equation solving. Another problem is models that are described
implicitly; in iteration calculation codes and not in a closed equation form, can not be
solved.
3. Simultaneous Modular Solution Strategy
This technique is a mixture of the two discussed above. The unit operations are
solved by the simultaneous technique and the overall flowsheet by the sequential
modular technique. This makes it possible to solve some special problems in two
ways. One example is advanced separator configurations. These can be defined as a
set of unit operations and therefore solved in sequence. It is sometimes possible to
define the same configuration inside one unit operation and therefore it is solved
simultaneously.
The dominating strategy of today is the simultaneous modular solution strategy. This
means that the steady state solution is derived through iteration. Two different iteration
problems can be derived.

72

Recycle loops results in a problem where a set of modules is connected in a loop.


To choose a proper variable to start the iteration is called tearing. When the
iteration variable is selected it gets an initial guess and the loop is calculated and
the iteration variable gets a new value. If this value is used directly this becomes a
direct substitution method. A special method of this kind is the Wegstein method
often used in flowsheeting programs.
Adjust loops are defined in order to specify design criterion. A parameter in the
flowsheet is manipulated by an iteration method based in the value of another
flowsheeting variable. In HYSYS this adjusts loop iteration method is a secant
method, which is a simplified Newton method.
Commercial Steady-State Flowsheeting Programs
There is a number of different flowsheeting packages available on the market today.
Many major chemical companies have developed their own software. Some software
are primary developed for a particular industry, mainly for refinery and petrochemical
industry. This means that organic compounds and continuous process industry are often
well covered by today's flowsheeting programs. To simulate other kinds of processes
often require additional modules that have to be user defined.
Below follows a list of some flowsheeting programs that are used in Swedish industry.
The list is not complete.

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

is a product from Chempute Software and it is a small and cheap


flowsheeting package with limitations.
http://www.chemstations.net/index.html

HYSYS Process is a product from HYPROTECH. It is an interactive and integrated


environment with a number of add-on modules. HYSYS is used in
the under graduate courses at the Chemical Engineering program.
http://www.hyprotech.com/

PRO/II

is a product from Simulation Science Inc. It is one of the major


flowsheeting programs in the petrochemical industry.
http://www.simsci.com/

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

is a product from ASPEN Tech. This is a new software that


integrated ASPEN PLUS and SPEEDUP. It is used in research at
the department of Chemical Engineering I.
http://www.aspentec.com/

gPROMS

is a product from Process Systems Enterprice. It is developed at


Imperial Collage, London, and is an advanced simulation tool that
can handle both ODEs and simple PDEs. It can also handle
sequences and batch procedures. gPROMS is available at the
department of Chemical Engineering I.

HYSYS Plant

is a product from HYPROTECH. It is an interactive and integrated


environment for both steady state and dynamic simulation with a
number of add on modules. HYSYS is used in the under graduate
courses at Chemical Engineering program.

http://www.psenterprise.com/

http://www.hyprotech.com/

SPEEDUP

is a product from ASPEN Tech. SPEEDUP is developed at


Imperial Collage, London. It is an open modelling language that
allows the user to specify their own models is a modelling
language. It is today integrated into ASPEN.
http://www.aspentec.com/

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

7.3 Computational Fluid Dynamics


Computational fluid dynamics programs, or just CFD, simulate distributed system
models based on microscopic continuity equations of mass, energy and momentum.
This means that these softwares solve problems based on partial differential equation
systems, PDEs. As the name indicates, CFD, these kinds of tool come from simulation
of aero dynamics, i.e. steady state momentum balances. Today these tools can also
handle mass and energy balances together with kinetic descriptions. Advanced
applications of CFD are in the area of combustion in turbulent flow patterns.
CFD problems require detailed geometry descriptions and a well-defined mathematical
description in form of a set of PDEs with boundary value conditions and initial
conditions. This means that the interface often is based on a drawing tool.
The numerical solver can be based on two different methods:
1. Finite volume method. This is a generalisation of the finite difference method
into 3 three dimensions. The method discretize the spatial dimensions into a large
set of finite volumes. The problem becomes a large set of AEs or ODEs.
2. Finite element method approximates the solution as a set of finite elements. The
method results in a large set of AEs or ODEs

Steady state CFD problems


Steady state CFD problems becomes a large or very large set of algebraic equations.
Ordinary Gaussian elimination can not be used for linear problems or inside a Newton
solver. Sometimes is it possible to use Gaussian elimination with sparse matrix
techniques but the dominating methods today are iterative linear equation solvers.

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

is a product of AEA Technology Inc. It is based on the finite


element methods. The department of Food Engineering and the
department of Heat and Power have CFX on Unix workstations.
http://www.aeat.co.uk/cfx/

FIDAP

is developed by Fluid Dynamics Inc. and is now a product from


Fluent Inc. It is based on the finite element method.
http://www.fluent.com/

Flow-3D

is a product from Flow Science Inc. It is based on the finite


volume method.
http://www.flow3d.com/

FLUENT

is also a products from Fluent Inc. It is based on finite volume


methods and it can handle multi-phase problems. The department
of Chemical Engineering I has two licences.
http://www.fluent.com/

FEMLAB

is a product from COMSOL and is an add-on to MATLAB. It can


handle 1D, 2D and 3D problems. The department has one
classroom licence and use it both in research projects and in the
Ph.D. courses.
http://www.femlab.com/

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

Figure 7.4: Geometry and mesh description in a CFD program.

Figure 7.5: Interface to the CFD program Fidap.

78

7.4 HYSYS a flowsheeting program


HYSYS is an interactive and intuitive process simulator for steady state and dynamic
problems. It is developed by Hyprotech Inc. and it is a modern program design and
implementation of a flowsheeting program. HYSYS has a graphical interface based on
menus, pop up forms, click buttons etc., which facilitates the flowsheet development.
Solution Strategy
HYSYS solution strategy and methods work in a flexible way. This flexibility makes
the process of building a simulation both interactive and intuitive. The solution strategy
can be characterised as follows
Event driven. HYSYS makes a calculation after the user supplies each piece

of information. This facility can be turned off.


Interactive information dialogue. HYSYS interactively indicates the status

of the solution. Different modules indicate if they need more information to be


solved and streams indicate if they propagate information to the new module.
Modular operation. Each unit operation and stream can perform all

necessary calculations, weather the information is specified in the operation


itself, or communicated from attached streams.
Object oriented solution algorithm. The objects, i.e. unit operations and

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

HYSYS Flowsheets and Sub-Flowsheets


As mentioned above in HYSYS it is possible to define parts of the flowsheet inside a
sub-flowsheet. This decreases the complexity of the main flowsheet for large problems.
It is also possible to define sub-flowsheets inside sub-flowsheets creating a hierarchy of
flowsheets. Column environment is a special sub-flowsheet for column definitions and
it functions in the same way. It is not possible to define sub-flowsheets inside column
environments.
Create a
new case

Basis
Environment

Column
Environment

Main Flowsheet
Environment

Oil
Envirnment

Sub-Flowsheet
Environment

Figure 7.6: The relations between the HYSYS environments.

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.

8.2 Linear Regression

Assume that the measurement y is proportional to x with an error . In other word we


would like to find a straight line.
yi = kxi + l + i

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 +

A perfect model with no error is as follows


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

Example 8.1: Line fit


Assume that a set of measurements, y, is performed at the conditions x, seen in the
table below and plotted in the figure. The data indicate a linear relationship
between x and y.
Table 8.1: Data in Example 8.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

Figure 8.1: Data in Example 8.1.

82

The model is assumed to be linear and the regression is as follows


k
yi = kxi + l + i y = [x 1] = X = X T X
l

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

Example 8.2: Polynomial fit


The data in Example 8.1 can be used to exemplify the regression of polynomial
models. The data matrix X must be modify as
k 2
1
yi = k x + k1 xi + l + i y = x x 1 k1 = X = X T X X T y
l
The result is k2 = 0.0045, k1 = 0.6915 and l = 1.2166. This indicates that the
second order term can be neglected. The MATLAB commands are
straightforward
2
2 i

X=[x.^2 x 1];
b=(X'*X)\(X'*y);

In MATLAB there is a predefined method for linear regression of polynomials called


polyfit. This command makes a linear regression of a polynomial of order N based
on the data x and y, [p] = polyfit(x,y,N). The result is the polynomial
coefficients and an additional structure S with information about the estimation.

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!

Example 8.3: Polynomial fit using polyfit


The estimation in Example 8.1 and 8.2 are performed in the following way.
p1=polyfit(x,y,1)
p1 =
0.7188
1.1939
p2=polyfit(x,y,2)
p2 =
0.0045
0.6915

1.2166

Nonlinear Transformation into Linear Form


Sometimes is it possible to make a transformation of the nonlinear model into a linear
version. The most common transformation is to take the logarithm of both sides of the
equation. Some examples are
ln k
ln y = [1 x ] 1
k2
ln k
ln y = ln k1 + k 2 ln x ln y = [1 ln x ] 1
k2

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.

Example 8.4: Linear regression of a nonlinear adsorption isotherm


Data from batch adsorption experiments are listed in Table 8.2 and plotted on the
right. One typical model for adsorption is the Langmuir model.

q=

84

qmax c

Kd + c

1
K
c
= d +
q qmax c qmax c

Kd
1 1 qmax

=
1
q c 1
qmax

Table 8.2: Data in Example 8.4

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

Figure 8.2: Data and model prediction in Example

Kd
q 0.0123
q 81.1
max =
max =

1 0.0019
K d 0.150
qmax

The estimated parameters have to be transformed back to original meaning

8.3 Nonlinear Regression


When the model is nonlinear in the parameters we can not use the procedure discussed
above. Instead we use a guess of the parameter vector together with a linearization of
the nonlinear model.

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).

Example 8.5: Nonlinear regression of an adsorption isotherm


In Example 8.4 a nonlinear model, a Langmuir adsorption isotherm, where
transformed into linear form before estimation. Here we use a nonlinear
regression instead. The Langmuir model is described in an M-file
function q = Langmuir(b,c)
q=b(1)*c./(b(2)+c);
The function call is as follows

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

Figure 8.3: Adsorption isotherm estimation based on nonlinear regression

8.4 Regression Analysis


Above we have discussed parameter estimation based on linear and nonlinear
regression. We can gain more then just a set of optimal parameters. With a little more
calculations we can find error measurements on parameter estimates and on model
predictions.
If the measurement error, , is normally distributed and with zero mean, i.e. N(0,2),
then the least square estimation is normally distributed and unbiased.

[]

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.

Parameter confidence regions


The standard error of a particular parameter estimation, parameter i, now becomes

si = 2 X T X

ii

The parameter estimation is Student T distributed and the 1- marginal confidence


interval can be expressed as

i s Tinv 1
i

, N P
2

where Tinv is the inverse of the Student T cumulative distribution.


0.4
N-P = 20
N-P = 10
N-P = 5

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 )

It is possible to do the same kind of calculations for a linearized version of a nonlinear


model and X is replaced by the Jacobian in the expression above. It only creates a joint
confidence interval that is an approximation. There are two functions in KATtools for
this namely confpar and confparplot. The first function calculates the individual
confidence interval
bci=confpar(XorJ,y,b,s2,cov,alpha)

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

Prediction confidence regions


The statistical measure discussed previously can be used to calculate the confidence
interval for a prediction of a measurement. For an individual measurement the sample is
T student distributed and the 1- marginal confidence interval for a prediction becomes

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

Example 8.8: Regression analysis of adsorption isotherm estimation


Nonlinear regression was performed, in Example 8.5, on adsorption data
estimating parameters in a Langmuir model. Below are the prediction confidence
band shown in Figure 8.8 and parameter confidence plots in Figure 8.9.
120

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

Energy Balances ............................................. 40


estimated variance .......................................... 89
Euler method ................................ 22, 23, 24, 26
stability ...................................................... 23
eulers.......................................................... 24
F
FEMLAB ....................................................... 78
Fick's first law................................................. 38
Fick's second law ............................................ 39
fictitious point................................................. 69
FIDAP............................................................ 78
Finite Difference Method ............................... 53
finite difference techniques ............................ 60
Finite element method .................................. 77
Finite volume method................................... 77
fixed mesh grid ............................................... 53
Flow-3D ......................................................... 78
Flowsheeting............................................ 70, 71
FLUENT ........................................................ 78
flux model ....................................................... 45
fsolve.......................................................... 21
fzero ...................................................... 17, 48
G
Gaussian elimination ................................ 13, 56
Gauss-Newton method.................................... 87
Gear methods.................................................. 31
General Shooting Method............................... 48
gPROMS........................................................ 74
H
Heat equation ................................................ 42
homogenous Neumann ................................... 65
hyperbolic PDE ........................................ 36, 60
HYSYS ........................................................... 80
HYSYS Plant................................................. 74
HYSYS Process............................................. 73
I
Implicit solvers ............................................... 28
index of a DAE problem................................. 33
J
Jacobian .......................................................... 86
Jacobian matrix............................................... 19
L
l'Hospitals rule ................................................ 68
linear regression............................................. 82
linreg.......................................................... 88
LU-decomposition .......................................... 13
M
Mass balance .............................................. 4, 35
mass matrix..................................................... 32
method of lines.......................................... 60, 69

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

Regula Falsi ....................................................17


Relaxation methods .........................................77
residual............................................................83
Robin .........................................................45, 65
Runge-Kutta
ode45 ..........................................................27
Runge-Kutta method
midpoint approximation .............................27
ode23........................................................27
Runge-Kutta method
Euler method ..............................................27
Runge-Kutta method
rk23 ..........................................................27
Runge-Kutta methods......................................25
S
secant method .................................................73
Sequential Modular.........................................72
Shooting Method.............................................46
Simultaneous Solution.....................................72
SPEEDUP ......................................................74
split boundary value........................................46
square error .....................................................83
state-space form ............8, 22, 23, 29, 32, 49, 50
steady-state distributed model.........................46
step-size...........................................................24
Stiff Problems .................................................28
Super Target....................................................74
surf ...............................................................67
T
Taylor expansion.............................................23
Taylor expansions ...........................................53
tearing .............................................................73
Tubular reactor ...............................................39
U
upwind.............................................................61
V
variable step-size.............................................27
Variable Step-Size...........................................27
variance...........................................................88
von Neuman ....................................................45
W
Wave-type PDE ...............................................60
X
x = A\b........................................................14

Vous aimerez peut-être aussi