170 likes | 272 Views
More stuff on plotting. Define x and y and call the plot function. Engineers always add …. Title title(‘y = cos(x)’) X axis label, complete with units xlabel(‘x-axis’) Y axis label, complete with units ylabel(‘y-axix’) Often it is useful to add a grid grid on. Single quotes are used.
E N D
Engineers always add … • Title title(‘y = cos(x)’) • X axis label, complete with units xlabel(‘x-axis’) • Y axis label, complete with units ylabel(‘y-axix’) • Often it is useful to add a grid grid on Single quotes are used.
Change the line type in a plot • Example plot(x, y, ':ok') • strings are identified with single quotes • the : means use a dotted line • the o means use a circle to mark each point • the letter k indicates that the graph should be drawn in black • (b indicates blue)
specify the drawing parameters for each line after the ordered pairs that define the line
Subplots • The subplot command allows you to subdivide the graphing window into a grid of m rows and n columns • subplot(m,n,p) rows columns location
subplot(2,2,1) 2 columns 2 1 2 rows 3 4
Other Types of 2-D Plots • Polar Plots • Logarithmic Plots • Bar Graphs • Pie Charts • Histograms • X-Y graphs with 2 y axes • Function Plots
Three Dimensional Plotting • 3D Line plots (i.e. plot3(x,y,z)) • Surface plots (i.e. mesh(z) and surf(z)) • Contour plots
Mesh plots • mesh(z) plots out a 3D plot • The z-axis are the values of ‘z’ • The x-axis is each point corresponding to a column • The y-axis is each point corresponding to a row Example: Z = [1 2 3; 4 5 6]mesh(Z)
Mesh plots continued… • Can also do mesh(x,y,z) this allows the x and y axis to be something else other than the number of rows and columns • x and y need to have the same dimensions as z. • If x and y are vectors, need to use meshgrid to make them a 2D matrix.
meshgrid example Example: Z = [1 2 3; 4 5 6] %2x3 matrixx=[0:3:6] %number of columns is 3 y=[0:6:6] %number of rows is 2 [X,Y] = meshgrid(x,y) %new X and Y that are 2x3 matrices mesh(X,Y,Z) xlabel(‘X’) ylabel(‘Y’) zlabel(‘Z’)
meshgrid example output Z = 1 2 3 4 5 6 x = 0 3 6 y = 0 6 X = 0 3 6 0 3 6 Y = 0 0 0 6 6 6
surf plots • A surf plot is basically the same as mesh plot except that surf plots give a color surface instead of a mesh Ex: surf(X,Y,Z)
In-class assignment • Problems 4.6, 4.7, and 4.9 from the Matlab book