Sei sulla pagina 1di 6

MGMT 4440/6440: Financial Simulations MATLAB Primer - Extract What is Matlab?

Spring 2012

The name Matlab is abbreviation for MATrix LABoratory, which is a good hint for what Matlab is. It is an interactive, matrix based system for engineering and scientific computations. Where is Matlab available? Matlab is available on RCS machines and RPI Laptops. Mac versions of Matlab may be purchased from Campus Computer store or downloaded from RCS provided software. How to start Matlab? After logging on to your account simply double-click on Matlab icon, Matlab begins with : < M A T L A B > Copyright 1984-2008 The MathWorks, Inc. Version 7.7.0.471 (R2008b) September 17, 2008

To get started, type one of these: helpwin, helpdesk, or demo. For product information, visit www.mathworks.com. >> The ">>" above is the Matlab prompt. Until you quit Matlab you will stay within Matlab environment. How to quit Matlab? Type "quit" on the Matlab prompt. e.g. >> quit How to set constants, vectors and matrices in Matlab? To Matlab every thing is a matrix, constant is a 1x1 matrix, a column vector is an mx1 matrix, etc. A matrix can be entered as follows: >> A = [1, 2, 3; 4, 5, 6; 7, 8, 9]

A = 1 4 7 2 5 8 3 6 9

So, ',' separates each column in a row, and ';' separates rows. Matlab is flexible with the commas, and allows space to separate columns of a row. How to perform computation on constants, vector and matrices in Matlab? The best way to find all the in-built routines of Matlab is the "help" command. >> help

Aparna Gupta

Jan 23, 2012

MGMT 4440/6440: Financial Simulations

Spring 2012

HELP topics: site-matlab/toolbox toolbox/adhamlet toolbox/eztools toolbox/matdraw toolbox/spctools (No table of contents file) Contents of adhamlet GUI folder (No table of contents file) MatDraw GUI Toolbox (No table of contents file)

Screens full of toolbox names will scroll by on the screen, which is not very helpful! But you can scroll up the screen to look for the specific toolbox that you may be interested in and type: >> help matlab/matfun Matrix functions - numerical linear algebra. Matrix analysis. norm - Matrix or vector norm. normest - Estimate the matrix 2-norm. rank - Matrix rank. This time another screen full of information scrolls by with Matlab's Matrix functions (matfun). Let's try and find out the rank of the matrix A we inputted before. >> rank(A) ans = 2 Typing a specific routine against "help" gives the details on usage of the routine >> help rank RANK Matrix rank. RANK(A) provides an estimate of the number of linearly independent rows or columns of a matrix A. RANK(A,tol) is the number of singular values of A that are larger than tol. RANK(A) uses the default tol = max(size(A)) * norm(A) * eps. How to get better help for routines and their descriptions on Matlab? Matlab has excellent resource for help which can be accessed using command: >> helpwin or >> helpdesk These commands open another window, so may not work if you are remotely logged in using 'telnet'. How to stop Matlab from echoing what you type?

Aparna Gupta

Jan 23, 2012

MGMT 4440/6440: Financial Simulations Type a ";" after your command. By doing this Matlab will not echo what you typed, e.g.: >> >> >> >> A = [1 2 3; 4 5 6; 7 r = rank(A); b = [2;1;0]; c= A*b; 8 9];

Spring 2012

against when you don't type the ";" after the command: >> A = 1 2 4 5 7 8 >> r = rank(A) r = 2 >> b = [2;1;0] b = 2 1 0 etc. How to see what variables are so far defined? This is accomplished using command "who" >> who Your variables are: A b c r 3 6 9 A = [1 2 3; 4 5 6; 7 8 9]

That way you can also know what other variables names you can use. Matlab is case sensitive for variable names, function names etc., so 'a' and 'A' are valid and different variables. (whos gives a more detailed description of your variables, like size, bytes etc. Lets see the description of the who function. >> help who WHO List current variables. WHO lists the variables in the current workspace. WHOS lists more information about each variable. WHO GLOBAL and WHOS GLOBAL list the variables in the global workspace. WHO -FILE FILENAME lists the variables in the specified .MAT file. WHO ... VAR1 VAR2 restricts the display to the variables specified. The wildcard character '*' can be used to display variables that match a pattern. For instance, WHO A* finds all variables in the current workspace that start with A. Use the functional form of WHO, such as WHO('-file',FILE,V1,V2), when the filename or variable names are stored in strings.

Aparna Gupta

Jan 23, 2012

MGMT 4440/6440: Financial Simulations S = WHO(...) returns a cell array containing the names of the variables in the workspace or file. You must use the functional form of WHO when there is an output argument. See also WHOS.

Spring 2012

At the bottom of the help response are suggested other similar variables you may want to take a lead from to learn about other useful functions. You can remove a variable from memory using "clear" command. >> clear A This clears the variable 'A'. >> clear all Clears all variables. If you want to re-use a variable name, clear it first, otherwise Matlab may give error messages if you are treating the new variable as a different sized matrix. Also, variables defined in a session are lost at the end of a session. How to save variables for another session? Use save command to save variables. >> save Saving to: matlab.mat Saves all the variables of the session in a file called matlab.mat by default. >> save MyVariables Saves in file called MyVariables.mat. Type help save for more details on save command. How to obtain variables saved in an earlier session? Done using load command. Again use help load to see the various usages for load. At the bottom of result from a help request you will notice there is a recommendation on See also so and so. These are related Matlab routines to what you asked help on. Looking at these occasionally is helpful. >> load MyVariables >> who Your variables are: A ans b

How to write comments on a line of Matlab command? Any thing written after a % in a Matlab command is treated as a comment. >> a = min(1, norm(b)) a = % Rest of this line is a comment

Aparna Gupta

Jan 23, 2012

MGMT 4440/6440: Financial Simulations

Spring 2012

1 Before getting into writing your own routines, a few more important tips: Tip 1: A.*B accomplishes element-wise product of two matrices. Matrix A and B need to be the same size. Tip 2: Submatrices of a matrix can be accessed in the usual way. If matrix A is of dimension 3x4. B = A(2:3, 1:4) is of size 2x4 and contains 2nd and 3rd rows of matrix A. This could also be accomplished by B = (2:3, : ) , implying all the columns of row 2 and 3 need to be taken. Tip 3: Matlab lets you define several standard type matrices with much ease. For instance, A = zeros(4,4); gives a matrix of dimension 4x4 containing all zeros. A = ones(4,1); gives a column vector of ones. A = eye(4,4); is a 4 dimensional identity matrix. Also type help matlab/elmat for more specialized matrices. Tip 4: If a routine has multiple outputs, to get access to them all you need to call it as follows: [M, I] = max(A); For A as follows: >> A A = 2 1 9 3 >> [M,I]=max(A) M = 9 3 I = 2 2 M is the column-wise maximum of A, and I is the row index where the maximum occurs. Type help max for more details. In general, help RoutineName gives details on the location of different outputs of the routine and what they mean. Tip 5: Control and Looping in Matlab. The following commands are available. if (X == 0), X = X + 1; elseif (X == 1), X = X - 1; else X = 5; end; for i = 0:10 A(i) = 2*i + 1; end; i = 0; while( i <= 10); A(i) = 2*i + 1; i = i + 1; end; Type help matlab/lang for more details. Tip for efficiency: Avoid looping too much - vectorize your code as much as possible. For instance, this initialization for vector A could be accomplished using A = 1:2:21;

Aparna Gupta

Jan 23, 2012

MGMT 4440/6440: Financial Simulations

Spring 2012

Tip 6: The diary command saves every thing that appears on the screen in a file, which you need to specify. Type diary MyLog on the Matlab prompt. After that what ever you type and what ever appears on the screen gets saved in the file called MyLog. To stop this saving type diary off. How to create your own routines? You write your routines in files that are called M-files, because they have extension .m. These routines may either be Script files or Function files. Script files contain a group of Matlab commands with no output or input. These commands work on the variables defined within your Matlab session or alternately load variables. Function files have an input and an output. The variables for these are by default local. Global variables have to either be inputted or have to explicitly be defined as global using the global command. A sample function file will contain: function[mean, stdev] = stat(x) % STAT Mean and Standard Deviation % For a vector x, stat(x) returns the % of x. % For a matrix s, stat(x) returns two % respectively, the mean and standard [m, n] = size(x); if (m == 1), m = n; % handle the case of a row end; mean = sum(x)/m; stddev = sqrt(sum(x.^2)/m mean.^2);

mean and standard deviation row vectors containing deviation of each column.

vector

This function will be called just as any other Matlab routine. help MyRoutine also gives the first few lines of comments in your MyRoutine.m file. So, it is a good idea to include some information about what the routine does and how to use it there. [xm, xstd] = stat(x); xm = stat(x); % To get both the outputs % To get just the first.

NOTE : The filename should be the same as the function name, for instance in the above case the file will be called stat.m, this is also case-sensitive. More debugging etc. help ? 1. display strings, error message etc. display using disp or error commands. Input data interactively using input command. 2. keyboard command pauses the execution of the M-file. Resume by typing return. 3. return command in the M-file returns control to the calling program or to the Matlab prompt.

Aparna Gupta

Jan 23, 2012

Potrebbero piacerti anche