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.
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.
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".
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.
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.
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.
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;
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.