Process Modelling and Simulation
Process Modelling and Simulation
3. Examples of DAE
For an isothermal CSTR, A
B C , model equations may be written as:
dV
Fa F
dt
dC A FA
(C A0 C A ) R1
dt V
dC B F
A C B R1 R2
dt V
dC C F
A C C R2
dt V
C
0 CA B
K eq
0 R2 k 2 C B
where, FA = Feed flow rate of A
CA0 = Feed concentration of A
R1 and R2 = Rates of reaction
F = product withdrawal rate
CA, CB, and CC = Concentration of species A, B, and C, respectively in mixture
Page 1
CE1532 Process Modeling and Simulation Lab
𝑑ℎ 1
= 𝐴 (𝐹1 − 𝐹2 ); ℎ(0) = ℎ0 (1)
𝑑𝑡
𝐹1 = 𝐶𝑣1 √𝑃1 − 𝑃2 (2)
𝐹2 = 𝐶𝑣2 √𝑃2 − 𝑃3 (3)
𝑃2 = 𝑃0 + 𝜌𝑔ℎ/1000 (4)
The values of the model parameters and constants are given below. Note that the upstream
pressure P1 is given by a forcing function; that is, P1 is a specified function of time that is
imposed on the system. It is provided in P1function.m, which you’ll need to download.
220
The imposed time variation of P1
200
is shown opposite. You can
P (kPa)
160
Page 2
CE1532 Process Modeling and Simulation Lab
of MATLAB’s ODE or DAE solvers. For consistency and to avoid confusion, we’re suggesting
that you name m-files as follows:
For your convenience, if you wish to use it, the model’s parameters and constants (excluding
tf and h0) have been typed into MATLAB, and can be downloaded as tankparam.m. To avoid
the added complication (but good practice) of placing all the model parameters and constants
in the script file and passing them using anonymous functions or nested functions, feel free to
put the parameters and constants in the function m-file.
All the solution methods should produce the same predictions of tank level versus time. The
purpose of the tutorial is for you to gain experience in solving a DAE and to compare the ease
of setting up and programming the solution using several different techniques. You may need
to refer to earlier tutorials for help with solving ODEs and sets of nonlinear algebraic
equations.
Do the symbolic substitution by hand and then code this method into MATLAB as files
tanksim1.m and tankf1.m.
Note:
(1) Use ode45 to solve the ODE.
(2) Set a relative solution tolerance to 10–5 (1e-5) using odeset to ensure accuracy.
(3) The script file should produce a plot of the tank level as a function of time.
(4) Include any relevant comments in your two m-files.
Page 3
CE1532 Process Modeling and Simulation Lab
Output Graph:
Page 4
CE1532 Process Modeling and Simulation Lab
So, look at the algebraic equations (2)–(4) and place them in the correct order (rearranging is
not needed in this case) for sequentially calculating the unknown algebraic variables: F1, F2
and P2.
The easiest way to code this in MATLAB is to copy and adapt the m-files you created for
method 1. For consistency, rename tanksim1.m to tanksim3.m and tankf1.m to tankf3.m.
Note:
(1) Since the flow rates and tank pressure change at every instant, the calculations for F1,
F2 and P2 need to be included in the derivative function m-file, not the script m-file.
(2) In method 3, the ODE is given by equation (1), not a modification of it.
(3) As before, in the script m-file, use ode45 to solve the system and set a relative solution
tolerance of 10–5.
(4) Also in the script m-file, make sure you change the name of the function called by
ode45 from @tankf1 to @tankf3, otherwise you’ll be using your old function.
(5) The script m-file should produce a plot of the tank level as a function of time.
(6) Include any relevant comments in your two m-files.
Page 5
CE1532 Process Modeling and Simulation Lab
Output Graph:
Look at equations (1)–(4) and write them in standard DAE form corresponding to the “state”
vector x = [h, F1, F2, P2]T. Also write down the corresponding mass matrix M.
One challenge in using a DAE solver is to provide a consistent set of initial conditions for all
the variables in x. Given h0 = 2 m and P1(0) = 139.5 kPa, calculate initial values
for F1, F2 and P2. These should be collected together in the initial condition vector x0 = [h0,
F1,0, F2,0, P2,0]T.
Now that the preparatory work has been done to write the equations in standard form, define
the mass matrix and find a consistent set of initial conditions, you can code the solution into
MATLAB. The easiest way to start is to copy and adapt the m-files you created for method 3.
For consistency, rename tanksim3.m to tanksim4.m and tankf3.m to tankf4.m.
Note:
(1) Inside the function m-file, it may improve clarity to unpack the state vector x, into its
separate variables, e.g.
h = x(1);
F1 = x(2);
F2 = x(3);
P2 = x(4);
Otherwise you’d need, for example, to refer to pressure P2 as “x(4)”, which could get
confusing.
(2) Likewise, in the function m-file, once you have completed the calculations of the
derivative and the residuals, you would need to pack up the results into the derivative
vector for return, e.g.
Page 6
CE1532 Process Modeling and Simulation Lab
(Assuming that you called the residuals of the algebraic equations r1, r2 and r3.)
(3) In the script file, change the solver from ode45 to ode15s.
(4) To the odeset line, add options for setting the mass matrix and indicating it is singular.
If you call the mass matrix “M”, then the odeset line would look like this:
opts = odeset('Reltol',1e-5,'MassSingular','yes','Mass',M);
(5) In the script m-file, make sure you change the name of the function called by ode15s
from @tankf3 to @tankf4, otherwise you’ll be using your old function.
(6) The script m-file should produce a plot of the tank level as a function of time. Note that
the output x matrix from ode15s contains h values in the first column, F1 values in
column 2, F2 values in column 3 and P2 values in column 4. So, to extract just the level
for plotting, you could use
h = x(:,1);
Output Graph:
If you have enough time, then try the optional Challenge problem in the next section, which
deals with solving DAEs by method 2. If you are pressed for time, then move on to Section 8.
The easiest way to code this in MATLAB is probably to copy and adapt the m-files you created
for method 3. For consistency, rename tanksim3.m to tanksim2.m and tankf3.m to tankf2.m.
Note:
Page 7
CE1532 Process Modeling and Simulation Lab
(1) In the function m-file, the main change needed is to replace the sequential solution of
the algebraic equations with a call to fsolve. To do this you’d need to write a function
that returns the residuals of the three algebraic equations (you’ve already had to arrange
them in residual form for method 4). The function also needs to be passed the various
model parameters. You can do this by either the anonymous function or nested function
approach.
(2) At each time step, when fsolve is called on to solve the algebraic equation set, it needs
an initial guess for the values of F1, F2 and P2. A suitable “guess” (actually derived from
averaging the results predicted by tanksim4.m) is zguess = [22 19 130];.
(3) The script m-file should be virtually identical to the one for method 3, but make sure
you change the name of the function called by ode45 from @tankf3 to @tankf2
(5) Once you have the simulation running, if you want to suppress the “Optimization
terminated” message from fsolve. You can use optimset to generate an options
structure that you can pass as a third argument to fsolve. The optimset line needed is:
opts = optimset('Display','none');
Output Graph:
DAE solution Compare the methods in terms of (1) the How do you think the method would
method preparation needed before programming, scale up to larger, more complex
(2) the ease of programming and (3) the problems?
outputs it provides
Page 8
CE1532 Process Modeling and Simulation Lab
Method 1:
Substitute the
algebraic
equations into
the differential
equations and
use an ODE
solver
Method 2:
Solve the
algebraic
equations
simultaneously,
calculate the
derivatives and
use an ODE
solver
Method 3:
Solve the
algebraic
equations
sequentially,
calculate the
derivatives and
use an ODE
solver
Method 4:
Write the model
in standard
DAE form and
use a DAE
solver
9. Summary
At the end, you should have created several files:
tanksim1.m, tankf1.m, tanksim2.m, tankf2.m, tanksim3.m, tankf3.m, tanksim4.m,
tankf4.m
Page 9