Modeling and simulation

Modeling and simulation Modeling and simulation

27.01.2015 Views

Modeling and simulation The simulation loop: Modeling Simulation Analysis In this lecture we focus on the modeling part of the loop What do we mean by modeling Ex. •A simplification of a complex problem in nature •A way of structure data from an experiment or from other measurements Important! •Remember that the model, in most cases, only give us a crude picture of reality •Understand the limitations of the model 080902 Modelling and Simulation 2008 1

<strong>Modeling</strong> <strong>and</strong> <strong>simulation</strong><br />

The <strong>simulation</strong> loop:<br />

<strong>Modeling</strong> Simulation Analysis<br />

In this lecture we focus on the modeling part of the loop<br />

What do we mean by modeling<br />

Ex.<br />

•A simplification of a complex problem in nature<br />

•A way of structure data from an experiment or from<br />

other measurements<br />

Important!<br />

•Remember that the model,<br />

in most cases, only give us<br />

a crude picture of reality<br />

•Underst<strong>and</strong> the limitations<br />

of the model<br />

080902 Modelling <strong>and</strong> Simulation 2008 1


Predator-prey systems<br />

- Ecological examples<br />

Disaster of Lake Victoria<br />

•The lake supported hundreds of small fishing<br />

communities fishing several species<br />

•A new big fish was introduced in the lake<br />

•Result: The new fish wiped out many other<br />

speices….<br />

•More: Diseases, felling of trees for fuel…<br />

Murray J.D., “Mathematical biology”, Springer-Verlag, 1993<br />

080902 Modelling <strong>and</strong> Simulation 2008 2


A simple predator-prey system<br />

Our toy system: An isl<strong>and</strong> where two species lives, predators (foxes)<br />

<strong>and</strong> preys (rabbits)<br />

Our first assumptions:<br />

•The foxes eat rabbits <strong>and</strong> breed<br />

•The rabbits eat grass <strong>and</strong> breed<br />

•The area is limited, but the grass grows faster than the rabbits can<br />

eat<br />

080902 Modelling <strong>and</strong> Simulation 2008 3


Let us build a mathematical model<br />

(Lotka-Volterra system)<br />

R = Number density of rabbits<br />

F = Number density of foxes<br />

1. Only rabbits, inifite food supply<br />

8 x 105 Prey model ODE<br />

6<br />

Rabbits<br />

dR =<br />

dt<br />

aR<br />

a<br />

= growth rate<br />

4<br />

2<br />

0<br />

0 5 10 15 20<br />

Limited food supply<br />

dR<br />

dt<br />

⎛ = a⎜<br />

1−<br />

⎝<br />

R ⎞<br />

⎟R<br />

K ⎠<br />

600<br />

400<br />

Prey model ODE<br />

Rabbits<br />

K<br />

= Carrying capacity<br />

200<br />

0<br />

0 5 10 15 20<br />

080902 Modelling <strong>and</strong> Simulation 2008 4


dR<br />

dt<br />

⎛ R ⎞<br />

a⎜1 − ⎟R<br />

= aR−<br />

⎝ K ⎠<br />

R<br />

K<br />

2<br />

Steady state solutions:<br />

R1 = 0 Unstable<br />

R<br />

2<br />

=<br />

K<br />

=<br />

0 5 10 15 20<br />

Stable<br />

600<br />

400<br />

200<br />

0<br />

Prey model ODE<br />

Rabbits<br />

080902 Modelling <strong>and</strong> Simulation 2008 5


2. Only foxes<br />

250<br />

200<br />

Predator-Prey model ODE<br />

Foxes<br />

dF<br />

dt<br />

= − dF<br />

d<br />

= death rate<br />

150<br />

100<br />

50<br />

3. Rabbits <strong>and</strong> foxes<br />

0<br />

0 5 10 15 20<br />

Assumptions:<br />

More rabbits => more food for foxes => foxes tends to breed more<br />

More foxes => more rabbits are killed<br />

Fox model:<br />

Rabbit model:<br />

dF<br />

dt<br />

dR<br />

dt<br />

= −dF<br />

+ cRF<br />

= aR−bFR<br />

Lotka-Volterra equations<br />

080902 Modelling <strong>and</strong> Simulation 2008 6


Solving an ODE-system using Matlab<br />

⎧dR<br />

⎪ dt<br />

⎨<br />

⎪dF<br />

⎩ dt<br />

=<br />

=<br />

aR − bFR<br />

cRF − dF<br />

a ≈ the rabbits growth rate<br />

bF ≈ the rabbits death rate<br />

cR ≈ the foxes growth rate<br />

d ≈ the foxes death rate<br />

To solve this ODE-system we have to use a numerical method.<br />

In the software Matlab there is a collection of methods that are easy<br />

to use.<br />

Note: Different choices of parameters may take longer/shorter to<br />

solve depending on numerical method<br />

Later in this course you will learn how to choose a numerical method<br />

best fitted to solve a specific problem<br />

080902 Modelling <strong>and</strong> Simulation 2008 7


Typical solutions…<br />

1500<br />

Predator-Prey model ODE<br />

Rabbits<br />

Foxes<br />

Predator-Prey Trajectory a=0.4, b=0.001, c=0.001, d=0.9<br />

1000<br />

800<br />

1000<br />

500<br />

Foxes<br />

600<br />

400<br />

200<br />

0<br />

0 20 40 60 80 100<br />

0<br />

0 500 1000 1500<br />

Rabbits<br />

1000<br />

800<br />

Predator-Prey model ODE<br />

Rabbits<br />

Foxes<br />

Predator-Prey Trajectory a=0.4, b=0.004, c=0.004, d=0.9<br />

800<br />

600<br />

600<br />

400<br />

200<br />

Foxes<br />

400<br />

200<br />

0<br />

0 20 40 60 80 100<br />

0<br />

0 200 400 600 800 1000<br />

Rabbits<br />

080902 Modelling <strong>and</strong> Simulation 2008 8


Limitations of our model<br />

• Infinite grass supply leads to exponential growth of the rabbit<br />

population if no foxes are alive<br />

• The fox population can grow without saturation<br />

• Only two species…<br />

080902 Modelling <strong>and</strong> Simulation 2008 9


More realistic predator-prey systems<br />

Finite grass resources<br />

⎛<br />

a → a ⎜1<br />

−<br />

⎝<br />

R<br />

K<br />

⎞<br />

⎟<br />

⎠<br />

800<br />

600<br />

400<br />

Predator-Prey model ODE<br />

Rabbits<br />

Foxes<br />

Foxes<br />

Trajectory a=0.4, b=0.004, c=0.004, d=0.9, K=800<br />

800<br />

600<br />

400<br />

200<br />

200<br />

0<br />

0 20 40 60 80 100<br />

0<br />

0 200 400 600<br />

Rabbits<br />

Finite prey consumption<br />

RF<br />

→<br />

1<br />

RF<br />

+<br />

R<br />

S<br />

5000<br />

4000<br />

3000<br />

2000<br />

1000<br />

Predator-Prey model ODE<br />

Rabbits<br />

Foxes<br />

Foxes<br />

Trajectory a=0.4, b=0.004, c=0.004, d=0.9, S=2500<br />

5000<br />

4000<br />

3000<br />

2000<br />

1000<br />

S<br />

= Fox eating saturation<br />

0<br />

0 20 40 60 80 100<br />

0<br />

0 1000 2000 3000 4000 5000<br />

Rabbits<br />

080902 Modelling <strong>and</strong> Simulation 2008 10


Results from a model with both limited grass supply <strong>and</strong><br />

limited prey consumption<br />

800<br />

600<br />

Predator-Prey model ODE<br />

Rabbits<br />

Foxes<br />

Trajectory a=0.4, b=0.004, c=0.004, d=0.9, K=800, S=600<br />

600<br />

500<br />

400<br />

400<br />

200<br />

0<br />

0 20 40 60 80 100<br />

Foxes<br />

300<br />

200<br />

100<br />

0<br />

0 200 400 600 800<br />

Rabbits<br />

080902 Modelling <strong>and</strong> Simulation 2008 11


Predator-prey laboration 1<br />

Exercises<br />

1. Implement <strong>and</strong> solve the st<strong>and</strong>ard predator-prey equations<br />

2. Add limited grass supply (see model above)<br />

3. Add limited prey consumption (see model above)<br />

4. Add a third species<br />

080902 Modelling <strong>and</strong> Simulation 2008 12


So, what is Matlab<br />

•A simple programming environment<br />

•Mathematical modules (ODE solvers, statistical tools, algebraic tools…)<br />

•Graphics<br />

•Possibility to connect to external <strong>simulation</strong>s<br />

How do we use Matlab<br />

080902 Modelling <strong>and</strong> Simulation 2008 13


% A Matlab code for solving the Predator-Prey system<br />

clear, clear global<br />

global a b c d<br />

%Define equation coefficients<br />

a=0.4; %Rabbit growth rate<br />

b=0.001; %Rabbit death coefficient<br />

c=0.001; %Fox growth coefficient<br />

d=0.9; %Fox death rate<br />

% Define the variables used as inputs in the ODE-solver<br />

t0=0;<br />

tmax=1000;<br />

Ntot=1000;<br />

p=0.4;<br />

RabbitIni=(1-p)*Ntot;<br />

FoxIni=p*Ntot;<br />

options = odeset('RelTol',1e-4,'AbsTol',[1e-7 1e-7]);<br />

Save as PredatorPrey.m<br />

Save as rabbitfox.m<br />

function dy = rabbitfox(t,y);<br />

dy = zeros(2,1);<br />

global a b c d<br />

% Solve ODE system<br />

[T,Y] = ode45(@rabbitfox,[t0 tmax],[RabbitIni FoxIni],options);<br />

% Definition of the ODE<br />

% y(1) = Number of rabbits<br />

% y(2) = Number of foxes<br />

%Plot<br />

figure(1)<br />

dy(1) = a*y(1) - b*y(2)*y(1);<br />

plot(T,Y(:,1),'r')<br />

hold on<br />

dy(2) = c*y(1)*y(2) - d*y(2);<br />

plot(T,Y(:,2),'--b')<br />

legend('Rabbits','Foxes')<br />

title('Predator-Prey model ODE')<br />

080902 Modelling <strong>and</strong> Simulation 2008 14


General remarks about ODE systems (a very short overview…)<br />

We have seen plots like those to the right<br />

Note, for example, the spiral motion into a single point…<br />

Foxes<br />

1000<br />

800<br />

600<br />

400<br />

y j y<br />

200<br />

800<br />

600<br />

Predator-Prey model ODE<br />

Rabbits<br />

Foxes<br />

Trajectory a=0.4, b=0.004, c=0.004, d=0.9, K=800<br />

800<br />

600<br />

0<br />

0 500 1000 1500<br />

Rabbits<br />

400<br />

Foxes<br />

400<br />

800<br />

200<br />

200<br />

600<br />

0<br />

0 20 40 60 80 100<br />

0<br />

0 200 400 600<br />

Rabbits<br />

Foxes<br />

400<br />

200<br />

Such a point is called a fixed point <strong>and</strong> represents<br />

a steady state situation:<br />

dR<br />

dt<br />

dF<br />

= 0 , =<br />

dt<br />

0<br />

(The unmodified case)<br />

⎧R<br />

= F<br />

⎨<br />

⎩R<br />

= d<br />

=<br />

/ c,<br />

0<br />

F<br />

=<br />

a / b<br />

Foxes<br />

0<br />

0 200 400 600<br />

Rabbits<br />

5000<br />

4000<br />

3000<br />

2000<br />

1000<br />

0<br />

0 1000 2000 3000 4000 5000<br />

Rabbits<br />

080902 Modelling <strong>and</strong> Simulation 2008 15


Typical state space trajectories near fixed points<br />

Node Repellor Spiral<br />

node<br />

Spiral<br />

repellor<br />

Cycle<br />

R = F = 0 is a saddle point<br />

R = d / c,<br />

F = a / b is a cycle<br />

Saddle<br />

point Hilborn R.C., “Chaos <strong>and</strong> nonlinear dynamics”, Oxford University Press Inc., 1994<br />

080902 Modelling <strong>and</strong> Simulation 2008 16


State space transitions<br />

Ex.<br />

•If the parameters in an ODE-system are changed, we can have a<br />

transition from one type of state space behavior to another.<br />

•Such a transition is called a bifurcation<br />

•In order to study bifurcation it is very useful to write the ODE in nondimensional<br />

form. In the unmodified case we make the following<br />

substitutions:<br />

⎧dF<br />

d<br />

⎪ =<br />

dt d<br />

⎨<br />

⎪ ⎛<br />

− dF = − d<br />

⎪<br />

⎜<br />

⎩ ⎝<br />

cR bF<br />

u = , v = , τ = at,<br />

α =<br />

d a<br />

( τ / a)<br />

⎛<br />

⎜<br />

⎝<br />

av<br />

b<br />

av ⎞<br />

⎟<br />

b ⎠<br />

⎞<br />

⎟<br />

⎠<br />

= −<br />

=<br />

a<br />

b<br />

ad<br />

b<br />

2<br />

dv<br />

dτ<br />

2<br />

a<br />

v = −<br />

b<br />

αv<br />

d<br />

a<br />

Non-dimensional form<br />

⎧du<br />

⎪ dt<br />

⎨<br />

⎪dv<br />

⎩ dt<br />

=<br />

= α<br />

( 1−<br />

v)<br />

u<br />

( u −1)<br />

ONLY ONE PARAMETER!!<br />

v<br />

080902 Modelling <strong>and</strong> Simulation 2008 17


Chaotic trajectories<br />

Poincaré-Bendixon Theorem says something like:<br />

(works only in 2D)<br />

Assume that we have a bounded system…<br />

Then the trajectory only have two choices:<br />

1. The trajectory approaches the fixed point<br />

2. The trajectory approaches a limit cycle (a cycle<br />

which attracts all neighboring trajectories)<br />

A further important result based on this theorem is that we need at<br />

least three dimensions in order to have chaos<br />

080902 Modelling <strong>and</strong> Simulation 2008 18


Further reading for those who are interested:<br />

1. N. Britton, “Essential Mathematical Biology”, Springer-Verlag,<br />

2003<br />

2. Murray J.D., “Mathematical biology”, Springer-Verlag, 1993<br />

3. Hilborn R.C., “Chaos <strong>and</strong> nonlinear dynamics”, Oxford University<br />

Press Inc., 1994<br />

080902 Modelling <strong>and</strong> Simulation 2008 19


An other way to simulate predator-prey<br />

system…<br />

•The ODE model above describes the evolution of the<br />

population densities<br />

• We do not get any information about dynamics of individual<br />

animals<br />

•Is it possible to construct a <strong>simulation</strong> of individuals<br />

that give us similar results as the ODE model<br />

080902 Modelling <strong>and</strong> Simulation 2008 20


Cellular Automata <strong>and</strong> Agent Based Models<br />

Game of Life, John Conway (1970)<br />

Ex<br />

-Individuals are distributed on a grid<br />

-Each individual has a well defined neighborhood<br />

-Simple rules stating the birth <strong>and</strong> death of<br />

individuals<br />

-The whole grid is updated each time step<br />

Simple rules on microscopic level (individuals)<br />

=> Complex behavior on macroscopic scales<br />

080902 Modelling <strong>and</strong> Simulation 2008 21


Predator-Prey <strong>simulation</strong> using a Cellular Automata (CA)<br />

080902 Modelling <strong>and</strong> Simulation 2008 22


1200<br />

1000<br />

Foxes<br />

Rabbits<br />

1200<br />

1000<br />

800<br />

800<br />

600<br />

400<br />

Rabbits<br />

600<br />

400<br />

200<br />

200<br />

0<br />

0 1000 2000 3000 4000 5000<br />

0<br />

0 200 400 600<br />

Foxes<br />

50<br />

Initial<br />

50<br />

Final<br />

40<br />

40<br />

30<br />

30<br />

20<br />

20<br />

10<br />

10<br />

0 10 20 30 40 50<br />

0 10 20 30 40 50<br />

080902 Modelling <strong>and</strong> Simulation 2008 23


1500<br />

1000<br />

500<br />

Foxes<br />

Rabbits<br />

Rabbits<br />

1500<br />

1000<br />

500<br />

0<br />

0 2000 4000 6000<br />

50<br />

40<br />

30<br />

20<br />

10<br />

Initial<br />

0 20 40<br />

0<br />

0 200 400 600<br />

Foxes<br />

50<br />

40<br />

30<br />

20<br />

10<br />

Final<br />

0 20 40<br />

080902 Modelling <strong>and</strong> Simulation 2008 24


Predator-prey laboration 2<br />

Exercises<br />

1. Implement a <strong>simulation</strong>, based on individuals, with following properties:<br />

- One predator <strong>and</strong> one prey<br />

- The species should be able to move around on a rectangular grid<br />

- Each individual should have limited energy capacity (they need to eat)<br />

- The behavior of individuals should be controlled by SIMPLE rules<br />

2. Add limited grass supply<br />

3. Add limited prey consumption<br />

4. Add a third species<br />

080902 Modelling <strong>and</strong> Simulation 2008 25

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!