MATLAB




MATLAB stands for Matrix Laboratory. According to The Mathworks, its producer, it is a "technical computing environment". We will take the more mundane view that it is a programming language.

This section covers the rudiments of the language; later sections will discuss additional features. Our goal is not to produce a user's guide. We aspire to promote a reasonable proficiency in readingprocedures that we will write in the language. Those who wish to use our procedures and write their own will need to spend some time with the manuals provided by the Mathworks.

MATLAB Versions

Versions of MATLAB are available for almost all major computing platforms. Our material was produced and tested on the version designed for the Microsoft Windows environment. The vast majority of it should work without change withh other versions, but no guarantees can be offered.

Of particular interest are the Student Versions of MATLAB. At this writing there are two -- one for the Microsoft DOS operating system, the other for the Macintosh environment. Each can be purchased for approximately $60. These systems include most of the features of the language, but each matrix is limited to a maximum of 1,024 elements. For many applications this proves to be of no consequence. At the very least, one can use a student version to experiment with the language. Presumably the term "student" can be interpreted broadly in this context.

The Student Editions are sold as books with disks enclosed. They are published by Prentice-Hall and can be ordered through bookstores.

In addition to the MATLAB system itself, Mathworks offers sets of Toolboxes, containing MATLAB functions for solvings a number of important types of problems. Of particular interest is optimization toolbox, which will be discussed in a later section.

Matrices as Fundamental Objects

MATLAB is one of a few languages in which each variable is a matrix (broadly construed) and "knows" how big it is. Moreover, the fundamental operators (e.g. addition, multiplication) know how to deal with matrices when required. Finally, the MATLAB environment handles much of the bothersome housekeeping that makes all this possible. Since so many of the procedures required for Macro-Investment Analysis involve matrices, this makes MATLAB an extremely efficient language for both communication and implementation.

Matrix Operations

Consider, for example the following expression: C = A + B If both A and B are scalars (1 by 1 matrices), C will be a sclar equal to their sum. If A and B are, say, row vectors of the same length, C will be a row vector of the same length, with each element equal to the sum of the corresponding elements of A and B. Finally, if A and B are, say, {3*4} matrices, C will be also, with each element equal to the sum of the corresponding elements of A and B.

In short the symbol "+" means "perform a matrix addition". But what if A and B are of incompatible sizes? Not surprisingly, Matlab will complain with a statement such as: ??? Error using ==> + Matrix dimensions must agree. So the symbol "+" means "perform a matrix addition if you can and let me know if you can't".

Assignment Statements

MATLAB uses the common pattern in which a variable can be assigned the value of an expression. The variable name is placed on the left of an equal sign and the expression on the right. The expression is evaluated and the resulting value assigned to the variable name. In MATLAB, there is no need to declare a variable before assigning a value to it. Moreover, if the variable has previously been assigned a value, the new value overrides the predecessor.

This may sound obvious, but consider that the term "value" now includes information concerning the size of matrix as well as its contents. Thus if A and B are of size {20*30} the statement: C = A + B Creates a variable named C that is also {20*30} and fills it with the appropriate values. If C already existed and was, say {20*15} it would be replaced with the required {20*30} matrix. Unlike some languages, there is no need to "pre-dimension" or "re-dimension" variables in MATLAB. It all happens without any explicit action on the part of the user.

Case Sensitivity

MATLAB variable names are normally case-sensitive. Thus variable Cc. A variable name can have up to 19 characters, including letters, numbers and underscores. While it is tempting to use names such as FundReturns it is safer to choose instead fund_returns. Adopt a simple set of naming conventions so that you won't write one version of a name in one place and another later. If you do so, you may get lucky (e.g. the system will complain that you have asked for the value of an undefined variable) or you may not (you will assign the new value to a newly-created wrong variable). In programming languages there are always tradeoffs. You don't have to declare variables in advance in MATLAB. This avoids a great deal of effort. But it allows nasty errors that are often difficult to creep in.

MATLAB does provide a crutch for those who are frustrated by case sensitivity. You may issue the command: casesen off This tells MATLAB to treat every character as if it were lower-case. You may then use capitals where you will. However, this is not recommended since those who use this capability with reckless abandon will produce procedures that will probably not work if case sensitivity is on, as it normally is.

Immediate and Deferred Execution

When MATLAB is invoked, the user is presented with an interactive environment: enter a statement, press the carriage return ("ENTER") and the statement is immediately executed. Given the power that can be packed into one MATLAB statement, this is no small accomplishment. However, for many purposes it is desirable to store a set of MATLAB statements for use when needed.

The simplest form of this approach is the creation of a script file. This is simply a set of commands in a file with a name ending in .m (e.g. do_it.m). Once such a file exists and is stored on disk in a directory that MATLAB knows about (i.e. one on the "MATLAB path"), the user can simply type: do_it at the prompt in interactive mode. The statements will then be executed. Even more powerful is the use of a function file. This is also a file with an .m extension, but one that stores a function. For example, assume that the file val_port.m contains a function to produce the value of a portfolio, given a vector of holdings and a vector of prices. One can simply type: v = val_port(holdings, prices); MATLAB will realize that it doesn't have a built-in function named val_port and search the appropriate directories for a file naned val_port.m, then use the function contained in it.

Whenever possible, you should try to create "m-files" to do your work, since they can then be re-used. To help in this process, MATLAB provides ways for you to recover a log of the commands you have entered; it may then be edited or pieces extracted from it to create script and/or function files.

Showing Values

If at any time you wish to size the contents of a variable, just type its name. MATLAB will do its best, although the result may take some space if the variable contains a large matrix.

MATLAB likes to do this and will tell you what it produces whenever it can. Thus if you type in: C = A + B MATLAB will show you the value of C. This may be a bit daunting if C is, say, a 20 by 30 matrix. To surpress this, put a semicolon at the end of any assignment statement. For example: C = A + B;

Getting Values Into Matrices

If a matrix is small enough, one can provide intial values by simply typing them in. For example: a = 3; b = [ 1 2 3]; c = [ 4 ; 5 ; 6]; d = [ 1 2 3 ; 4 5 6]; Here, a is a scalar, b is a {1*3} row vector, c a {3*1} column vector, and d a {2*3} matrix. For example, typing "d" produces: d = 1 2 3 4 5 6 The system is very simple. Values separated by spaces are on the same row; those separated by semicolons are on separate rows. Values are enclosed in square brackets to indicate a matrix.


Prof. William F. Sharpe, Stanford University / wfsharpe@leland.stanford.edu