Introduction to MATLAB

Similar documents
Chapter 1 Introduction to MATLAB

MATLAB Tutorial. Mohammad Motamed 1. August 28, generates a 3 3 matrix.

Introduction to MATLAB

TUTORIAL 1 Introduction to Matrix Calculation using MATLAB TUTORIAL 1 INTRODUCTION TO MATRIX CALCULATION USING MATLAB

Introduction to Matlab for Econ 511b

The Mathematics of Big Data

MATLAB for beginners. KiJung Yoon, 1. 1 Center for Learning and Memory, University of Texas at Austin, Austin, TX 78712, USA

University of Alberta

How to Use MATLAB. What is MATLAB. Getting Started. Online Help. General Purpose Commands

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment

Introduction to Matlab

Getting started with MATLAB

Lecture 2: Variables, Vectors and Matrices in MATLAB

Appendix A. Introduction to MATLAB. A.1 What Is MATLAB?

A Guide to Using Some Basic MATLAB Functions

1 Week 1: Basics of scientific programming I

Introduction to MATLAB LAB 1

Introduction to Matlab

1 Overview of the standard Matlab syntax

An Introduction to MATLAB and the Control Systems toolbox Aravind Parchuri, Darren Hon and Albert Honein

Matlab Tutorial. The value assigned to a variable can be checked by simply typing in the variable name:

MATLAB TUTORIAL FOR MATH/CHEG 305

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain

To start using Matlab, you only need be concerned with the command window for now.

MATLAB Tutorial. 1. The MATLAB Windows. 2. The Command Windows. 3. Simple scalar or number operations

Introduction to Matlab

Introduction to MATLAB

MATLAB: Quick Start Econ 837

Eng Marine Production Management. Introduction to Matlab

Introduction to Matlab

Introduction to MATLAB

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

1 Introduction to Matlab

Desktop Command window

AMATH 352: MATLAB Tutorial written by Peter Blossey Department of Applied Mathematics University of Washington Seattle, WA

MATLAB Vocabulary. Gerald Recktenwald. Version 0.965, 25 February 2017

Some elements for Matlab programming

CDA5530: Performance Models of Computers and Networks. Chapter 8: Using Matlab for Performance Analysis and Simulation

MATLAB Premier. Middle East Technical University Department of Mechanical Engineering ME 304 1/50

Introduction to Matlab

Introduction to Matlab

Introduction to MatLab. Introduction to MatLab K. Craig 1

Introduction to Matlab. Summer School CEA-EDF-INRIA 2011 of Numerical Analysis

Experiment 1: Introduction to MATLAB I. Introduction. 1.1 Objectives and Expectations: 1.2 What is MATLAB?

Fall 2014 MAT 375 Numerical Methods. Introduction to Programming using MATLAB

ECON 502 INTRODUCTION TO MATLAB Nov 9, 2007 TA: Murat Koyuncu

Lecture 5: Matrices. Dheeraj Kumar Singh 07CS1004 Teacher: Prof. Niloy Ganguly Department of Computer Science and Engineering IIT Kharagpur

Introduction to Matlab. By: Hossein Hamooni Fall 2014

STAT/MATH 395 A - PROBABILITY II UW Winter Quarter Matlab Tutorial

FreeMat Tutorial. 3x + 4y 2z = 5 2x 5y + z = 8 x x + 3y = -1 xx

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

CDA6530: Performance Models of Computers and Networks. Chapter 4: Using Matlab for Performance Analysis and Simulation

Digital Image Analysis and Processing CPE

Matlab notes Matlab is a matrix-based, high-performance language for technical computing It integrates computation, visualisation and programming usin

Introduction to MATLAB. CS534 Fall 2016

Matlab (Matrix laboratory) is an interactive software system for numerical computations and graphics.

A Quick Introduction to MATLAB/Octave. Kenny Marino, Nupur Chatterji

CDA6530: Performance Models of Computers and Networks. Chapter 4: Using Matlab for Performance Analysis and Simulation

EGR 111 Introduction to MATLAB

MATLAB Basics. Mohamed Taha. Communication Engineering Department Princess Sumaya University Page 1 of 32. Full Screen.

Introduction to Matlab

Introduction to Matlab

Identity Matrix: >> eye(3) ans = Matrix of Ones: >> ones(2,3) ans =

Physics 326G Winter Class 2. In this class you will learn how to define and work with arrays or vectors.

Inlichtingenblad, matlab- en simulink handleiding en practicumopgaven IWS

MATLAB Premier. Asst. Prof. Dr. Melik DÖLEN. Middle East Technical University Department of Mechanical Engineering 10/30/04 ME 304 1

MATLAB COURSE FALL 2004 SESSION 1 GETTING STARTED. Christian Daude 1

0_PreCNotes17 18.notebook May 16, Chapter 12

Due date for the report is 23 May 2007

1 Introduction to MATLAB

A 30 Minute Introduction to Octave ENGR Engineering Mathematics Tony Richardson

LAB 2 VECTORS AND MATRICES

CS129: Introduction to Matlab (Code)

Lecture 2. Arrays. 1 Introduction

1 Introduction to MATLAB

Chapter 7: Programming in MATLAB

Introduction to MATLAB. Simon O Keefe Non-Standard Computation Group

Matlab Tutorial: Basics

Introduction to MATLAB

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

Table of Contents. Basis CEMTool 7 Tutorial

Laboratory 1 Octave Tutorial

ECE Lesson Plan - Class 1 Fall, 2001

Matlab Tutorial. CS Scientific Computation. Fall /51

Introduction to MATLAB

MATLAB Basics. Configure a MATLAB Package 6/7/2017. Stanley Liang, PhD York University. Get a MATLAB Student License on Matworks

Computational Mathematics

MATLAB Tutorial EE351M DSP. Created: Thursday Jan 25, 2007 Rayyan Jaber. Modified by: Kitaek Bae. Outline

Introduction to MATLAB

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

A Quick Tutorial on MATLAB. Zeeshan Ali

MATLAB: The Basics. Dmitry Adamskiy 9 November 2011

An Introductory Tutorial on Matlab

Computer Packet 1 Row Operations + Freemat

A QUICK INTRODUCTION TO MATLAB

MATLAB TUTORIAL WORKSHEET

Matlab Workshop I. Niloufer Mackey and Lixin Shen

NatSciLab - Numerical Software Introduction to MATLAB

Learning from Data Introduction to Matlab

Introduction to Matlab

Transcription:

Introduction to MATLAB Econ 8305 Fall 2015 Hang Zhou The George Washington University

Overview 1 Before Getting Started 2 Vector and Matrix Basic Scalar Calculation Matrix Arithmetic Operation Some Useful Functions on Matrix Save and Load Data Graphics 3 Programming in Matlab Relation Operators Conditional Statements Loops 4 M-files Script Files Function Files An Example of Function File Miscellaneous References

Warning You will never handle a programming language by just reading notes or books. The best way to learn is to get more practice. So, write your own code!

Why do Macroeconomists Need to Learn Programming Languages? Estimate and simulate DSGE models (e.g. Dynare) Some newly developed econometrics methods which are not available in the build in statistic software (STATA, Eviews, OxMetrics...) You should at least be familiar with one of the following languages: Matlab, R, Guass, SAS, Python, etc.

Study Recourses MATLAB Tutorials and Learning Resources on Mathwork website: link MIT online course: link On-line help facility: use help command

Assignment and Operators Assignment (=) a=3 Addiction (+) a+b Subtraction (-) a-b Multiplication (*) a*b Division (/) a/b Power ( ) a b The order in which calculations are performed: ( ),, * or /, + or - You can use ; at the end of each command to suppress the output on interface.

Define a Matrix Entering matrix (vector) variables: A = [1, 2; 3, 4] 4 by 4 matrix B = [1, 2, 3, 4] 1 by 4 matrix C = [1; 2; 3; 4] 4 by 1 matrix, or is used to separate columns, ; is used to separate rows. Some Useful functions zeros(m,n): creates an mxn matrix whose elements are equal to zero ones(m,n): creates an mxn matrix whose elements are equal to one. eye(m,n): creates an mxn identity matrix rand(m,n): creates an mxn matrix whose elements are all random number between

Submatrix Submatrix is frequently used in Matlab programming, make sure you are familiar with the following commends. Define a Submatrix A(i,j): returns the element of entry (i,j) in matrix A. A(:,j): returns the jth column of A. A(i,:): returns the ith row of A. A(:,j:k): returns the submatrix of A consisting of th columns j, j+1,...k. A(i:k,:): returns the submatrix of A consisting of th rows i, i+1,...k. A(:,:): returns A unchanged Tips: The colon operator ( : ) can be used as a way of generating row vector A: A=a:i:b a is the starting number, b is the ending number, i is the increment.

Matrix Operations MATLAB allows you to do operations on variables as single entities like in matrix-matrix multiplication as well as operations on the individual elements of the matrix. The arithmetic operators for matrix are the same as the ones for scalar ( +, -, *, \ ) A = ( 1 2 3 4 ) B = ( 5 6 7 8 ) A+B = ( 6 8 10 12 ) ( 4 4 A B = 4 4 ) A B = ( 19 22 43 50 )

Elementwise Arithmetic Operations Arithmetic operators can also be performed element-by-element (.*,.\,. ) A = ( 1 2 3 4 ) B = ( 5 6 7 8 ) A. B = ( 1 5 2 6 3 7 4 8 ) A.\B = ( 1\5 2\6 3\7 4\8 ) ( 1 2 2 A. 2 = 2 ) 3 2 4 2

Some Useful Functions on Matrix A : gives the transpose of the matrix A. det(a): gives the determinant of the square matrix A. rank(a): givens the rank of A. inv(a): gives the inverse of the square matrix A. diag(a): gives a column vector containing the elements on the main diagonal of A. chol(a): gives an upper triangular matrix which is the Cholesky factor of A.

Save and Load Data Matlab can save and read the data in files suffix with.mat. Commands save filename saves all variables on the file filename.mat. save filename v1 v2... saves the variable v1, v2,... on file filename.mat. load filename loads all variable form filename.mat into the work space. You can also load other types of data: Microsoft Excel workbook, text file, etc. Import Data from Excel Files xlsread(filename,xlrange) load the data from Excel file filename. xlrange specifies the data range (e.g. A1:C63). Or, you can import the data interactively

Graphics Function plot(x,y) plots the vector y and vector x with x on the horizontal axis and y on the vertical axis. example: x= -1:0.05:1; plot(x, sin(x)) You can obtain the plot of sine function of which the domain is [-1,1]. To add new graphics onto the current plot, use the command hold on before the new plot function. To release the graphics, so the next plot will replace the current graphic, type the command hold off.

Relation and Logical Operators There are 6 relation operators to compare between variables: < smaller than <= smaller than or equal to > greater than >= greater than or equal to == equal to = not equal to The relational operators generate binary variables: 1 stands for that the comparison is true, 0 means comparison is false. There are 3 logical operators in Matlab: & and or not The logical operators have the lowest precedence of the operators. Both relational and arithmetic operations are performed prior to logical operations.

Conditional Statement The most commonly used conditional statement is if statement: Basic form if logical expression statements end example: if A(1,1) =0 A(1,:)=B(1,:); end According to the logical expression, if the first element in matrix A is not 0, then, elements in the first row of matrix A will be replaced by the first row of matrix B.

Conditional Statement Extensions of if statement: else if logical expression statements 1 else statements 2 end The commands of statements 1 are executed if the logical expression is true, otherwise, the statements 2 will be executed. example: if A(1,1) =0 A(1,:)=B(1,:); else A(:,1)=B(:,1); end

Conditional Statement Extensions of if statement: elseif if logical expression 1 statements1 elseif logical expression 2 statements2 end The statements 1 are executed if logical expression 1 is true, while the statements 2 are executed if logical expression 1 is false and logical expression 2 is true. example: if A(1,1) =0 A(1,:)=B(1,:); elseif A(2,2) =0 A(:,1)=B(:,1); end

Loops Two loops commands are used in Matlab: for and while Command for repeats a statement or group of statements predefined number of times. The statements are terminated by end. for loop for variable = expression statements end example: for i=1:2 A(i,1)=100; end As a result, the first 2 elements in the first column of matrix A is replaced by 100.

Loops Command while repeats a statement as long as a logical expression is true. The construction is terminated by end. while loop while logical expression statements end example: i=1; while i<3 A(i,1)=100; i=i+1; end Once again, the first 2 elements in the first column of matrix A is replaced by 100.

Script Files M-file is a file with suffix.m. It includes scripts files, function files, etc. Life will be much easier if you store all the commands into one file. Such MATLAB command files are called script. Steps In the menu, choose new and then script Write you program code Save the file by choosing the save from the menu Click run or press F5

Function Files Matlab has a large amount of built-in functions. However, they still can t satisfy what you need in most of the cases. Therefore, you need to write you own function. Function files are M-files that start with word function. They can accept inputs arguments and return outputs. Steps In the menu, choose new and then function Change the function name, input arguments, output arguments Write you program code Save the file by choosing the save from the menu Call the function in script file, command window or another function file

Example: Monte Carlo Simulation in Econometrics Function File function [ beta ] = ols( X,Y ) % returns to the OLS estimator of Y=beta*X beta=inv(x *X)*X *Y; end Script File x=rand(200,1); % Generate a series of x from uniform distribution BETA=zeros(10000,1); % Define a 10000 by 1 matrix BETA for i=1:10000 y=0.5*x+randn(1); %Simulate y 10000 times. The error term is drawn from standard normal distribution. BETA(i)=ols(x,y); end mean(beta) % Calculate the mean of those 10000 estimators

Miscellaneous Define a matrix before the loop, otherwise, computational speed will be slowed down. Always remember to write your comments when you are programming. If you don t remember a build-in function, google it. Learn details of DSGE model/econometrics technique from reading code.

References Part-Enander, Eva (1996) The MATLAB handbook, Prentice Hall Brian R. Hunt, Ronald L. Lipsman and Jonathan M. Rosenberg (2006) A Guide to MATLAB - For Beginners and Experienced Users, Cambridge University Press Steven C. Chapra (2008) Applied numerical methods with MATLAB for engineers and scientists, McGraw-Hill Higher Education