User:Eml4500.f08.bike.viale/homework report 1

By using Matlab, a user can produce plots of planar curves, 3-D curves, 3-D mesh surfaces, and 3-D faceted surfaces. This section deals with each of these different plots, using the commands 'plot', 'plot3', 'mesh', and 'surf', respectively. Rather than uploading all graphs produced in this section, the reader is encouraged to try the provided Matlab code for themselves to see the plots.

The 2-D Plot
First, the 'plot' command is demonstrated by graphing a sine function over the interval -4 to 4 as follows: This will produce a sine curve.

For another example, the graph y = e- x 2  is produced over the interval -1.5 to 1.5 as follows: The period precedes the ^ in order to perform entry wise operations on the x vector.

The Figure Command
The command 'figure' can be used to produce an empty figure if the user desires. The most recently created figure will be considered the current figure by Matlab, unless the user reselects a different figure. This is done by typing the command 'figure(1)', with the 1 designating the first created figure to be the one made current. The command 'gcf' is used to return the number of the current figure.

The Fplot Command
If an M-file of a function has already been created, the command 'fplot' can be used to quickly plot the graph of that function. As an example, one can create the M-File 'expnormal', with the contents of that function as follows: Once this is saved, then typing: In the workspace will produce the same results as obtained in the 'The Plot Function' section above.

Parametrically Defined Curves
Plots of parametrically defined curves can be made as follows:

Graph Labels
The following is a list of commands which take a string as their argument and the effect the command has on the current plot.

An example is:

>> title('Best Least Squares Fit')

Which will give the title 'Best Least Squares Fit' to the current plot.

To produce grid lines on the graph, the command 'grid' is used'.

Changing Scales on Axes
While Matlab auto-scales the axes, sometimes it is desired to customize the scales. This is done by the following commands:

These commands are only valid after a plot is produced.

Producing Multiple Graphs on One Plot
One way of putting multiple graphs on one plot is as follows:

>> x = 0:.01:2*pi; y1 = sin(x); y2 = sin(2*x); y3 = sin(4*x); plot(x,y1,x,y2,x,y3)

This produces three sine waves on one plot. Another way to produce the same result is:

>> x = 0:.01:2*pi; Y = [sin(x)', sin(2*x)', sin(4*x)']; plot(x,Y)

Also, typing the command 'hold' will freeze the current graph so that another one can be entered on top of it, typing 'hold off' will release the hold.

Line Types and Colors
There are many different line types and colors that can be used in Matlab. They are as follows:

Line types: solid (-), dashed (--), dotted, dashdot(-.) Mark types: point (.), plus (+), star(*), circle(o), x-mark(x)

Colors are chosen as follows:

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

Thus, if a user wants a plot with a red dashed line, the command:

>> plot(x,y,'r--')

Should be used.

Specialized Commands
The 'subplot' command will partition the plot screen into several small plots so that they can be seen all at once in the same figure, but on different plots. Other less used 2-D plotting functions are: 'polar', 'bar', 'quiver', 'compass', 'feather', 'rose', 'stairs', 'fill'.

The 3-D Plot
The command 'plot3' is used to produce 3-D curves in Matlab. It works in basically the same way that the 'plot' command works for 2-D graphs.

A quick example of producing a 3-D plot follows:

>> t = .01:.01:20*pi; x = cos(t); y = sin(t); z = t.^3; plot3(x,y,z)

This produces a helix whose points are all the elements of x,y, and z. The vectors passed into the 'plot3' command must all be the same length to work properly.

3-D Mesh and Surface Plots
3-D wire meshes are produced with the command 'mesh(z)' with z being a 2 dimensional matrix. An example is as follows:

>> mesh(eye(10))

The mesh surface is defined by the elements in the 2 dimensional matrix equaling the z coordinates of the mesh.

Surface plots are similarly created with the command 'surf' as follows:

>> surf(eye(10))

To draw a mesh over a rectangle such as a graph of z = e- x 2 - y 2 over the square [-2,2] x [-2,2], the following procedure is used:

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

All 2-D formatting commands work for 3-D plots, so please reference that section.

Color and Shading
Color shading of 3-D plots is done using the 'shading' command. These are set by typing either:

shading faceted, shading interp, or shading flat

Into the workspace, with a shading of faceted, interpolated, or flat being the result. The user is encouraged to experiment with all three in order to find which fits the graph best.

The color of the surface can be set with the 'colormap' command, with the input argument being one of:

hsv (default), hot, cool, jet, pink, copper, flag, gray, bone

Thus, the command 'colormap(pink)' will result in a pink colormap.

Parametrically Defined Surfaces
The M-File below gives an example of how to plot a parametrically defined 3-D function:

Sparse Matrix Computations
Matlab has many functions that can speed up computation time if they are used when a matrix is sparse, in other words, many of the entries are zero. To demonstrate the matrix:

>> F = floor(10*rand(6)); F = triu(tril(F,1),-1);

is created. Next the statement:

>> S = sparse(F)

is used to convert F into sparse mode. This statement also returns the nonzero entries of F since the output is not suppressed.

>> F = full(S)

restores S to full storage, and the function issparse(F) can be used to check whether a matrix is sparse or full.

However, sparse matrices are usually generated directly rather than through the sparse function. For example, a sparse tridiagonal matrix is created as shown below:

>> m = 6; n = 6; e = ones(n,1); d = -2*e; >> T = spdiags([e,d,e],[-1,0,1],m,n)

T =

(1,1)      -2       (2,1)        1       (1,2)        1       (2,2)       -2       (3,2)        1       (2,3)        1       (3,3)       -2       (4,3)        1       (3,4)        1       (4,4)       -2       (5,4)        1       (4,5)        1       (5,5)       -2       (6,5)        1       (5,6)        1       (6,6)       -2

Sparse Analogs
The sparse analogs of full matrix functions are as follows:

Operations With Sparse and Full Matrices
Since Matlab's default storage is full matrix, if a sparse and full matrix are operated on together (such as in S*F), Matlab will return a full matrix.

If the only matrices involved in the operation are sparse, the resulting matrix will be stored as sparse.