Top Banner
851-0585-04L Modelling and Simulating 851 0585 04L Modelling and Simulating Social Systems with MATLAB Lesson 3 – Dynamical Systems Anders Johansson and Wenjian Yu 2010-03-08 © ETH Zürich |
42

851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Sep 17, 2018

Download

Documents

vancong
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

851-0585-04L – Modelling and Simulating851 0585 04L Modelling and Simulating Social Systems with MATLAB

Lesson 3 – Dynamical Systems

Anders Johansson and Wenjian Yu

2010-03-08© ETH Zürich |

Page 2: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

ProjectsImplementation of a model from theImplementation of a model from the Social-Science literature in MATLAB.Carried out in pairs.The projects will be assigned next week:The projects will be assigned next week:March 15, 2010

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 2

Page 3: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Lesson 3 - ContentsDifferential EquationsDifferential Equations

Dynamical SystemsPendulumLorenz attractorLotka-Volterra equationsEpidemics: Kermack-McKendrick modelEpidemics: Kermack McKendrick model

Exercises

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 3

Page 4: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

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

dx

,...)(xfdtdx

= x(t)

,...)()()( xfttxtxdt

=Δ−−

x(t-Δt)

)()()(

,...)(

xftttxtx

xft

Δ+Δ−=Δ

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 4

,..)()()( xftttxtx Δ+Δ= Δt

Page 5: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Dynamical systemsA dynamical system is a mathematicalA dynamical system is a mathematical description of the time dependence of a point in a space.

A dynamical system is described by a set ofA dynamical system is described by a set of linear/non-linear differential equations.

Even though an analytical treatment of dynamical systems is often complicated, y y p ,obtaining a numerical solution is straight forward

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 5

forward.

Page 6: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

PendulumA pendulum is a simple dynamical system:A pendulum is a simple dynamical system:

L = length of pendulum (m)

ϴ = angle of pendulum

l ti d tg = acceleration due to gravity (m/s2)

The motion is described by:

)sin(θθLg

−=′′

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 6

L

Page 7: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Pendulum: MATLAB code

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 7

Page 8: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Set time step

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 8

Page 9: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Set constants

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 9

Page 10: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Set starting point of pendulum

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 10

Page 11: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Time loop: Simulate the pendulum

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 11

Page 12: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Perform 1st order Euler’s method

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 12

Page 13: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Plot pendulum

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 13

Page 14: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Set limits of window

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 14

Page 15: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Make a 10 ms pause

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 15

Page 16: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Pendulum: Executing MATLAB code

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 16

Page 17: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Lorenz attractorThe Lorenz attractor defines a 3 dimensionalThe Lorenz attractor defines a 3-dimensional trajectory by the differential equations:

yxdx−= )(σ

dy

yxdt

)(σ

xzyrxdtdy

−−=

σ, r, b are parameters. bzxydz−=

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 17

σ, r, b are parameters. ydt

Page 18: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Lorenz attractor: MATLAB code

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 18

Page 19: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Set time step

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 19

Page 20: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Set number of iterations

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 20

Page 21: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Set initial values

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 21

Page 22: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Set parameters

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 22

Page 23: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Solve the Lorenz-attractor equations

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 23

Page 24: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Compute gradient

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 24

Page 25: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Perform 1st order Euler’s method

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 25

Page 26: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Update time

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 26

Page 27: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Plot the results

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 27

Page 28: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Animation

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 28

Page 29: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 29

Page 30: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Food chain

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 30

Page 31: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Lotka-Volterra equationsThe Lotka Volterra equations describe theThe Lotka-Volterra equations describe the interaction between two species, prey vs. predators, e.g. rabbits vs. foxes.

x: number of preyy: number of predators

)( yxdtdx βα −=

y: number of predatorsα, β, γ, δ: parameters

)(dydt

δ )( xydty δγ −−=

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 31

Page 32: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Lotka-Volterra equationsThe Lotka Volterra equations describe theThe Lotka-Volterra equations describe the interaction between two species, prey vs. predators, e.g. rabbits vs. foxes.

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 32

Page 33: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Lotka-Volterra equationsThe Lotka Volterra equations describe theThe Lotka-Volterra equations describe the interaction between two species, prey vs. predators, e.g. rabbits vs. foxes.

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 33

Page 34: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Epidemics

S l l 2009

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 34

Source: Balcan, et al. 2009

Page 35: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

SIR modelA model for epidemics is the SIR model whichA model for epidemics is the SIR model, which describes the interaction between Susceptible, Infected and Removed (immune) persons, for a given disease. g

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 35

Page 36: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Kermack-McKendrick modelof diseases like the plague and cholera Aof diseases like the plague and cholera. A popular SIR model is the Kermack-McKendrick model. The model was proposed for explaining the spreading p g

The model assumes:A t t l ti iA constant population size.A zero incubation period.The duration of infectivity is as long as the duration of the clinical disease.

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 36

Page 37: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

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

S: Susceptible personsI: Infected persons SpR: Removed (immune) persons

Sβ transmission

personsβ: Infection rate IRγ: Immunity rate R γ

recovery

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 37

Page 38: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

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

S: Susceptible personsI: Infected persons )()( tStI

dtdS β−=p

R: Removed (immune) persons )()()( tItStIdI

dt

γβ=personsβ: Infection rate

)()()(

dR

tItStIdt

γβ −=

γ: Immunity rate )( tIdtdR γ=

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 38

Page 39: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

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

S: Susceptible personsI: Infected personspR: Removed (immune) personspersonsβ: Infection rateγ: Immunity rate

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 39

Page 40: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Exercise 1Implement and simulate the KermackImplement and simulate the Kermack-McKendrick model in MATLAB.

Use the starting values: gS=I=500, R=0, β=0.0001, γ =0.01

Slides/exercises: www.soms.ethz.ch/matlab

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 40

(Download only possible with Firefox!)

Page 41: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Exercise 2A key parameter for the Kermack McKendrickA key parameter for the Kermack-McKendrick model is the epidemiological threshold, βS/γ.

Plot the time evolution of the model and investigate the influence of the epidemiological threshold in particular the cases:threshold, in particular the cases:

1. βS/γ < 1βS/ 12. βS/γ > 1

St ti l S I 500 R 0 β 0 0001

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 41

Starting values: S=I=500, R=0, β=0.0001

Page 42: 851-0585-04L – Modelling and SimulatingModelling and ...webarchiv.ethz.ch/soms/teaching/MatlabSpring10/Lesson3.pdf · 851-0585-04L – Modelling and SimulatingModelling and Simulating

Exercise 3 - optionalImplement the Lotka Volterra model andImplement the Lotka-Volterra model and investigate the influence of the timestep, dt.

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

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

2010-03-08 A. Johansson & W. Yu / [email protected] [email protected] 42