Surface Plotting
Bijan Mobasseri
Department Of Electrical and Computer Engineering Villanova University
October 29, 2012
1 / 43
Objectives
By the end of this lecture, youll learn the following:
Create surface plots in a variety of shapes. Shading surfaces for better visibility. View point control. Draw 3D geometric fgures such as spehres, cylinders and cones.
2 / 43
A 3D surface is a set of heights above a plane
A 3-D plot can be interpreted as heights above the ground plane. These heights are evaluated at some predened grid points
3 / 43
Preparing the ground plane
The groundplane must rst be represented by a grid This grid in reality consists of two matrices: x and y
4 / 43
Sampling the ground plane: meshgrid
Lets say we want to sample the ground plane by a grid of points; x=-8:0.5:8; y=x; [X,Y]=meshgrid(x,y) X and Y are now matrices, as shown on the next slide.
5 / 43
Examples of meshgrid output
6 / 43
The mesh command
The mesh command produces a plot of the desired surface expressed in z z= sin x2 + y2 x2 + y2
There is a NaN in z at R=0. It may be ignored or replaced.
7 / 43
Another look
8 / 43
Pre-dened MATLAB functions
There are a number of built-in functions in MATLAB that we can just use In the 2D case, we have humps
Example: plot(humps)
In the 3D case, we have peaks
mesh(peaks)
9 / 43
Generating True Axis Units
Use of mesh(Z) plots Z vs. index positions To plot Z vs. actual units of X and Y:
[X,Y]=meshgrid(-3:0.2:3) Z=peaks(X,Y); mesh(X,Y,Z)
10 / 43
Most common mistake in using mesh
In mesh(X,Y,Z), X and Y must come out of meshgrid. This is wrong x=-3:0.1:3 y=x; Z=peaks(x,y) mesh(Z) This is right x=-3:0.1:3 y=x; [X,Y]=meshgrid(x,y) Z=peaks(X,Y) mesh(Z)
11 / 43
Contour Plot
Contours are slices of constant height that are then projected onto the ground plane In its simplest form meshc(Z) does the job
12 / 43
Curtain Plot
You can put your plot on a pedestal by using meshz(Z)
13 / 43
Waterfall Plots
An interesting effect can be generated by just plotting the rows of the Z matrix using
waterfall(X,Y,Z)
14 / 43
Shaded surface plots
mesh generates discrete plots. To generate surfaces with solid shading, surf and its variations are used These variations are
surf surfl surfc
15 / 43
Using surf
Usage:
surf(X,Y,Z,C)
(X,Y) is generated via meshgrid command and Z is the height of the function. Z-to-color mapping is done according to the entries into "colormap" via C (more on that later) If surf(Z) is used, color is proportional to height Z.
16 / 43
Example using surf
17 / 43
Shading control
Shading can be applied to surface objects to enhance viewing. There are three types of shading, 1) at, 2) faceted and 3) interpolated In at shading, surface patches are shaded in constant colors. In interpolated shading, surface patches have a color gradient proportional to the z-value. Faceted shading is the same as at shading except for the black mesh lines.
18 / 43
Flat shading
type shading flat in your code after surf.
19 / 43
Faceted shading
type shading faceted in your code after surf.
20 / 43
Interpolated shading
type shading interp in your code after surf.
21 / 43
2D Contour Example
Here you slice the surface at 10 different heights and project the contours on the ground plane. Each contour represent a xed height.
22 / 43
3D contours
contour3(x,y,z,20)
23 / 43
Filled contours
contourf(x,y,z,10)
24 / 43
Combination 2D Contours and 3D surfaces
surfc(x,y,z,10)
25 / 43
Controlling Viewpoint
Viewpoint is controlled by two angles: azimuth and elevation Azimuth is rotation around the Z-axis Elevation is rising above the ground plane
26 / 43
Default Viewpoints
In MATLAB, default viewpoints are az=-37.5 and el=30 degrees Zero degrees azimuth is like looking up the rst column of the Z matrix. Zero degrees of elevation is like looking "edge on" the Z matrix
27 / 43
Negative and positive azimuths
Increasingly negative azimuth is like holding the object in front of you while rotating it counterclockwise. Look at the next slide for a demonstration
28 / 43
32 / 43
Negative and positive elevation
Positive elevation angle means rising above the object. Elevation of +90 degrees means being directly overhead and looking down Check this out yourself
33 / 43
View command
Controlling view point is done through the view command Display your 3D plot then use view([-35,60])
34 / 43
Recovering view point angles
If you have a 3D-display but dont know what your viewpoint is, use the following: [az,el]=view MATLAB returns azimuth and elevation view points of the current plot
35 / 43
Interactive change of viewpoint
Display your 3D plot then type rotate3d in the command window Now bring the gure window to the fore, click on it, hold down the mouse button and then move it around
36 / 43
Controlling Lighting Direction-surfl
You can shine light on a surface from a desired direction Shading is based on a combination of diffuse, specular and ambient lighting models Usage:
surfl(X,Y,Z,S) S=lighting direction=[az,el]
37 / 43
Multiple Graphs on Same Axes-Subplot
To save space, we can divide the paper into a grid and place a separate plot inside each grid
38 / 43
Subplot command
subplot(mnp) divides the page into m(rows)n(columns) tiles then selects the pth tile for plotting
39 / 43
Special surfaces: cylinder and sphere
sphere(n) will generate a plot of unit sphere using (n + 1)2 points. Another usage is
[x,y,z]=sphere(25) surf(x,y,z)
40 / 43
Cylinder
cylinder(radius,pnts) generates a cylinder of radius using N points along each axis
41 / 43
Generalized cylinder
A generalized cylinder is a solid whose axis is a 3-D space curve and a variable radius cross section.
42 / 43
Creating a generalized cylinder
Cylinder(radius) where radius is the cross sectional radius described by a vector that changes in length.
43 / 43