851 0585 04 L Modeling and Simulating Social

  • Slides: 55
Download presentation
851 -0585 -04 L – Modeling and Simulating Social Systems with MATLAB Giovanni Luca

851 -0585 -04 L – Modeling and Simulating Social Systems with MATLAB Giovanni Luca Ciampaglia, Stefano Balietti and Karsten Donnay Chair of Sociology, in particular of Modeling and Simulation © ETH Zürich | 2010 -09 -27

Lesson 1 - Contents § Organisatorial Things § MATLAB environment § What is MATLAB?

Lesson 1 - Contents § Organisatorial Things § MATLAB environment § What is MATLAB? § MATLAB basics: Variables and operators § Data structures § Loops and conditional statements § Scripts and functions § § Exercises 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 2

Modelling and Simulating Social Systems with MATLAB Weekly lecture with computer exercises. The two

Modelling and Simulating Social Systems with MATLAB Weekly lecture with computer exercises. The two hours will be split into about 30 minutes lecture and 60 minutes exercises. We will put the lecture slides and other material on this web page: www. soms. ethz. ch/matlab 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 3

Aims of the course § Learning the basics of MATLAB, attain practical knowledge of

Aims of the course § Learning the basics of MATLAB, attain practical knowledge of MATLAB necessary to run computer simulations. § We also want to encourage you to use the internet/books to further expand your knowledge of MATLAB when working on the final project! § Learning how to implement (simple) models of various social processes and systems. 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 4

Prüfungsmodus (please also refer to the course description) Students have to earn their credit

Prüfungsmodus (please also refer to the course description) Students have to earn their credit points by implementing an established mathematical model from the sociological literature in MATLAB. Furthermore, they have to document their project in a 30 page seminar thesis and present it in a 15 minute seminar talk. The thesis should include a discussion of the mathematical model, the sociological concept behind it, the meaning of formulas, properties of the model, and parameter dependencies, but also possible practical implications. The computer code should be sufficiently well documented. 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 5

Seminar thesis Studying a scientific paper 2010 -09 -27 Reproducing results in MATLAB Writing

Seminar thesis Studying a scientific paper 2010 -09 -27 Reproducing results in MATLAB Writing a report and giving a talk G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 6

Projects from previous semesters Sugarscape Civil violence Group dynamics Trust Facebook social networks Space

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 Size of wars Civil war Queuing models Synchronized clapping Game theory tournament Game theory Language formation 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 7

Contents of the course § The two first lectures will be spent on introducing

Contents of the course § The two first lectures will be spent on introducing the basic functionality of MATLAB: matrix operations, data structures, conditional statements, statistics, plotting, etc. § In the later lectures, we will introduce various modeling approaches from the social sciences: dynamical systems, cellular automata, game theory, networks, multi-agent systems, … 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 8

Contents of the course Introduction to MATLAB 27. 09 04. 10. 11. 10. 18.

Contents of the course Introduction to MATLAB 27. 09 04. 10. 11. 10. 18. 10. 25. 10. 01. 11. Working on projects (seminar theses) 08. 11. 15. 11. 22. 11. 29. 11. 06. 12. 13. 12. 20. 12. 2010 -09 -27 Introduction to social-science modeling and simulation Handing in seminar thesis and giving a presentation G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 9

MATLAB § Why MATLAB? Language is quick to learn, easy to use, rich in

MATLAB § Why MATLAB? Language is quick to learn, easy to use, rich in functionality, good plotting abilities. § MATLAB is commercial software from The Math. Works, but there are free MATLAB clones with limited functionality (octave and Scilab). § MATLAB can be downloaded from ides. ethz. ch 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 10

MATLAB environment § Command window - Where the user enters commands. - Where MATLAB

MATLAB environment § Command window - Where the user enters commands. - Where MATLAB displays its results. 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 11

MATLAB environment § Command History - Store the typed commands. - A double click

MATLAB environment § Command History - Store the typed commands. - A double click on a line execute it on the Command window. 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 12

MATLAB environment § Directory and workspace window - Current directory shows local hard drive.

MATLAB environment § Directory and workspace window - Current directory shows local hard drive. - Workspace displays current variables and their value. 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 13

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

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 § § Allows for object oriented programming. 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 14

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

Overview - What is MATLAB? § MATLAB derives its name from matrix laboratory Matrices § Vectors § Scalars § 2010 -09 -27 x 11 x 12 x 13 x 21 x 22 x 23 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 15

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

Overview - What is MATLAB? § MATLAB derives its name from matrix laboratory Matrices § Vectors § Scalars § 2010 -09 -27 x 11 x 12 x 13 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 16

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

Overview - What is MATLAB? § MATLAB derives its name from matrix laboratory Matrices § Vectors § Scalars § 2010 -09 -27 x 11 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 17

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

Overview - What is MATLAB? § MATLAB derives its name from matrix laboratory Matrices § Vectors § Scalars § Multi-dimensional § 2010 -09 -27 x 111 x 121 x 131 x 211 x 221 x 231 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 18

Pocket calculator § MATLAB can be used as a pocket calculator: >> 1+2+3 ans=

Pocket calculator § MATLAB can be used as a pocket calculator: >> 1+2+3 ans= 6 >> (1+2)/3 ans= 1 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 19

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

Variables and operators § Variable assignment is made with ‘=’ >> num=10 num = 10 § Variable names are case sensitive: § Num, num, NUM are all different to MATLAB § The semicolon ‘; ’ cancels the validation display >> B=5; >> C=10*B C = 50 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 20

Variables and operators § Basic operators: + - * / : addition subtraction multiplication

Variables and operators § Basic operators: + - * / : addition subtraction multiplication division § ^ : Exponentiation § sqrt() : Square root § >> a=2; >> b=5; >> c=9; >> R=a*(sqrt(c) + b^2); >> R R = 56 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 21

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

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 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 22

Data structures: Defining vectors § Vectors can be used to generate a regular list

Data structures: Defining vectors § Vectors can be used to generate a regular list of scalars by means of colon ‘: ’ § n 1: k: n 2 generate a vector of values going from n 1 to n 2 with step k >> x=0: 2: 6 x = 0 2 4 6 The default value of k is 1 § >> x=2: 5 x = 2 3 4 5 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 23

Data structures: Accessing vectors § Access to the values contained in a vector §

Data structures: Accessing vectors § Access to the values contained in a vector § x(i) return the ith element of vector x >> x=1: 0. 5: 3; >> x(2) ans = 1. 5 § 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 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 24

Data structures: Size of vectors § Vectors operations § The command length(x) return the

Data structures: Size of vectors § Vectors operations § The command length(x) return the size of the vector x >> x=1: 0. 5: 3; >> s=length(x) s = 5 § x(i) return an error >> x=1: 0. 5: 3; if i>length(x) >> x(6) ? ? ? Index exceeds matrix dimensions. 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 25

Data structures: Increase size of vectors § Vectors operations § Vector sizes can be

Data structures: Increase size of vectors § Vectors operations § Vector sizes can be dynamically increased by assigning a new value, outside the vector: >> x=1: 5; >> x(6)=10; >> x x = 1 2 3 4 5 10 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 26

Data structures: Sub-vectors § Vectors operations Subvectors can be addressed by using a colon

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 2010 -09 -27 1. 4 1. 6 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 27

Data structures: Matrices § Matrices are two dimensional vectors § Can be defined by

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 1 8 = 2 4 3 5 8 8 >> x=[1: 4 ; 5: 8 ; 1: 2: 7] x 1 5 1 2010 -09 -27 = 2 3 4 6 7 8 3 5 7 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 28

Data structures: Matrices § Accessing the elements of a matrix x(i, j) return the

Data structures: Matrices § Accessing the elements of 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 § >> x=[0 2 4 ; 1 3 5 ; 8 8 8] x 0 1 8 = 2 4 3 5 8 8 >> y=x(2, 3) y = 5 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 29

Data structures: Matrices § Access to the values contained in a matrix x(i, j)

Data structures: Matrices § 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 § >> x=[0 2 4 ; 1 3 5 ; 8 8 8] x 0 1 8 = 2 4 3 5 8 8 >> y=x(2, : ) y = 1 3 5 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 30

Data structures: Matrices § Access to the values contained in a matrix x(i, j)

Data structures: Matrices § 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 § >> x=[0 2 4 ; 1 3 5 ; 8 8 8] x 0 1 8 = 2 4 3 5 8 8 >> y=x(: , 3) y = 4 5 8 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 31

Matrices operations: Transpose § Transpose matrix Switches lines and columns § transpose(x) or simply

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 2 3 = 4 5 6 >> x’ x 1 2 3 = 4 5 6 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 32

Matrices operations § Inter-matrices operations C=A+B : returns C with C(i, j) = A(i,

Matrices 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 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 33

Matrices operations: Multiplication § Inter-matrices operations § C=A*B is a matrix product. Returns C

Matrices 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 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 34

Element-wise multiplication § Inter-matrices operations § C=A. *B returns C with C(i, j) =

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 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 35

Element-wise division § Inter-matrices operations § C=A. /B returns C with C(i, j) =

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 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 36

Matrices operations: Division § Inter-matrices operations § x=Ab returns the solution of the linear

Matrices operations: Division § Inter-matrices operations § x=Ab 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=Ab x = 1 -2 -2 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 37

Matrices: Creating § Matrices can also created by these commands: rand(n, m) a matrix

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 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 38

The for loop § Vectors are often processed with loops in order to access

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 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 39

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

The for loop § MATLAB waits for the keyword end before computing the result. >> for i=1: 3 i^2 end i = 1 i = 4 i = 9 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 40

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

The for loop § MATLAB waits for the keyword end before computing the result. >> for i=1: 3 y(i)=i^2; end >> y y = 1 2010 -09 -27 4 9 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 41

Conditional statements: if § The keyword if is used to test a condition §

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 § 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 42

Logical operators § <, > == && || ~ § (1 stands for true,

Logical operators § <, > == && || ~ § (1 stands for true, 0 stands for false) § § 2010 -09 -27 : less than, greater than : equal to : and : or : not ( ~true is false) G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 43

Conditional statements: Example § An example: >> threshold=5; >> x=4. 5; >> if (x<threshold)

Conditional statements: Example § An example: >> threshold=5; >> x=4. 5; >> if (x<threshold) diff = threshold - x; end >> diff = 0. 5 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 44

Conditional statements: else § The keyword else is optional Syntax : § if (condition).

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 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 45

Scripts and functions § External files used to store and save sequences of commands.

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 § 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 46

Scripts and functions § Should be saved as. m files : From the Directory

Scripts and functions § Should be saved as. m files : From the Directory Window 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 47

Scripts and functions § Scripts : § Create. m file, e. g. sum. Vector.

Scripts and functions § Scripts : § Create. m file, e. g. sum. Vector. m. Type commands in the file. § Type the file name, . e. g sum. Vector, in the command window. § sum. Vector. m %sum of 4 values in x >> sum. Vector x=[1 3 5 7]; R=x(1)+x(2)+x(3)+x(4); R R = 16 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 48

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

Scripts and functions § Make sure that the file is in your working directory! 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 49

Scripts and functions § Functions : § Create. m file, e. g. absolute. Val.

Scripts and functions § Functions : § Create. m file, e. g. absolute. Val. m § Declare inputs and outputs in the first line of the file, function [out 1, out 2, …] = function. Name (in 1, in 2, …) e. g. function [R] = absolute. Val(x) § Use the function in the command window function. Name(in 1, in 2, …) e. g. absolute. Val(x) 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 50

Scripts and functions absolute. Val. m function [R] = absolute. Val(x) % Compute the

Scripts and functions absolute. Val. m function [R] = absolute. Val(x) % Compute the absolute value of x if (x<0) R = -x ; else R = x ; end >> A=absolute. Val(-5); >> A A = 5 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 51

Scripts and functions absolute. Val. m function [R]= absolute. Val(x) % Compute the absolute

Scripts and functions absolute. Val. m function [R]= absolute. Val(x) % Compute the absolute value of x if (x<0) R = -x ; else R = x ; end >> A=absolute. Val(-5); >> A A = 5 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 52

Exercise 1 § Compute: a) b) c) § Slides/exercises: www. soms. ethz. ch/matlab (use

Exercise 1 § Compute: a) b) c) § Slides/exercises: www. soms. ethz. ch/matlab (use Firefox!) 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 53

Exercise 2 § Solve for x: 2010 -09 -27 G. L. Ciampaglia, S. Balietti

Exercise 2 § Solve for x: 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 54

Exercise 3 § Fibonacci sequence: write a function which compute the Fibonacci sequence of

Exercise 3 § Fibonacci sequence: write a function which compute the Fibonacci sequence of a given number n and return the result in a vector. § The Fibonacci sequence F(n) is given by : 2010 -09 -27 G. L. Ciampaglia, S. Balietti & K. Donnay / ciampagg@ethz. ch sbalietti@ethz. ch kdonnay@ethz. ch 55