L – Modelling and Simulating Social Systems with MATLAB

Slides:



Advertisements
Similar presentations
Introduction to Matlab
Advertisements

Introduction to Matlab
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 10.
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Matlab tutorial course Lesson 2: Arrays and data types
FOR 520: Geospatial data analysis with Matlab
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
Introduction to MATLAB
Lecture 4 MATLAB Windows Arithmetic Operators Maintenance Functions
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
MATLAB Tutorial EE 327 Signals and Systems 1. What is MATLAB? MATLAB – Matrix Laboratory The premier number-crunching software Extremely useful for signal.
Numerical Computation Lecture 2: Introduction to Matlab Programming United International College.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
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.
Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
MATLAB Practice 1 Introducing MATLAB Lecture Notes on Video Search & Mining, Spring 2012 Presented by Jun Hee Yoo Biointelligence Laboratory School of.
MATLAB Harri Saarnisaari, Part of Simulations and Tools for Telecommunication Course.
Basics of MATLAB By DR. Wafaa Shabana
L – Modelling and Simulating Social Systems with MATLAB Lesson 2 – Statistics and plotting Anders Johansson and Wenjian Yu © ETH.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Scientific Computing Introduction to Matlab Programming.
Introduction to Matlab. What is Matlab? A software environment for interactive numerical computations Examples:  Matrix computations and linear algebra.
INTRODUCTION TO MATLAB MATLAB is a software package for computation in engineering, science, and applied mathemat-ics. It offers a powerful programming.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
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 DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Introduction to Programming on MATLAB Ecological Modeling Course Sep 11th, 2006.
L – Modeling and Simulating Social Systems with MATLAB Lecture 2 – Statistics and plotting © ETH Zürich | Giovanni Luca Ciampaglia,
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators.
Physics 114: Lecture 1 Overview of Class Intro to MATLAB
L – Modeling and Simulating Social Systems with MATLAB
ECE 1304 Introduction to Electrical and Computer Engineering
Computer Application in Engineering Design
Introduction to Matlab
Matlab Training Session 4: Control, Flow and Functions
L – Modeling and Simulating Social Systems with MATLAB
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
(Mohammed Sami) Ashhab
Matlab Workshop 9/22/2018.
StatLab Matlab Workshop
MATH 493 Introduction to MATLAB
Use of Mathematics using Technology (Maltlab)
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Matlab tutorial course
Lecture 2 Introduction to MATLAB
Communication and Coding Theory Lab(CS491)
Introduction to Matlab
INTRODUCTION TO 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.
Using Script Files and Managing Data
Announcements P3 due today
Introduction to Matlab
Matlab Basics Tutorial
Matlab Basics.
Presentation transcript:

851-0585-04L – Modelling and Simulating Social Systems with MATLAB 12.11.2018 851-0585-04L – Modelling and Simulating Social Systems with MATLAB Lesson 1 Anders Johansson and Wenjian Yu © ETH Zürich |

Lesson 1 - Contents Introduction MATLAB environment What is MATLAB? 12.11.2018 Lesson 1 - Contents Introduction MATLAB environment What is MATLAB? MATLAB basics: Variables and operators Data structures Loops and conditional statements Scripts and functions Exercises

Modelling and Simulating Social Systems with MATLAB 12.11.2018 Modelling and Simulating Social Systems with MATLAB Weekly lecture with computer exercises. The two hours will be split into 30 minutes lecture and 60 minutes exercises. We will put the lecture slides and other material on the web page: www.soms.ethz.ch/matlab

Aims of the course Learning the basics of MATLAB. 12.11.2018 Aims of the course Learning the basics of MATLAB. Learning how to implement models of various social processes and systems. In the end of the course, all students (in pairs) should hand in a Seminar Thesis describing the implementation of a social-science model. The thesis should be about 20 pages long (including figures and source code) and be accompanied by a 10-minutes presentation.

Seminar thesis Studying a scientific paper 12.11.2018 Seminar thesis Studying a scientific paper Reproducing results in MATLAB Writing a report and giving a talk

Projects from previous semesters 12.11.2018 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

12.11.2018 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, …

Contents of the course Introduction to MATLAB 12.11.2018 Contents of the course Introduction to MATLAB 22.02. 01.03. 08.03. 15.03. 22.03. 29.03. 12.04. 26.04. 03.05. 10.05. 17.05. 31.05. Introduction to social-science modeling and simulation Working on projects (seminar theses) Handing in seminar thesis and giving a presentation

12.11.2018 MATLAB Why MATLAB? Quick to learn, easy to use, rich in functionality, good plotting abilities. MATLAB is commercial software from The MathWorks, but there are free MATLAB clones with limited functionality (octave and Scilab). MATLAB can be downloaded from ides.ethz.ch

MATLAB environment Command window Where the user enters commands. 12.11.2018 MATLAB environment Command window Where the user enters commands. Where MATLAB displays its results.

MATLAB environment Command History Store the typed commands. 12.11.2018 MATLAB environment Command History Store the typed commands. A double click on a line execute it on the Command window.

MATLAB environment Directory and workspace window 12.11.2018 MATLAB environment Directory and workspace window Current directory shows local hard drive. Workspace displays current variables and their value.

What is MATLAB? MATLAB derives its name from matrix laboratory 12.11.2018 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

Overview - What is MATLAB? 12.11.2018 Overview - What is MATLAB? MATLAB derives its name from matrix laboratory Matrices Vectors Scalars x11 x12 x13 x21 x22 x23

Overview - What is MATLAB? 12.11.2018 Overview - What is MATLAB? MATLAB derives its name from matrix laboratory Matrices Vectors Scalars x11 x12 x13 x11 x12 x13

Overview - What is MATLAB? 12.11.2018 Overview - What is MATLAB? MATLAB derives its name from matrix laboratory Matrices Vectors Scalars x11

Overview - What is MATLAB? 12.11.2018 Overview - What is MATLAB? MATLAB derives its name from matrix laboratory Matrices Vectors Scalars Multi-dimensional x111 x121 x131 x211 x221 x231

Pocket calculator MATLAB can be used as a pocket calculator: 12.11.2018 Pocket calculator MATLAB can be used as a pocket calculator: >> 1+2+3 ans= 6 >> (1+2)/3 1

Variables and operators 12.11.2018 Variables and operators Variable assignment is made with ‘=’ Variable names are case sensitive: Num, num, NUM are all different to MATLAB >> num=10 num = 10 The semicolon ‘;’ cancel the validation display >> B=5; >> C=10*B C = 50

Variables and operators 12.11.2018 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

Data structures: Vectors 12.11.2018 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

Data structures: Defining vectors 12.11.2018 Data structures: Defining vectors 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 >> x=0:2:6 x = 0 2 4 6 The default value of k is 1 >> x=2:5 x = 2 3 4 5

Data structures: Accessing vectors 12.11.2018 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

Data structures: Size of vectors 12.11.2018 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 if i>length(x) >> x=1:0.5:3; >> x(6) ??? Index exceeds matrix dimensions.

Data structures: Increase size of vectors 12.11.2018 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

Data structures: Sub-vectors 12.11.2018 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

Data structures: Matrices 12.11.2018 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] 1 2 3 4 5 6 7 8 1 3 5 7

Data structures: Matrices 12.11.2018 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 2 4 1 3 5 8 8 8 >> y=x(2,3) y = 5

Data structures: Matrices 12.11.2018 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 2 4 1 3 5 8 8 8 >> y=x(2,:) y =

Data structures: Matrices 12.11.2018 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 2 4 1 3 5 8 8 8 >> y=x(:,3) y = 4 5 8

Matrices operations: Transpose 12.11.2018 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) 1 4 2 5 3 6 >> x’

Matrices operations Inter-matrices operations 12.11.2018 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 -1 0 2 3

Matrices operations: Multiplication 12.11.2018 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

Element-wise multiplication 12.11.2018 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

Element-wise division 12.11.2018 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

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

12.11.2018 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

12.11.2018 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

12.11.2018 The for loop MATLAB waits for the keyword end before computing the result. >> for i=1:3 i^2 end i = 1 4 9

12.11.2018 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 4 9

Conditional statements: if 12.11.2018 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

Logical operators Logical operators 12.11.2018 Logical operators Logical operators < , > : less than, greater than == : equal to && : and || : or ~ : not ( ~true is false) (1 stands for true, 0 stands for false)

Conditional statements: Example 12.11.2018 Conditional statements: Example An example: >> threshold=5; >> x=4.5; >> if (x<threshold) diff = threshold - x; end >> diff diff = 0.5

Conditional statements: else 12.11.2018 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

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

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

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

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

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)

Scripts and functions >> A=absoluteVal(-5); >> A A = 5 absoluteVal.m function [R] = absoluteVal(x) % Compute the absolute value of x if (x<0) R = -x ; else R = x ; end >> A=absoluteVal(-5); >> A A = 5

Scripts and functions >> A=absoluteVal(-5); >> A A = 5 absoluteVal.m function [R]= absoluteVal(x) % Compute the absolute value of x if (x<0) R = -x ; else R = x ; end >> A=absoluteVal(-5); >> A A = 5

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

12.11.2018 Exercise 2 Solve for x:

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 :