### Create a StudySoup account

#### Be part of our community, it's free to join!

Already have a StudySoup account? Login here

# Class Note for ECON 520 with Professor Hirano at UA

### View Full Document

## 13

## 0

## Popular in Course

## Popular in Department

This 5 page Class Notes was uploaded by an elite notetaker on Friday February 6, 2015. The Class Notes belongs to a course at University of Arizona taught by a professor in Fall. Since its upload, it has received 13 views.

## Reviews for Class Note for ECON 520 with Professor Hirano at UA

### What is Karma?

#### Karma is the currency of StudySoup.

#### You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 02/06/15

Econ 520 An Introduction to Matlab Keisuke Hirano September 14 2006 In this course you will be using Matlab to perform numerical computations for the problem sets Matlab is a matrix programming language a variable is considered a matrix by default and matrix operations such as matrix multiplication transpose inverse etc are built in Since it is a programming language it gives you a lot of control over its calculations but you must be rather precise in telling it what to do This short guide is intended to help you get started using Matlab but it only scratches the surface of what you can do with it Much more information can be found in the Matlab manual which also contains a very useful tutorial section 1 Getting Started If you are using the Windows version of Matlab many operations can be done using the pull down menus However for all platforms you can type commands directly to the Matlab intrepreter at the gtgt prompt A very useful command is help If you just type help and hit return you will be presented with a menu of help options If you type help command Matlab will provide help for that particular command For example help hist will call up information on the hist command which produces histograms In addition you can use the Help menu at the top of the Matlab window to get the Table of Contents and Index of commands You might have to change the working directory using the command ed to tell Matlab where your les are or will be located The commands pwd and dir may also be useful for le manage ment To end a Matlab session type exit or quit 2 Working with Variables There are two main things to remember First Matlab is case sensitive So the variable X is not the same as x Second variables in Matlab are treated as matrices For example the command XY is a matrix multiplication and works only ifthe two matrices are conformable To perform army multiplication type XY This multiplies the ab element of X with the ab element of Y for all ab and so requires the two matrices to be of the same size X means the transpose of X Suppose a and b are scalars 1 x 1 matrices You can refer to the ab element of the matrix X by Xab In a similar vein X b refers to the b th column of X and Xa refers to the a th row of X You can get even fancier X110b refers to the 10 by 1 vector comprised of X1 b through X10b Suppose that i is a m x 1 vector of integers between 1 and 71 repeats are ok and X is an n x k matrix Then Xi z is the m x k matrix formed by selecting rows of X according to 2 You can also perform logical indexing of a matrix If i is an n x 1 vector of logical true false values X is an n x k matrix Xi returns the matrix formed by only including the rows of X that correspond to a value of 1 in the vector i As an example of a logical vector suppose that a is an n x 1 vector of numbers The expression a gt O returns an n x 1 logical vector the i th element of a gt O is equal to true if ai gt O and is equal to false otherwise A vector of ones and zeros can be turned into a logical vector by using the logical command For small matrices we can de ne them by typing something like a123456789 which will put the following matrix in Matlab s memory 2 3 5 6 8 9 9 ll THgtH To de ne a new matrix it will sometimes be useful to use a command along the lines of X onesn1 z This will create a matrix X by putting an n x 1 vector of ones in front of the matrix 2 The variable 2 must have exactly 11 rows Note that some commands including sqrt log and exp perform calculations elementibyi element There are different commands for the matrix counterparts but you probably won t be needing those 3 Getting Stuff Done There are two ways to get things done in Matlab 1 Interactive Mode 2 Batch Mode 31 Interactive Mode In this approach you just sit at the computer type a command at the gtgt prompt then when it is done type another command and so on until you have the answer you want There are a couple of things to keep in mind when doing this First if you add a semicolon at the end of a command before hitting the return key Matlab will perform the calculation but not return the result to the screen This can be very useful For example suppose x is a 1000 by 1 vector of numbers and you want to create a new vector y as follows y 3 2x This will take each element of x double it and add 3 If you don t include the semicolon at the end Matlab will actually list the entire y vector it calculates on the screen Second as you go along making calculations and de ning new variables as in the previous example Matlab will keep these variables in its memory To see which variables are currently in Matlab s memory type who The alternative command whos lists the variables in memory plus some additional information about the variables Third you may want to keep track of the operations you perform and the things that get printed to the screen To do this type diary lename Anything that appears on the Matlab screen after you issue the diary command will also get written to the text le lename until you type the command diary off If there is already a le called lename in the working directory the screen output will get appended to the end of that le Fourth after performing a set of calculations you may end up with some possibly large variables that you wish to save for later use The command save lename y Y X will save the variables y Y X to the le lenameMAT The MAT le is a Matlab storage le It is a binary le not a text le so you cannot look at the contents with a text editor Instead you will have to issue the Matlab command load lename to tell Matlab to read the le and put it back in its memory Below are some other commands you may nd useful Remember you can always use the help facility or consult the manual to see how to use these and any other commands randn log clear sqrt rand exp size plot i f ones length hi st for zeros mean min while eye median max kron diag cov sort chol cd std find inv dir pwd 32 Batch Mode You can automate things by typing in all the commands you want to perform in order in a text le that has the extension m Such les are generally called batch les77 ln Matlab these are called Mi les and you should use them extensively when working on the problem sets Suppose you write the following program in an Mi le called progm A short sample program xrandn1001 y32x histy20 At the Matlab prompt you would type prog and hit return Matlab would read progm then perform the commands in order rst generate 100 independent standard normal draws then multiply those by 2 and add 3 then graph the histogram of these draws using 20 bins The rst line is ignored because it starts with 70 it s a good idea to comment your code extensively with 701 Keep in mind that the Mi le must be a text le not for example a Word le If you write the Mi le in a word processor just remember to save it as a text le If you are using Matlab for Windows you can use the New M file and Open M file options in the File menu to work with Mi les When handing in problem sets you should include a copy of the Mi les used to solve them 4 Functions Matlab lets you de ne new functions which can then be called from within a Matlab session or from a batch le The function needs to reside in the current working directory or along Matlab s search path which can be displayed and modi ed with the command path Here is an example of a function in Matlab The le containing the function should be called bernoulli m 11f you are experiencing dif culties printing an Mi le that starts with try adding an extra blank line at the top of the le Postscript printers may misinterpret the le type if the Mi le starts with function ybernoulli p n 70 BERNOULLIM 70 This function generates n independent draws of a Bernoulli 70 random variable with probability of success p 70 first draw n uniform random variables x randn1 70 set y1 if x is less than p This gives probability p of success y x lt p 70 end function definition This function takes two arguments7 p and n7 and returns a vector of n independent Bernoulli random variables with probability of success equal to p Notice that l have commented the le extensively The function can then be called from a Matlab interactive session7 or from within another batch le or another function For example7 the following interactive session calls the bernoulli function gtgt p7 gtgt ybernoulli p1000 gtgt meany ans 070800 This calls bernoulli m to generate 1000 independent Bernoulli random variables with p 7 The average of these 1000 variables is 7087 quite close to the expected value 7 Note when de ning functions7 you can have it return more than one variable For example7 you could de ne a function func1m that takes as inputs x1 and x27 and returns yl and y2 The rst line of the function de nition would be function y1 y2 func1x1x2 y1x1x2 y2x1x2x1 70 end function definition

### BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.

### You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

## Why people love StudySoup

#### "There's no way I would have passed my Organic Chemistry class this semester without the notes and study guides I got from StudySoup."

#### "I used the money I made selling my notes & study guides to pay for spring break in Olympia, Washington...which was Sweet!"

#### "I was shooting for a perfect 4.0 GPA this semester. Having StudySoup as a study aid was critical to helping me achieve my goal...and I nailed it!"

#### "It's a great way for students to improve their educational experience and it seemed like a product that everybody wants, so all the people participating are winning."

### Refund Policy

#### STUDYSOUP CANCELLATION POLICY

All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email support@studysoup.com

#### STUDYSOUP REFUND POLICY

StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here: support@studysoup.com

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to support@studysoup.com

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.