0% found this document useful (0 votes)
93 views

Process Modelling and Simulation

The document describes a laboratory experiment on solving differential algebraic equations (DAEs) using MATLAB. It presents a model of a tank system as a DAE problem and provides instructions on solving the model using four different DAE solution methods. The tank model is described by a set of equations involving the tank level, inlet and outlet flows, and pressures. Students are asked to code the model using the four methods and compare the results.

Uploaded by

sukhmani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views

Process Modelling and Simulation

The document describes a laboratory experiment on solving differential algebraic equations (DAEs) using MATLAB. It presents a model of a tank system as a DAE problem and provides instructions on solving the model using four different DAE solution methods. The tank model is described by a set of equations involving the tank level, inlet and outlet flows, and pressures. Students are asked to code the model using the four methods and compare the results.

Uploaded by

sukhmani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

CE1532 Process Modeling and Simulation Lab

Process Modelling and Simulation Lab (CE 1532)

LAB 4: DIFFERENTIAL ALGEBRAIC EQUATIONS


(DAE)
1. Aim
 To solve differential algebraic equation (DAE) in MATLAB
 Allow you to better understand the dynamic response of a typical model

2. Differential Algebraic Equations (DAE)


There are various engineering applications (process engineering, mechanical
engineering, electrical engineering, mechatronics etc.) that lead DAE model equations.
In DAE, the derivatives are not, in general, expressed explicitly. In fact, derivatives of some
of the dependent variables typically do not appear in the equations. For example, the
following equations does not explicitly contain any derivatives of y.
x' ' (t )  y (t )  cos(t )
x(t )  cos(t )
Such variables are often referred to as algebraic variables. The general form of a system of
DAEs is represented as:
F (t , x, x' )  0
Solving systems of DAEs often involves many steps. Generally, a system of DAEs can be
converted to a system of ODEs by differentiating it with respect to the independent variable.

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

Defining, x = (V, CA, CB, CC)T


z = (R1, R2)T
CSTR model equations may be rewritten as:
x  f ( x, z )
0  g ( x, z )

2. An open tank model


A dynamic model for the level (h) of an open tank with one inlet and one outlet stream, both
of which flow through fixed valves, may be given by the following DAE system:

𝑑ℎ 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.

Parameter Value Comment


A 12.5 m2 Tank cross-sectional area
Cv1 3.41 (m3/h) / kPa1/2 Upstream valve coefficient
Cv2 3.41 (m3/h) / kPa1/2 Downstream valve coefficient
P0 100 kPa Atmospheric pressure
Ρ1 P1function.m (kPa) Upstream pressure (forcing function)
P3 100 kPa Downstream pressure
Ρ 1000 kg/m3 Fluid density
G 9.81 m/s2 Gravitational acceleration
tf 72 h Final simulation time
h0 2m Initial tank level

220
The imposed time variation of P1
200
is shown opposite. You can
P (kPa)

produce this graph if you like by 180

running P1plot.m. The tank level


1

160

responds to this changing 140

upstream pressure. 120


0 10 20 30 40 50 60 70 80
Time (h)

3. Summary of m-files to be created


Solve the tank model using three or four of the DAE solution techniques outlined below. All
the methods require the creation of a script m-file, which sets up, drives the simulation and
does plotting, and a function m-file, which contains the model equations and is called by one

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:

DAE solution method Suggested script m- Suggested function


file name m-file name
Method 1: Substitute the algebraic tanksim1.m tankf1.m
equations into the differential equations
and use an ODE solver
Method 2: Solve the algebraic equations tanksim2.m tankf2.m
simultaneously, calculate the derivatives
and use an ODE solver
Method 3: Solve the algebraic equations tanksim3.m tankf3.m
sequentially, calculate the derivatives and
use an ODE solver
Method 4: Write the model in standard tanksim4.m tankf4.m
DAE form and use a DAE solver

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.

4. Solving the tank model by DAE method 1


The first step in solving the DAE, given by equations (1)–(4), by this method is to symbolically
substitute the algebraic equations (2)–(4) into differential equation (1). This converts the DAE
into a pure ODE system. The ODE can then be solved using a standard ODE solver like
MATLAB’s ode45.

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

Script File: tanksim1.m


clc
global A CV1 CV2 P0 P3 rho g
% Tank model parameters and constants
A = 12.5; % m^2
CV1 = 3.41; % (m^3/h)/kPa^0.5
CV2 = 3.41; % (m^3/h)/kPa^0.5
P0 = 100; % kPa
P3 = 100; % kPa
rho = 1000; % kg/m^3
g = 9.81; % m/s^2
tspan=[0 72];
h0=2;
options=odeset('Reltol',1e-5);
[t,h]=ode45(@tankf3,tspan,h0,options);
plot(t,h)%tank level verswus time
xlabel('simulation time')
ylabel('tank level')
legend('method1')
title('tank level as a function of time')
grid on

Function File: tankf1.m


function dhdt=tankf3(t,h)
global A CV1 CV2 P0 P3 rho g
P1=P1function(t)
P2=(P0+((rho*g*h)/1000))
F1=(CV1)*(sqrt(P1-P2))
F2=(CV2)*(sqrt(P2-P3))
P1=P1function(t);
dhdt=(1/A)*(F1-F2);
end

Output Graph:

Page 4
CE1532 Process Modeling and Simulation Lab

5. Solving the tank model by DAE method 3


DAE solution method 3 involves ordering (and rearranging if necessary) the algebraic
equations so that they can be evaluated sequentially to determine the values of algebraic
variables. For complicated sets of algebraic equations a systematic method, such as the Sargent-
Westerberg algorithm, is needed to determining the equation partitions and precedence order.
However, the required order for the tank model equations can be established just by inspection.

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

Script File: tanksim3.m

Function File: tankf3.m

Output Graph:

6. Solving the tank model by DAE method 4


This involves arranging the model equations into a standard form (first order, semi-explicit
DAE form) then calling a DAE solver. In fact, ode15s, which you used in last lab, can solve
both ODEs and DAEs, but you need to tell it to switch over to DAE mode. You do this by
setting options using odeset that tell it to use a mass matrix and also to say that the mass
matrix is singular.

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.

dxdt = [dhdt r1 r2 r3]';

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

(7) Include any relevant comments in your two m-files.

Script File: tanksim4.m

Function File: tankf4.m

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.

7. Challenge problem (optional): Solving the tank model by DAE method 2


This is similar in concept to method 3, expect instead of analysing the algebraic equations and
arranging them for a sequential solution, you solve them simultaneously with a zero-finding
routine. MATLAB’s function fsolve can do this. It’s similar in syntax and functionality to
fzero, but can solve multiple simultaneous nonlinear equations.

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

(4) Include any relevant comments in your two m-files.

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

Script File: tanksim2.m

Function File: tankf2.m

Output Graph:

8. Comparing the methods


Reflect on the different methods for solving the same DAE system and complete the table below.

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

You might also like