Presentation is loading. Please wait.

Presentation is loading. Please wait.

Modelling and Simulating Social Systems with MATLAB

Similar presentations


Presentation on theme: "Modelling and Simulating Social Systems with MATLAB"— Presentation transcript:

1 Modelling and Simulating Social Systems with MATLAB
Modelling and Simulating Social Systems with MATLAB Lesson 3 – 3D plots and population growth A. Johansson & M. Moussaid © ETH Zürich |

2 Lesson 3 - Contents Repetition Additional plotting commands
Lesson 3 - Contents Repetition Additional plotting commands 3-dimensions plots Case studies: exponential and logistic growth

3 Repetition Plotting functions >> x=[0:0.2:10]; >> a=3;
Repetition Plotting functions >> x=[0:0.2:10]; >> a=3; >> y=a*exp(-x); >> plot(x,y,’b+-’)

4 Repetition Plotting functions
Repetition Plotting functions >> xlabel(“X”) >> ylabel(“Y=3*exp(-x)”) >> title(“Plotting a simple function”) 4 4

5 Repetition Plotting functions >> x=randn(10^4 , 1);
Repetition Plotting functions >> x=randn(10^4 , 1); >> hist(x); 5 5

6 Repetition Statistics >> mean(x) >> std(x) >> min(x)
Repetition Statistics >> mean(x) 0.021 >> std(x) 1.005 >> min(x) -3.797 >> max(x) 4.198 6 6

7 Plotting Two useful commands >> x=[-5:0.1:5];a=2;b=3;
Plotting Two useful commands Hold on|off Grid on|off >> x=[-5:0.1:5];a=2;b=3; >> y1=exp(-x.^2); >> y2=a*exp(-x.^2); >> y3=exp(-(x.^2)/b); 7 7

8 Plotting Two additional useful commands >> plot(x,y1);
Plotting Two additional useful commands >> plot(x,y1); >> hold on >> plot(x,y2,’r’); >> plot(x,y3,’g’); 8 8

9 Plotting Two additional useful commands >> plot(x,y1);
Plotting Two additional useful commands >> plot(x,y1); >> hold on >> plot(x,y2,’r’); >> plot(x,y3,’g’); >> grid on 9 9

10 Plotting Saving a plot >> hgsave(‘fileName.fig’) 07.05.2018 10

11 3 dimensions plots Plot a surface surf(X,Y,Z) with: x2 x1 x3 y1 y2 y3
3 dimensions plots Plot a surface surf(X,Y,Z) with: X a vector containing the x-axis Y a vector containing the y-axis Z a matrix with Z(i,j) is the corresponding value for Y(i) and X(j) Number of lines and columns in Z must respectively equal length(Y) and length(X). x2 x1 x3 y1 y2 y3 z11 z12 z13 z21 z22 z23 z31 z32 z33 11 11

12 3 dimensions plots Plot a surface >> X=-2:0.1:2;
3 dimensions plots Plot a surface >> X=-2:0.1:2; >> Y=0:0.1:1; >> Z = Y(2)*exp(-X.^2) Z = >> for i=1:length(Y) Z(i,:)= Y(i)*exp(-X.^2); end 12 12

13 3 dimensions plots Plot a surface >> surf(X,Y,Z) 07.05.2018 13

14 3 dimensions plots Plot a surface >> surf(X,Y,Z) 07.05.2018 14

15 3 dimensions plots Plot a surface >> surf(X,Y,Z) 07.05.2018 15

16 3 dimensions plots Plot a surface
3 dimensions plots Plot a surface >> contourf(X,Y,Z) >> contour(X,Y,Z) 16 16

17 Case 1 Exponential growth
Case 1 Exponential growth Consider a population that has a constant birth rate through time and is never limited by food or disease. At each new generation the population increases with a given birth rate r This is called an exponential growth: the birth rate alone controls how fast the population grows. Generation 1 Generation 2 Generation 3 Generation 4

18 Case 1 Exponential growth
Case 1 Exponential growth An exponential growth can be described as follow: Xt+1 = xt * (1 + r) XT : Size of population at time T r : birth rate 18 18

19 Case 2 Logistic growth In most real populations, there is an upper limit to the number of individuals the environment can support. This limit is called the "carrying capacity" of the environment : K Populations in this kind of environment show what is known as logistic growth. Generation 1 Generation 2 Generation 3 Generation 4 19 19

20 Case 2 Logistic growth The logistic growth can be described as follow:
Case 2 Logistic growth The logistic growth can be described as follow: Xt+1 = xt exp(r (1-xt/K) ) XT : Size of population at time T r : birth rate K: Carrying capacity 20 20

21 Application Logistic growth Food NEST
Application Logistic growth Experimental data: Ants recruitment dynamic Each ant recruit N others Limited space around the food source Data in: antsData.m Food NEST 21 21

22 Exercise 1 Exponential Growth
Exercise 1 Exponential Growth Write a function expGrowth.m that computes the population size xT, after T generations. The initial population x0, the birth rate r and the final generation T must be given as function parameters. For an initial population of 10 individuals and a birth rate of 0.1, use the function to compute the population size after T=30, 40 and 50 generations. Modify the function so that it returns a vector ST that contains the sequence x0, x1, x2, … xT. Plot S30 (with the same parameters). On the same figure plot S30 for r = -0.05, 0, 0.05, Set title, axis names and save the file. Download and launch file exercise1.m . Read the code and understand how it works. Add some comments. 22 22

23 Exercise 2 Logistic Growth
Exercise 2 Logistic Growth Write a function logGrowth.m that compute the evolution of population size ST, after T generations. The initial population x0, the birth rate r, the capacity K and the final generation T must be given as function parameters. Consider x0=10 , r=0.3 , and K = 500 , plot evolution of population after T=30. On a new plot, draw the evolution of population size for r=0.3, 0.5, 1, 1.5, and 2. What can you say about the overall dynamic? How do you interpret it? Use the functions surf or contourf to determine the critical value of r beyond which the stationary dynamic switch to an oscillatory dynamic. In order to do so, build and plot a matrix that contains the evolution of population during 30 generations with r varying from 0.3 to 2.5 with a step of 0.01 23 23

24 Exercise 3 Ants recruitment
Exercise 3 Ants recruitment Download the data file antsData.m and read the data description written into the file. On the same plot, represent the data from the 5 experiments: number of ants as a function of time. Use the option ‘.’ to plot dots instead of lines. Build a vector called avgAnts of length 15 that contains the average value of the 5 experiments at each time step. Always on the same plot, draw the vector avgAnts in red. Try to estimate the recruitment rate r and the capacity K. Choose x0=6 and T=15. 24 24

25 Exercise 4 (optional) Ants recruitment : least-square fitting
Exercise 4 (optional) Ants recruitment : least-square fitting The ‘trial and error' approach is obviously not the best way to fit a curve. The least squares method assumes that the best-fit curve is the one that has the minimal sum of the squared deviations from a given set of data. 1. Build a function lsDeviation that computes the least-square error for a given set of parameters, as follow: ls = lsDeviation (observedData, r,K,x0,T) where observedData is the observed mean values vector (use the vector avgAnts from exercise3) r,K,x0,T is a given set of parameters ls is the sum of the squared deviations between observed data and the theoretical data at each time step: ls = ( avgAnts(i) - S(i) ) with S = logGrowth(x0,r,K,T) 25 25

26 Exercise 4 (optional) Ants recruitment : least-square fitting
Exercise 4 (optional) Ants recruitment : least-square fitting For r = 0.1:0.01:1 and K=50: compute and plot the matrix LS , defined as : LS(i,j) = lsDeviation(avgAnts, r(i), K(j) , x0 , T) Choose x0=6 and T=15; What is the minimum value of LS? What are the corresponding optimal parameters r and K ? 26 26


Download ppt "Modelling and Simulating Social Systems with MATLAB"

Similar presentations


Ads by Google