47
2012-10-07 © ETH Zürich | Modeling and Simulating Social Systems with MATLAB Lecture 3 – Dynamical Systems © ETH Zürich | Chair of Sociology, in particular of Modeling and Simulation Tobias Kuhn and Olivia Woolley

Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2012-10-07 © ETH Zürich |

Modeling and Simulating Social Systems with MATLAB

Lecture 3 – Dynamical Systems

© ETH Zürich |

Chair of Sociology, in particular of

Modeling and Simulation

Tobias Kuhn and Olivia Woolley

Page 2: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB

Repetition §  plot(), loglog(), semilogy(), semilogx()

§  hist(), sort() §  subplot() vs figure() §  The amplitude of the sample matters! law of large

numbers – LLN. (Ex.1) §  Outlier observations can significantly skew the

distribution, e.g. mean != median. (Ex. 2) §  Cov() and corrcoef() can measure correlation

among variables. (Ex. 3)

2

Page 3: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB

Repetition – Ex. 4

3

>> loglog(germany(:,2),'LineWidth',3);

Page 4: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 4

Project §  Implementation of a model from the Social

Science literature in MATLAB

§  During the next two weeks: §  Form a group of two to four persons §  Choose a topic among the project suggestions

(available on line) or propose your own idea §  Set up a GitHub repository for your group §  Complete the research proposal (It is the readme file of

your GitHub repository)

Page 5: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5

Research Plan Structure §  1-2 (not more!) pages stating:

§  Brief, general introduction to the problem §  How you abstract the problem with a model §  Fundamental questions you want to try to answer §  Existing literature, and previous projects you will

base your model on and possible extensions. §  https://github.com/msssm/interesting_projects/wiki

§  Research methods you are planning to use

Page 6: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 6

Research Plan §  Upon submission, the Research Plan can:

§  be accepted §  be accepted under revision §  be modified later on only if change is justified (create

new version) §  Talk to us if you are not sure §  Final deadline for signing-up for a project is:

October 21st (midnight)

Page 7: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 7

Dynamical systems

§  Mathematical description of the time dependence of variables that characterize a given problem/scenario in its state space.

Page 8: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 8

Dynamical systems §  A dynamical system is described by a set of

linear/non-linear differential equations.

§  Even though an analytical treatment of dynamical systems is usually very complicated, obtaining a numerical solution is (often) straight forward.

§  Differential equation and difference equation are two different tools for operating with Dynamical Systems

Page 9: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 9

Differential Equations §  A differential equation is a mathematical

equation for an unknown function (dependent variable) of one or more (independent) explicative variables that relates the values of the function itself and its derivatives of various orders

§  Ordinary (ODE) :

§  Partial (PDE) :

df (x)dx

= f (x)

∂f (x,y)∂x

+∂f (x,y)∂y

= f (x,y)

Page 10: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 10

Numerical Solutions for Differential Equations

§  Solving differential equations numerically can be done by a number of schemes. The easiest way is by the 1st order Euler’s Method:

,..)( )()(

,...)()()(

,...)(

xftttxtx

xft

ttxtx

xfdtdx

Δ+Δ−=

Δ−−

=

t

x

Δt

x(t)

x(t-Δt)

Page 11: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 11

A famous example: Pendulum §  A pendulum is a simple dynamical system:

L = length of pendulum (m)

ϴ = angle of pendulum

g = acceleration due to gravity (m/s2)

The motion is described by:

)sin(θθLg

−=ʹ′ʹ′

Page 12: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 12

Pendulum: MATLAB code

Page 13: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 13

Set time step

Page 14: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 14

Set constants

Page 15: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 15

Set starting point of pendulum

Page 16: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 16

Time loop: Simulate the pendulum

Page 17: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 17

Perform 1st order Euler’s method

Page 18: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 18

Plot pendulum

Page 19: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 19

Set limits of window

Page 20: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 20

Make a 10 ms pause

Page 21: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 21

Pendulum: Executing MATLAB code

The file pendulum.m may be found on the website!

Page 22: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 22

Lorenz attractor §  The Lorenz attractor defines a 3-dimensional

trajectory by the differential equations:

§  σ, r, b are parameters.

dxdt

=σ(y − x)

dydt

= rx − y − xz

dzdt

= xy − bz

Page 23: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 23

Lorenz attractor: MATLAB code

Page 24: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 24

Set time step

Page 25: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 25

Set number of iterations

Page 26: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 26

Set initial values

Page 27: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 27

Set parameters

Page 28: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 28

Solve the Lorenz-attractor equations

Page 29: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 29

Compute gradient

Page 30: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 30

Perform 1st order Euler’s method

Page 31: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 31

Update time

Page 32: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 32

Plot the results

Page 33: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 33

Animation

The file lorenzattractor.m may be found on the website!

Page 34: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 34

Page 35: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB

Food chain

35

Page 36: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 36

Lotka-Volterra equations §  The Lotka-Volterra equations describe the

interaction between two species, prey vs. predators, e.g. rabbits vs. foxes. x: number of prey y: number of predators α, β, γ, δ: parameters

)(

)(

xydtdy

yxdtdx

δγ

βα

−−=

−=

Page 37: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 37

Lotka-Volterra equations §  The Lotka-Volterra equations describe the

interaction between two species, prey vs. predators, e.g. rabbits vs. foxes.

Page 38: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 38

Lotka-Volterra equations §  The Lotka-Volterra equations describe the

interaction between two species, prey vs. predators, e.g. rabbits vs. foxes.

Page 39: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 39

SIR model §  A general model for epidemics is the SIR model,

which describes the interaction between Susceptible, Infected and Removed (Recovered) persons, for a given disease.

Page 40: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 40

Kermack-McKendrick model §  Spread of diseases like the plague and cholera?

A popular SIR model is the Kermack-McKendrick model.

§  The model assumes: §  Constant population size. §  No incubation period. §  The duration of infectivity is as long as the duration of

the clinical disease.

Page 41: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB

Kermack-McKendrick model

41

Page 42: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 42

Kermack-McKendrick model §  The Kermack-McKendrick model is specified as:

S: Susceptible I: Infected R: Removed/recovered β: Infection/contact rate γ: Immunity/recovery rate

)(

)( )()(

)()(

tIdtdR

tItStIdtdI

tStIdtdS

γ

γβ

β

=

−=

−=

Page 43: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB

Kermack-McKendrick model

43

Page 44: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 44

Exercise 1 §  Implement and simulate the Kermack-

McKendrick model in MATLAB. Use the values: S(0)=I(0)=500, R(0)=0, β=0.0001, γ =0.01

Page 45: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 45

Exercise 2 §  A key parameter for the Kermack-McKendrick

model is the reproductive number R0= β/γ. Plot the time evolution of the model and investigate the epidemiological threshold, in particular the cases:

1.  R0S(0) < 1 2.  R0S(0) > 1

S(0)=I(0)=500, R(0)=0, β=0.0001

Page 46: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 46

Exercise 3 - optional §  Implement the Lotka-Volterra model and

investigate the influence of the timestep, dt.

§  How small must the timestep be in order for the 1st order Euler‘s method to give reasonable accuracy?

§  Check in the MATLAB help how the functions ode23, ode45 etc, can be used for solving differential equations.

Page 47: Modeling and Simulating Social Systems with MATLAB · 2015-05-08 · 2013-10-07 Modeling and Simulation of Social Systems with MATLAB 5 Research Plan Structure ! 1-2 (not more!) pages

2013-10-07 Modeling and Simulation of Social Systems with MATLAB 47

References

§  Strogatz, Steven. "Nonlinear dynamics and chaos: with applications to physics, biology, chemistry and engineering”, 2001.

§  Pop science: Gleick, James. "Chaos: Making a new science", 1997.

§  Matlab and Art: http://vlab.ethz.ch/ROM/DBGT/MATLAB_and_art.html

§  Kermack, W.O. and McKendrick, A.G. "A Contribution to the Mathematical Theory of Epidemics." Proc. Roy. Soc. Lond. A 115, 700-721, 1927.

§  Keeling, Matt J., and Pejman Rohani. "Modeling infectious diseases in humans and animals". Princeton University Press, 2008.