• No results found

BOX 1.3 MATLAB SCRIPT TO SIMULATE THE RICKER MODEL IT_max =20; % Maximum number of steps

1

1

max (1.109)

This equation describes the evolution of a population xt as a function of its carrying capacity K and its growth rate Rmax. In spite of its innocent look, Equation 1.109 may exhibit surprisingly differing behaviours depending on the value of Rmax, as already pointed out in Section 1.9.6.1. The pertinent MATLAB script is listed in Box 1.3, and the model responses for two differing values of Rmax are shown in Figure 1.48. In Figure 1.48a, the growth rate R is such that it drives the population to a steady state through damped oscillations, whereas in b the large value of R produces a pseudo-random chaotic behaviour.

1.10.2 matlaB handlingoF continuous-timE modEls

The simulation of continuous-time model is considerably more complex because it involves the numerical solution of differential equations. Basically, there are two kinds of differential equa-tions: those that start from an initial condition and move forward, and those for which the initial and final points of the solution are given. The first kind gives rise to the so-called initial-value problem, whereas the second defines a boundary-value problem. Although it is fairly straightfor-ward to perform forstraightfor-ward integration from a given initial condition, composed of the initial val-ues of the function and of its derivatives, it is more difficult to satisfy the boundary condition, in

BOX 1.3 MATLAB SCRIPT TO SIMULATE THE RICKER MODEL IT_max=20; % Maximum number of steps

K=20; % Carrying capacity Rmax=6.8; % Growth rate x=0.5*K; % Initial population

Xs=zeros(1,IT_max); % Prepare empty vector of results Xs(1)=x; % Set the initial population

% as the first vector element

% Start simulation for t=1:IT_max x1=x*Rmax^(1-x/K);

Xs(t)=x; % Store partial result

x=x1; % Exchange the new state for the old end

Figure(1) plot(Xs,’-o’) xlabel(‘time’) ylabel(‘Population’)

which the conditions are specified at both ends of the integration domain. In this case, successive approximation of the initial derivatives is required, in order to satisfy the conditions at the other extreme of the integration interval. The successive approximation of the initial derivative originates the so-called shooting method, reminiscing of the improving accuracy in hitting a target repre-sented by the final (boundary) condition, as will be considered in Chapter 6. The two methods are sketched in Figure 1.49 for a first-order differential equation.

Since we are interested in the solution starting from an initial condition x0 onwards, we shall consider only initial-value problems for the time being, although boundary-value problems may be encountered in river quality modelling and will be treated in Chapter 6. In practice, given a dif-ferential equation x f x=

( )

, we want to extend the solution from x t

( )

to x t h

(

+

)

. This requires an approximation of the derivative to project the solution from the current time t to the next integration time t + h, with h being the integration step. The crudest (and inaccurate) numerical approximation of the derivative is the Euler method, which projects the current value of the solution x t

( )

along

the tangent at f x

( )

so that x t h

(

+

)

is approximated as shown in Figure 1.50, where the error in estimating x t h

(

+

)

is quite evident.

Instead of moving from t to t + h in a single step, more accurate methods use intermediate steps to improve the derivative projection. The fourth-order Runge–Kutta (FORK) method is a very popular (and accurate) algorithm that approximates x t h

(

+

)

based on intermediate function evaluations in

R = 6.8 Sample

(a) (b)

Population density

30 25 20 15 10

50 2 4 6 8 10 12 14 16 18 20

R = 18

Population density

0 2 4 6 8 10 12 14 16 18 20

Sample 60

50 40 30 20 10 0

FIGURE 1.48 Differing behaviours of the Ricker equation (1.109) as a function of the growth rate R. In (a), a moderate value of R produces a damped oscillatory stable response, whereas in (b) a large R value results in a chaotic behaviour.

(a) (b)

t f (t0)

tf

f (t0) f (t0)

f (t1)

t0 t0

f (t) f (x)

t

FIGURE 1.49 Possible integration conditions of a first-order differential equation. In the initial-value problem (a), the solution is specified on the basis of the initial value of the function and its derivative, whereas in the boundary-value problem (b), the value of the function is specified at the boundaries of the solution domain.

the middle of the integration interval

(

t h+ 2 , as shown in Figure 1.51a. The intermediate weights

)

There is a vast body of literature concerning numerical integration methods based on the FORK algorithm, one of its most important variations being the adaptation of the integration step size h, as shown in Figure 1.51b, depending of the shape of the function f x

( )

to maintain a constant integra-tion error, or the ability to deal with stiff systems of differential equaintegra-tions (Quinney, 1987), involv-ing simultaneous equations with widely differinvolv-ing time scales. In this latter case, the difficulty is represented by the choice of the integration step h, because fast equations would require a short step h, but this would unnecessarily hamper the solution of slow equations for which a longer h would

t

FIGURE 1.50 Illustration of the Euler numerical integration method.

k1

FIGURE 1.51 Graphical illustration of the FORK integration method (a) and the need to adapt the integra-tion step to the funcintegra-tion shape (b).

suffice. For a deeper understanding of the fascinating world of numerical methods, the reader is referred to the abundant available literature (see, e.g. Athans et al., 1974; Chapra and Canale, 1991;

Press et al., 1986; Dormand, 1996; Dahlquist and Bjorck, 2003). In particular, differential equa-tions are accurately treated by Butcher (2003), whereas boundary-value problems are considered by Quinney (1987).

Coming to the use of MATLAB for solving systems of ordinary differential equations, there are a number of different solvers and it is up to the user to select the most appropriate method for the problem at hand. These methods fall in two main categories: fixed step and variable step, based on either the Euler or Runge–Kutta algorithm just explained, with the latter method offering several versions, depending on the order of the approximation. There are also specific methods for stiff systems of equations.

1.10.3 introducingthE simulink toolBox

There are two ways for handling initial-value problems in MATLAB: one is to use the appropriate primitive, with the following syntax [t, x] = ode45(‘model _ name’,[time _ span], x0) where ode45, among others, is the differential equation solver using a FORK method, and the function ‘model _ name’ contains the model definition. The other, immensely more powerful, approach is to build the model using the Simulink® toolbox and running it from a calling MATLAB script. The great advantage of building the model with Simulink is the friendly graphical editor, the huge paradigm of available functions and the wide choice of integration methods. Further, a Simulink-implemented model controlled by a MATLAB script can be linked to all the other func-tionalities of the platform, such as optimization, signal analysis and graphics.

There are of course other simulation platforms to build and simulate nonlinear dynamical mod-els. One that gained some favour in the ecological community is STELLA (http://www.iseesystems.

com), which, however, does not compare to MATLAB in terms of functional capabilities and ease of use. Also, STELLA uses a graphical language that follows the notation introduced by Odum (1983), which won little acceptance outside the ecological circles, because the resulting diagrams are not as intuitive as the Simulink models.

The combined MATLAB/Simulink approach will be used throughout this book, but first let us try the purely MATLAB-based approach, if anything, to discover its limitations. As an example, consider the well-known logistic equation describing the growth of a single-species population (Chapter 5  will be devoted to population dynamics). This continuous-time differential equation describes the growth rate of a single species as

dx

dt r x x

= ⋅ ⋅ − K

 



1 (1.111)

where:

r is the growth rate

K is the population carrying capacity, that is, its limit population density

These parameters, though conceptually equivalent to those defined in Equation 1.109, have a significantly different meaning and role, as will be explained in Chapter 5. Solving this equa-tion with MATLAB requires the prior definiequa-tion of the right-hand side of Equaequa-tion 1.111 as a MATLAB function. Then, a MATLAB script is defined in which the ode45 solver calls this function to compute the solution. The two pieces of code (function and main script) are listed in Box 1.4. They must be saved as separate file, with the logist function being invoked by the calling MATLAB script. Let us examine the pros and cons of this modelling approach: packing all the model equation in a single function is certainly handy, but lacks flexibility. However, the main limitation of this method is the inability to interact with the model during runtime, for

example, changing the inputs and/or the parameters during the simulation. The latter is a fun-damental aspect of the model identification techniques treated in Chapter 2. Another difficulty is parameters passing the definition of the logist function requires the input arguments to be the time and the integration variable, that is, (t, x). If we want to experiment with the effect of varying the growth rate r or the carrying capacity K, these parameters should be passed to the function by declaring them global, enabling the function logist to access their values, set by the MATLAB script, in the common memory area reserved to MATLAB-named Workspace.

This method of parameter passing will be instrumental when using the combined MATLAB/

Simulink approach for parameter estimation and optimization. As a bottom line, the simulation of continuous time models using the direct MATLAB integration is rather clumsy and is defi-nitely unsuitable for the kind of problems that we shall be treating in this book. Conversely, the Simulink graphic environment represents a very attractive alternative for handling all kinds of models, either continuous-time, discrete-time or a hybrid combination of the two. This toolbox has a very intuitive user interface consisting of a window into which graphic symbols represent-ing computational functions can be dragged from the library palette and connected (wired) to form a diagram that represents the model. Behind the graphical interface, Simulink generates an m-file, invisible to the user, that is run by the MATLAB interpreter like any other MATLAB script when the pertinent command (sim) is invoked. The computation of a Simulink diagram is based on the data-flow approach, whereby the output of each block is produced as soon as all

BOX 1.4 MATLAB SCRIPT FOR THE DIRECT