
3.10 More Examples 221
12: dSdt=-beta(T(t),S)*X/Yes
13: list(c(dXdt,dNdt,dEdt,dSdt))
14: }
The general structure of the dfn function has been discussed in Section 3.8.3.
The correspondence between Equations 3.295–3.298 with lines 9–12 of program
3.299 is obvious. You may be irritated by the fact that the time dependence of
r(t) and T(t) is written explicitly in the code, while this is not done for the state
variables
X, N, E and S. This is related with the fact that Equations 3.295–3.298 are
a nonautonomous system of ODEs. As it was discussed in Section 3.5.3, this means
that at least one of the right-hand sides of these equations depends on t not only
(implicitly) via the state variables, but also via some explicitly given functions of t.
Looking at the equations, you see that all right-hand sides of the system depend
explicitly on the temperature T(t) or on the nitrogen supply rate r(t). When writing
down the
dfn function for nonautonomous ODEs, you must take care that only the
state variables are written without an explicit ‘‘
(t)’’, while all other time-dependent
function must be written similar to
T(t), r(t) and so on.
But even if you have done this correctly, you might run into another problem
characteristic of nonautonomous ODEs when you try to compute the numerical
solution. Remember our definition of the nitrogen supply rate in Equation 3.294.
In the above example, nitrogen is supplied at two times only, t
1
add
= 130 h and
t
2
add
= 181 h. Since the overall time interval in the example is from 0 to 400 h,
this means that the function r(t) defined in Equation 3.294 is almost everywhere
zero, except for two small 1-h intervals around t
1
add
= 130 h and t
2
add
= 181 h.
Remember also the discussion of
lsoda in Section 3.8: lsoda determines its
stepsize h automatically, choosing the stepsize as small as it is required by the error
tolerances, but not smaller. Now if
lsoda sets the stepsize relatively large around
t
1
add
= 130 h and t
2
add
= 181 h, it may happen that it ignores the nitrogen supply
rate r(t) in the sense that r(t) is never evaluated at one of its nonzero points. Then,
although you prescribe a substantial nitrogen supply in two small 1-h intervals
around t
1
add
= 130 h and t
2
add
= 181 h, you will not see this in the solution, that is,
the problem is solved by
lsoda in a way as if there would be no nitrogen supply
at all (r(t) = 0). Implementing the fermentation model yourself by an appropriate
editing of
ODEFitEx1.r, you will see that you get exactly this problem when you
do not apply appropriate measures to avoid this.
Note 3.10.2 (Problem with short external events) Numerical solutions of
nonautonomous ODE systems may ignore short external events (such as nitrogen
addition in the wine fermentation model).
The first and simplest thing that one can do is to prescribe a maximum stepsize
in
lsoda, which can be done using an argument of lsoda called hmax (see [108,
109] and R’s hep pages). Choosing
hmax sufficiently small, one can make sure that
r(t) is used in the computation as desired. For example,, based on
hmax=1/10, r(t)