User:Eml4500.f08.ateam.carr/HW1

HW 1- Matlab Tutorial Sections 4, 8, 12, 16
Matlab Primer, University of Florida "Matlab is an interactive, matrix-based system for scientific and engineering numeric computation and visualization." Below you will find an introductory tutorial to Matlab programming as part of Ateam's submission. Topics covered include:

Section 4 - Statements, expressions, and variables
"MATLAB is an expression language. Expressions you type are interpreted and evaluated." Examples of statement formatting:

$$variable = expression$$ or    $$expression$$

Expressions may consist of functions, operators, and names of variables. The return key executes your typed command. MATLAB is case sensitive and therefore care must be exercised when entering variable names. For example: $$plot(x,Y) \ne plot(X, y)$$

The command "who" or "whois" displays variables currently stored in MATLAB. The command " clear variablename " deletes a specific variable.

The command "eps" prints the machine round off point useful for iterative convergence.

If a run away computation is experienced "CTRL-C" or "CTRL-BREAK" will break the calculation.

Saving the Workspace
When MATLAB is exited, all variables are lost unless previously saved. The command "save" will generate a file entitled "matlab.mat". When re-entering the program, the command "load" will reopen the saved workspace.

Section 8 - Vector functions
Some MATLAB functions perform actions on vectors in a column by column fashion and produce a row vector with the results. If one wants to operate on a row by row basis, the transpose can be used.

max min sort sum prod median mean std any all

For example, to calculate on a row by row basis and find the mean:

mean(A')'

For example, the max value in a matrix B is determined by:

max(max(B))

The command example above is nested within itself. The inner most "max" function produces a single row of the max values for each column of the original matrix. The outer function produces the maximum value of these remaining row vector values.

Section 12 - M-files
M-files are a specific type of file MATLAB is capable of executing. A M-file consists of a series of sequential commands and are characterized by the ".m" file extension. MATLAB M-files fall into two categories: script files and function files.

Script Files
Script files are composed of normal MATLAB commands. To execute the commands inside of a script file, simply type the file name. For example, if the file name is "transpose.m", the command "transpose" will execute all the statements to be performed.

Details of script files:
 * 1) Can reference other M-files or itself
 * 2) Variables are global and will overwrite current session variables
 * 3) Useful to avoid repetitious data entry

To create a .m script file, simply save the current workspace. (fix) For example if the following M-file entitled "test.m" was executed by using the command "test":

B=[1,2,3;4,5,6;7,8,9] A=[9,8,7;6,5,4;3,2,1] C=A+B

The current workspace will now have three variables imported into memory.

Function Files
The second type of M-file is the function file. This tool extends the convenience of built in functions but allows for customization to specific tasks. Note that variables in a function file are local by default. See the following example from the MATLAB Primer document.

function a = randint(m,n) %RANDINT Randomly generated integral matrix. % randint(m,n) returns an m-by-n such matrix with entries % between 0 and 9 a = floor(10*rand(m,n));

Note the following procedures when creating a function file:
 * 1) First line must begin with "function" and define the input variables
 * 2) "%"-Allows for comments
 * 3) Last line defines output variables

As an example to run the above program, "k = randint(4,3)" could be used.

This command passes 4 and 3 to the program and stores the result in a new matrix, k. Note that variable names are not carried over outside of the function M-file.

Section 16 - Output Format
MATLAB calculations are performed in double precision. However, output formatting can be changed with the following commands:

format short      fixed point with 4 decimal places (default) format long       fixed point with 14 decimal places format short e    scientific notation with 4 decimal places format long e     scientific notation with 15 decimal places format rat        approximation by ratio of small integers format hex        hexadecimal format format bank       fixed dollars and cents format +          +, -, blank