Modeling and simulation Modeling Simulation Analysis The simulation loop: In this lecture we focus on the modeling part of the loop What do we mean by modeling? Important! Ex. •A simplification of a complex problem in nature •Remember that the model, in most cases, only give us a crude picture of reality •A way of structure data from an experiment or from other measurements •Understand the limitations of the model 090902 Modelling and Simulation 2009 1 Predator-prey systems - Ecological examples Disaster of Lake Victoria •The lake supported hundreds of small fishing communities fishing several species •A new big fish was introduced in the lake •Result: The new fish wiped out many other speices…. •More: Diseases, felling of trees for fuel… Murray J.D., “Mathematical biology”, Springer-Verlag, 1993 090902 Modelling and Simulation 2009 2 A simple predator-prey system Our toy system: An island where two species lives, predators (foxes) and preys (rabbits) Our first assumptions: •The foxes eat rabbits and breed •The rabbits eat grass and breed •The area is limited, but the grass grows faster than the rabbits can eat 090902 Modelling and Simulation 2009 3 Let us build a mathematical model R = Number density of rabbits (Lotka-Volterra system) F = Number density of foxes 1. Only rabbits, inifite food supply 8 x 10 5 Prey model ODE Rabbits 6 dR = aR dt 4 a = growth rate 2 0 0 5 10 15 20 Limited food supply Prey model ODE dR ⎛ R ⎞ = a⎜1− ⎟R dt ⎝ K⎠ K = Carrying capacity 600 200 0 090902 Rabbits 400 0 5 Modelling and Simulation 2009 10 15 20 4 R2 dR ⎛ R ⎞ = a⎜1− ⎟R = aR − dt K ⎝ K⎠ Steady state solutions: R1 = 0 Unstable R2 = K Stable Prey model ODE 600 Rabbits 400 200 0 090902 0 5 10 15 Modelling and Simulation 2009 20 5 Predator-Prey model ODE 250 2. Only foxes dF = −dF dt Foxes 200 150 d = death rate 100 50 0 0 5 10 15 20 3. Rabbits and foxes Assumptions: More rabbits => more food for foxes => foxes tends to breed more More foxes => more rabbits are killed Fox model: Rabbit model: 090902 dF = −dF + cRF dt Lotka-Volterra equations dR = aR − bFR dt Modelling and Simulation 2009 6 Solving an ODE-system using Matlab ⎧ dR ⎪⎪ dt = aR − bFR ⎨ ⎪ dF = cRF − dF ⎪⎩ dt a ≈ the rabbits growth rate bF ≈ the rabbits death rate cR ≈ the foxes growth rate d ≈ the foxes death rate To solve this ODE-system we have to use a numerical method. In the software Matlab there is a collection of methods that are easy to use. Note: Different choices of parameters may take longer/shorter to solve depending on numerical method Later in this course you will learn how to choose a numerical method best fitted to solve a specific problem 090902 Modelling and Simulation 2009 7 Typical solutions… Predator-Prey Trajectory a=0.4, b=0.001, c=0.001, d=0.9 1000 Predator-Prey model ODE 1500 Rabbits Foxes 800 1000 Foxes 600 400 500 200 0 0 20 40 60 80 0 100 0 500 1000 1500 Rabbits Predator-Prey Trajectory a=0.4, b=0.004, c=0.004, d=0.9 800 Predator-Prey model ODE 1000 Rabbits Foxes 800 600 Foxes 600 400 200 200 0 090902 400 0 20 40 60 80 100 0 0 200 Modelling and Simulation 2009 400 600 Rabbits 800 1000 8 Limitations of our model? • Infinite grass supply leads to exponential growth of the rabbit population if no foxes are alive • The fox population can grow without saturation • Only two species… 090902 Modelling and Simulation 2009 9 More realistic predator-prey systems Trajectory a=0.4, b=0.004, c=0.004, d=0.9, K=800 800 Predator-Prey model ODE R ⎞ ⎛ a → a ⎜1 − ⎟ K ⎠ ⎝ Rabbits Foxes 600 600 Foxes Finite grass resources 800 400 200 0 400 200 0 20 40 60 80 0 100 0 200 400 600 Rabbits RF → RF 4000 R 1 + S 3000 S = Fox eating saturation 090902 Predator-Prey model ODE Trajectory a=0.4, b=0.004, c=0.004, d=0.9, S=2500 5000 5000 Rabbits Foxes 4000 Foxes Finite prey consumption 2000 1000 0 3000 2000 1000 0 20 40 60 80 100 Modelling and Simulation 2009 0 0 1000 2000 3000 Rabbits 4000 5000 10 Results from a model with both limited grass supply and limited prey consumption Trajectory a=0.4, b=0.004, c=0.004, d=0.9, K=800, S=600 600 Predator-Prey model ODE 800 Rabbits Foxes 500 600 Foxes 400 400 300 200 200 100 0 0 090902 20 40 60 80 100 0 0 200 Modelling and Simulation 2009 400 Rabbits 600 800 11 Predator-prey laboration 1 Exercises 1. Implement and solve the standard predator-prey equations 2. Add limited grass supply (see model above) 3. Add limited prey consumption (see model above) 4. Add a third species 090902 Modelling and Simulation 2009 12 So, what is Matlab??? •A simple programming environment •Mathematical modules (ODE solvers, statistical tools, algebraic tools…) •Graphics •Possibility to connect to external simulations How do we use Matlab? 090902 Modelling and Simulation 2009 13 % A Matlab code for solving the Predator-Prey system clear, clear global global a b c d Save as PredatorPrey.m %Define equation coefficients a=0.4; %Rabbit growth rate b=0.001; %Rabbit death coefficient c=0.001; %Fox growth coefficient d=0.9; %Fox death rate Save as rabbitfox.m % Define the variables used as inputs in the ODE-solver t0=0; tmax=1000; Ntot=1000; p=0.4; RabbitIni=(1-p)*Ntot; FoxIni=p*Ntot; options = odeset('RelTol',1e-4,'AbsTol',[1e-7 1e-7]); function dy = rabbitfox(t,y); dy = zeros(2,1); global a b c d % Definition of the ODE % y(1) = Number of rabbits % y(2) = Number of foxes % Solve ODE system [T,Y] = ode45(@rabbitfox,[t0 tmax],[RabbitIni FoxIni],options); %Plot figure(1) plot(T,Y(:,1),'r') hold on plot(T,Y(:,2),'--b') legend('Rabbits','Foxes') title('Predator-Prey model ODE') 090902 dy(1) = a*y(1) - b*y(2)*y(1); dy(2) = c*y(1)*y(2) - d*y(2); Modelling and Simulation 2009 14 General remarks about ODE systems (a very short overview…) y j y 1000 We have seen plots like those to the right Note, for example, the spiral motion into a single point… Foxes 800 600 400 200 Predator-Prey model ODE Trajectory a=0.4, b=0.004, c=0.004, d=0.9, K=800 800 800 0 Rabbits Foxes 0 500 1000 1500 Rabbits 600 Foxes 600 400 800 400 200 0 0 0 20 40 60 80 100 Foxes 600 200 0 200 400 600 400 200 Rabbits 0 Such a point is called a fixed point and represents a steady state situation: Modelling and Simulation 2009 600 4000 3000 2000 1000 0 090902 400 5000 Foxes ⎧R = F = 0 ⎨ ⎩ R = d / c, F = a / b 200 Rabbits (The unmodified case) dR dF = 0, =0 dt dt 0 0 1000 2000 3000 Rabbits 4000 5000 15 Typical state space trajectories near fixed points Node Repellor Spiral Spiral node repellor R = F = 0 is a saddle point R = d / c, F = a / b is a cycle Cycle Saddle point 090902 Hilborn R.C., “Chaos and nonlinear dynamics”, Oxford University Press Inc., 1994 Modelling and Simulation 2009 16 State space transitions •If the parameters in an ODE-system are changed, we can have a transition from one type of state space behavior to another. •Such a transition is called a bifurcation •In order to study bifurcation it is very useful to write the ODE in nondimensional form. In the unmodified case we make the following substitutions: u= Non-dimensional form bF d cR ,v= , τ = at , α = d a a ⎧ du ⎪⎪ dt = (1 − v )u ⎨ ⎪ dv = α (u − 1)v ⎪⎩ dt Ex. ⎧ dF d ⎛ av ⎞ a 2 dv = ⎜ ⎟= ⎪ ⎪ dt d (τ / a ) ⎝ b ⎠ b dτ ⎨ 2 ⎪− dF = − d ⎛⎜ av ⎞⎟ = − ad v = − a αv ⎪⎩ b b ⎝ b ⎠ 090902 ONLY ONE PARAMETER!! Modelling and Simulation 2009 17 Further reading for those who are interested: 1. N. Britton, “Essential Mathematical Biology”, Springer-Verlag, 2003 2. Murray J.D., “Mathematical biology”, Springer-Verlag, 1993 3. Hilborn R.C., “Chaos and nonlinear dynamics”, Oxford University Press Inc., 1994 090902 Modelling and Simulation 2009 18 An other way to simulate predator-prey system… •The ODE model above describes the evolution of the population densities • We do not get any information about dynamics of individual animals •Is it possible to construct a simulation of individuals that give us similar results as the ODE model? 090902 Modelling and Simulation 2009 19 Cellular Automata and Agent Based Models Game of Life, John Conway (1970) -Individuals are distributed on a grid -Each individual has a well defined neighborhood -Simple rules stating the birth and death of individuals -The whole grid is updated each time step Ex Simple rules on microscopic level (individuals) => Complex behavior on macroscopic scales 090902 Modelling and Simulation 2009 20 Predator-Prey simulation using a Cellular Automata (CA) 090902 Modelling and Simulation 2009 21 090902 Modelling and Simulation 2009 22 090902 Modelling and Simulation 2009 23 Predator-prey laboration 2 Exercises 1. Implement a simulation, based on individuals, with following properties: - One predator and one prey - The species should be able to move around on a rectangular grid. Each individual should have limited energy capacity (they need to eat). OR You may use non-moving individuals and make a “Game of life”-like simulation. - The behavior of individuals should be controlled by SIMPLE rules 2. Add limited grass supply 3. Add limited prey consumption 4. Add a third species 090902 Modelling and Simulation 2009 24 Hints to get started • Represent the domain by a matrix. Represent your different species by different numbers. empty spaces = 0 rabbits = 1 foxes = 2 (It may in some cases be advantageous to use different matrices for different species, e.g. a separate matrix for foxes and one for rabbits.) • How to treat boundaries: let the domain be an island. • Random matrix with different species. • Count the number of species. >> island = zeros(5); >> island(:,1) = 9; >> island(:,5) = 9; >> island(1,:) = 9; >> island(5,:) = 9 island = >> nrFox = sum(sum(island==2)) >> floor(3*rand(2,3)) ans = 2 0 090902 1 1 2 2 Modelling and Simulation 2009 9 9 9 9 9 9 0 0 0 9 9 0 0 0 9 9 0 0 0 9 9 9 9 9 9 25 Hints to get started (continued) • You can use logic in if-cases if (wall(i,,j)==0) & (Foxes(i,,j)==1) .......... • You can use logicals instead of for-loops to find sites where two matrices overlap >> F=[0 0 1; 1 1 1; 1 0 0]; >> R=[1 1 1; 0 0 2; 0 0 0]; >> F&R ans = • Example of hunting routine 0 0 0 0 0 0 1 1 0 foxrabbit = F&R; % Creates a matrix where R and F overlap – here the foxes may hunt killrabbit=(rand(size(foxrabbit))<=killprobability)&foxrabbit % Creates a matrix indicating where the hunt was successful R(killrabbit)=0; % The rabbits that were killed are removed. 090902 Modelling and Simulation 2009 26 Test your code • Remove birth → if your population still grows you have a bug? • Remove death → if your population decreases you have a bug? • Remove grass → does your rabbits still survive? • Remove rabbits → does your foxes still survive? 090902 Modelling and Simulation 2009 27 How to implement graphics (optional) • Suppose you have a matrix, Tot, in which 1 represents rabbits and 2 represents foxes. The following algorithm within the time-loop will display of the evolution in real time. figure(1) set(gcf,'DoubleBuffer','on') subplot(1,2,1) plot(main,Popfox(main),'.b') hold on plot(main,Poprabbit(main),'.r') hold on plot(main,Popgrass(main),'.g') legend('Foxes','Rabbits','Grass',2) subplot(1,2,2) Tot(1,1)=0; Tot(1,2)=2; pcolor(Tot) axis equal colormap(hot) pause(0.00001) 090902 % makes the real time update smooth % this will plot the number of each species % Popfox is a vector with the fox popnr for each timestep % main is a vector that keeps track of the time % this will show the species on a grid in real time % this is to help fix colorscales Modelling and Simulation 2009 28 Example of predator-prey agent based simulation 090902 Modelling and Simulation 2009 29