Modeling and Simulating Social Systems with MATLAB · 2016-10-10 · 2016-10-10 Modeling and...

Preview:

Citation preview

2016-10-10 © ETH Zürich |

Modeling and Simulating Social Systems with MATLAB

Lecture 3 – Dynamical Systems and plotting with MATLAB

© ETH Zürich |

Computational Social Science

Olivia Woolley, Lloyd Sanders, Dirk Helbing

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 2

Schedule of the course 26.09. 03.10. 10.10. 17.10. 24.10. 31.10. 07.11. 14.11. 21.11. 28.12. 05.12. 12.12. 19.12.

Introduction to MATLAB

Introduction to social science modelling and simulations Working on projects

(seminar thesis)

Handing in seminar thesis and giving a presentation

Flash Talks

Modeling overview

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 3

Dynamical systems

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

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 4

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

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 5

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)

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 6

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)

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 7

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

−=ʹ′ʹ′

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 8

Pendulum: MATLAB code

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 9

Set time step

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 10

Set constants

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 11

Set starting point of pendulum

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

Time loop: Simulate the pendulum

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 13

Perform 1st order Euler’s method

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 14

Plot pendulum

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 15

Set limits of window

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 16

Make a 10 ms pause

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 17

Pendulum: Executing MATLAB code

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

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 18

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

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 19

Lorenz attractor: MATLAB code

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 20

Set time step

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 21

Set number of iterations

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 22

Set initial values

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 23

Set parameters

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 24

Solve the Lorenz-attractor equations

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 25

Compute gradient

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 26

Perform 1st order Euler’s method

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 27

Update time

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 28

Plot the results

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 29

Animation

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

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 30

2016-10-10 Modeling and Simulation of Social Systems with MATLAB

Food chain

31

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 32

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

δγ

βα

−−=

−=

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 33

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

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

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 34

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

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

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 35

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.

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 36

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.

2016-10-10 Modeling and Simulation of Social Systems with MATLAB

Kermack-McKendrick model

37

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 38

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

γ

γβ

β

=

−=

−=

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 39

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

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

2016-10-10 Modeling and Simulation of Social Systems with MATLAB

SIR model with interaction structure

40

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 41

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

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 42

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

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 43

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.

2016-10-10 Modeling and Simulation of Social Systems with MATLAB 44

References §  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.

§  "Lotka-Volterra Equations”.http://mathworld.wolfram.com/Lotka-VolterraEquations.html

§  "Lorenz Attractor”. http://mathworld.wolfram.com/LorenzAttractor.html

§  Strogatz, Steven H. “Nonlinear dynamics and chaos: with applications to physics, biology, chemistry, and engineering.” Westview press, 2014.

Recommended