You are on page 1of 13

# A K V A M O D

I nt e g r a t i ng e duc a t i on i n t he r e s u n d r e gi on
Mode l l i ng Aqua t i c s y s t ems Conne c t i ng r e s e a r c h

Theoretical population ecology exercises in MATLAB

Teaching material from the Theoretical Ecology Division at
Lund University
adopted for AKVAMOD partners by Anders Brodin

INTRODUCTION
Population ecology is an unusual branch in ecology in the sense that it basically is a
theoretical and mathematical discipline. It is based on mathematical equations that
are used to describe population level effects of phenomena such as competition,
migration and predation. The most well-known of these are known as the Lotka-
Volterra system where growth equations for interacting populations are connected in
some way. Many of these equations can appear both in a discrete form (for example
year to year changes in an annual organism) and a differential form (continuous
changes with overlapping generations). Ecology students tend to find mathematics
difficult and hard to digest. Still, knowledge of the basic theoretical population
ecology framework is fundamental for all ecologists. The exercises in this
compendium start from a basic level, working you through the most common
equations in population ecology. They are interactive, going through the subject step
by step, thereby hopefully giving you a hands-on feeling for population dynamics.

The exercise: You can copy the code from a downloadable Word file into the
MATLAB editor. This may help you to avoid typing errors, but also copying may give
some errors when you paste into MATLAB. A frequent such error is that citation or
quotation marks that should be straight in MATLAB ( ' ) may become comma shaped
in Word (
,

http://www.teorekol.lu.se/~abrodin/AKVAMOD/CODE.doc

This material belongs to AKVAMOD
Any copying or use requires permission.
www.Akvamod.dk
A K V A M O D

I nt e g r a t i ng e duc a t i on i n t he r e s u n d r e gi on
Mode l l i ng Aqua t i c s y s t ems Conne c t i ng r e s e a r c h

MODELS IN POPULATION ECOLOGY
1 Population growth
If an organism is not limited by its resources the growth rate, from time t to t+1, of
the population to which this organism belongs can be described by the equation:

(1)
t t t
N D BN N ) 1 (
1
+ =
+

N
t
is the size of the population at time t, B is the number of offspring per individual
and D is the probability that an individual will die in the period between t and t+1
(this period can for example be a year). The population size at time t+1, denoted by
N
t+1
, is the sum of those born during the year and those that survived from year t.
For simplicity one can combine the number of offspring and deaths into one
parameter, r, which frequently is called "the intrinsic rate of increase". Then

(2) r = 1+(B D)

which gives us

(3) N
t +1
= rN
t

If you start at time t=0, with population size N
0
and each time step is a year the size
of the population one year later will be

(4) N
1
= rN
0

After two years the population will be:

(5) N
2
= rN
1
= r
2
N
0

and so on. Hence the size of the population after t years will be

(6)
N
t
=
r
t
N
0

In reality time is a continuous variable, but in many cases it may be more practical to
regard it as discrete, that is divided into equally long intervals of specified length. In
calculations with a discrete time scale the births and deaths during one interval are
considered to occur simultaneously, before the start of the next time period.

If the time scale is continuous we must express changes in population size over time
in differential equations rather than difference equations. Rather than expressing r as
in equation (2) we should then express it as the net change in population

(7) D B r =

which gives us the to possibility to express the growth rate as

(8) rN
dt
dN
=

This material belongs to AKVAMOD
Any copying or use requires permission.
www.Akvamod.dk
A K V A M O D

I nt e g r a t i ng e duc a t i on i n t he r e s u n d r e gi on
Mode l l i ng Aqua t i c s y s t ems Conne c t i ng r e s e a r c h

which is a differential equation with the solution

(9) .
rt
t
e N N
0
=

Exercise 1: Simulate the growth of a population according to equation (9). The
parameters that can be varied are N
0
, r and t. Copy and paste or type in:

% Population_growth.m
% Parameters and start values used in this excercise:
n0=100; % size of population at time 0
r=2; %population growth rate
t=0:0.2:10; % timeperiod (in years)
nt=n0*exp(r.*t); % the equation for pop. growth in continuos time

figure % opens a new figure-window
plot(t,nt) %plots the figure

Answer the following questions by varying the parameter values in the equation.
Print the figures and write down the parameter values.

Questions
1. Is equation 8 suitable for changes on a discrete or continuous time-scale?
2. What is a differetial equation as opposed to a difference equation?
3. What parameter has the greatest effect on the population size after a couple of
time intervals? N
0
or r? Try different values of N
0
and r.
4. What value of r gives a constant population size? Can you think of any population
with this r-value? What would happen to such a population if there is a catastrophe
or epidemic?

2 Intraspecific competition
2a) Differential equations in continuous time
So far we have assumed that resources are unlimited. More realistically the
individuals in a population will compete for limited resources. For simplicity we
assume that all individuals have the same competitive ability. Both per capita births
(B) and deaths (D) will be affected by competition. We can therefor assume that B
and D are functions of the population size (N).

(10)

+ =
=
dN D N D
bN B N B
0
0
) (
) (

This material belongs to AKVAMOD
Any copying or use requires permission.
www.Akvamod.dk
A K V A M O D

I nt e g r a t i ng e duc a t i on i n t he r e s u n d r e gi on
Mode l l i ng Aqua t i c s y s t ems Conne c t i ng r e s e a r c h

Where B
0
and D
0
are per capita births and deaths, b is the change in births and d the
change of deaths.

The intrinsic population growth rate (the growth rate without competition) is then

(11) r = B
0
D
0

At some population size the birth and death rates will be equal, that is B(N) = D(N)
which is an equilibrium. This population size is the carrying capacity of the
environment and is called K. It can be seen as the number of individuals that can be
supported by the environment of the population. Setting B(N) = D(N) and solving for
N gives us

(12) N* =
B
0
D
0
b + d
= K

The rate of change of the population becomes

(13) ( ) ( ) [ ] N D N B N
dt
dN
=

which can be written as

(14)
( ) ( ) [ ] ( ) [ ]
( )
( )
( )

+

+ =
d b
D B
N
d b
D B
D B N
D B
d b N
D B N
d b N D B N N D N B N
dt
dN
0 0
0 0
0 0
0 0
0 0
0 0
1

If we look at equations (11) and (12) we can insert r and K in equation 13

(15)
dN
dt
= rN
K N
K

This differential equation is called the logistic equation.

The size of the population at a specific time, N(t), is given by the solution to the
logistic equation. This solution is rather complicated (it can for example be found in
Hastings (1997) Population ecology, page 84-85)

This material belongs to AKVAMOD
Any copying or use requires permission.
www.Akvamod.dk
A K V A M O D

I nt e g r a t i ng e duc a t i on i n t he r e s u n d r e gi on
Mode l l i ng Aqua t i c s y s t ems Conne c t i ng r e s e a r c h

(16) N t ( )=
K
1+
K
N
0
1

e
rt

Exercise 2: Simulate the growth of a population (equation 16). Type or copy:

%ContinuousCompetition
% Parameters and start values used in this excercise:
n0=2; % size of population at time 0
r=1; % population growth rate
t=0:0.2:20; % timeperiod (in years)
k=1000; %carrying capacity

nt=k./(1+(k./n0-1)*exp(-r.*t)); % the solution (equation 16) to the logistic equation

figure
plot(t,nt)

Questions
5. Which one of the parameters K, r and N
0
seem to be most important for the
population development over this time period?
6. What happens when N
0
>K, what does that mean for the population?
7. What happens when N
0
=K and r = -1? Can this happen in reality?

Exercise 3: Check the change in growth of a population according to equation (15)
by adding following to the m-file IntraSpecificCompetition: (if you do not want to plot
both number and growth rate you can erase the bottom two lines of the code above
that plots population size as a function of t).

% parameters
n=0:10:1000; % initial and end population size
dndt=r.*n.*(1-n/k); % the logistic equation

figure
plot(n,dndt)

Question
8. At what population size is the growth rate maximal? Compare this curve with the
S-curve from the previous question.

This material belongs to AKVAMOD
Any copying or use requires permission.
www.Akvamod.dk
A K V A M O D

I nt e g r a t i ng e duc a t i on i n t he r e s u n d r e gi on
Mode l l i ng Aqua t i c s y s t ems Conne c t i ng r e s e a r c h

2b Difference equations in discrete time
If we work in a discrete time scale the intervals will frequently be years in which case
the population size will be measured and updated once a year. This approach is
suitable for organisms with discrete breeding intervals and perfect if they also only
survive one year. However, it may also simplify calculations compared to calculations
in continuous time. By some alterations of the equations used in section 2 we can
make similar computations in discrete time. Using the same symbols in the equations
for continuous time, the size of the population at time t+1 becomes

(17)
N
t +1
= B N
t
( )
N
t
+ 1 D N
t
( ) [ ]
N
t

N
t +1
= 1+ B
0
D
0
( ) b + d ( )N
t
[ ]
N
t

We define N
t+1
as the change in population size between a time step and the next
(N
t+1
-N
t
). Dividing the right hand side of the logistic equation by N will give us the
per capita increase:

(18)

K
N
r 1

From derivation of the logistic equation we observe that:

(19)

= +
K
N
r d b N D B 1 ) (
0 0

Hence we may write:

(20) N
t +1
= rN
t
1
N
t
K

Equation 20 is a difference equation that is similar to the logistic equation. In order
to simulate the population dynamics we may use the difference between two time
intervals

(21)
1 1 + +
+ =
t t t
N N N

In order to calculate the population dynamics for longer periods of time one starts
with an initial population size, for example N
0
at time t=0 and then calculate step by
step until one reaches a final time T.

Excersise 4: Plot a figure of the population dynamics following equation 20 using the
following code. Compare with the result of section 2.

This material belongs to AKVAMOD
Any copying or use requires permission.
www.Akvamod.dk
A K V A M O D

I nt e g r a t i ng e duc a t i on i n t he r e s u n d r e gi on
Mode l l i ng Aqua t i c s y s t ems Conne c t i ng r e s e a r c h

% DiscreteCompetition.m
% parameters
clear all;
n0=2; %start value
r=1; %rate of increase
k=1000; %carrying capacity

%loop:
n(1) =n0; %assign a value
for t =1:20 %loop from 1 to end
%equation 16, calculates values into matrix n:
n(t+1)=n(t)+(r.*n(t).*(1-n(t)./k));
end
t=1:21; %final step 21 because of t+1
figure % figure window
plot(t,n); % plot n

Questions:
9. Are there any differences between the calculations in discrete and continuos time?
Compare the results with the figure from section 2. Use the same parameter values
as you used in that simulation and compare figures on the screen. Alternatively you
may use the command hold and put one of the figures on top of the other.

10. For what kind of populations are modeling with discrete timesteps realistic? When
is continous time more appropriate?

11. In what way will increasing values of r affect the population growth? Simulate
with small increasing steps from 0.1 to 3. To get longer time series: change the for
loop to iterate up to 100 and the vector length to 101 instead of 21.

3 Predation
Species can interact and affect each other in many ways, for example by competion
as we have discussed below. Other ways in which species can interact is predation
(when one species eat another) parasitism and mutualism. These interactions can be
modelled with similar equation systems as in the interspecific competions case, here
we will also go thorugh the most common way to model predation in population
ecology. The density of the predator, and thereby its impact on the prey, is
dependent of the density of the prey population which in turn is determined by the
growth rate of the prey. Denoting the density of the prey by N
b
and the density of
predators by N
p
, we can describe the rate of change in the prey population as

(22)
p b
b
b
b
N aN
K
N K
rN
dt
dN

=

and in the predator population as

This material belongs to AKVAMOD
Any copying or use requires permission.
www.Akvamod.dk
A K V A M O D

I nt e g r a t i ng e duc a t i on i n t he r e s u n d r e gi on
Mode l l i ng Aqua t i c s y s t ems Conne c t i ng r e s e a r c h

(23)
p p b
p
dN N baN
dt
dN
=

where r and K are the same variables as above, a is the frequency by which a
predator finds and kills a prey, b is a coefficient describing the efficiency at turning
prey into predator offspring and d is the predator mortality in abscence of prey.
Using functions 22 and 23 we may let a prey population and predator population
interact over time.

Exercise 5: Plot figures to displaying the population dynamics for this predator prey
system:

% Predation.m
% Parameters
% n1 =density of prey population
% n2 =density of predator population

clear all;
global r a b k d;
r=0.5; % Growth rate of prey population
a=0.01; % predator kill rate
b=0.02; % predator conversion coefficient
d=0.1; % predator mortality in abscence of prey
k=2000; % carrying capacity of prey population
n0=[10;10]; % density of prey and predator at t=0
t0=0; tend=400; % simulation start and stop

dat=[ ]; % Vector for storing the result
t=[ ]; % time vector

% Calculate population densities with ODE45, a MATLAB application which
% uses the differential equation below (pred.m), solves the equation numerically

[t,dat]=ode45('pred',[t0 tend],n0); %pred is the equation below
figure % opens a new figure window
plot(t,dat); % draws the figure
legend('prey','pred'); % makes a legend

Make a new m-file of the code below and name it pred.m:

% pred.m % make a separate file of the equation
function ndot=pred(t,n)
global k r a b d;
if n(1)<0
n(1)=0;
end
if n(2)<0
n(2)=0;
end
ndot=[r.*n(1).*(1- n(1)./k)-a.*n(1).*n(2);b.*a.*n(1).*n(2)-d*n(2)];
This material belongs to AKVAMOD
Any copying or use requires permission.
www.Akvamod.dk
A K V A M O D

I nt e g r a t i ng e duc a t i on i n t he r e s u n d r e gi on
Mode l l i ng Aqua t i c s y s t ems Conne c t i ng r e s e a r c h

Questions:
12. Find values of the parameters r, a, b, d so that a) the prey goes extinct, b) the
predator goes extinct, c) the predator and prey densities oscillates in cyclic manner
and where the amplitude decreases and finally leads to a stable coexistence?

13. Which variables seem to affect the tendency to oscillations?

Exercise 6: Plot a phase diagram with the predator density on the y-axis and the
prey density on the x-axis by adding the following code to the end of the file
Predation.m.

figure
plot(dat(:,1),dat(:,2));
ylabel('predator');
xlabel('prey');

Question:
14. Repeat the simulations in question 10 to see how they appear in phase diagram
form.

4 Interspecific competition
Apart from competition from the own species, most organisms are subject to
competition from other species interspecific competition. A result from models of
interspecific competition is that coexistence of ecologically equal species is only
possible between certain limits of r and K.

Interspecific competition could be modelled by extending the equation of intraspecific
competition (eq. 14) with a competition term. The effect of species 2 on species 1
could be described by N
2
and the effect of species 1 on species 2 could be described
by N
1
where and denotes the per capita effect of one species on the other,
respectively. The equations then becomes

(24)
dN
1
dt
= r
1
N
1
K
1
N
1
N
2
K
1

and

(25)
dN
2
dt
= r
2
N
2
K
2
N
2
N
1
K
2

.

These equations are called the Lotka-Volterra equations for two competing species. If
both and equal zero there is no competition between the species. If =1 and =1
This material belongs to AKVAMOD
Any copying or use requires permission.
www.Akvamod.dk
A K V A M O D

I nt e g r a t i ng e duc a t i on i n t he r e s u n d r e gi on
Mode l l i ng Aqua t i c s y s t ems Conne c t i ng r e s e a r c h

the two species compete with similar effect and for a certain individual it does not
matter if it meets an individual of species 1 or 2.

It is evident from the logistic equation (equation 15) that when a population becomes
larger than K it will decrease, when it is smaller than K it will increase. If you plot this
graphically in one dimension on an axis this population changes can be depicted as
vectors:

0_________________K _______Population size

In phase diagrams the interaction between two species is plotted as a joint
movement, with the population size of one species on each axis. The direction of this
movement can be calculated by geometrical vector addition:

N
2
dN
2
dt
= r
2
N
2
K
2
N
2
N
1
K
2

K
1,sp2
N
1
K
1
dN
1
dt
= r
1
N
1
K
1
N
1
N
2
K
1

Figure 1. The equilibrium isocline for species 1 expressed as a function of the
densities of species 1 and species 2. The isocline can be seen as the carrying capacity
of species one when it competes for resources not only with its conspecifics but also
with another species.

If we assume for simplicity that interaction effects, etc are linear, the carrying
capacity, K, will now change into an isocline that sums up how large the combined
population can become given competition from both species.

Excersise 7: Plot a figure with the densities of both species as a function of time,
with parameters as follows:

This material belongs to AKVAMOD
Any copying or use requires permission.
www.Akvamod.dk
A K V A M O D

I nt e g r a t i ng e duc a t i on i n t he r e s u n d r e gi on
Mode l l i ng Aqua t i c s y s t ems Conne c t i ng r e s e a r c h

% Interspec_comp.m
% Interspecific competition
clear all;
close all;
global r k alfa beta;
% Parameters, initial values
n0=[1;1]; % initial densities of the population n(1)=1 & n(2)=1
r=[1;1]; % growth rate of the populations r(1)=1 & r(2)=1;
k=[1000;1000]; % carrying capacity of the system k(1)=1000 och K(2)=1000
alfa=0.6; % per capita effect of n(2) on n(1)
beta=0.5; % per capita effect of n(1) on n(2)

t0=0;tend=300; % time step 0 - 300
dat=[]; % vector for storing the result
t=[]; % vector for storing the timesteps

% Function, equation, see separate file icomp.m
% ode-routine
[t,dat]=ode45('icomp',[t0 tend],n0);
% solves the equation numerically
% making the plot
plot(t,dat);ylabel('Population density');
xlabel('Time');

Store the function below in a separate m-file, icomp.m.

% icomp.m
% functions for n(1) species 1 and n(2) species 2, as a function of time

function ndot=icomp(t,n)
global r k alfa beta;
ndot=[r(1).*n(1).*((k(1)-n(1)-alfa*n(2))/k(1));r(2).*n(2).*((k(2)-n(2)-beta*n(1))/k(2))];

Question:
15. Let a typical r- and K-strategist compete. Imagine a newly deforested area. Soon
new trees will establish, fast growing species (poplar, birch) as well as more slowly
growing species (elm, oak). Assume firstly that the competition between species 1 (r-
strategist) and species 2 (K-strategist) are equal, for example ==0.5. Start testing
with the variables r
1
=1, r
2
=0.1 and K
1
=700, K
2
=1000. What happens with the
growing forest with time?

The ability for a species to grow when rare depends on the parameter settings. The
condition for species 1 to be able to grow when rare could be studied by replacing N
1

with a number close to 0 and setting N
2
equal to K
2
. This results in the following
conditions for a population of species 1 to be able to grow when rare in an
environment dominated by species 2:
This material belongs to AKVAMOD
Any copying or use requires permission.
www.Akvamod.dk
A K V A M O D

I nt e g r a t i ng e duc a t i on i n t he r e s u n d r e gi on
Mode l l i ng Aqua t i c s y s t ems Conne c t i ng r e s e a r c h

(26)
2
1
K
K
<

The corresponding condition for species 2 then becomes

(27)
1
2
K
K
<

There are four cases in which one, both or none of the species are able to grow when
rare. These four cases are

(28)
2
1
K
K
< ,
1
2
K
K
< (coexistence)
(29)
2
1
K
K
> ,
1
2
K
K
< (species 2 wins)
(30)
2
1
K
K
< ,
1
2
K
K
> (species 1 wins)
(31)
2
1
K
K
> ,
1
2
K
K
> (?)

Questions:
16. Show these four cases by varying the parameters, for example by letting K
1
=K
2

and vary only and . What happens in the last case (31) and how can we interpret
it?

17. Show the same cases, as a phase diagram, by adding the following code to
inter_comp.m

% phase diagram
[t,dat]=ode45('icomp',[t0 tend], n0);
figure(2);
plot(dat(:,1),dat(:,2))
xlabel ('species 1')
ylabel ('species 2')

%add starting point as a star
hold on;
plot(dat(1,1),dat(1,2),'*')
%make dn1dt=0 isocline
y1=linspace(0,k(1)/alfa,10);
x1=k(1)-alfa*y1;

%make dn2dt=0 isocline
x2=linspace(0,k(2)/beta,10);
y2=k(2)-beta*x2;
%observe that the code continues on the next page!
This material belongs to AKVAMOD
Any copying or use requires permission.
www.Akvamod.dk
A K V A M O D

I nt e g r a t i ng e duc a t i on i n t he r e s u n d r e gi on
Mode l l i ng Aqua t i c s y s t ems Conne c t i ng r e s e a r c h

%plot the isoclines
plot(x1,y1,'r');
plot(x2,y2,'g');

%make legend and set axis
legend('Population sizes','Starting point','dn1dt=0','dn2dt=0')
axis([0 max([x1 x2]) 0 max([y1 y2])]);

%switch back to figure 1
figure(1);

18. Use the co-existence case (28) and vary the initial population densities so that
you get starting points in all four sectors. What is the end result?

This material belongs to AKVAMOD
Any copying or use requires permission.
www.Akvamod.dk