2010-03-01 851-0585-04L – Modelling and Simulating Social Systems with MATLAB Lesson 2 – Statistics and plotting Anders Johansson and Wenjian Yu © ETH.

Slides:



Advertisements
Similar presentations
Introduction to MATLAB The language of Technical Computing.
Advertisements

Introduction to Matlab
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.
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 3.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
EGR106 Week 6 MATLAB FILES Two Dimensional Plots Multiple Plots
Vectors and Plotting Selim Aksoy Bilkent University Department of Computer Engineering
MATLAB TUTORIAL Dmitry Drutskoy Some material borrowed from the departmental MATLAB info session by Philippe Rigollet Kevin Wayne.
Basic Mathematics for Portfolio Management. Statistics Variables x, y, z Constants a, b Observations {x n, y n |n=1,…N} Mean.
Dr. Jie Zou PHY Welcome to PHY 3320 Computational Methods in Physics and Engineering.
MATLAB FOR PATTERN RECOGNITION By: Özge Öztimur. How Much Do We Know? Anybody who has never used MATLAB?
Matlab intro The Environment
Matlab tutorial course Lesson 2: Arrays and data types
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
„  1999 BG Mobasseri1 9/18/2015 June 2 GRAPHICS IN MATLAB- PART I BASIC PLOTTING.
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.
MATLAB An Introduction to MATLAB (Matrix Laboratory) 1.
Matlab for Engineers Built-in Matlab Functions Chapter 3.
Numerical Computation Lecture 2: Introduction to Matlab Programming United International College.
Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
MAE 1202: AEROSPACE PRACTICUM An Introduction to MATLAB: Part 2 Mechanical and Aerospace Engineering Department Florida Institute of Technology Developed.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Recap Sum and Product Functions Matrix Size Function Variance and Standard Deviation Random Numbers Complex Numbers.
Scientific Computing Introduction to Matlab Programming.
L – Modelling and Simulating Social Systems with MATLAB Lesson 8 – Thesis Writing and Presentation of Results Wenjian Yu © ETH Zürich.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
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.
A string is an array of characters Strings have many uses in MATLAB Display text output Specify formatting for plots Input arguments for some functions.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
L – Modelling and Simulating Social Systems with MATLAB © ETH Zürich | Lesson 3 – Dynamical Systems Anders Johansson and Wenjian.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
Introduction to MATLAB Session 2 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2010.
1 Lecture 5 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Lecture 24: Rough and Ready Analysis. MATLAB uses function names consistent with most major programming languages For example sqrt sin cos log.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
An Introduction to Programming in Matlab Emily Blumenthal
L – Modeling and Simulating Social Systems with MATLAB Lecture 2 – Statistics and plotting © ETH Zürich | Giovanni Luca Ciampaglia,
Outline What is MATLAB MATLAB desktop Variables, Vectors and Matrices Matrix operations Array operations Built-in functions: Scalar, Vector, Matrix Data.
How to use MATLAB (using M-files) Double click this icon To start Matlab 6.5.
Statistics made easy Basic principles. Statistics made easy Assume we are interested in getting some information about the average height of people in.
Some “What’s the output” questions to get the day started… >>A = [1 2 3; 3 5 6] This statement stores the matrix: 1. A= 2. A= 3. A= 4. A= Ask Garvin’s.
Modelling and Simulating Social Systems with MATLAB
Working with Data in MATLAB
Computer Application in Engineering Design
Built-in MATLAB Functions Chapter 3
Modelling and Simulating Social Systems with MATLAB
L – Modeling and Simulating Social Systems with MATLAB
L – Modeling and Simulating Social Systems with MATLAB
Lecture 25: Exploring data
MATLAB DENC 2533 ECADD LAB 9.
L – Modelling and Simulating Social Systems with MATLAB
MATLAB How to use (using M-files) Double click this icon
MATLAB How to use (using M-files) Double click this icon
MATLAB How to use (using M-files)
CS 175 Project in AI Discussion -- matlab
Stat 251 (2009, Summer) Lab 1 TA: Yu, Chi Wai.
Plotting Signals in MATLAB
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Announcements P3 due today
Introduction to Matlab
Data analysis with R and the tidyverse
Simulate Multiple Dice
Presentation transcript:

L – Modelling and Simulating Social Systems with MATLAB Lesson 2 – Statistics and plotting Anders Johansson and Wenjian Yu © ETH Zürich |

A. Johansson & W. Yu/ 2 Lesson 2 - Contents  Repetition  Creating and accessing scalars, vectors, matrices  for loop  if case  Solutions for lesson 1 exercises  Statistics  Plotting  Exercises

A. Johansson & W. Yu/ 3 Contents of the course Introduction to MATLAB Introduction to social-science modeling and simulation Working on projects (seminar theses) Handing in seminar thesis and giving a presentation

A. Johansson & W. Yu/ 4 Repetition  Creating a scalar: >> a=10 a = 10

A. Johansson & W. Yu/ 5 Repetition  Creating a row vector: >> v=[ ] v =

A. Johansson & W. Yu/ 6 Repetition  Creating a row vector, 2 nd method: >> v=1:5 v =

A. Johansson & W. Yu/ 7 Repetition  Creating a row vector, 3 rd method: linspace(startVal, endVal, n) >> v=linspace(0.5, 1.5, 5) v =

A. Johansson & W. Yu/ 8 Repetition  Creating a column vector: >> v=[1; 2; 3; 4; 5] v =

A. Johansson & W. Yu/ 9 Repetition  Creating a column vector, 2 nd method: >> v=[ ]’ v =

A. Johansson & W. Yu/ 10 Repetition  Creating a matrix: >> A=[ ; ] A =

A. Johansson & W. Yu/ 11 Repetition  Creating a matrix, 2 nd method: >> A=[1:4; 5:8] A =

A. Johansson & W. Yu/ 12 Repetition  Accessing a scalar: >> a a = 10

A. Johansson & W. Yu/ 13 Repetition  Accessing an element in a vector: v(index) (index=1..n) >> v(2) ans = 2

A. Johansson & W. Yu/ 14 Repetition  Accessing an element in a matrix: A(rowIndex, columnIndex) >> A(2, 1) ans = 5

A. Johansson & W. Yu/ 15 Repetition - operators  Scalar operators: Basic: +, -, *, / Exponentialisation: ^ Square root: sqrt()

A. Johansson & W. Yu/ 16 Repetition - operators  Matrix operators: Basic: +, -, *  Element-wise operators: Multiplication:.* Division:./ Exponentialisation:.^  Solving Ax=b: x = A\b

A. Johansson & W. Yu/ 17 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

A. Johansson & W. Yu/ 18 Repetition – if case  Conditional computation can be made with if : >> val=-4; if (val>0 ) absVal = val; else absVal = -val; end

A. Johansson & W. Yu/ 19 Lesson 1: Exercise 1  Compute: a) b) c)

A. Johansson & W. Yu/ 20 Lesson 1: Exercise 1 – solution  Compute: a) >> (18+107)/(5*25) ans = 1

A. Johansson & W. Yu/ 21 Lesson 1: Exercise 1 – solution  Compute: b) >> s=0; >> for i=0:100 s=s+i; end >> s s = 5050

A. Johansson & W. Yu/ 22 Lesson 1: Exercise 1 – solution  Compute: c) >> s=0; >> for i=5:10 s=s+i^2-i; end >> s s = 310

A. Johansson & W. Yu/ 23 Lesson 1: Exercise 2  Solve for x:

A. Johansson & W. Yu/ 24 Lesson 1: Exercise 2 – solution >> A=[ ; ; ; ]; >> b=[1; 2; 3; 4]; >> x=A\b x = >> A*x ans = Ax=b

A. Johansson & W. Yu/ 25 Lesson 1: Exercise 3  Fibonacci sequence: Write a function which compute the Fibonacci sequence until a given number n and return the result in a vector.  The Fibonacci sequence F(n) is given by :

A. Johansson & W. Yu/ 26 Lesson 1: Exercise 3 – 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); end >> fibonacci(7) ans =

A. Johansson & W. Yu/ 27 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)

A. Johansson & W. Yu/ 28 Plotting functions  Plotting in MATLAB can be performed with the following commands: Plot vector x vs. vector y: 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)

A. Johansson & W. Yu/ 29 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’)

A. Johansson & W. Yu/ 30 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*’) * * * *

A. Johansson & W. Yu/ 31  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); Plotting tips

A. Johansson & W. Yu/ 32 Plotting tips >> plot(x,y1); >> hold on >> plot(x,y2,’r’); >> plot(x,y3,’g’);

A. Johansson & W. Yu/ 33 Plotting tips >> plot(x,y1); >> hold on >> plot(x,y2,’r’); >> plot(x,y3,’g’); >> grid on

A. Johansson & W. Yu/ 34 Datasets  Two datasets for statistical plotting can be found on the course web page you will find the files: countries.m cities.m

A. Johansson & W. Yu/ 35 Datasets  Download the files countries.m and cities.m and save them in the working directory of MATLAB.

A. Johansson & W. Yu/ 36 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

A. Johansson & W. Yu/ 37 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.

A. Johansson & W. Yu/ 38 Datasets – countries  The sort() function can be used to sort all items of a vector in inclining order. >> life = A(:, 4); >> plot(life)

A. Johansson & W. Yu/ 39 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)

A. Johansson & W. Yu/ 40 Datasets – countries  The histogram hist() is useful for getting the distribution of the values of a vector. >> life = A(:, 4); >> hist(life)

A. Johansson & W. Yu/ 41 Datasets – countries  Alternatively, a second parameter specifies the number of bars: >> life = A(:, 4); >> hist(life, 30)

A. Johansson & W. Yu/ 42 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() Slides/exercises: (use Firefox!)

A. Johansson & W. Yu/ 43 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.

A. Johansson & W. Yu/ 44 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.

A. Johansson & W. Yu/ 45 Exercise 4 – optional  Zipf’s law: Zipf’s law says that the rank, x, of cities (1: largest, 2: 2 nd largest, 3: 3 rd largest,...) and the size, y, of cities (population) has a power-law relation: y ~ x b 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() )