47
12-10-2015 © ETH Zürich | Modeling and Simulating Social Systems with MATLAB Lecture 4 – Dynamical Systems © ETH Zürich | Computational Social Science Stefano Balietti, Olivia Woolley, and Dirk Helbing

Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

  • Upload
    others

  • View
    2

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 © ETH Zürich |

Modeling and Simulating Social Systems with MATLAB

Lecture 4 – Dynamical Systems

© ETH Zürich |

Computational Social Science

Stefano Balietti, Olivia Woolley, and Dirk Helbing

Page 2: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB

Checking variable relationship- recap

2

• Taking logarithm allows to clearly see relationship between dependent and independent variable (easy to see straight line) • Matlab command: plot(x, log(y), ‘m--’, ‘LineWidth’, 2)

Color of line: magenta (m) Style of line: --

Use ‘LineWidth’ to set width of line to 2 in this case

Note the scale change: y∈[e,2.5*10^4] ⇒y’=log(y) ∈[1,10.12]

Page 3: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 3

Research Plan Structure

Brief, general introduction to the problem (the context)

How you abstract the problem with a model

What type of model are you going to use? (Diff eqs, cellula automata, agent based…)

What rules are you assuming, how do basic elements of your model interact

Fundamental questions you want to try to answer

What are you trying to show?

Why are you modelling the system?

- Understanding effects of certain parameters and initial conditions

- Reproducing relationship seen in data

Existing literature, and previous projects you will base your model on and possible

extensions.

You need to have at least one reference

Research methods you are planning to use

Page 4: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 4

Project Upload your project proposals by October 19th as

the readme file on github Quickly present in 1 (3 minute) slide your

research project next week (19 October 2015) Please upload your flash slide by October 18th

midnight to the flashtalk folder on github in pdf format Email us the link to your repo

Page 5: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB

Flash Slide One speaker is enough

On the slide have (at least): Topic Research question Methods (how do you intend to model your system) Reference

5

Page 6: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 6

Dynamical systems

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

Page 7: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 7

Dynamical systems A dynamical system is described by a set of

linear/non-linear differential equations.

Even though an analytical solution of dynamical systems can get quickly very complicated, obtaining a numerical solution is (for simple equations) straight forward.

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

Page 8: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 8

Differential Equations A differential equation is a mathematical

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

Ordinary (ODE) :

Partial (PDE) :

),(),(),( yxf

yyxf

xyxf

=+∂

∂∂

Page 9: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB

Simplest differential equation:

9

0)( ccxxf +=c

dxxdf

=)(

• Describes a constant rate of change • To solve a differential equation we

must always perform an integration

0)0( cf =

Page 10: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 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:

,..)( )()(

,...)()()(

,...)(

xfttxttx

xft

txttx

xfdtdx

∆+=∆+

=∆

−∆+

=

t

x

Δt

x(t+Δt)

x(t)

Page 11: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 12

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 · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 13

Pendulum: MATLAB code

Page 13: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 14

Set time step

Page 14: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 15

Set constants

Page 15: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 16

Set starting point of pendulum

Page 16: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 17

Time loop: Simulate the pendulum

Page 17: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 18

Perform 1st order Euler’s method

)]sin()('[)()(

)()()('

)sin()(')('

)sin()(')(')(''

θθθθ

θθθ

θθθ

θθθθ

Lgtttttt

tttttt

Lgtttt

Lg

ttttt

∆−∆+=∆+

∆−∆+

=∆+

∆−=∆+

−=∆

−∆+=

Notice that we are essentially updating θ & θ’ each time step.

Page 18: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 19

Plot pendulum

Line connnecting point (0,0) to point (cos(θ), sin(θ) )

Page 19: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 20

Set limits of window

Page 20: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 21

Make a 10 ms pause

Page 21: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 22

Pendulum: Executing MATLAB code

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

Page 22: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB

Food chain

24

Page 23: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 25

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 24: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 26

Lotka-Volterra equations The Lotka-Volterra equations describe the

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

Page 25: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 27

Lorenz equations The Lorenz equations defines a 3-dimensional trajectory by the

differential equations below: A simplified model of convection (movement of molecules in a fluid) in the earth’s

atmosphere used to understand weather (whole books are written analyzing its results)

σ, r, b are parameters of the flow

Page 26: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 28

Lorenz attractor: MATLAB code

Page 27: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 29

Set time step

Page 28: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 30

Set number of iterations

Page 29: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 31

Set initial values

Page 30: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 32

Set parameters

Page 31: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 33

Solve the Lorenz-attractor equations

Page 32: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 34

Compute gradient

Page 33: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 35

Perform 1st order Euler’s method

Page 34: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 36

Update time

Page 35: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 37

Plot the results

Page 36: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 38

Animation

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

Page 37: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 39

Page 38: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 40

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 · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 41

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 · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 42

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 · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB

Kermack-McKendrick model

44

Page 42: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 45

Kermack-McKendrick model (reaction equations, nonlinear)

The Kermack-McKendrick model is specified as: S: Susceptible I: Infected R: Recovered β: Infection/contact rate γ: Recovery rate )(

)( )()(

)()(

tIdtdR

tItStIdtdI

tStIdtdS

γ

γβ

β

=

−=

−= Note: No one is added to the susceptible group in this case.

Page 43: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 46

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

S: Susceptible I: Infected R: Removed/recovered β: Contact rate γ: Recovery rate

Page 44: Modeling and Simulating Social Systems with MATLAB · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 51

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 · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 52

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 · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 53

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 · linear/non-linear differential equations. Even though an analytical solution of dynamical systems can get quickly very complicated,

12-10-2015 Modeling and Simulation of Social Systems with MATLAB 54

References Strogatz, Steven. "Nonlinear dynamics and chaos: with applications

to physics, biology, chemistry and engineering”, 2001.

Chen et al. “A minimal model of predator- swarm interactions”, JRSI, 2014.

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.

Aoki. “A simulation study on the schooling mechanism in fish.” Bulletin of the Japanese Society of Scientific, 1982.