User:Eml4500.f08.bottle.ranto/HW 1 My section

M-Files
M-files are sections of MATLAB code that are saved to a disk for later use. M-files have the file extension “.m” at the end of their filename. M-files can be created using the built in editor or a word processor such as Notepad.

M-files are broken up into two different categories, script files and function files.

Script Files Script files consist of a series of statements. When a script file is called within MATLAB the commands within that script file are executed, changing the values of any variables called in the sequence. A script file can execute other M-files, including itself, within its code. Function Files Function files can be used to extend the capability of MATLAB. User created functions can be added by saving the file to the MATLAB directory. The following is an example of a simple function file.

function r=mod(a,d) % r=mod(a,d). If a and d are integers, then % r is the integer remainder of a after % division by d. If a and b are integer matrices, % then r is the matrix of remainders after division % by corresponding entries. Compare with REM.

r=a-d.*floor(a./d);

The MATLAB statement r = mod(20,3) would call this function, assuming it has been saved to a diskfile, and assign 20 and 3 to the variables a and d, respectively, in the function. Variables in a function are by default local variables, so the previous statement would not change the values of variables a or d in the global MATLAB interface, only in the function itself. Functions can have multiple outputs, as in the following example:

function [mean, stdev] = stat(x) % STAT Mean and standard deviation %     For a vector x, stat(x) returns the %     mean and standard deviation of  x.     %      For a matrix x, stat(x) returns two row vectors containing, %     respectively, the mean and standard deviation of each column. [m n] = size(x); if m == 1 m = n;    % handle case of a row vector end mean = sum(x)/m; stdev = sqrt(sum(x.^ 2)/m - mean.^2);

With this function file saved, the statement [mn, dev] = stat(q) would call the stat function and assign the standard deviation of vector q to the global variable dev, and the mean of vector q to the global variable mn. MATLAB function are either built in or saved as M-files. To discover the type of file, built in or user-created, simply command type functionname.

Text Strings
To enter a text string into MATLAB, simply surround the string with single quotes. An example of this is:

string1 = ‘this is a text string’

This statement assigns the text “this is a text string” to the variable string1. To display a given text string the MATLAB function disp is used. For example:

disp (‘this string will be shown’)

The MATLAB function error is used to display a text string that explains to the user that an error has been committed and aborts the M-file.

To prompt the user to enter data, the function input is used. For example the statement

iter = input('Enter average test score: ')

would cause MATLAB to display the text asking for a number until the user has entered in data.

Managing M-files
MATLAB makes it possible to easily edit or add M-files while remaining in the MATLAB user interface. The symbol ! is used to denote a command concerning directory files. To edit any existing M-file, use the command !ed. For example, the statement

!ed stat.m

would open the MATLAB file editor with the code for the function stat. In many cases, it is preferable to run the editor and MATLAB running simultaneously, with the application not in use suspended in the background.

To see the files saved in your set MATLAB directory, the command dir is used to list all files saved to that folder. The command what displays only the M-files saved in that folder. To change the working directory, use the command chdir. To delete an M-file, use the command delete. As noted all these commands should be preceded by a ! to avoid system confusion.

Comparing Efficiency of Algorithms
To determine the efficiency of an algorithm within MATLAB, two measures can be employed. The function flops keeps track of the number of floating point operations performed in an algorithm. The command flops(0) resets this count to 0. The other measure is the total elapsed time to execute a task. The function etime(t2,t1) gives the time elapsed between two times. The function clock gives an extremely accurate gauge of the current time. To measure the time to perform the function stat the following command could be used.

t = clock; [mn, dev] = stat(q) time = etime(clock,t)

The measure of flops is considered much more accurate as the time required to complete a task depends greatly on the number of processes currently running on the system.