MATLAB Tutorial: Part 2
MATLAB Tutorial: Part 2
14. Managing M-files. The !-feature allows for editing or printing to be done without having to exit MATLAB. Using the command !ed will let the named file be edited and then returned to MATLAB where you left off. Many commands control the working directory. Pwd returns the name of the present working directory. Cd changes the working directory. Dir or ls lists the contents of the working directory, but what lists only the M-files. Files named matlab are accessible from any directory. M-files have to be in a directory MATLAB can excess.
15. Comparing efficiency of an algorithms: flops, tic and toc. The efficiency of an algorithm can be measured by the number of floating point operations (flops) performed and the elapsed time. The total number of flops performed is kept track of by the function flops. This number can be reset by the command flops(0). Tic and toc are stopwatch times that can return the elapsed time. Tic starts the timer and toc returns the elapsed time.
16. Output format. The output on display is controlled by a variety of format commands, but the computations are done in double precision. Format compact suppresses most of the blank lines so more information can be added on the screen, while format loose returns the display back.
17. Hardcopy. The diary command causes the next code written to be written on the name diskfile until the command diary off is given. After this is done the file can be edited and printed on the local system.
18. Graphics. MATLAB can create planar plots of curves, 3-D plots of curves, 3-D mesh surface plots, and 3-D faceted surface plots.
Planar plots. The command plot produces a liner x-y plot. Plot(x,y) will draw an x-y plot if the vectors x and y are the same length. There can be many different graphic figures, but only one at a time will be call “current.” The command figure(1) will make figure 1 the current figure if it was not before, and gcf will return the number of which figure is current. An example of a linear x-y plot is shown below.
- x = -1.5:.01:1.5; y = exp(-x.^2); plot(x,y)
Fplot is used to plot the graph of a function. Plots can be made of parametrically defined curves as well.
- t=0:.001:2*pi; x=cos(3*t); y=sin(2*t); plotx,y)
All graphs can be given titles, labeled axes, and text within the graph.
- Title: graph title
- xlabel: x-axis label
- ylabel: y-axis label
- gtext: place text on graph using mouse
- text: place text at specified coordinates
Axes are auto-scaled, but this can be overwritten with the command axis. This command should always be given after the plot command. Multiple plots can also be displayed on a single graph.
- x = 0: .01:2*pi; y1 = sin(x); y2 = sin(2*x); y3 = sin(4*x); plot(x,y1,x,y2,x,y3)
This can also be done with the hold command. It freezes the current on screen graphics so other plots can be put on the same graph, but the axes may become rescaled. Colors and line types can be added to a plot as well. The code plot(x,y,’r--‘) shows a red dashed line.
Graphics hardcopy The command print is the easiest way to send a high-resolution copy of the current graphics figure to the default printer. The printopt M-file identifies the default setting used by the print command. Print filename saves the figure to the filename given in the default format. If there is no extension in the filename then the default file format will be assigned. A file will be overwritten if the filename already exists. The option –append will append the current graphics figure into the existing file so more than one figure can be saved in each file. The command code is print –append filename. This command can be overwritten with the option –f3.
3-D line plots. Plot3 makes a curve in three dimensions. If x, y, and z are defined vectors of the same size, then plot3(x,y,z) will create a 3-D curve passing through the coordinates of the respective elements. A zlabel can still be added to this plot. The helix shown below was created using the code:
- t = .01: .01:20*pi; x = cos(t); y = sin(t); z = t.^3; plot3(x,y,z)
3-D mesh and surface plots. A three dimensional wire mesh surface plot with the elements of matrix z is created with the command mesh(z). The function meshgrid allows for a graph to be drawn over a rectangle by allowing matrices to be created. Surface plots are created using surf(z).
z=e-x^2-y^2 can be drawn over the square [-2,2]x[-2,2] with the following codes:
- xx = -2: .2:2;
- yy = xx;
- [x,y] = meshgrid(xx,yy);
- z = exp(-x.^2 – y.^2);
- mesh(z)
Surf(z) of the same code is shown as well.
There are three different settings for the color shading of surfaces: faceted, interpolated, and flat. The command shading should be done after the surf command. Different color profiles can be produced by the command colormap.
Handle Graphics. MATLAB contains many different graphic functions. The user is able to control almost all of the graphics allowing for detailed plots to be made.
19. Sparse Matrix Computations. MATLAB provides the ability to save storage space, so larger problems can be solved, by only storing the nonzero elements of the matrix. Matlab has the storage mode full, the default, and sparse. The sparse matrix is stored as a linear array of its nonzero elements. The output of a matrix in sparse mode is a list of nonzero entries and their row and column indices. A matrix is sparse if any constituent block is sparse.