To turn in: a mesh plot using mesh, saved as a .jpg file (Mesh.jpg). Surface Plots: Surface plots are mesh plots colored in. You've already done one surface plot ...
Matlab Plotting Part 2: 3-D Plotting Due Sunday, May 12 at midnight. Matlab offers some neat 3-dimensional plotting functions such as plot3, mesh, surf, etc. We’ll start with a basic 3-D line plot. This is essentially the same as the 2-D line plot, only you’ll need a 3rd vector (array). Try the following: >> z = 1:0.01:1000; >> x = sin(z); >> y = cos(z); >> plot3(x,y,z) You should get something like this:
You can use plot3 to plot any 3 vectors in this way. You can add titles, labels, etc. in the same way that you added them to the 2-D plot. You can label the z axis using the labeling zlabel. Sidenote: if you want to create a vector of x equally spaced numbers between y and z, you can use the linspace function. The default is to create 100 numbers. So, for instance, to create a vector of 100 evenly spaced numbers between 0 and 200, you’d use the following command: >>v = linspace(0,200) This would give you a vector similar to: >>v = 0:2:200 The difference here is you get t ospecivy the number of points you want in the vector. To override the default, you’d use the 3rd parameter: >>v = linspace(0,210,30) This will give you 30 equally spaced points between 0 and 210. You can use linspace if you know the number of points you want, but not necessarily the increment value. To turn in: a 3-d plot using plot3, saved as a .jpg file (ThreeD.jpg)
If you want to see the animated version of the plot, try: >>>comet3(x,y,z) with the figure window open and watch the plot happen (it’s slow!) Mesh and Surface Plot: These plots allow you to plot the data as a surface. You can use the mesh plot to plot a 2-dimensional matrix. If you do this, the x and y values are the dimensions of the matrix, and the z value is the value at x,y in the matrix. So, for example, if I made the following matrix: >> z = [1:10;2:2:20;3:12] I just created a matrix with 3 rows (separated by ;) and 10 columns, 1-10 in the first row, 2-20 by 2s in the second row, and 3-12 in the third row. Now if I do: >>mesh(z) the x axis is 3, the y axis is 10, and z is the value at each point, or the surface being plotted. The resulting plot will look like this:
The graph is created by connecting the points defined in z ( the matrix) into a rectilinear grid. Notice that x goes from 1 – 3 (the number of rows) and y goes from 1 – 10 (the number of columns in the matrix). To turn in: a mesh plot using mesh, saved as a .jpg file (Mesh.jpg) Surface Plots: Surface plots are mesh plots colored in. You’ve already done one surface plot at the beginning of the plotting lab (part 1). You can do a surface plot of the mesh plot you just did to see the difference: >>surf(z) Again, the x and y coordinates are the number of rows and columns, respectively, and the z values in the matrix are those plotted as the surface plot. You should get something like this:
You can control the shading of the surface plot using the shading command. What you’re seeing above is the default shading, a.k.a. faceted flat. It’s not very interesting. You can get cooler surface maps using shading interp. Try the following: >>z=peaks(25); >>surfl(z); You should get something like this:
Then try: >>shading interp; >>colormap(colorcube);
You should get something like:
Oh, c’mon. That’s really cool. I have no idea when you’d need to use the colorcube for shading of a surface map, but it looks really neat. Other colormap options are: autumn bone hot spring colorcube hsv summer cool pink winter copper prism jet(default) flag white To turn in: a surface plot with interpolated shading, using the colormap of your choice, saved as a .jpg (Interp.jpg) Contour Plot You can also make a plot, which is a 2-dimensional representation of a 3-dimensional surface. It takes a matrix, and then chooses the number of contour levels automatically based on the values in the matrix. So if we take the surface map from the previous example and flatten it, then plot it, that would be a contour plot. To do it, do the following (in this example, we’re overriding the default number of contour levels and specifying that we want 16 different contour levels): >>z=peaks(25); >>contour(z,16); >>colormap(hsv)
You should get something that looks like this:
To turn in: a contour plot using the colormap of your choice, saved as a .jpg file (Contour.jpg) Pseudo ColorPlots If you’d prefer to have a contour plot that is shaded, instead of with distinct edges, you’d use a pseudo color plot. Again, this plot takes as input a matrix. So let’s try it: >> z = peaks(25); >>pcolor(z); >>shading interp; You should get something like this:
To turn in: a pseudocolor plot, saved as a .jpg file (ColorPlot.jpg) Quiver or Velocity Plots A quiver plot displays velocity vectors as arrows with components (u,v) at the points (x,y). For example, the first vector is defined by components u(1),v(1) and is displayed at the point x(1),y(1). So, a simple example would be: >> x = [1 12]; >> y = [1 2]; >> u = [3 4]; >> v = [4 6]; >> quiver(x,y,u,v); You should get a plot that looks like this:
Here we see that the first vector is represented as an arrow, starting at coordinates 1, 1, and then travels in the direction of over 3 and up 4. The second vector starts at coordinates 12, 2 and travels in the direction of over 4 and up 6. For quiver to work, the matrices x, y, u, and v must all be the same size and contain corresponding position and velocity components. By default, the arrows are scaled to just not overlap, but you can scale them to be longer or shorter if you want.
quiver(...,scale) automatically scales the arrows to fit within the grid and then stretches them by the factor scale. scale = 2 doubles their relative length, and scale = 0.5 halves the length. Use scale = 0 to plot the velocity vectors without automatic scaling. Try the following (from Mathworks): >>x = -2:.2:2; >>y = -1:.2:1; >>[xx,yy] = meshgrid(x,y); >>zz = xx.*exp(-xx.^2-yy.^2); >>[px,py] = gradient(zz,.2,.2); >>quiver(x,y,px,py,2);
You should get a plot that looks like this:
To turn in: a quiver plot, saved as a .jpg file (Quiver.jpg) Slice plotting: “slice displays orthogonal slice planes through volumetric data. slice(V,sx,sy,sz) draws slices along the x, y, z directions in the volume V at the points in the vectors sx, sy, and sz. V is an m-by-n-by-p volume array containing data values at the default location X = 1:n, Y = 1:m, Z = 1:p. Each element in the vectors sx, sy, and sz defines a slice plane in the x-, y-, or z-axis direction.” (Mathworks). In the following command: slice(X,Y,Z,V,sx,sy,sz)
X, Y, and Z are 3 dimensional arrays specifying the coordinates for V X, Y, and Z must be monotonic and orthogonally spaced (this is what meshgrid produces for us). So try the following: >>[x,y,z] = meshgrid(-2:.2:2,-2:.25:2,-2:.16:2); >>v = x.*exp(-x.^2-y.^2-z.^2);
>>xslice = [-1.2,.8,2]; >>yslice = 2; >>zslice = [-2,0]; >>slice(x,y,z,v,xslice,yslice,zslice) >>colormap colorcube
You should get something like this:
To turn in: a slice plot, saved as a .jpg file (Slice.jpg) End of 3-D Plotting To Turn in: 1. 2. 3. 4. 5. 6. 7.
ThreeD.jpg Mesh.jpg Interp.jpg Contour.jpg ColorPlot.jpg Quiver.jpg Slice.jpg
If you finish early, work on your project.