User:Eml4500.f08.team.allen.oca/hw1

13. Text strings, error messages, input.

Text strings are entered into MATLAB surrounded by single quotes. The following assigns variable s with a string:

s = ‘This is a test’

The function disp is used to display a text string. Using the above text string as an example,

disp(‘This is a test’) or disp(s)

The function error is used to display a string just like disp, but will also abort the running M-file where it was called from.

A method of taking in data from a user from inside an m-file is the function input as seen in the following example.

iter = input(‘Enter the number of iterations: ‘);

Here the user is prompted by the string asking for a number. After the user enters the number and presses the Return key, the number entered is stored in iter.

14. Managing M-files

On systems that support only one process at a time, to avoid MATLAB exiting while working with a local editor, proceed the system commands normally used to enter a local program or action with !. For example,

!ed rotate.m

will open a local editor to work on rotate.m. After closing the editor, MATLAB will be found to be just as you left it. This is not a concern on most Unix or Windows systems as they support multiple programs running simultaneously.

Many debugging tools are available for M-files. They can be accessed by typing help dtype.

Directory and File commands with strings as operators:

pwd returns the current working directory

cd 'newdirectory' changes directory to that of newdirectory

dir or ls lists all files in working directory

what lists only M-files in working directory

delete 'filename' deletes a file in the working directory

type 'filename' can be used to print an M-file to the screen.

Along with the current working directory, a directory called MATLAB found in the users root directory will also be accessed when the user requests an M-file. The command path returns all file directories MATLAB searches when looking for an M-file. See help path on how directories can be added to the aforementioned list.

15. Comparing efficiency of algorithms: flops, tic and toc.

The number of flops used and time spent processing an algorithm are good indicators of the efficacy of the algorithm.

The command flops(0) resets the current count of flops to zero and should be placed just before the start of the algorithm to be watched. After the algorithm has finished, the command flops returns the number of operations that took place.

The commands tic and toc operate a stopwatch timer which measures the number of seconds between the two command calls. Placing tic before and toc after an algorithm will return how many seconds the algorithm took to complete.

16. Output format.

Although all computations in MATLAB are performed in double precision, the format of the displayed output can be controlled by the following commands.

format short 		fixed point with a 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 + 		+, -, bank

Once invoked, the chosen format remains in effect until changed.

Other commands include: format compact		 suppresses blank lines to display more information format loose		 returns to un-compacted format

17. Hardcopy.

The command diary filename creates a file called filename in the working directory to record all activity in the MATLAB command window. The command diary off stops the recording and actually places the recording into the file.

18. Graphics.

The command plot(x,y) displays an x-y plot in a separate window of the elements x vs the elements y. For example, the following produces a graph of sinusoid:  x = -4:0.01:4; y = sin(x); plot(x,y)

In MATLAB there can be multiple graph windows open, but only one can be current at a time. To place a plot in a new graph window, use the command figure. To create a graph window with a specific number (one that is not already in current use) one can type figure(16) for a figure window of number 16. The command gcf will return the number of the current graph window.

MATLAB supplies a function fplot to easily and efficiently plot the graph of a function. For example, to plot the graph of the function above, one can first define the function in an M-file called say, expnormal.m containing

'''Function y = expnormal(x) y = exp(-x.^2);'''

Then the command  fplot(‘exnormal’, [-1.5,1.5])

will produce the graph.

The following commands can be used to format the graph and take a string as an argument:

title 		graph title

xlabel 		x-axis label

ylabel 		y-axis label

gtext		 place text on the graph using mouse

text 		position text at specified coordinates

For further details on how to use the above commands, type help text at the command prompt.

The command grid will place grid lines on the current graph.

By default the axes are auto-scaled. The axis command can be used to to modify the axes and what follow are some features of the axis command:

axis([xmin, xmax, ymin, ymax])	set axis scaling to prescribed limits

axis (axis)				freezes scaling for subsequent graphs

axis 					auto returns to auto-scaling

v = axis 				returns vector v showing current scaling

axis square 				same scale on both axes

axis equal 				same scale and tic marks on both axes

axis off 				turns off axis scaling and tic marks

axis on				 turns on axis scaling and tic marks

The axis command should be given after the plot command.

One method to display multiple plots on a single graph is to define an independent domain and a number of dependent functions. Then the following command will place the plots in a single graph:

plot(x,y1,x,y2,x,y3)

Another method is to hold the current graphics screen so that subsequent plots are superimposed onto it. The axes may be rescaled, however. Entering hold off releases the “hold.”

To override the default line types on a plot the plot command can be called with additional arguments, for example:

plot(x,y1,’--‘, x, y2, ‘r:’, x, y3, ‘y+’)

Where the first graph will be represented by a dashed line, the second by a red dotted line and the third by a yellow cross symbol at every node. Acceptable line type and color arguments are:

Line types: solid (-), dashed (--), dotted (:), dashdot (-.)

Mark types: point (.), plus (+), star (*), circle (o), x-mart (x)

Colors: yellow (y), magenta (m), cyan (c), red (r), green (g), blue (b), white (w), black (k)

The command subplot is used to partition the screen so that several small plots can be placed in one figure. See help subplot.

Other specialized 2-D functions:

Polar, bar, hist, quiver, compass, feather, rose, stairs, fill