77
2016-09-26 ETH Zürich Modeling and Simulating Social Systems with MATLAB Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders, Dirk Helbing

Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

ETH Zürich

Modeling and Simulating Social Systems with MATLAB

Lecture 1 – Introduction to the Course and MATLAB

Computational Social Science

Olivia Woolley, Lloyd Sanders, Dirk Helbing

Page 2: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 2

Lecture 1 – Contents §  Content, rules, and goals of the course

§  Short motivation on Modeling and Simulation

§  MATLAB basics

Page 3: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 3

Modelling and Simulating Social Systems with MATLAB

Weekly lecture with exercises, each session will be split into about 40 minutes lecture and 60 minutes exercises / project work. We will put the lecture slides and other material on http://www.coss.ethz.ch/education/matlab.html and we will use GitHub (more on that later): github.com/msssm

Page 4: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 4

How to contact us

§  in the lecture, please do not hesitate to approach us with any questions you might have

§  via email to

[email protected] or [email protected]

Page 5: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 5

Mailing List §  Send an email to:

[email protected] §  with text:

SUBSCRIBE msssm Your Name §  E.g.

SUBSCRIBE msssm Karsten Donnay

§  Text to unsubscribe:

SIGNOFF msssm

Page 6: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 6

6

Purpose of the Mailing List §  Foster cooperation among students

1. Discuss technical issues

2. Discuss organizational issues

3. Share code

4. Share references to literature

5. Share references to datasets

6. Form and communicate groups

Page 7: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 7

7

Rules of the Mailing List 1.  Be nice.

2.  Be collaborative.

3.  Only small attachments. Add links to external resources.

4.  Do not spam.

Page 8: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 8

Examination Format

Students earn their credit points by:

1.  Implementing a model / running simulations / analyzing data from the social science literature in MATLAB

2.  Submitting a short proposal specifying their project

3.  Producing a seminar thesis (up to 15 pages)

4.  Presenting the results it in a 15 minute talk (The thesis should include a discussion of the model, its theory and properties, details of the simulation, parameter dependencies, results from data analyses, but also possible practical implications.)

§  Submission of the work through GitHub: github.com/msssm (more on that later on)

(also see the course description)

Page 9: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 9

9

Seminar thesis

Studying a scientific paper and/or a data set

Reproducing and extending results / running a simulation in MATLAB

Writing a report and giving a talk

Page 10: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 10

10

Project §  Implementation of a model of a social system in

MATLAB §  During the next 2-3 weeks:

§  Form a group of 2-4 students

§  Decide together on a topic and research question

There are a lot of different project topics available on the Project section of the course website but we encourage you to realize your own ideas for example with one of the datasets listed.

Page 11: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 11

11

Project §  If you are looking for inspiration:

§  A number of reports from the previous course are linked on the Project section of the website

§  Take a look what they did, maybe you would like to build up on their work…

§  If you are a Master/PhD student: §  Your project can, of course, be related to your research

topic

Please approach us in class or write us an email if you have any questions.

Page 12: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 12

12

Project §  Procedure:

§  Organize yourself into groups (use the mailing list!)

§  Carefully select a topic (and corresponding papers)

§  Think about which particular question you want to study

§  Each group has to submit a short 1-2 page proposal that very briefly describes what they are planning to do

§  You will give a 3 minute flash-talk on your proposal

§  More information will follow in the next lectures

Page 13: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 13

13

Flash Talks Each group will have to present their proposal in the

form of a flash talk:

§  Only 1 slide!

§  Only 3 minutes! (plus 2 minutes questions/feedback)

§  The entire 5th lecture (October 24) is reserved for these flash talks

Page 14: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 14

14

Projects from previous semesters

Sugarscape Civil violence Group dynamics

Trust Facebook social networks

Space syntax

Pedestrian dynamics Cycling strategies Tumour growth

Segregation Cancer Traffic dynamics

Swarms Sailing strategies Migration

Flocks Cockroaches Arab Revolutions

Civil war Queuing models Synchronized clapping

Game theory tournament

Game theory Language formation

Page 15: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 15

15

Schedule of the course §  The first lecture will be on the basic functionality

of MATLAB: matrix operations, data structures, conditional statements, statistics, plotting, etc.

§  In later lectures, we will introduce various modeling approaches from the social sciences: dynamical systems, cellular automata, game theory, networks, multi-agent systems, …

Page 16: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 16

16

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

Page 17: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 17

17

Schedule of the course §  Please take note of the dates for the hand-in and

the oral presentation of your projects:

§  Deadline for project proposals: Sunday 23 October 2014 at midnight

§  Flash talks: Monday 24 October 2014

§  Deadline for project reports: Friday 16 December 2014 at midnight

§  Oral project presentations: 19 December to 20 December 2014

Page 18: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 18

18

Goals of the course: 1.  Acquiring understanding of the basics of MATLAB.

2.  Attaining practical knowledge of MATLAB necessary to run computer simulations.

3.  Understanding basic properties of social systems.

4.  Acquaintance with main quantitative modeling approaches for social systems.

5.  Implementing (simple) models of social systems, replicating and extending established models.

6.  Learning to pose a scientific research question

7.  Becoming confident in communicating scientific results in an academic context.

Page 19: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 19

19

Requisites of the Course §  Highly interdisciplinary course: Physics, Sociology, Game

Theory, Biology, Computer Science, Software Engineering…

This course requires:

1.  Hard work

2.  Curiosity and creativity

3.  Interest in scientific methods

Page 20: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 20

20

MATLAB §  Why MATLAB?

Language is quick to learn, easy to use, rich in functionality, good plotting abilities.

§  MATLAB is commercial software from MathWorks, but there are free MATLAB clones with limited functionality (Octave and Scilab).

§  Python SciPy packages similar to MATLAB

§  MATLAB can be downloaded from ides.ethz.ch

Page 21: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 21

What is a Social System?

§  A set of interdependent relationships, customs, and institutions that constitute a society. (Oxford English dictionary)

§  A social system is the patterned series of interrelationships existing between individuals, groups, and institutions and

forming a whole. (Merriam-Webster dictionary)

Page 22: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 22

What is a model?

§  A reduced abstraction of reality aimed at understanding certain components of a more complicated/complex whole.

Page 23: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 23

On Exactitude in Science . . . In that Empire, the Art of Cartography attained such Perfection that the map of a single Province occupied the entirety of a City, and the map of the Empire, the entirety of a Province. In time, those Unconscionable Maps no longer satisfied, and the Cartographers Guilds struck a Map of the Empire whose size was that of the Empire, and which coincided point for point with it. The following Generations, who were not so fond of the Study of Cartography as their Forebears had been, saw that that vast Map was Useless… Source: Jorge Luis Borges, Collected Fictions, Translated by Andrew Hurley Copyright Penguin 1999

What is a model?

Page 24: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 24

24

Introduction to Modelling and Simulations §  Modelling and simulations allow us to formalize,

discover and even predict real-world phenomena.

§  Simulations are the instantiation of a process specified by a formal model.

§  Simulations allow us to study models that are not analytically tractable.

§  Simulations can also be very entertaining (a nice byproduct but not the main purpose of the course).

Page 25: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 25

25

Modelling and Simulation

Model

Social processes

Simulated data

Collected Data

Source: Gilbert and Troitzsch (2005)

Abstraction

Simulation

Data gathering

Similarity

Page 26: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 26

26

Emergence and Simulations §  Explaining macro-phenomena based on

micro-mechanisms

§  Especially relevant in a complex system.

§  Simulations can capture “Emergence”:

“Interactions among objects at one level give rise to different types of objects at another level.”

“The whole is more than the sum of its parts.”

Page 27: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 27

Emergence and Simulations

Courtesy of Iain Couzin

Page 28: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 28

Emergence of Tokyo transportation network

Page 29: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 29

Fig. 1 Network formation in Physarum polycephalum.

Atsushi Tero et al. Science 2010;327:439-442 Published by AAAS

Page 30: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 30

Fig. 2 Comparison of the Physarum networks with the Tokyo rail network.

Atsushi Tero et al. Science 2010;327:439-442 Published by AAAS

Page 31: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 31

Fig. 4 Network dynamics for the simulation model.

Atsushi Tero et al. Science 2010;327:439-442 Published by AAAS

Page 32: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 32

32

References §  Gilbert, N. and Troitzsch K.G. “Simulation for the Social Scientist” 2nd

Ed. Open Uni. Press (2005)

§  Epstein J.M. and Axtell R. “Growing Artificial Societies. Social science

from the bottom up” MIT Press (1996)

§  Epstein J.M. “Generative Social Science” Princeton Uni. Press (2006)

§  Tero, Atsushi, et al. "Rules for biologically inspired adaptive network

design." Science (2010).

§  Emergence: https://www.youtube.com/watch?v=aEaZHWXmbRw

Page 33: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 33

33

MATLAB environment

Page 34: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 34

34

What is MATLAB? §  MATLAB derives its name from matrix laboratory

§  Interpreted language §  No compilation like in C++ or Java §  The results of the commands are immediately

displayed

§  Procedural/imperative programming

§  Matrix/array/vector programming

§  (Object-oriented programming)

Page 35: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 35

35

Overview - What is MATLAB? §  MATLAB derives its name from matrix laboratory

§  Scalars §  Vectors §  Matrices

x11

Page 36: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 36

36

Overview - What is MATLAB? §  MATLAB derives its name from matrix laboratory

§  Scalars §  Vectors §  Matrices

x11 x12 x13

x11

x12

x13

Page 37: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 37

37

Overview - What is MATLAB? §  MATLAB derives its name from matrix laboratory

§  Scalars §  Vectors §  Matrices

x11 x12 x13

x21 x22 x23

Page 38: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 38

38

Overview - What is MATLAB? §  MATLAB derives its name from matrix laboratory

§  Scalars §  Vectors §  Matrices §  Multi-dimensional

x111 x121 x131

x211 x221 x231

Page 39: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 39

39

Pocket calculator §  MATLAB can be used as a pocket calculator:

>> 1+2+3

ans=

6

>> (1+2)/3

ans=

1

Page 40: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 40

40

Variables and operators §  Variable assignment is made with ‘=’

§  Variable names are case sensitive: §  Num, num, NUM are all different variables

>> num=10

num =

10

§  The semicolon ‘;’ cancels the validation display >> B=5;

>> C=10*B

C =

50

Page 41: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 41

41

Variables and operators §  Basic operators:

§  + - * / : addition subtraction multiplication division §  ^ : Exponentiation §  sqrt() : Square root §  % comment >> a=2; % First term

>> b=5; % Second term

>> c=9; % Third term

>> R=a*(sqrt(c) + b^2);

>> R

R =

56

Page 42: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 42

42

Data structures: Vectors §  Vectors are used to store a set of scalars

§  Vectors are defined by using square bracket [ ]

>> x=[0 2 4 10]

x =

0 2 4 10

Page 43: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 43

43

Data structures: Defining vectors

>> x=0:2:6

x =

0 2 4 6

§  Vectors can be used to generate a regular list of scalars by means of colon ‘:’ §  n1:k:n2 generate a vector of values going from n1 to n2

with step k

§  The default value of k is 1 >> x=2:5

x =

2 3 4 5

Page 44: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 44

44

Data structures: Accessing vectors

>> x=1:0.5:3;

>> x(2)

ans =

1.5

§  Access to the values contained in a vector §  x(i) return the ith element of vector x

§  x(i) is a scalar and can be assigned a new value >> x=1:5;

>> x(3)=10;

>> x

x =

1 2 10 4 5

Page 45: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 45

45

Data structures: Size of vectors

>> x=1:0.5:3;

>> s=length(x)

s =

5

§  Vectors operations §  The command length(x) return the size of the vector x

§  x(i) return an error if i>length(x) >> x=1:0.5:3;

>> x(6) ??? Index exceeds matrix dimensions.

Page 46: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 46

46

Data structures: Increase size of vectors

>> x=1:5;

>> x(6)=10;

>> x

x =

1 2 3 4 5 10

§  Vectors operations §  Vector sizes can be dynamically increased by

assigning a new value, outside the vector:

Page 47: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 47

47

Data structures: Increase size of vectors

>> x=1:5;

>> x(6)=10;

>> x

x =

1 2 3 4 5 10

§  Vectors operations §  Vector sizes can be dynamically increased by

assigning a new value, outside the vector:

Important: the first element of a vector has index 1

Page 48: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 48

48

Data structures: Sub-vectors

§  Vectors operations §  Subvectors can be addressed by using a colon §  x(i:j) return the sub vector of x starting from the ith

element to the jth one

>> x=1:0.2:2;

>> y=x(2:4);

>> y

y =

1.2 1.4 1.6

Page 49: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 49

49

Data structures: Matrices

§  Matrices are two dimensional vectors §  Can be defined by using semicolon into square

brackets [ ] >> x=[0 2 4 ; 1 3 5 ; 8 8 8]

x = 0 2 4 1 3 5 8 8 8

>> x=[1:4 ; 5:8 ; 1:2:7]

x = 1 2 3 4 5 6 7 8 1 3 5 7

Page 50: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 50

50

Data structures: Matrices

>> x=[0 2 4 ; 1 3 5 ; 8 8 8]

x = 0 2 4 1 3 5 8 8 8

>> y=x(2,3)

y =

5

§  Accessing the elements of a matrix §  x(i,j) return the value located at ith row and jth column §  i and j can be replaced by a colon ‘:’ to access the

entire line or column

Page 51: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 51

51

Data structures: Matrices

>> x=[0 2 4 ; 1 3 5 ; 8 8 8]

x = 0 2 4 1 3 5 8 8 8

>> y=x(2,:)

y =

1 3 5

§  Access to the values contained in a matrix §  x(i,j) return the value located at ith line and jth column §  i and j can be replaced by a colon ‘:’ to access the

entire line or column

Page 52: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 52

52

Data structures: Matrices

>> x=[0 2 4 ; 1 3 5 ; 8 8 8]

x = 0 2 4 1 3 5 8 8 8

>> y=x(:,3)

y =

4 5 8

§  Access to the values contained in a matrix §  x(i,j) return the value located at ith line and jth column §  i and j can be replaced by a colon ‘:’ to access the

entire line or column

Page 53: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 53

53

Matrices operations: Transpose §  Transpose matrix

§  Switches lines and columns §  transpose(x) or simply x’ >> x=[1:3 ; 4:6]

x = 1 2 3 4 5 6 >> transpose(x) x = 1 4 2 5 3 6 >> x’ x = 1 4 2 5 3 6

Page 54: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 54

54

Matrix operations §  Inter-matrices operations

§  C=A+B : returns C with C(i,j) = A(i,j)+B(i,j) §  C=A-B : returns C with C(i,j) = A(i,j)-B(i,j)

A and B must have the same size, unless one of them is a scalar

>> A=[1 2;3 4] ; B=[2 2;1 1];

>> C=A+B

C = 3 4 4 5

>> C=A-B

C = -1 0 2 3

Page 55: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 55

55

Matrix operations: Multiplication §  Inter-matrices operations

§  C=A*B is a matrix product. Returns C with C(i,j) = ∑ (k=1 to N) A(i,k)*B(k,j) N is the number of columns of A which must equal the

number of rows of B

Page 56: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 56

56

Element-wise multiplication §  Inter-matrices operations

§  C=A.*B returns C with C(i,j) = A(i,j)*B(i,j) A and B must have the same size, unless one of them is a scalar

>> A=[2 2 2;4 4 4];

>> B=[2 2 2;1 1 1];

>> C=A.*B C = 4 4 4 4 4 4

Page 57: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 57

57

Element-wise division §  Inter-matrices operations

§  C=A./B returns C with C(i,j) = A(i,j)/B(i,j) A and B must have the same size, unless one of them is a scalar

>> A=[2 2 2;4 4 4];

>> B=[2 2 2;1 1 1];

>> C=A./B C = 1 1 1 4 4 4

Page 58: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 58

58

Matrices operations: Division §  Inter-matrices operations

§  x=A\b returns the solution of the linear equation A*x=b A is a n-by-n matrix and b is a column vector of size n

>> A=[3 2 -1; 2 -2 4; -1 0.5 -1];

>> b=[1;-2;0];

>> x=A\b

x = 1 -2 -2

3x + 2y � z = 12x� 2y + 4z = �2

�x +12y � z = 0 Attention:

/ (slash) and \ (back slash) are different operators

Page 59: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 59

59

Matrices: Creating

§  Matrices can also created by these commands: rand(n, m) a matrix of size n x m, containing random numbers [0,1] zeros(n, m), ones(n, m) a matrix containing 0 or 1 for all elements

Page 60: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 60

60

Matrices Dimensions

>> A = zeros(3,4);

>> size(A)

ans =

3 4

>> size(A,1)

ans =

3

>> size(A,2)

ans =

4

§  size() returns info about a matrix’s dimensions.

Page 61: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 61

61

The for loop

§  Vectors are often processed with loops in order to access and process each value, one after the other: §  Syntax : for i=x

…. end

§  With -  i the name of the running variable -  x a vector containing the sequence of values assigned to i

Page 62: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 62

62

The for loop

>> for i=1:3

i^2

end

i =

1

i =

4

i =

9

§  MATLAB waits for the keyword end before computing the result.

Page 63: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 63

63

The for loop

>> for i=1:3

y(i)=i^2;

end

>> y

y =

1 4 9

§  MATLAB waits for the keyword end before computing the result.

Page 64: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 64

64

Conditional statements: if

§  The keyword if is used to test a condition §  Syntax :

if (condition) ..sequence of commands.. end

§  The condition is a Boolean operation §  The sequence of commands is executed if the tested

condition is true

Page 65: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 65

65

Logical operators

§  Logical operators §  < , > : less than, greater than §  == : equal to §  && : and §  || : or §  ~ : not ( ~true is false) §  (1 stands for true, 0 stands for false)

Page 66: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 66

66

Conditional statements: Example

>> threshold=5;

>> x=4.5;

>> if (x<threshold)

diff = threshold - x;

end

>> diff

diff =

0.5

§  An example:

Page 67: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 67

67

Conditional statements: else §  The keyword else is optional

§  Syntax : if (condition)

..sequence of commands n°1..

else

..sequence of commands n°2..

end

>> if (x<threshold)

diff = threshold - x ;

else

diff = x – threshold;

end

Page 68: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 68

68

Scripts and functions §  External files used to store and save sequences

of commands.

§  Scripts: §  Simple sequence of commands §  Global variables

§  Functions: §  Dedicated to a particular task §  Inputs and outputs §  Local variables

Page 69: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 69

69

Scripts and functions §  Scripts :

§  Create .m file, e.g. sumVector.m. §  Type commands in the file. §  Type the file name, .e.g sumVector, in the command

window or run code from text editor with open file %sum of 4 values in x x=[1 3 5 7]; R=x(1)+x(2)+x(3)+x(4); R

sumVector.m

>> sumVector

R = 16

Page 70: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 70

70

Scripts and functions §  Make sure that the file is in your working

directory!

Page 71: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 71

71

Scripts and functions §  Functions :

§  Create .m file, e.g. absoluteVal.m §  Declare inputs and outputs in the first line of the file,

function [out1, out2, …] = functionName (in1, in2, …) e.g. function [R] = absoluteVal(x)

§  Use the function in the command window functionName(in1, in2, …) e.g. absoluteVal(x)

Page 72: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 72

72

Scripts and functions

function [R] = absoluteVal(x) % Compute the absolute value of x if (x<0) R = -x ; else R = x ; end

absoluteVal.m

>> A=absoluteVal(-5);

>> A

A =

5

Page 73: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 73

73

Scripts and functions

function [R] = absoluteVal(x) % Compute the absolute value of x if (x<0) R = -x ; else R = x ; end

absoluteVal.m

>> A=absoluteVal(-5);

>> A

A =

5

Page 74: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 74

74

Exercise 1

§  Compute: a) b)

c) §  Slides/exercises:

http://www.coss.ethz.ch/education/matlab.html

25510718

×

+ ∑=

100

0ii

∑=

−10

5

2 )(i

ii

Page 75: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 75

75

Exercise 2

§  Solve for x:

452232223321432

4321

4321

4321

4321

=++−

=−−−

=+−+

=+−−

xxxxxxxxxxxxxxxx

Page 76: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 76

76

Exercise 3 §  Fibonacci sequence: write a function which

computes the Fibonacci sequence of a given number n and returns the result in a vector.

§  The Fibonacci sequence F(n) is given by :

Page 77: Modeling and Simulating Social Systems with MATLAB · 2016. 9. 26. · Lecture 1 – Introduction to the Course and MATLAB Computational Social Science Olivia Woolley, Lloyd Sanders,

2016-09-26

Modeling and Simulating Social Systems with MATLAB 77

77

References §  http://www.mathworks.ch/products/matlab/

index.html

§  http://www.mathworks.ch/matlabcentral/index.html

§  https://ides.ethz.ch/