dy(t)/dt = -y
In this equation t, representing time, is called the independent variable. The single unknown or dependent variable is y. The solution is strictly speaking y(t), i.e. y as a continuous function of t. Where the equation is solved numerically this will be a set of discrete values of y at corresponding values of t.
To enable o.d.e.s to be solved it is necessary to have a known fixed point in the solution space. For the present type of problem this will invariably be a starting or initial value of y at an initial time normally designated as t=0, i.e.:
y = y0 at t=0
This equation is one of the few o.d.e.s that has an analytical solution. If we solve it with initial condition:
y = 1 at t=0
We obtain:
y(t) = exp(-t)
To obtain a numerical solution we write:
dy/dt ~ dy/dt
Here dy and dt represent small changes in y and t rather than differentials. Hence we can calculate the change in y for a given change in t or timestep dt approximately as in general:
dy = dy/dt dt
Hence we can calculate a `new' y value since:
y(t + dt ) = y (t)
+ dy
= y (t)
+ dy(t)/dt dt
For this particular equation therefore:
dy = - y dt
And so:
y(t + dt ) = y(t)
- dt y(t)
Starting with a known value of y = y0 = 1
at t = 0:
y1 = y(dt)
= - y0 dt + y0
y2 = y(2dt)
= - y1 dt + y1
y3 = y(3dt)
= - y2 dt + y2
... etc.
This numerical solution is implemented in a simple spreadsheet. Try changing the size of the timestep in cell B7 between say 0.01 and 0.5 and see how the numerical solution compares to the analytical one.
dy(t)/dt = - k y
dy(t)/dt = - y / T
T is called the time constant of the equation. The analytical solution of this equation is now:
y(t) = exp(-t / T )
Here is a spreadsheet for the modified problem. You can change the time constant. If you experiment with different time constants and time steps, you will observe that the accuracy of the solution depends on the ratio of the time step. Be careful at this stage to keep the time step always less than the timeconstant.
In discussion of lags in `black box' models it was shown that
the time constant of a linear o.d.e. is a measure of how
fast its solution progresses towards a final steady state.
E.g. 63% in a time equal to the time constant and 90% in
approximate 2.3 times the time constant. If we wanted to observe
99% of the response towards steady state this would require a time given
by:
0.01 = exp (-t/T)
This gives gives a time approximately 4.6 times T.
Now make the time step just larger than the time constant, e.g. 1.1. Observe that the solution now starts to behave in a quite different, and completely incorrect oscillatory manner. If the time step is made exactly twice the time constant the solution oscillates between a maximum and minimum value. If the timestep is made any larger, then the oscillations will grow and eventually become infinite.
dx/dt = - x
dy/dt = x - y
Starting from initial values y0
and x0 we just apply
repeatedly the two relationships:
x1 =
- x0 dt + x0
y1 = (
x0 - y0) dt
+ y0
Here is a spreadsheet which solves a slightly more general form of these equations where each has a separately adjustable time constant, denoted by T1 and T2 respectively in the spreadsheet program.
It is clear from our investigation of stability that we must not use a time step greater than the time constant, and for reasonable accuracy it should normally be less than one tenth of the timestep. Since we would usually wish to follow most of the behaviour of the system towards its final steady state, since 4.6 times the time constant takes us 90% of the way there for a single equation a total simulation time of perhaps 5 times the time constant should be adequate,
For a single equation, or a pair of equations like those above which have the same time constant, a total of around 50 time steps would be adequate to provide sufficient accuracy and to cover the range of `interesting' behaviour.
Unfortunately, for a set of equations, the shortest time constant governs the acceptable choice of time step and longest determines the length of time for which the simulation must be run. Experiment with this using the program supplied. With the second time constant of say 5 the system will still be a long way from its final steady state, which is both variable and equal to zero, after a simulation time of 2.5. However, making the first time constant too small, i.e. less than 0.1 with the time step of 0.1, still causes instability.
This phenomenon is call stiffness and becomes a problem whenever time constants differ by more than an order of magnitude. Since in practice these may differ by several factors of 10, more sophisticated numerical methods than the one described above often have to be used to allow larger timesteps to be taken.
A complete discussion of how to develop chemical engineering models is really an complete course in chemical engineering, with practical experience besides! The objective of this section is thus only to show that there is a good, as opposed to a bad, way of constructing models. If this approach, which is systematic, is followed, then, assuming that the model builder's chemistry, physics and chemical engineering understanding of the process being modelled is correct, then there is a better chance than otherwise that the final model will be correct.
The basis of this approach to model building is that the equations which constitute a model are not arbitrary mathematical entities, but have a consistent physical basis. There are certain types of equation which describe different aspects of a model. A knowledge of this helps to ensure that all equations are written down.
Another point is that the set of model equations should be written without regard to how they might be solved. A confusing feature of some of the classical textbooks in chemical engineering is that the author, who knows the answer he wants to reach, slips steps in the solution process in amongst the construction of the model. The way to avoid confusion is to write all the equations down without attempting to perform any algebraic manipulation or simplification.
This section might therefore be subtitled: How to avoid algebra. The final models may be less elegant than those in some text books, but the computer will have no difficulty in solving them.
If the following conditions are met, then solution of a set of d.a.e.s can be handled simply as described below.
We will also assume the following additional restrictions which are not fundamental but which make solution very straightforward:
dx/dt = w - y
dy/dt = y - x
z = y - x
w = z - xº
With initial conditions:
x=1 and
y=0 at t=0
Note that:
Initialisation (once only):
O.D.E solution:
This illustrates both the nature of a d.a.e. system and the approach to its solution.
This section has a more mathematical discussion of ordinary differential equations and is provided to complement the other notes in the section.
Material balance:
Dynamic balance:
Need two constraint equations e.g. for flows
Now we have a DAE system. Get back to ODE by substituting the AEs into the material balance.
Some DAE systems are hard! Care must be used in formulating DAE systems. (See later:sections 4.4.9 and section 4.4.10).
Solution:
Here : one differential variable, M
Give initial holdup M0 at time t0 = 0
Substitute into ODE:

If we specify a constant outflow greater than the inflow (also constant) the tank may empty.
A careless simulation could then lead to the holdup becoming negative.
Care must be taken to arrange process specifications which affect dynamics so that they do not lead to infeasible situations.

Tank Outlet flow proportional to
.. from Bernoulli, if
Dynamic material balance
Can we solve it analytically?
Needs a change of variable.
Details in section 4.4.2.1 if interested.
Result:
We get t as function of M, but we want M as function of t: Awkward!

The dynamic material balance is
The integral is

m is well approximated by m0 or m1 if
(Backward difference
Implicit Euler - see later)
Solve
Calculate f(x) at t0
f0
Finite difference formula:

! The ODE is in general![]()
! The right hand side function may
! depend on time t explicitly
! as well as implicitly through
! the behaviour of x.Specify initial conditions:
Specify final time tf and step size h.
- time t0
- value of differential variable x0
Initialise counter n = 0 to count steps.
do while tn < tf
! we still have some way to go
set tn+1 = tn + hend do
if tn+1 > tf then! truncate the final step set tn+1 = tfend if
! for the last step only redefine h = tf - tn
evaluate fn = f(xn, tn)
calculate xn+1 = xn + h fn
increment n by 1
return
IC: M0 = 500 kg at t0 = 0 s
Final time:
tf = 100 s
Step size: h = 20 s
| step number | time t (s) | holdup M (kg) |
| 0 | 0 | 500 |
| 1 | 20 | 520 |
| 2 | 40 | 540 |
| 3 | 60 | 560 |
| 4 | 80 | 580 |
| 5 | 100 | 600 |
The numerical solution equals the exact solution because the exact solution is linear.
This statement must be qualified because computers do not work in exact arithmetic. Real numbers are stored in a finite form.
Perhaps 500.00000 + 20.000000 is calculated as 519.999999.
Rounding error accumulates in computer arithmetic because of the finite representation of numbers.
A further consequence of rounding error.
The algorithm contains tests to decide whether
The latter may have unexpected effects if, say,
80 + 20 = 99.99999
The algorithm will execute the loop again after reaching `100' (or as the computer thinks, 99.99999).
It will chop h to 0.00001.
Probably not serious, but output may look odd.
Exact solution is known:
k is a rate constant for decay of x.
Units: (1/seconds).
Parameter values:
k = 1 s-1
IC: x0 = 100
at: t0 = 0 s
final time: tf = 2 s
step size: h = 0.25 s
| step | time (s) | x(Euler) | x(Exact) | abs error |
| 0 | 0.00 | 100.0000 | 100.0000 | 0.0000 |
| 1 | 0.25 | 75.0000 | 77.8801 | 2.8801 |
| 2 | 0.50 | 56.2500 | 60.6531 | 4.4031 |
| 3 | 0.75 | 42.1875 | 47.2367 | 5.0492 |
| 4 | 1.00 | 31.6406 | 36.7879 | 5.1473 |
| 5 | 1.25 | 23.7305 | 28.6505 | 4.9200 |
| 6 | 1.50 | 17.7979 | 22.3130 | 4.5151 |
| 7 | 1.75 | 13.3484 | 17.3774 | 4.0290 |
| 8 | 2.00 | 10.0113 | 13.5335 | 3.5222 |

| step | time (s) | abs error | rel error |
| 0 | 0.00 | 0.0000 | 0.0000 |
| 1 | 0.25 | 2.8801 | 0.0370 |
| 2 | 0.50 | 4.4031 | 0.0726 |
| 3 | 0.75 | 5.0492 | 0.1069 |
| 4 | 1.00 | 5.1473 | 0.1399 |
| 5 | 1.25 | 4.9200 | 0.1717 |
| 6 | 1.50 | 4.5151 | 0.2024 |
| 7 | 1.75 | 4.0290 | 0.2319 |
| 8 | 2.00 | 3.5222 | 0.2603 |
Error in numerical solution related to curvature in true solution:
second derivative
Solve
One step, size h.
Euler:
x1 = x0 (1 - hk)
Exact:

k : rate constant (s-1)
1/k: time constant
or characteristic time (s)
h : time step (s)
: desired local truncation error (units of x)
General non linear equation: solution x(t)
One step: h = 0.04s
Exact:
Euler:
Error = 0.079
Two steps: h = 0.02s
First step:
(exact: 98.020)
Second step:
Error = 0.039
Local truncation error proportional to h2.Gives error over one step. Subsequent steps start with an error. Global error proportional to h.
Decaying exponential (linear ODE):
Local error declines as steady state is approached.
Global error also declines.
Maximum global error at.

Example:
V = 0.5 m3,
q = 0.002 m3/s
so T = 250 s
c1 = cf = 0.1 kmol/m3
c0 = 0 at t0 = 0
Exact solution:

| step | time | c Euler | c exact | abs error |
| 0 | 0.000 | 0.000000 | 0.000000 | 0.000000 |
| 1 | 35.355 | 0.014142 | 0.013188 | 0.000954 |
| 2 | 70.710 | 0.026284 | 0.024636 | 0.001648 |
| 3 | 106.065 | 0.036709 | 0.034575 | 0.002134 |
| 4 | 141.420 | 0.045660 | 0.043203 | 0.002457 |
| 5 | 176.775 | 0.053344 | 0.050693 | 0.002652 |
| 6 | 212.130 | 0.059942 | 0.057195 | 0.002747 |
| 7 | 247.485 | 0.065607 | 0.062840 | 0.002767 |
| 8 | 282.840 | 0.070471 | 0.067741 | 0.002731 |
| 9 | 318.195 | 0.074647 | 0.071995 | 0.002652 |
For desired local accuracy we can choose a step size which depends on time constant 1/k for linear system, or alternatively some estimate of second derivative.
local error![]()
global error![]()
linear ODE - decaying exponentialmax global error at

| step | time | c | step size | abs error |
| 0 | 0.000 | 0.000000 | ||
| 1 | 35.355 | 0.014142 | 35.355000 | 0.000954 |
| 2 | 70.710 | 0.026284 | 35.355000 | 0.001648 |
| 3 | 106.065 | 0.036709 | 35.355339 | 0.002134 |
| 4 | 144.222 | 0.046369 | 38.156248 | 0.002533 |
| 5 | 186.208 | 0.055376 | 41.986497 | 0.002857 |
| 6 | 231.751 | 0.063505 | 45.542669 | 0.003079 |
| 7 | 281.040 | 0.070700 | 49.289334 | 0.003193 |
| 8 | 335.044 | 0.077030 | 54.003740 | 0.003210 |
| 9 | 394.952 | 0.082534 | 59.907804 | 0.003135 |
| 10 | 462.030 | 0.087220 | 67.078139 | 0.002974 |
| 11 | 537.973 | 0.091102 | 75.943619 | 0.002729 |
| 12 | 625.322 | 0.094211 | 87.348124 | 0.002409 |
| 13 | 727.867 | 0.096586 | 102.545520 | 0.002025 |
| 14 | 851.443 | 0.098273 | 123.575870 | 0.001592 |
| 15 | 1000.000 | 0.099299 | 148.557098 | 0.001131 |
High accuracy needs small steps:
But if accuracy is not important, can we take large steps?
Example:
| step | time (s) | c |
| 0 | 0.0 | 0.099000 |
| 1 | 900.0 | 0.102600 |
| 2 | 1800.0 | 0.093240 |
| 3 | 2700.0 | 0.117576 |
| 4 | 3600.0 | 0.054302 |
| 5 | 4500.0 | 0.218814 |
| 6 | 5400.0 | -0.208916 |
| 7 | 6300.0 | 0.903181 |
| 8 | 7200.0 | -1.988271 |
Numerical instability if time step is large.
Reason for the instability:
1 - hk negative if
oscillations;
1 - hk < -1 if
oscillations grow.
Approximate dx/dt by backward difference.
Implicit Euler (for first step):
Must be treated as an equation for x1. If f(x) is linear, solution is easy. If f(x) is non-linear, we have a NLAE; solution needs iteration.
Implicit Euler:
Does this overcome the problem of instability?
Always between one and zero for positive k.
Tends to zero at large k.
`Strongly A-stable' method.
| step | time (s) | Implicit Euler | exact | error |
| 0 | 0.0 | 0.099000 | 0.099000 | 0.000000 |
| 1 | 900.0 | 0.099783 | 0.099973 | 0.000190 |
| 2 | 1800.0 | 0.099953 | 0.099999 | 0.000047 |
| 3 | 2700.0 | 0.099990 | 0.100000 | 0.000010 |
| 4 | 3600.0 | 0.099998 | 0.100000 | 0.000002 |
| 5 | 4500.0 | 0.100000 | 0.100000 | 0.000000 |
| 6 | 5400.0 | 0.100000 | 0.100000 | 0.000000 |
| 7 | 6300.0 | 0.100000 | 0.100000 | 0.000000 |
| 8 | 7200.0 | 0.100000 | 0.100000 | 0.000000 |
Non-linear equation: solve a NLAE at each time step.
For
Choice of NLAE method:
For short time steps direct substitution may work well enough:
Newton's method is recommended. Use the value at xn as a starting guess for xn+1, i.e.
Algorithm is similar to explicit Euler except for the point within the time step loop where we calculate xn+1.
Specify initial conditions: t0 and x0.
Specify final time tf and
step size h.
Initialise step counter n = 0.Do while tn < tf
set tn+1 = tn + hend do
if tn+1 > tf thenset tn+1 = tfend if
redefine h = tf - tn
! THIS IS DIFFERENT:
solve
for xn+1.
increment n by 1return
Initialise
x(0) = xn.
Do k = 1,max_iterations
! max_iterations is maximum number of Newton iterations at each time step
call deriv_and_fnend do
(x(k-1),tn+1, f,dfdx)
! you must supply a subroutine
! deriv_and_fn (x,t,f,dfdx) which
! when given x and t will calculate
! and return
! f = f(x,t) and dfdx =![]()
calculate g = x(k-1) - xn - h f
if |g| <nlae_tolerance exit
calculate![]()
| step | time | M | iterations |
| 0 | 0.0 | 100.000 | 0 |
| 1 | 50.0 | 169.719 | 3 |
| 2 | 100.0 | 221.043 | 3 |
| 3 | 150.0 | 259.846 | 3 |
| 4 | 200.0 | 289.654 | 3 |
| 5 | 250.0 | 312.794 | 3 |
| 6 | 300.0 | 330.890 | 3 |
| 7 | 350.0 | 345.117 | 3 |
| 8 | 400.0 | 356.346 | 3 |
| 9 | 450.0 | 365.235 | 3 |
| 10 | 500.0 | 372.280 | 2 |
Total number of iterations 29
Implicit Euler uses backward difference and is always stable for problems which tend to a steady state, no matter how large the time step.
If the ODE is non-linear, then with implicit Euler we must solve a NLAE at each time step.
Newton's method will converge quickly in most cases, using x at the start of the step as a guess for the end of the step.
Flow through two tanks
two ODEs
Assume F0 = a, constant;
,
Two ODEs in two unknowns M1, M2:
General problem:
Approximate the derivative at the start of the step (tn):
NEW NOTATION!
xi(n) is the ith differential variable at time step n.
Explicit Euler:
Specify initial conditions: time t0, value of each differential variable xi(0).Vector notation:
Specify final time tf and step size h.
Initialise step counter n = 0.
do while tn < tfset tn+1 = tn + hend do
if tn+1 > tf thenset tn+1 = tfend if
redefine h = tf - tn
! DIFFERENT
do i=1,Mcalcend do![]()
calc xi(n+1) = xi(n) + h fi(n)
increment n by 1
return
Choose step size for initial local error of 1 kg on M1 and M2.
Calculation for M1
h = 10s.
Calculation for M2
h = 7.746s.
Details in section 4.4.6.1
So choose h < 7.746 s, say h = 7 s.
Results
Note minimum in M1.
Dynamic simulation reveals interesting, possibly unexpected behaviour.
Large tanks and vessels
large time constant
want large steps.
Small volumes or fast rate processes
small time constant
need small steps.
Step size is limited by the shortest time constant in the process:
(a) for accuracy of the fast dynamics,
(b) for numerical stability,even if fast dynamics no longer needs to be accurate.
If a system contains widely different time constants it is said to be stiff. Usually this means a range of at least 3 orders of magnitude in the time constants.
Assume total holdups in steady state. Recycle flow (F3) is fraction r of feed F0.
Model mole fraction of a dilute solute. Assume tanks are well stirred with mole fractions x1 and x2. Let T1 and T2 be tank residence times.
The solute mole fractions are modelled by
Model derivation in section 4.4.6.1
Simulate with T1 = 10s, T2 = 2000s, r = 0.6, x0 = 0.1 = constant.
ODEs:
| step | time | x(1) | x(2) |
| 0 | 0.0 | 0.00000 | 0.00000 |
| 1 | 1.0 | 0.01000 | 0.00000 |
| 2 | 2.0 | 0.01840 | 0.00001 |
| 3 | 3.0 | 0.02546 | 0.00002 |
| 4 | 4.0 | 0.03138 | 0.00004 |
| 5 | 5.0 | 0.03637 | 0.00007 |
| 10 | 10.0 | 0.05160 | 0.00024 |
| 20 | 20.0 | 0.06075 | 0.00069 |
| 40 | 40.0 | 0.06295 | 0.00167 |
| 100 | 100.0 | 0.06410 | 0.00457 |
| 200 | 200.0 | 0.06585 | 0.00922 |
| 300 | 300.0 | 0.06751 | 0.01364 |
| 800 | 800.0 | 0.07469 | 0.03271 |
| 900 | 900.0 | 0.07592 | 0.03599 |
2. h = 200 s ( = 10% of larger time constant)
| step | time | x(1) | x(2) |
| 0 | 0.0 | 0.000 | 0.000 |
| 1 | 200.0 | 2.000 | 0.000 |
| 2 | 400.0 | -60.000 | 0.320 |
| 3 | 600.0 | 1865.840 | -9.331 |
| 4 | 800.0 | -57951.014 | 290.696 |
Numerical instability destroys x(1) and then x(2).
Implicit Euler: solve a system of M NLAEs in M unknowns,
x1(n+1),..,xM(n+1):
Specify initial conditions: time t0, and each xi(0).
Specify tf and h.
Initialise step counter n = 0.
do while tn < tfset tn+1 = tn + hend do
if tn+1 > tf thenset tn+1 = tfend if
redefine h = tf - tn
! DIFFERENT FROM
! EXPLICIT EULER
solve simultaneous system of M NLAEsincrement n by 1![]()
...
return
Algorithm for NLAE solution insection 4.4.6.1
Example: two tanks with recycle.Linear ODEs and initial conditions as for explicit Euler.
h = 200 s
| step | time | x(1) | x(2) |
| 0 | 0.0 | 0.00000 | 0.00000 |
| 1 | 200.0 | 0.06381 | 0.00880 |
| 2 | 400.0 | 0.06875 | 0.01707 |
| 3 | 600.0 | 0.07163 | 0.02460 |
| 4 | 800.0 | 0.07421 | 0.03144 |
| 5 | 1000.0 | 0.07655 | 0.03766 |
| 10 | 2000.0 | 0.08543 | 0.06126 |
| 15 | 3000.0 | 0.09094 | 0.07592 |
| 20 | 4000.0 | 0.09437 | 0.08504 |
Implicit Euler solves the problem very effectively with a large time step.
Values are slightly behind (lower than) explicit Euler with h = 1 s.
Systems of ODEs
Stiff ODEs
For each ODE we have a desired local error, say at t0 = 0.
Calculate step size h to meet the desired error for each equation.
Choose the smallest of the step sizes calculated.
Example
ODEs:
Let desired local error be 1 kg for both M1 and M2.
Local errors are:
M1:
.
At t = 0 this gives
We require
,
or
.
So h = 10 s.
M2:
.
At t = 0,
.
We require
,
or
.
So
s.
Overall, step is restricted to be less than 7.746 s.
Steady state flow balances, no overall accumulation in tanks:
Component balance on solute, Mi being constant:
do i = 1,M
initialise xi(0) = xi(n).end do
do k = 1,max_iterations
call multi_der_fnend dof,DFDX)
do i = 1,Mcalculate gi = xi(k-1) - xi(n) - h fiend do
do j = 1,Mcalculate DGDXij =end doDFDXij
!if i = j, or 0 otherwise
if |gi| <nlae_tolerance for all i = 1 to M, exit
solve the linear equation system
![]()
! use solve_linear
do i=1,Mcalculateend do
do i = 1,M
set xi(n+1) = xi(k-1)end do
Remark:
You must supply a subroutine
multi_der_fn (x,t,f,DFDX)which when given x and t will calculate and return f and DFDX where DFDXij =
Euler gives exact solution if and only if exact solution is linear function of time.
More sophisticated methods match exact solution to higher orders:
Can use larger step sizes for specified accuracy, but still shorter than smallest time constant.
Graph suggests two possible approaches:
Case 1: for initial local error = 0.01 x(0)
Euler step:
,
or
2nd order step:
,
or
Two function evaluations per step, but more than twice the step size (
2.77).
Case 2: for initial local error = 0.0001 x(0)
Euler step:
,
or
2nd order step:
,
or
2nd order has nearly six times the step, so nearly three times as efficient overall.
has a minimum at h k = 1, value
For h k > 2,
exceeds 1.
Stability limit at h k = 2, same as explicit Euler.
Similar limits for all higher order explicit methods (usually limiting h k in a range around 2 - 4).
Apply each stage in the time step to all equations at the same time.
Example
Modified Euler:
Extend the idea of the 2nd order explicit methods. Exactly match higher degree polynomials by making more estimates of the derivatives at suitably chosen fractions of the time step and averaging them with appropriate weights.
Up to order 4 or 5 are commonly used.
Number of function evaluations per step is usually equal to or greater than the order. But overall efficiency is better than 2nd order.
Some methods contain built-in error estimates, for only a little extra work (1 or 2 more function evaluations per step.)
Explicit RK methods all have limited stability (
).
Another way of getting higher order polynomial approximations is to use information from previous steps.
Use only one function evaluation or (for implicit methods) NLAE solution at each time step, like Euler.
BDF methods are popular. Good stability. More efficient than RK, BUT they need help getting started. Euler must be used initially, then 2nd order and so on. Also, changing step size is less flexible than with RK.
Second order means:
the method matches the exact solution if the exact solution is a quadratic:
x(t) = x(n) + a1 (t - tn) + a2 (t - tn)2.
For such a function,
.
See how the proposed methods work on
.
Compare this with the exact solution
For an exact match a second order method must have
Thus if p=1 (Improved Euler with full first order step),
we need
(average of start and end of step derivative estimates).
If
(Modified Euler with half first order step),
we need q=1 (use the mid point derivative for the full step).
These values of q are what we assumed before, so the methods are indeed second order.
Apply the general second order explicit method (with parameters p and
)
to the linear ODE
Thus
Local error
In general
In the gravity flow tank we might control the outflow with a flow controller.
Without control:
With control:
kv is a parameter which varies with valve position:
Equations:
error = set point - measured valueor
output = proportional gain * error
Assume valve 50% open at zero output signal, and linear response.
Might think
kv depends on the flow F2 through the error e and output y. If a single ODE is used:
It might be better to leave this as an algebraic equation and treat the system as a DAE (see section 4.4.9).
So
F2, kv, e, y etc. would be kept explicitly in the model and an equation would be added to define each extra variable.
Similar equations, but
Also, y = K e,
and
kv = fk(kv,1), as before.
Direct evaluation of F2 given M in this case - no iteration.
k = 0.2 kg0.5/s,
kv,max = 1.0 kg0.5/s.
Assume
Mrange = 500 kg, Ms = 300 kg, and controller gain K = -2.
Minus because low level
positive
error
negative output, which is needed to close the outflow valve.
Initial condition: M0 = 100 kg
Feed flow: F1 = 4 kg/s until t = 180 s,
then F1 = 2 kg/s
Controller fails to reach set point (300 kg) but does achieve steady state. This is normal for `proportional only' controllers. If the current steady state is not exactly the one designed for, a continuing offset is needed give a non-zero error and maintain the control action.
In this case, zero output gives 50% valve opening: kv = 0.5 kg0.5/s
For 300 kg holdup, this needs an outlet flow of
kg/s
Since the feed flow is finally 2 kg/s, we need a smaller final flow, more closed valve (18.2%) and hence positive 'error': set point > measured holdup.
The independent variable need not be time!
ODE solving methods are useful in thermodynamics, where the independent variable may be a thermodynamic variable.
Example: Pressure dependence of enthalpy at constant temperature
Find change in enthalpy from a standard pressure p0 (e.g. 1 atm = 1.013
105 Nm-2),
to required pressure p1.
Treat T and p as independent variables:
We would like to get
We could solve iteratively for v at each value of p in the integration. But this means many iterative calculations between p0 and p1.
Section 4.4.8.1 shows how to get functions
and
for Redlich-Kwong.
| step | time | Holdup | Outlet flow | % control signal |
| 0 | 0.00 | 100.0000 | 0.0000 | 0.0000 |
| 1 | 10.00 | 140.0000 | 0.0000 | 0.0000 |
| 2 | 20.00 | 178.6650 | 0.1954 | 1.4660 |
| 3 | 30.00 | 208.2578 | 1.5985 | 13.3031 |
| 4 | 40.00 | 228.7844 | 2.2156 | 21.5138 |
| 5 | 50.00 | 244.8290 | 2.5444 | 27.9316 |
| 6 | 60.00 | 258.2513 | 2.7553 | 33.3005 |
| 7 | 70.00 | 269.8923 | 2.9068 | 37.9569 |
| 8 | 80.00 | 280.2064 | 3.0238 | 42.0825 |
| 9 | 90.00 | 289.4720 | 3.1183 | 45.7888 |
| 10 | 100.00 | 297.8756 | 3.1972 | 49.1502 |
| 11 | 110.00 | 305.5505 | 3.2647 | 52.2202 |
| 12 | 120.00 | 312.5971 | 3.3235 | 55.0388 |
| 13 | 130.00 | 319.0932 | 3.3752 | 57.6373 |
| 14 | 140.00 | 325.1017 | 3.4213 | 60.0407 |
| 15 | 150.00 | 330.6740 | 3.4627 | 62.2696 |
| 16 | 160.00 | 335.8534 | 3.5001 | 64.3414 |
| 17 | 170.00 | 340.6767 | 3.5341 | 66.2707 |
| 18 | 180.00 | 345.1755 | 3.5651 | 68.0702 |
| 19 | 190.00 | 339.3776 | 3.5250 | 65.7510 |
| 20 | 200.00 | 324.6830 | 3.4181 | 59.8732 |
| 21 | 210.00 | 311.0609 | 3.3109 | 54.4243 |
| 22 | 220.00 | 298.5167 | 3.2030 | 49.4067 |
| 23 | 230.00 | 287.0576 | 3.0945 | 44.8230 |
| 24 | 240.00 | 276.6902 | 2.9854 | 40.6761 |
| .. | ...... | ........ | ...... | ....... |
| 27 | 270.00 | 252.1062 | 2.6644 | 30.8425 |
| 30 | 300.00 | 236.6112 | 2.3888 | 24.6445 |
| 33 | 330.00 | 228.1177 | 2.1995 | 21.2471 |
| 36 | 360.00 | 223.9512 | 2.0938 | 19.5805 |
| 39 | 390.00 | 222.0400 | 2.0421 | 18.8160 |
| 42 | 420.00 | 221.1926 | 2.0185 | 18.4771 |
| 45 | 450.00 | 220.8229 | 2.0080 | 18.3292 |
| 48 | 480.00 | 220.6627 | 2.0035 | 18.2651 |
| 54 | 540.00 | 220.5636 | 2.0006 | 18.2255 |
| 60 | 600.00 | 220.5452 | 2.0001 | 18.2181 |
| 66 | 660.00 | 220.5418 | 2.0000 | 18.2167 |
| 72 | 720.00 | 220.5411 | 2.0000 | 18.2165 |
In general a pressure explicit EOS is
Though rather complex to look at, these expressions are easy to evaluate given v. Non-integer powers of T = T1 would be calculated before the integration over p, to avoid repetition of the more expensive computations.
So far, ODEs only:
E.g.
If f(M) involves non-trivial calculation of other variables we may be better to `open out' the problem and make all these variables explicitly part of the formulation.
E.g. flow controller:
f(M) = F1 - F2 where F2 depends on M and kv, the valve opening.
But kv depends on F2 because of the control algorithm.
Full set of equations in section 4.4.9.1
Instead of 1 ODE (mass balance) in 1 variable (M),
we have 12 equations in 12 variables.
1 equation is an ODE (the mass balance).
The other 11 are algebraic equations.
The 6 simple specification equations are included to show the full structure that is possible, though we might prefer to regard the corresponding variables as constants or explicit functions of time instead.
General DAE system:
: MD ODEs
: MA algebraic equations.
x: variables whose time derivatives appear in the model,
y: variables whose time derivatives do not appear in the model.
x are called differential variables,
y are called algebraic variables.
We have MD differential and MA algebraic variables.
For a DAE system to be easy to solve,
it must be possible to solve the AEs
If not, `high-index' difficulties occur.
Will not be discussed in this course.
Solution of high-index problems remains an active research topic.
It's always best to make sure that a DAE system you formulate satisfies the above condition.
M is a differential variable because
appears in the model.
The other 11 variables are algebraic variables:
F1, F2, k, kv,
,
kv,max, y, K, e, Fs and Frange.
If M is given the AEs can be used to solve for the algebraic variables.
For example:
Explicit Euler:
Discretise the ODE as
Needs values for all the algebraic variables at tn.
Integration gives only the differential variable(s) at tn+1.
Then solve the AEs for the algebraic variables at tn+1 using the value calculated for M(n+1) by explicit Euler.
Solution is no different from treating the system as an ODE:
If the flow controller is treated as an ODE the initial condition is M(0) at time t0.
This must remain true if the problem is treated as a DAE system.
General Rule for ICs:
One IC for each differential variable.
A safe choice is to give the initial value of each differential variable (as here). In other cases this may be inconvenient - see next section.
What about the algebraic variables?
Consistent initial values must be found by solving the AEs at time t0 with specified values
for the differential variables.
I.e. solve
DO NOT GIVE INDEPENDENT
INITIAL CONDITIONS
FOR THE ALGEBRAIC VARIABLES.
However, initial guesses may be needed for the algebraic variables when solving the initial point equations above, e.g. by Newton's method.
Implicit Euler:
Discretise the ODE as
M(n+1), F1(n+1) and F2(n+1) are all unknown. So it makes sense to solve the discretised ODE simultaneously with the AEs at tn+1.
The unknowns in the AEs are the differential and algebraic variables at time tn+1:
This is different from the method used if we regard the system as just an ODE.
If the flow controller is treated as an ODE
Solving for M(n+1) involves an iterative loop, if f is non-linear.
Each iteration involves calculation of f at an iterated value M(k) for M(n+1).
But
f = F1 - F2 and, in the flow control example, iterative solution for F2 is required at a given M.
Thus we have an inner iterative loop for F2,
and an outer iterative loop for M.
Which is better?
Nested loops each for one unknown,
or a single loop for 12 variables
(though a partitioning scheme can easily be devised which reduces the number of simultaneous unknowns in the implicit DAE solution to 2: M and F2.)
An open question.
Probably depends on the problem.
The flow controller was solved for 200 time steps of 10 s, by which time (2000 s) steady state had been reached.
(Feed F1 and initial holdup M0 as in section 7 level control example).
Solving as a DAE needed 418 simultaneous linear equation solutions for the 12 variables, i.e. 418 Newton iterations of the full system.
(This could have been reduced to a 2 variable system with special programming.)
3 iterations per step to t=540 s, 2 iterations per step to 1610 s, 1 iteration per step thereafter.
Solving as an ODE took 464 outer loop iterations:
3 outer to 710 s, 2 outer to 1910 s, then 1 outer.
All inner loops converged in 3 secant iterations.
|
|
ODE: material balance |
|
|
specify feed flow F1 as function of time |
|
|
outflow - pressure drop equation |
| k - (0.2) = 0 | specify pipe flow constant |
|
|
valve characteristic equation |
| kv,max - (1.0) = 0 | specify fully-open valve flow constant |
|
|
valve opening as function of controller output |
| y - K e = 0 | proportional controller algorithm |
|
|
controller error definition |
| K - (1.2) = 0 | specify proportional gain |
| Fs - (2) = 0 | specify flow set point |
| Frange - (5) = 0 | specify flow range |
Constants in brackets could equally well take other values, or indeed some could be specified functions of time (e.g. Fs, perhaps K).
is a `multiple choice' function:
if
-50 < y < +50
if y > +50
if y < -50
This ensures that
,
the fractional valve opening, is always in the range 0 to 1.
There are 1 differential and 11 algebraic equations. 6 of the algebraic equations are simple specifications (constant or function of time):
F1, k, kv,max, K, Fs, Frange.
There are 12 variables (`in order of appearance'):
| M | tank holdup |
| F1 | feed flow |
| F2 | outlet flow |
| k | pipe flow constant |
| kv | valve characteristic |
| valve open fraction | |
| kv,max | valve flow constant (fully open) |
| y | controller output |
| K | controller proportional gain |
| e | controller error signal |
| Fs | flow set point |
| Frange | flow range for controller |
y1(0) = 1, y2(0) = 1These are NOT initial conditions for the y variables, because they do not satisfy both the AEs. Applying Newton we get
| y1 | 1.0000 | 1.2000 | 1.2710 | 1.2720 | = y1(0) |
| y2 | 1.0000 | 0.8000 | 0.7860 | 0.7862 | = y2(0) |
| x | 1.0000 | 0.9921 |
| y1 | 1.2720 | 1.2578 |
| y2 | 0.7862 | 0.7825 |
Build a material and energy balance model for a tank with water flows in and out.
Tank is assumed to have non-constant cross section: sloping sides.
Gravity outflow model should be modified, and expressed in terms of level instead of holdup. Therefore we need tank geometry equations.
Energy balance introduces energy and enthalpy. Temperature is a more convenient quantity to specify, and to express thermal equilibrium. Hence thermodynamic and physical property equations are needed.
See section 4.4.10.1, `Material and energy balance for a tank'.
For each equation
Choose something sensible. If there are no suitable `unsolved' candidates, go back to where there was a choice, make a different choice and repeat from there. If making a different choice further back is not possible, consider whether the new equation repeats earlier information.
When all modelling equations have been written there will generally be some `unsolved' variables left. This is because there are usually more variables than equations in the basic model of a unit. The remaining degrees of freedom in the model can be satisfied by specifying the variables which are left on the `unsolved' list.
For a dynamic model you get a DAE system.
In counting equations and variables exclude the independent variable (time).
Apply the following `ticking off' rule for the ODEs in a DAE system:
For an ODE![]()
always tick off the differential variable xi.
This ensures that the specifications obtained from the final unsolved variable list do not cause a difficult (high index) DAE system.
The ticking-off rule for ODEs also means that if we replace the ODEs with `standard' initial conditions at t0, the AEs can still be ticked off in exactly the same way, because if we replace
It is always safe to use standard ICs for a well-behaved DAE, but not always convenient.
E.g. do we really know the initial holdup and internal energy in the tank? Liquid level and temperature might be preferable for ICs.
In a DAE system it may be acceptable to give ICs for some of the algebraic variables instead of differential variables, as long as
Having constructed a model, including proposed specifications and initial conditions, you should perform two checks.
Check 1 verifies that the DAE system with standard ICs is well-behaved (not high index).
Check 2 verifies that the proposed ICs are in some sense equivalent to the standard ICs for x.
In the event of a problem ..
If check 1 fails, the specifications and perhaps the modelling equations themselves need to be re-examined.
If check 1 succeeds but check 2 fails, you need different ICs.
The standard ICs can always be used.
It is important not to confuse specifications and initial conditions. Specifications are valid throughout the simulation: for example F1, T1, p. ICs are valid only at the starting point t0: for example L, T.
Never try to give ICs for the same variables as specifications.
The tank with material and energy balances is perhaps the simplest non-trivial dynamic process model.
General modelling must also take account of composition dynamics, where there is more than one chemical component.
Thus we should also have equations for the individual component balances. The `well mixed' assumption requires extra equations equating concentrations in the outlet stream to those in the tank.
More complex models (e.g. for an enclosed vessel) should introduce the possibility of multiple phases (e.g. vapour + liquid), with some way of relating them (e.g. phase equilibrium or a mass transfer model).
Then there is an endless range of possible reactor models!
DAE formulation fits well with the `equation-based approach' to process simulation, which at steady state views the process model as a system of NLAEs:
`Sequential modular simulators' view the process as a collection of unit operations. Each unit is solved independently in sequence (or nowadays sometimes in parallel). This approach may lend itself more to the ODE formulation for some (simple) units, and a DAE approach in others.
The nested iterations of the ODE method may be slower, but also tend to converge more reliably, than the simultaneous solution of the DAE method.
Assume water properties.
Equation name |
Equation |
Variables (units) |
|
| Balances | |||
| Material balance |
|
M | holdup (kg) |
| F1 | feed flow (kg/s) | ||
| F2 | outlet flow (kg/s) | ||
| Energy balance |
|
U | internal energy of contents (kJ) |
| (note 1) | h1 | feed specific enthalpy (kJ/kg) | |
| h2 | outlet specific enthalpy (kJ/kg) | ||
| Tank Model | |||
| gravity flow model |
|
v | specific volume ( m3/kg) |
| L | level of contents (m) | ||
| `well mixed' model | T - T2 = 0 | T | tank temperature (0C) |
| T2 | outlet temperature (0C) | ||
| mass - volume rel'n | M v - V = 0 | V | volume of contents (m3) |
| volume - level rel'n | V - 0.2 ((1 + 0.8 L)3 - 1) = 0 | ||
| (note 2) | |||
| Thermo and | definitions | ||
| energy - enthalpy rel'n | u - h + pv = 0 | u | specific internal energy (kJ/kg) |
| h | specific enthalpy (tank) (kJ/kg) | ||
| p | pressure (kPa) (note 3) | ||
| specific energy def'n | U - M u = 0 | ||
| Phys props | |||
| enthalpy correl'n | h1 - 4.1846 T1 = 0 | T1 | feed temperature (0C) |
| (feed) | |||
| enthalpy correl'n | h - 4.1846 T = 0 | ||
| (tank) | |||
| enthalpy correl'n | h2 - 4.1846 T2 = 0 | ||
| (outlet) | |||
| density correl'n | (998.23 - 0.255 (T - 20)) v - 1 = 0 | ||
| Specifications | |||
| feed flow spec |
|
||
| feed temp. spec | T1 - 20 = 0 | ||
| pressure spec | p - 100 = 0 |
Notes:
dx/dt = (u-x) / T1
dy/dt = (x-y) / T2
u = mu (s - y)
The initial conditions are: x = 1 ; y = 0 at t=0 This represents a process consisting of two lags in series, with its out put y regulated at setpoint s with a proportional controller with gain mu.
You can copy the model here generate a spreadsheet from it.
Notice the form of the simple proportional controller equation. If we wanted to model a P+I controller this would require a further differential equation to model the integral action.
We can write the equations for a P+I controller with input measurement y and output u as follows:
e = s - y ; ! error u = mu * (e + ei/ti) ! controller algebraic equation .ei = e ! o.d.e. for integral actionHere mu is the controller gain and ti is the integral action time.
The modified program is here. Save it and try it out.
Click here to go to the model generator.
Click here to go to the model generator.