Presentation is loading. Please wait.

Presentation is loading. Please wait.

Modelling and Simulating Social Systems with MATLAB

Similar presentations


Presentation on theme: "Modelling and Simulating Social Systems with MATLAB"— Presentation transcript:

1 Modelling and Simulating Social Systems with MATLAB
Modelling and Simulating Social Systems with MATLAB Lesson 2 – Statistical plotting and animation A. Johansson & W. Yu © ETH Zürich |

2 Lesson 2 - Contents Repetition Statistical Plotting Animation
Lesson 2 - Contents Repetition Statistical Plotting Animation

3 Repetition Creating a scalar: >> a=10 a = 10

4 Repetition Creating a row vector: >> v=[1 2 3 4 5] v = 1 2 3 4 5
Repetition Creating a row vector: >> v=[ ] v =

5 Repetition Creating a row vector, 2nd method: >> v=1:5 v =
Repetition Creating a row vector, 2nd method: >> v=1:5 v =

6 Repetition Creating a column vector: >> v=[1; 2; 3; 4; 5] v =
Repetition Creating a column vector: >> v=[1; 2; 3; 4; 5] v =

7 Repetition Creating a column vector, 2nd method:
Repetition Creating a column vector, 2nd method: >> v=[ ]’ v =

8 Repetition Creating a matrix: >> A=[1 2 3 4; 5 6 7 8] A =
Repetition Creating a matrix: >> A=[ ; ] A =

9 Repetition Creating a matrix, 2nd method: >> A=[1:4; 5:8] A =
Repetition Creating a matrix, 2nd method: >> A=[1:4; 5:8] A =

10 Repetition Accessing a scalar: >> a a = 10

11 Repetition Accessing an element in a vector: v(index) >> v(2)
Repetition Accessing an element in a vector: v(index) >> v(2) ans = 2

12 Repetition Accessing an element in a matrix:
Repetition Accessing an element in a matrix: M(rowIndex, columnIndex) >> A(2, 1) ans = 5

13 Repetition - operators
Repetition - operators Scalar operators: Basic: +, -, *, / Exponentialisation: ^ Square root: sqrt()

14 Repetition - operators
Repetition - operators Matrix operators: Basic: +, -, * Element-wise operators: Multiplication: .* Division: ./ Exponentialisation: .^ Solving Ax=b: x = A\b

15 Repetition – for loop Computation can be automized with for loops:
Repetition – for loop Computation can be automized with for loops: >> y=0; for x=1:4 y = y + x^2 + x; end >> y y = 40

16 Repetition – if case Conditional computation can be made with if:
Repetition – if case Conditional computation can be made with if: >> val=-4; if (val>0 ) absVal = val; else absVal = -val; end

17 Statistics Function Statistics in MATLAB can be performed with the following commands: Mean value: mean(x) Median value: median(x) Min/max values: min(x), max(x) Standard deviation: std(x) Variance: var(x) Covariance: cov(x) Correlation coefficient: corrcoef(x)

18 Plotting Function Plotting in MATLAB can be performed with the following commands: Plot vector x vs. vector y: Linear scale: plot(x, y) Logarithmic scale: loglog(x, y) Plot histogram of x: hist(x)

19 Details of plot An additional parameter can be provided to plot() to define how the curve will look like: plot(x, y, ‘key’) Where key is a string which can contain: Color codes: ‘r’, ‘g’, ‘b’, ‘k’, ‘y’, … Line codes: ‘-’, ‘--’ (solid, dashed, etc.) Marker codes: ‘*’, ‘.’, ‘s’, ’c’ Examples: plot(x, y, ‘r--’) plot(x, y, ‘g*’) * * * *

20 Where can I get help? >>help functionname >>helpwin

21 Plotting Tips To make the plots look nicer, the following commands can be used: Set label on x axis: xlabel(“text”) Set label on y axis: ylabel(“text”) Set title: title(“text”)

22 Plotting Tips Two additional useful commands
Plotting Tips Two additional useful commands hold on|off grid on|off >> x=[-5:0.1:5];a=2;b=3; >> y1=exp(-x.^2); >> y2=a*exp(-x.^2); >> y3=exp(-(x.^2)/b); 22 22

23 Plotting Tips >> plot(x,y1); >> hold on
Plotting Tips >> plot(x,y1); >> hold on >> plot(x,y2,’r’); >> plot(x,y3,’g’); 23 23

24 Plotting Tips >> plot(x,y1); >> hold on
Plotting Tips >> plot(x,y1); >> hold on >> plot(x,y2,’r’); >> plot(x,y3,’g’); >> grid on 24 24

25 File I/O Saving a plot >> hgsave(‘fileName.fig’) 10.06.2018 25

26 Tips for I/O Some commands in Linux are also applicable in Matlab.
rmdir,mkdir delete,edit ls,cd Tab

27 Animation A simple example of computer animation.
r = 10;k = 1;pi = 3.14; hold on; for t = 0 : 0.01 : 2*pi x = r*cos(t);y = r*sin(t); plot(x,y,'o','MarkerSize',20); axis([-10,10,-10,10]); %adjust the range of axis M(k) = getframe;%get the current frame k = k+1; end%for

28 Replay a Movie movie(M,N) plays the movie in matrix M for N times.

29 Save Movie as avi File movie2avi(M,’demo.avi’) saves the movie in matrix M as ‘demo.avi’.

30 3 Dimensions Plots Plot a surface surf(X,Y,Z) with: x2 x1 x3 y1 y2 y3
3 Dimensions Plots Plot a surface surf(X,Y,Z) with: X a vector containing the x-axis Y a vector containing the y-axis Z a matrix with Z(i,j) is the corresponding value for Y(i) and X(j) Number of lines and columns in Z must respectively equal length(Y) and length(X). x2 x1 x3 y1 y2 y3 z11 z12 z13 z21 z22 z23 z31 z32 z33 30 30

31 3 Dimensions Plots Plot a surface >> X=-2:0.1:2;
3 Dimensions Plots Plot a surface >> X=-2:0.1:2; >> Y=0:0.1:1; >> Z = Y(2)*exp(-X.^2) Z = >> for i=1:length(Y) Z(i,:)= Y(i)*exp(-X.^2); end 31 31

32 3 Dimensions Plots Plot a surface >> surf(X,Y,Z) 10.06.2018 32

33 3 Dimensions Plots Plot a surface >> surf(X,Y,Z) 10.06.2018 33

34 3 Dimensions Plots Plot a surface >> surf(X,Y,Z) 10.06.2018 34

35 3 Dimensions Plots Plot a surface
3 Dimensions Plots Plot a surface >> contourf(X,Y,Z) >> contour(X,Y,Z) 35 35

36 Datasets The datasets for statistical plotting can be found at the course web page you will find the files: countries.m cities.m

37 Datasets Download the files countries.m and cities.m and save them in the working directory of MATLAB.

38 Datasets - countries This dataset countries.m contains a matrix A with the following specification: Rows: Different countries Column 1: Population Column 2: Annual growth (%) Column 3: Percentage of youth Column 4: Life expectancy (years) Column 5: Mortality

39 Datasets - countries Most often, we want to access complete columns in the matrix. This can be done by A(:, index) For example if you are interested in the life- expectancy column, it is recommended to do: >> life = x(:,4); and then the vector life can be used to access the vector containing all life expectancies.

40 Datasets - countries The sort() function can be used to sort all items of a vector in inclining order. >> life = A(:, 4); >> plot(life)

41 Datasets - countries The sort() function can be used to sort all items of a vector in inclining order. >> life = A(:, 4); >> lifeS = sort(life); >> plot(lifeS)

42 Datasets - countries The histogram hist() is useful for getting the distribution of the values of a vector. >> life = A(:, 4); >> hist(life)

43 Datasets - countries Alternatively, a second parameter specifies the number of bars: >> life = A(:, 4); >> hist(life, 30)

44 Exercise 1 Statistics: Generate a vector of N random numbers with randn(N,1) Calculate the mean and standard deviation. Do the mean and standard deviation converge to certain values, for an increasing N? Optional: Display the histogram with hist(randn(N,1)) and compare with the histogram of rand()

45 Exercise 2 Demographics: From the countries.m dataset, find out why there is such a large difference between the mean and the median population of all countries. Hint: Use hist(x, n) Also sort() can be useful.

46 Exercise 3 Demographics: From the countries.m dataset, see which columns have strongest correlation. Can you reason why these columns have stronger correlations? Hint: Use corrcoef() to find the correlation between columns.

47 Exercise 4 - optional Zipf’s law: Zipf’s law says that the rank, x, of cities (1: largest, 2: 2nd largest, 3: 3rd largest, ...) and the size, y, of cities (population) has a power-law relation: y ~ xb Test if Zipf’s law holds for the three cases in the cities.m file. Try to estimate the b parameter. Hint: Use log() and plot() (or loglog())


Download ppt "Modelling and Simulating Social Systems with MATLAB"

Similar presentations


Ads by Google