• No results found

BOX 1.4 MATLAB SCRIPT FOR THE DIRECT SOLVING OF THE LOGISTIC EQUATION

% Example of Logistic equation

% direct solution with the

% Matlab solver ODE45

global r, K % make r and K visible to Logist t0=0;tfin=40; % Simulation interval

K=10; % Carrying capacity x0 = 0.5; % Initial population

% Call the ODE solver in the loop for r=0.2:0.1:0.4

[t,x] = ode45(‘logist’,[t0 tfin], x0);

% Plot of the solution Figure(1)

plot(t,x) hold on end

% Plot the results xlabel(‘time’) ylabel(‘population’)

title(‘Logistic equation with ODE45 solver’) function xp = logist(t,x)

global r, K% Repeat the global statement in the % receiving function

% Logistic equation xp = r*x*(1-x/K);

the required data are available at its inputs. Simulink has a powerful sorting algorithm, which schedules the block evaluation order to comply with the data flow of the diagram. The logical data availability implicit in the sorting requires that no block in a loop has contemporary data at its input and outputs, in which case an algebraic loop error is generated. To avoid this, each loop should contain at least one integrator, or another memory element like a delay block, to avoid the simultaneity issue.

Though the generated Simulink file (with extension *.mdl) can be run on its own, it is very convenient to control its execution from a calling MATLAB script as previously described, so that the MATLAB/Simulink model combination has a nested structure as illustrated in Figure 1.52. The Simulink model is invoked with the command sim, with the following minimal syntax:

[t, x, y] = sim(‘Model_Name’,t_final); (1.112) where the left-hand side contains three vectors, respectively, containing the simulation times (t) and the corresponding integrator outputs (x) and model outputs (y). The model name is a string refer-ring to the model that we want to simulate (without the.mdl extension), from 0 until the final time t _ final. Other options controlling the simulation may be added by the expert user.

The example of Figure 1.52 refers again to the logistic model, so that this approach can be com-pared with the previous one, based on the ode45 MATLAB solver. In this simple example, the two methods produce identical results, as Figure 1.53 shows, but with the model complexity growing, the combined MATLAB/Simulink approach provides superior flexibility and is definitely the pre-ferred approach to ecosystem modelling.

In fact, the MATLAB environment provides the amplest freedom in specifying the simulation conditions and in plotting the results, whereas the Simulink toolbox, with its a graphical representa-tion, provides the highest flexibility in model building. Further, running the model from a calling MATLAB script enables full control of the simulation in the most diverse conditions. One last advantage of the Simulink approach is the ability to feed the model with numerically defined input functions, a feature that is of paramount importance when calibrating models subject to experimen-tal inputs.

MATLAB SIMULINK

Model

x dxdt = r ⋅ x ⋅ 1 − K clear

% Define parameters

% and initial conditions K=10;tfin=40;x0=0.5;

%

% Simulation loop

%

figure(1) hold off;clf for r=0.2:0.1:0.4

[t,x,y]=sim('logistic',tfin);

plot(t,x) hold on end

FIGURE 1.52 Nested structure of Simulink-implemented model controlled by a MATLAB script. The arrow shows the connection between the two environment, with Simulink launched by the MATLAB command sim.

1.10.4 tWo simPlE BEnchmark modEls

Chapter 2 will deal with model identification, and of course some example models will be required to demonstrate the various properties and procedures. Therefore, we now introduce two very sim-ple, but much studied, models that will be used as benchmarks in Chapter 2.

1.10.4.1 The Monod Kinetics

This model, which will be treated in great detail in Chapter 7, is amply described in the biochemical and environmental literature (Maynard Smith, 1974; Holmberg, 1982; Campbell, 1983; Roels, 1983;

Bailey and Ollis, 1986; Battley, 1987; Orhon and Artan, 1994; Cloete and Muyima, 1997; Jorgensen and Bendoricchio, 2001), and it is at the basis of all the models describing the interaction between a biodegradable substrate and a microbial mass that feeds upon it. The full derivation of the model will be described in Chapter 7, but for now it suffices to introduce its basic equations and show its MATLAB implementation. The model equations describing the substrate consumption and the bio-mass growth in a batch, that is, a closed vessel, are

Substrate 1

Though the derivation of the model and the biochemical meaning of its parameters will be explained in due course, some basic considerations can be anticipated here:

• The substrate is supposed to be totally biodegradable, and therefore it decays to zero, being consumed by the biomass. Its rate of consumption follows the same dynamics of the bio-mass growth and the proportion (yield factor) Y denotes the fact that for each unit of con-sumed substrate, Y units of biomass are produced, that is, Y =XS.

• A fixed fraction (bh) of the biomass decays.

• The biomass growth rate cannot exceed its maximum value

(

µmax

)

.

• The relative speed of the reaction is represented by the so-called half-saturation constant Ks

( )

, where a small value implies that the biomass grows at a high rate (near μmax) even with a limited substrate availability.

0 5 10 15 20 25 30 35 40

Population r = 0.2

r = 0.4

FIGURE 1.53 Simulation of the logistic growth equation with the direct ode45 solver (a) or via Simulink (b), with the ode45 (Dormand-Prince) integration method. The results obviously coincide.

As in the previous examples, Equation 1.113 will be implemented with a Simulink model, shown in Figure 1.54, controlled by a MATLAB script (Box 1.5).

The resulting batch simulation is shown in Figure  1.55. Here, the substrate is gradually con-sumed, providing nourishment for the biomass, which starts growing exponentially after an initial incubation period. When the substrate is not sufficient to sustain the rising population, the biomass stops growing and begins to decline until all the available substrate in the batch, including the decay material produced by the dead biomass, is exhausted.

This model is at the basis of many biotechnical and environmental microbial models and will be used in Chapter 2 as an important benchmark for model identification.

1.10.4.2 The Streeter & Phelps Water Quality Model

Another fundamental, and tutorially relevant, model is the river water quality dynamics due to Streeter and Phelps (S&P) to explain the dissolved oxygen (DO) sagging curve in a river, down-stream of a pollution point source (Rinaldi et al., 1979; Tchobanoglous and Schroeder, 1985;

Chapra, 1997). This oversimplified model describes the interaction between a biodegradable BOX 1.5 MATLAB SCRIPT LAUNCHING THE BATCH