L – Modeling and Simulating Social Systems with MATLAB

Slides:



Advertisements
Similar presentations
Empirical Model Building I: Objectives: By the end of this class you should be able to: find the equation of the “best fit” line for a linear model explain.
Advertisements

Matlab Graphics S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn Introduction to Matlab: 2D Graphics.
1. Overview 2. plot in 2D 3. Plot in 3D 4. Other possible charts 5. Engineers: label your plots! 6. Plots & Polynomial Plotting 11.
MATLAB GRAPHICS 2-D.
EGR106 Week 6 MATLAB FILES Two Dimensional Plots Multiple Plots
Vectors and Plotting Selim Aksoy Bilkent University Department of Computer Engineering
Chapter 5 Plotting Data Curve Fitting, Good Graphing Practices Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Introduction to Matlab Jianguo Wang CSSCR September 2009.
An introduction to Plotting in MATLAB Rikard Johansson Department of Biomedical Engineering (IMT) Linköping University
Matlab intro The Environment
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
ENG College of Engineering Engineering Education Innovation Center 1 2D Plots 1 in MATLAB Topics Covered: 1.Plotting basic 2-D plots The plot()
MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called.
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
Descriptive Statistics II: By the end of this class you should be able to: describe the meaning of and calculate the mean and standard deviation of a sample.
Numerical Computation Lecture 2: Introduction to Matlab Programming United International College.
L – Modelling and Simulating Social Systems with MATLAB Lesson 2 – Statistics and plotting Anders Johansson and Wenjian Yu © ETH.
Lecture 10 2D plotting & curve fitting Subplots Other 2-D Plots Other 2-D Plots Curve fitting © 2007 Daniel Valentine. All rights reserved. Published by.
Recap Sum and Product Functions Matrix Size Function Variance and Standard Deviation Random Numbers Complex Numbers.
Scientific Computing Introduction to Matlab Programming.
Master in Optical Fiber Communications and Photonic Technologies Foundations of Digital Transmission - Fall quarter Introduction to Matlab.
Matlab Screen  Command Window  type commands  Current Directory  View folders and m-files  Workspace  View program variables  Double click on a.
PH24010 Data Handling and Statistics Use of MathCAD to handle statistical data Data storage in vectors and matrices MathCAD’s built-in functions: –Mean,
Recap Chapter 5 “Plotting” Two Dimensional Plots Simple x-y Plots Titles, Labels and Grids Multiple Plots.
EGR 106 Lecture 6 2-D Plotting Graphical presentation has become the standard method to show technical information. Engineers use plots to analyze, visualize,
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
Data Analysis, Presentation, and Statistics
1 Lecture 5 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
MATLAB ® for Engineers, Holly Moore Fourth Edition, Global Edition © Pearson Education Limited 2015 All rights reserved. Figure 5.1 Simple Plot of Time.
L – Modeling and Simulating Social Systems with MATLAB Lecture 2 – Statistics and plotting © ETH Zürich | Giovanni Luca Ciampaglia,
IENG-385 Statistical Methods for Engineers SPSS (Statistical package for social science) LAB # 1 (An Introduction to SPSS)
How to use MATLAB (using M-files) Double click this icon To start Matlab 6.5.
Chapter 5 EXCEL Graphing Data. Intro In EXCEL graphs are referred to as CHARTS. We will do 2-D graphs known as Scatter Charts or X-Y Charts. Vocabulary:
ECE 1304 Introduction to Electrical and Computer Engineering
Modelling and Simulating Social Systems with MATLAB
Performance Eval Slides originally from Williamson at Calgary
Introduction to MATLAB
L – Modeling and Simulating Social Systems with MATLAB
Computer Application in Engineering Design
Modelling and Simulating Social Systems with MATLAB
Introduction to SPSS.
Plotting Chapter 5.
Lecture 25.
L – Modeling and Simulating Social Systems with MATLAB
Lecture 25: Exploring data
Two-Dimensional Plots
MatLab – Palm Chapter 5 2-D & 3-D Plots
MATLAB DENC 2533 ECADD LAB 9.
Net 222: Communications and networks fundamentals (Practical Part)
Statistical Analysis with Excel
L – Modelling and Simulating Social Systems with MATLAB
Statistical Analysis with Excel
Lecture 10 2D plotting & curve fitting
MATLAB How to use (using M-files) Double click this icon
CPSC 531: System Modeling and Simulation
Data Presentation Carey Williamson Department of Computer Science
MATLAB How to use (using M-files) Double click this icon
MatLab – Palm Chapter 5 Curve Fitting
Warm Up Which plan yields the most interest? Invest $100 Plan A: A 7.5% annual rate compounded monthly for 4 years Plan B: A 7.2% annual rate compounded.
MatLab – 2D Plots 2 MATLAB has many built-in functions and commands to create various types of plots. Instructor notes: We start with an example of some.
Tutorial 2 SEG7550 Introduction to MATLAB II
MATLAB How to use (using M-files)
Stat 251 (2009, Summer) Lab 1 TA: Yu, Chi Wai.
Plotting Signals in MATLAB
Demo of Basic Matlab Features
Carey Williamson Department of Computer Science University of Calgary
How to Use MATLAB A Brief Introduction.
Introduction to Matlab
Data analysis with R and the tidyverse
Graphing & Statistics Honors Biology.
Presentation transcript:

851-0585-04L – Modeling and Simulating Social Systems with MATLAB 01.08.2018 851-0585-04L – Modeling and Simulating Social Systems with MATLAB Lecture 2 – Statistics and Plotting in MATLAB Karsten Donnay and Stefano Balietti Chair of Sociology, in particular of Modeling and Simulation © ETH Zürich |

Goals of Lecture 2: students will Consolidate knowledge of basic MATLAB functionalities acquired during lecture 1 through brief repetition of the main points and revision of the exercises. Get familiar with some basic statistical functionality of MATLAB. Understand the basic MATLAB 2D plotting functionality and apply it appropriately; the example of logarithmic plots is discussed in detail. Master positioning multiple graphs in the screen with subplot() and figure() and custom .m files.

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

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

Repetition Creating a row vector, 3rd method: 01.08.2018 Repetition Creating a row vector, 3rd method: linspace(startVal, endVal, n) >> v=linspace(0.5, 1.5, 5) v = 0.50 0.75 1.00 1.25 1.50

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

Repetition Creating a column vector, 2nd method: 01.08.2018 Repetition Creating a column vector, 2nd method: >> v=[1 2 3 4 5]’ v = 1 2 3 4 5

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

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

Repetition Accessing an element in a vector: 01.08.2018 Repetition Accessing an element in a vector: >> v = 1:10; >> v(2) ans = 2

Repetition Accessing an element in a vector: 01.08.2018 Repetition Accessing an element in a vector: >> v = 1:10; >> v(2) ans = 2 Vector indexes are 1-based index = 1..size ( v , 2 ) 1: column vector 2: row vector

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

Repetition - operators 01.08.2018 Repetition - operators Matrix operators: Basic: +, -, * Element-wise operators: Multiplication: .* Division: ./ Exponentialisation: .^ Solving Ax=b: x = A\b Attention! / (slash) and \ (back slash) produce different results

01.08.2018 Lesson 1: Exercise 1 Compute: a) b) c)

Lesson 1: Exercise 1 – solution 01.08.2018 Lesson 1: Exercise 1 – solution Compute: a) >> (18+107)/(5*25) ans = 1

Lesson 1: Exercise 1 – solution 01.08.2018 Lesson 1: Exercise 1 – solution Compute: b) >> s=sum(1:100) or >> s=sum(1:1:100) >> s=sum(linspace(1,100)) >> s=sum(linspace(1,100,100)) s = 5050 default value default value

Lesson 1: Exercise 1 – solution 01.08.2018 Lesson 1: Exercise 1 – solution Compute: c) >> s=0; >> for i=5:10 >> s=s+i^2-i; >> end >> s s = 310

01.08.2018 Lesson 1: Exercise 2 Solve for x:

Lesson 1: Exercise 2 – solution 01.08.2018 Lesson 1: Exercise 2 – solution >> A=[2 -3 -1 4; 2 3 -3 2; 2 -1 -1 -1; 2 -1 2 5]; >> b=[1; 2; 3; 4]; >> x=A\b x = 1.9755 0.3627 0.8431 -0.2549 >> A*x ans = 1.0000 2.0000 3.0000 4.0000 Ax=b

Lesson 1: Exercise 3 Fibonacci sequence: Write a function which computes the Fibonacci sequence until a given number n and return the result in a vector. The Fibonacci sequence F(n) is given by :

Lesson 1: Exercise 3 – iterative solution fibonacci.m: function [v] = Fibonacci(n) v(1) = 0; if ( n>=1 ) v(2) = 1; end for i=3:n+1 v(i) = v(i-1) + v(i-2); >> Fibonacci(7) ans = 0 1 1 2 3 5 8 13

Lesson 1: Exercise 3 – recursive solution fibo_rec.m: function f = fibo_rec(n)   if n == 0      f(1) = 0;   elseif n == 1     f(2) = 1;     elseif n > 1     f        = fibo_rec(n - 1);     f(n + 1) = f(n) + f(n - 1);   end end >> fibo_rec(7) ans = 0 1 1 2 3 5 8 13

Plotting and Statistics in Matlab

Statistics functions 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) Vector-based Matrix-based

Statistics functions: focus on covariance Covariance and correlation coefficient are linked by a precise mathematical function They express the same type of information, but the correlation coefficient is often more useful. Why?

Where can I get help? >>help functionname >>doc functionname >>helpwin Click on “More Help” from contextual pane after opening first parenthesis, e.g.: plot(… Click on the Fx symbol before command prompt.

2D Plotting functions 2D plotting of curves (y vs x) in MATLAB Linear scale: plot(x, y) Double-logarithmic scale: loglog(x, y) Semi-logarithmic scale: semilogx(x, y) semilogy(x, y) Plot histogram of x: hist(x)

Logarithmic Scale It uses the logarithm of a physical quantity instead of the quantity itself. E.g., increment on the axis are powers of 10 (1,10,100, 1000,…) instead of 1, 2, 3, 4. Examples:

Logarithmic Scale It uses the logarithm of a physical quantity instead of the quantity itself. E.g., increment on the axis are powers of 10 (1,10,100, 1000,…) instead of 1, 2, 3, 4. Examples: Same distance between 1-2-3 and 10-20-30

Logarithmic Rescaling: example ?

Logarithmic Rescaling: example

Logarithmic Scale: examples Decibel Richter scale Entropy in thermodynamics Information in information theory. PH for acidity and alkalinity; Stellar magnitude scale for brightness of stars bit [log 2] and the byte 8[log 2]

Logarithmic Scale: why and when to use it If the data cover a large range of values the logarithm reduces this to a more manageable range. Some of our senses operate in a logarithmic fashion (Weber–Fechner law) Some types of equations get simplified: Exponential: Y = eaX -> semilog scale -> Y = -aX Power Law: Y = Xb -> log-log scale -> logY = b logX

Logarithmic Scale

Back to plotting… 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’, ’x’ Examples: plot(x, y, ‘r--’) plot(x, y, ‘g*’) * * * *

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’)

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

Plotting tips >> plot(x,y1); >> hold on 01.08.2018 Plotting tips >> plot(x,y1); >> hold on >> plot(x,y2,’r’); >> plot(x,y3,’g’); 38 38

Plotting tips >> plot(x,y1); >> hold on 01.08.2018 Plotting tips >> plot(x,y1); >> hold on >> plot(x,y2,’r’); >> plot(x,y3,’g’); >> grid on 39 39

Subplot() and figure() 01.08.2018 Subplot() and figure() subplot(m,n,p) breaks the figure window into an m- by-n matrix and selects the pth axes object for the current plot. The axes are counted along the top row of the figure window, then the second row, etc. h = figure(...) adds a new figure window to the screen. It also returns the handle to it, so it can be further processed. E.g.: saveas(h,’myimage.jpg’); 40 40

01.08.2018 Subplot() by the way… We have already seen an example of a chart created using subplot on slide 31. Could you reproduce it? 41 41

Exercises: working with datasets Two datasets for statistical plotting can be found on the course web page (with NETHZ login) http://www.soms.ethz.ch/matlab you will find the files: countries.m cities.m

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

Exercises: 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

Exercises: 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.

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

Exercises: 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)

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

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

01.08.2018 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 and compare the output of the following two commands hist(randn(N,1)) hist(rand(N,1))

01.08.2018 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. Plus: play with subplot()

01.08.2018 Exercise 3 Demographics: From the countries.m dataset, see which columns have strongest correlation. Can you explain why these columns have stronger correlations? Hint: Use corrcoef() to find the correlation between columns. Use imagesc()to get an immediate visualization of the correlations.

01.08.2018 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 cases in the cities.m file. Try to estimate b. Hint: Use log() and plot() (or loglog()) Plus: use the fitting tool cftool() to get b

01.08.2018 References Additional material on plotting on the home page of the course http://www.soms.ethz.ch/matlab "Log or Linear? Distinct Intuitions of the Number Scale in Western and Amazonian Indigene Cultures”, Science 320 (5880): 1217 http://en.wikipedia.org/wiki/Weber%E2%80%93F echner_law