User:Eml4500.f08.RAMROD.A/MyPartForHW1

Managing M Files
Matlab has a neat command to allow users to manage/create new files without the need to exit Matlab or losing active data.

This tool is the ! feature.

! feature
When any command that would require the system to exit is preceded with an "!", Matlab will be returned to automatically with all current data - after whatever file management is completed.

Example: !ed Shamrock.m  will allow the user to edit the m-file named Shamrock and then return to Matlab exactly as it was.

More File Management Features
command pwd returns the present working directory name

command cd is used to change the working directory

commands dir or ls will show all contents of the current working directory

command what shows the M-files in the current directory

command delete can delete a diskfile

command type will print an M-file to the screen (PrtSC)

command path shows the current directories list

Note: M-files need to be in a directory accessible to Matlab.

Efficiency of Matlab Algorithms: Commands: Flops, Tic and Toc
Flops or Floating Point Operations are one way to measure the efficiency of your Matlab algorithm.

The total duration of time is also a good measure of the efficiency.

Flops Command
Enter flops(0) prior to algorithm and flops after and Matlab will count the number of flops registered during the algorithm sequence.

Example: flops(0), x7+ey 2 =x3; flops

Tic and Toc
Similarly, if you precede the Algorithm with Tic and follow it with Toc and Matlab will post the time elapsed during calculations.

Example: tic, x7+ey 2 =x3; toc

Output Format Precision
Matlab computations will always be done in double precision but there are commands to format what is displayed.

Display Commands
format short - fixed point with 4 decimal places

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

Format Commands
format compact - eliminates blank lines compressing the page

format loose - returns to the original expanded format, similar to double space

Note: format commands remain in effect until changed

Hardcopy
A hardcopy of the information on the screen can be copied to a disk file with the command diary

diary filename - writes information to screen until diary off is entered

Graphics and 3D Graphing in Matlab
The most important commands are plot, plot3, mesh, and surf. The Demo command will give a brief into to each.

Plot
creates x-y plots of the linear form. The following examples will show how to plot vectors over an interval for x.

Example: x = -10:2:10; y=sin(x); plot(x,y)

Example: x = -1.5:0.1:1.5; y=exp(x.^3); plot(x,y)

Note: A period precedes the superscript for proper entry-wise operation

Fplot
Fplot works just the same as plot command and is used for plotting functions

Graph Commands
tittle - add a graph tittle

xlabel - label the x axis

ylabel - label the y axis

gtext - place text on the graph at desired location

text - position text by coordinates

Axis Commands
axis - 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

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

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

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

Example: plot(x,y,'b:') will plot a blue dotted line

Plot3
Plot3 will produce a 3-Dimensional plot of vectors: x, y and z'

The vectors will usually be defined parametrically

Example: t = 0.05:0.01:10*pi; x = cos(2t); y = sin(0.5*t); z = t.^4; plot(x,y,z)

Title and axis commands are the same as for command Plot with the z axis included.

Mesh and Surf Commands
These commands will plot 3-Dimensional "mesh" graphs of matrices. Example: z = [3,9,1] mesh(z)

Surf creates surface plots

Advanced Example
Plot the graph of z = e-x 3+y2 over the square [-4,4] x [-4.4]

xx = -4:0.4:4; yy = xx; [x,y] = meshgrid(xx,yy); z = exp(-x.^3+y.^2); mesh(z)

Sparse Matrix Computations
The computing efficiency of large matrix based problems can be increased using the Sparse command.

The Command will force Matlab to only run computation on nonzero matrix elements - significantly increasing efficiency if said matrix is dense with zeroes.

Say we have Matrix A

To find the number of nonzero elements in A use command: nnz(A) to return the number.

Now we can convert it to a sparse matrix with S = sparse(A)

F = Full(S) will bring the matrix back to full

Reference
Sigmon, K. (1993). MATLAB Primer (Third Edition). Available Here Previously Accessed September, 12, 2008.