You are on page 1of 8

MATLAB Lecture 2

1. Introduction Today we create 2-dimensional and 3-dimensional plots. Once again, you should create a new directory for todays exercises.

2. 2-D Graphics 2.1. The Plot Command. We have already seen the plot command, which is the most common command for plotting 2-dimensional data. This command plots sets of data arrays, connecting the points with straight lines. Example 2.1. x=linspace(0,2*pi,30); y=sin(x); plot(x,y)

This example creates 30 data points 0 x 2 which are used as the horizontal axis of the plot. Next we create a vector y containing the sine of the data points in x. The plot command opens a graphics window, scales the axes to t the data, and then connect adjacent ponts with a straight line. It also adds a numerical scale and tick marks to both axes automatically. We can easily plot two data sets at once: z=cos(x); plot(x,y,x,z)
1

This example draws both sin(x) and cos(x) versus x on the same plot. Note that plot automatically draws the second curve using a dierent color. Additional curves may be plotted by supplying additional pairs of arguments to plot. If one of the arguments is a matrix and the other is a vector, then the plot command plots each row of the matrix versus the vector.

W=[y;z] % note that you must use ; to make this a matrix plot(x,W) title(plot(x,W))

2.2. Linestyles, Markers and Colors. By default, MATLAB uses solid lines. You can specify the color and style of your plot by supplying an additional argument after each pair of data arrays. This optional argument is a string of up to 3 characters.

Symbol Color y yellow m magenta c cyan r red g green b blue w white k black

Symbol . o x + * : -. --

Linestyle point circle x-mark plus star solid line dotted line dash-dot line dashed line

Use of the point, circle, x-mark, plus and star symbols places the chosen symbol at each point, but does not connect the data points with a curve. plot(x,y,mx,x,z,b-.);

However, you can replot the same data set with multiple styles if you want to combine these symbols with a curve. plot(x,y,mx,x,y,r:,x,z,b-.);

2.3. Other Options. Here is a list of other options for your plot: The grid on command adds grid lines to the plot, and grid off removes them.

The horizontal and vertical axes can be labeled via the xlabel and ylabel commands. You can add a title with the title command. You can add a label or other text by specifying the location on your plot for the label using the text command. grid on xlabel(x axis) ylabel(y axis) title(Sine and Cosine) text(2.5,0.7,sin(x))

2.4. Other 2-D Plots. 2.4.1. Plotting a Function. You can automatically plot a function of one variable between specied limits without creating a data set. Instead, you can use the fplot command. Note that you must put the function in a string (delimited by single quotes), and that it must be a function of x. fplot(sin(x)^2/x,[-10,10] title(Plot of sin(x)^2/x)

2.4.2. Polar Plot.

Polar plots can be created using the polar command. t=0:.02:2*pi; r=sin(2*t) .* cos(2*t); polar(t,r) title(Polar plot of sin(2t)cos(2t))

2.4.3. Bar Plots. Bar plots are created using the bar command. x=linspace(0,pi,30); y=sin(x); bar(x,y) title(Bar Chart of sin(x))

2.4.4. Histographs. Histograms are created using the hist command hist(y) draws a 10-bin histogram for the data in vector y hist(y,n) draws a histogram for the data in vector y using n bins. hist(y,x) where x is a vector draws a histogram using the bins specied in x

a=-4:.25:4; b=randn(5000,1); hist(b,a) title(Histograph of Gaussian Data)

3. 3-D Graphics 3.1. Line Plots. The plot3 command plots a one dimensional data set in a three dimensional plot. The format is the same as the 2-D plot, except the data is in triples rather than pairs. Note that there is also a zlabel command. t=linspace(0,10*pi,500); plot3(sin(t),cos(t),t) title(Helix) xlabel(sin(t)) ylabel(cos(t)) zlabel(t)

3.2. Mesh Plots. MATLAB denes a mesh surface by the z -coordinates of points above a rectangular grid in the x-y plane. It forms a plot by joining adjacent points with straight lines. First, we must dene the mesh grid in the x-y plane on which the values of z will be calculated. If we have a vector x and a vector y, we must take all possible

combinations of one element from each vector. MATLAB provides a convenient way to create such a data set via the meshgrid function. [X,Y]=meshgrid(x,y) creates a matrix X whose rows are copies of the vector x and a matrix Y whose columns are copies of the vector y. This pair of matrices can be used to evaluate a function of two variables over the desired area. We dene a matrix Z as a function of the two matrices X and Y. Finally, we can plot the result using mesh(X,Y,Z). x=0:.5:20; y=0:.5:40; [X,Y]=meshgrid(x,y); Z=sin(X) .* (Y-20).^2; mesh(X,Y,Z)

There are two alternate forms of the mesh plot. meshc plots the mesh and adds a contour plot beneath it. meshz plots the mesh and adds a curtain plot beneath it. Try them out on the previous example.

3.3. Surface Plots. A surface plot of the same matrix Z looks like the mesh plot, except that the spaces between the lines are lled in. surf(X,Y,Z)

There are two alternate forms of the surface plot. surfc plots the surface and adds a contour plot beneath it. surfl plots the surface and adds surface highlights from a light source. 3.4. Manipulating Your View. You can specify the angle from which you view the 3-D plot. The command view([x y z]) places your view on a vector containing the Cartesian coordinate (x,y,z) in 3-D space. view([1 2 5])

Finally, you can alter the mesh view so that the hidden lines are not removed. mesh(X,Y,Z) hidden off

You might also like