View
1
Download
0
Category
Preview:
Citation preview
2015-09-21 ETH Zürich
Modeling and Simulating Social Systems with MATLAB
Lecture 1B– Introduction to MATLAB
Computational Social Science
Olivia Woolley, Stefano Balietti, Dirk Helbing
2015-09-21 Modeling and Simulating Social Systems with MATLAB 2
2
MATLAB environment
2015-09-21 Modeling and Simulating Social Systems with MATLAB 3
3
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)
2015-09-21 Modeling and Simulating Social Systems with MATLAB 4
4
Overview - What is MATLAB? § MATLAB derives its name from matrix laboratory
§ Scalars § Vectors § Matrices
x11
2015-09-21 Modeling and Simulating Social Systems with MATLAB 5
5
Overview - What is MATLAB? § MATLAB derives its name from matrix laboratory
§ Scalars § Vectors § Matrices
x11 x12 x13
x11
x12
x13
2015-09-21 Modeling and Simulating Social Systems with MATLAB 6
6
Overview - What is MATLAB? § MATLAB derives its name from matrix laboratory
§ Scalars § Vectors § Matrices
x11 x12 x13
x21 x22 x23
2015-09-21 Modeling and Simulating Social Systems with MATLAB 7
7
Overview - What is MATLAB? § MATLAB derives its name from matrix laboratory
§ Scalars § Vectors § Matrices § Multi-dimensional
x111 x121 x131
x211 x221 x231
2015-09-21 Modeling and Simulating Social Systems with MATLAB 8
8
Pocket calculator § MATLAB can be used as a pocket calculator:
>> 1+2+3
ans=
6
>> (1+2)/3
ans=
1
2015-09-21 Modeling and Simulating Social Systems with MATLAB 9
9
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
2015-09-21 Modeling and Simulating Social Systems with MATLAB 10
10
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
2015-09-21 Modeling and Simulating Social Systems with MATLAB 11
11
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
2015-09-21 Modeling and Simulating Social Systems with MATLAB 12
12
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
2015-09-21 Modeling and Simulating Social Systems with MATLAB 13
13
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
2015-09-21 Modeling and Simulating Social Systems with MATLAB 14
14
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.
2015-09-21 Modeling and Simulating Social Systems with MATLAB 15
15
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:
2015-09-21 Modeling and Simulating Social Systems with MATLAB 16
16
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
2015-09-21 Modeling and Simulating Social Systems with MATLAB 17
17
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
2015-09-21 Modeling and Simulating Social Systems with MATLAB 18
18
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
2015-09-21 Modeling and Simulating Social Systems with MATLAB 19
19
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 line and jth column § i and j can be replaced by a colon ‘:’ to access the
entire line or column
2015-09-21 Modeling and Simulating Social Systems with MATLAB 20
20
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
2015-09-21 Modeling and Simulating Social Systems with MATLAB 21
21
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
2015-09-21 Modeling and Simulating Social Systems with MATLAB 22
22
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
2015-09-21 Modeling and Simulating Social Systems with MATLAB 23
23
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
2015-09-21 Modeling and Simulating Social Systems with MATLAB 24
24
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
2015-09-21 Modeling and Simulating Social Systems with MATLAB 25
25
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
2015-09-21 Modeling and Simulating Social Systems with MATLAB 26
26
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
2015-09-21 Modeling and Simulating Social Systems with MATLAB 27
27
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
2015-09-21 Modeling and Simulating Social Systems with MATLAB 28
28
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
2015-09-21 Modeling and Simulating Social Systems with MATLAB 29
29
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.
2015-09-21 Modeling and Simulating Social Systems with MATLAB 30
30
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
2015-09-21 Modeling and Simulating Social Systems with MATLAB 31
31
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.
2015-09-21 Modeling and Simulating Social Systems with MATLAB 32
32
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.
2015-09-21 Modeling and Simulating Social Systems with MATLAB 33
33
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
2015-09-21 Modeling and Simulating Social Systems with MATLAB 34
34
Logical operators
§ Logical operators § < , > : less than, greater than § == : equal to § && : and § || : or § ~ : not ( ~true is false) § (1 stands for true, 0 stands for false)
2015-09-21 Modeling and Simulating Social Systems with MATLAB 35
35
Conditional statements: Example
>> threshold=5;
>> x=4.5;
>> if (x<threshold)
diff = threshold - x;
end
>> diff
diff =
0.5
§ An example:
2015-09-21 Modeling and Simulating Social Systems with MATLAB 36
36
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
2015-09-21 Modeling and Simulating Social Systems with MATLAB 37
37
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
2015-09-21 Modeling and Simulating Social Systems with MATLAB 38
38
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.
%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
2015-09-21 Modeling and Simulating Social Systems with MATLAB 39
39
Scripts and functions § Make sure that the file is in your working
directory!
2015-09-21 Modeling and Simulating Social Systems with MATLAB 40
40
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)
2015-09-21 Modeling and Simulating Social Systems with MATLAB 41
41
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
2015-09-21 Modeling and Simulating Social Systems with MATLAB 42
42
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
2015-09-21 Modeling and Simulating Social Systems with MATLAB 43
43
Exercise 1
§ Compute: a) b)
c) § Slides/exercises:
http://www.coss.ethz.ch/education/matlab.html
25510718
×
+ ∑=
100
0ii
∑=
−10
5
2 )(i
ii
2015-09-21 Modeling and Simulating Social Systems with MATLAB 44
44
Exercise 2
§ Solve for x:
452232223321432
4321
4321
4321
4321
=++−
=−−−
=+−+
=+−−
xxxxxxxxxxxxxxxx
2015-09-21 Modeling and Simulating Social Systems with MATLAB 45
45
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 :
2015-09-21 Modeling and Simulating Social Systems with MATLAB 46
46
References § http://www.mathworks.ch/products/matlab/
index.html
§ http://www.mathworks.ch/matlabcentral/index.html
§ https://ides.ethz.ch/
Recommended