DOC PREVIEW
Yale ECON 510 - Introduction to Matlab

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Introduction to Matlab for Econ 511b Jinhui Bai January 20, 2004 I. Introduction Matlab means “Matrix Laboratory”. From the name you can see that it is a matrix programming language. Matlab includes both the computer language and a rich toolbox written by it. The current version of Matlab as a language is 6.5, with the full package including toolbox called “Matlab Release 13”. II. Matlab Basics 1. Start to Use Matlab 1.1. Get to Know your Friend You can launch Matlab as any other program in the Windows Operation System. When you enter Matlab, you will see “command window”, where you can write simple command and read the result. You may also see other windows besides that. For example, in “Launch Pad” you can see main products included in your copy of Matlab. “Workspace” includes the basic properties of all the variables generated by your process, and you can double click to see the contents of those variables. “Command history” reminds you about the commands you have typed in “Command Window”. “Current Directory” gives your working directory, where Matlab will search once you start to execute a program. Therefore, you should put your program in “current directory”. You can change current directory by clicking the “browse button” beside that. To launch a window, just click “View” menu. Try to play around with those menus for a while. As a window user, you will know the meaning of those menu items without learning them. 1.2. Get Help about Matlab Usually the first thing to learn about a computer language is to know where to find help. There are mainly three ways to find a help. First, you can click menu “Help\Matlab Help”. There you can find a very good introduction (indeed the best help among software I ever used). For example, in “Getting Started” you can find an introduction for beginners; in “MATLAB Functions Listed Alphabetically” you can find the list of all functions. The second way is to type “help function name” in command window. For example, if you want to know how to use the function to do OLS in Statistics Toolbox, just type “help regress”. Then you will see the introduction to “regress” function. If you type “help stats”, you will find the whole list of functions in Statistics Toolbox. The third way to get help is to go to Matlab website. In Matlab Version 6.5, you can click menu “Web\MATLAB Newsgroup Access”. In the BBS system there, a lot of friendly people are ready to answer your questions.1.3. Set Path If you get a toolbox from a third party (e.g. the Econometrics Toolbox written by James LeSage on the website www.spatial-econometrics.com), you need to set search path to make Matlab treat it in the same way as its own toolbox. To do this, just click menu “File\Set Path\Add Folders (or “Add with Subfolders” if there are multiple folders there)\Save”. After this, you can call the functions in that toolbox in the same way as other toolboxes. 1.4. Edit and Run a program In the Matlab, the file containing code is called M-File. For example, the function to do bootstrap is called “bootstrp.m”. If you want to read its code, you can (a) type “type bootstrp” in the command window; or (b) double click “bootstrp.m” in the folder if you can find it; or (c) type “edit bootstrp”. For either (b) or (c), you are led to Matlab editor\debugger. There you can edit the code in your desired way. To run an M-File, just type “file name” in the command window. For example, after you finish writing a code “shooting.m” to solve a second-order nonlinear difference equation using shooting algorithm, type “shooting” in command window. 2. Creating Matrix Now it is time to start writing your own code. Loosely speaking, the basic element to work with is matrix, although it can be more than two dimensions. There are four ways to get a matrix. First, you can input a matrix manually. For example, type “a = [3, 4, 5; 2, 3, 4];” in the command window. This will generate a 2 by 3 matrix called “a”. We use “[ ]” to enclose the matrix elements; use comma (or equivalently space) to separate the elements within the same row; and use “;” to represent the end of a row. One thing to keep in mind is that Matlab is case-sensitive. Therefore, “A” and “a” are two different matrices. As a practice, try to type “A = [1 2; 3 5]” without a semicolon at the end of this command. What have you seen? Second, we can use built-in functions to get matrix. For example, “a = zeros (3, 4)” generates a 3-by-4 zero matrix; “b = ones (2)” generates a 2-by-2 matrix with each element equal to one; “c = rand (2, 4)” generates a 2-by-4 matrix with each element withdrawn from a uniform distribution between 0 and 1; “d = randn (2,5)” is similar to the above case, but with standard normal distribution. Third, load matrix from an outside data set. In Matlab, the data file is called “*.mat” file. If you want to use “a.mat”, type “load a;”. The last way is to merge available matrices. To concatenate a matrix, just treat each matrix as a number and do the concatenation as manually input a matrix. Try to type the following “A=ones(2,3); B=zeros(2,4); C=rand(3,3); D=randn(3,4); E=[A B;C D]” and read the output. 3. Submatrix Quite often we need to refer to submatrix. If we want to refer to one element of a matrix “a”, just type “a ( i, j)” where i and j are row and column number of that element. To get a submatrix, we need to specify both the row number and column number of the submatrix. For example, “b = a ([1 3], [2, 4])” picks up the row 1 and 3, column 2 and 4 of matrix “a” and put that in the matrix “b”. If we want to pick up some rows or columns consecutively, it is easier to use colon operator “:”. For example, “1:3;” means row vector (1 2 3), while “1:.5:2;” means row vector (1 1.5 2).Here you can see that the middle number means the size of increment, with default case as increment 1. Now you can understand the meaning of “b = a (1:3, 2:4)” or “b = a (1:3, :)”. If you want to delete one row (or column) of a matrix, just type “a (i, :) = [ ]” (or “a (:, j) = [ ]”). 4. Matrix Operation 4.1. Arithmetic Operator Here is a list of main arithmetic operators on matrix. Table 1: Arithmetic Operators Operator + - * / \ ‘ ^ Meaning Addition Subtraction


View Full Document

Yale ECON 510 - Introduction to Matlab

Download Introduction to Matlab
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Introduction to Matlab and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction to Matlab 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?