Documente Academic
Documente Profesional
Documente Cultură
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, youd 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, youd 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)
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. Youve 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 youre seeing above
is the default shading, a.k.a. faceted flat. Its 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);
Oh, cmon. Thats really cool. I have no idea when youd 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, were overriding the default number of contour
levels and specifying that we want 16 different contour levels):
>>z=peaks(25);
>>contour(z,16);
>>colormap(hsv)
To turn in: a contour plot using the colormap of your choice, saved as a .jpg file (Contour.jpg)
Pseudo ColorPlots
If youd prefer to have a contour plot that is shaded, instead of with distinct edges, youd use a pseudo
color plot. Again, this plot takes as input a matrix. So lets try it:
>> z = peaks(25);
>>pcolor(z);
>>shading interp;
You should get something 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);
>>xslice = [-1.2,.8,2];
>>yslice = 2;
>>zslice = [-2,0];
>>slice(x,y,z,v,xslice,yslice,zslice)
>>colormap colorcube
ThreeD.jpg
Mesh.jpg
Interp.jpg
Contour.jpg
ColorPlot.jpg
Quiver.jpg
Slice.jpg