Title: MATLAB GRAPHICS PART II ADVANCED PLOTTING
1MATLAB GRAPHICS - PART IIADVANCED PLOTTING
23-D PLOTTING
- There are numerous ways to display a function in
3-D in black and white as well as color - One way to interpret 3D data is a series of
points in space given by (x,y,z) coordinates - The direct extension of the 2-D plot function,
plot(x,y), to 3-D is plot3(x,y,z)
3PLOTTING A CORKSCREW
- How would you model a corkscrew?
- Corkscrew, or spiral, is the 3-D equivalent of
a spiral - It goes around a circle but it also rises from
the ground plane. So, what is its equation?
43-D EQUATION
- A circle can be parametrically described by
- xcos(t)
- ysin(t)
- To make it rise from the ground plane, let zt
and run t from 0 to 10pi.
5Try it!
6Try it!
- Another interesting plot is the same as corkscrew
but you are going up around a cone rather than a
cylinder. Can you write the code?
7DISPLAYING A FUNCTION AS A SET OF HEIGHTS
- A 3-D plot can be interpreted as heights above
the ground plane. These heights are evaluated at
some predefined grid points
8mesh and meshgrid
- To plot a function in 3D we need to understand
mesh and meshgrid. - meshgrid samples the ground plane into a grid of
points - x- 80.58
- yx
- x,ymeshgrid(x,y)
- x and y are now matrices
- mesh evaluates the function over the grid
9EXMAPLE SOMBRERO
- Sombrero, looking like a Mexican hat, is defined
by sin(r)/r. - r is the distance of a point (x,y) to the origin,
i.e. - r2x2y2
r
10Try it!
11SOMBRERO
12GENERATING TRUE AXIS UNITS
- Use of mesh (z) plots z vs. index positions not
actual x or y values - To plot z vs. actual units of x and y, just use x
and y in the mesh command like - mesh(x,y,z)
- Note that x and y can come from the output of
meshgrid
13Try it!
- MATLAB has a built-in function called peaks
- How can you make the plot look smoother?
14What to do in the following slides
- Each slide shows a variation of the mesh command
on a function z - Since you already have z defined for both
sombrero and peaks, for each slide duplicate the
command shown and see the result for yourself
15CONTOUR PLOT
- Contours are slices of constant height that are
then projected onto the ground plane - In its simplest form meshc (z) does the job
16CURTAIN PLOT
- You can put your plot on a pedestal by using
meshz (Z)
17CONTROLLING VIEWPOINT
- Viewpoint is controlled by two angles azimuth
and elevation - Azimuth is rotation around the Z-axis
- Elevation is rising above the ground plane
z
18DEFAULT VIEWPOINTS
- In MATLAB, default viewpoints are az- 37.5 and
el30 degrees - Zero degrees azimuth is like looking up the
x-axis shown in the previous slide . - 90 degrees of elevation is like looking directly
down on the the surface
19Working with viewpoint
- The best way to understand viewpoint is to play
around - To understand the effect of elevation, fix your
azimuth at 0 then change your elevation - view(0,10),view(0,30),view(0,60)
- Or fix your elevation at 30 degrees and change
your azimuth - Compare view(30,60) with view(-20, 60)
20INTERPRETING SIGNS OF VIEWPOINT ANGLES
- Increasingly negative azimuth angle corresponds
to holding the object in front of you and
rotating it counterclockwise. - Equivalently, it corresponds to keeping the
object stationary and moving around it clockwise - Positive elevation angle mean rising above the
object. Elevation of 90 degrees means being
directly overhead and looking down
21Homework1
- For the following function
- do a mesh plot then title and label all axis
- visually find out how deep the hole is?
- what is happening inside(looking underneath)?
- generate 3D contours(30 of them)
- Write a procedure that would cap the plot to
70 of its peak value then plot it. Your plot
should show a flat top
22GENERATING SHADED PLOTS
- mesh generates wiremesh plots(can see lines)
- To generate surfaces with solid shading, surf and
its variations are used - These variations are
- surf
- surfl (this is surf followed by lower case L)
- surfc
23Using surf
- Usage
- surf(x,y,z,C)
- (x,y) is generated via meshgrid and z is the
height of the function. - z-to-color mapping is done according to the
entries into colormap via C. More on this later - If surf(z) is used, color is proportional to
height z.
24Plotting peaks using surf
- Generate the peaks function in the range (-4 to
4) in increments of .5 - Then use
- surf(x,y,z)
- For comparison, use mesh and display it in a
second window
25Try it!
- Look closely and see if you can tell the
difference between surf and mesh
26mesh vs. surf
- Display mesh and surf side-by-side
27SOLID SHADING- shading
- To plot solid looking shapes, as opposed to
wiremeshes, shading command comes in - shading flat
- shading faceted
- shading interp
28Try it!
- Display one of your favorite 3D shapes you have
done so far and in the command window type and
observe - shading flat
- shading faceted
- shading interp
29FLAT vs. FACETED vs. INTERPOLATED SHADING
- Flat shading assigns constant colors to surface
patches - Faceted shading assigns constant colors but also
shows the wiremeshes - Interpolated shading assigns colors proportional
to height of the function
303D CONTOURS
- contour projects 3D contours of a surface onto
the ground plane - contour3 shows the true 3D contours
- Usages are
- contour3(x,y,z,N)
- This command generates N contours of the function
Z. Default is N10
31Try it!
- Display the peaks function over x and y ranging
over - 5 to 5 in increments of 0.1 - Then do the following
- Display 50 2D contours using contour
- Display 50 3D contours using contour3d
32FEW EXAMPLES
20 contours
33Can you get this?
- Hint contour displays its plots on the z0 plane
34CONTROLLING 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)
- slighting directionaz,el where az and el are
azimuth and elevation angles previously defined
35Lighting example
- Keep changing the s parameter and watch
36WATERFALL PLOTS
- An interesting effect can be generated by just
plotting the rows of the Z matrix using - waterfall(x,y,z)
37Putting several plots on a single page
- subplot(mnp) divides the page into
m(rows)xn(columns) tiles then selects the pth
tile for the current plot
This tile would be Referred to by subplot (235)
38Seeing subplot at work
- Lets say we want to partition the page into
3x26 tiles. Simply type the following in the
command window and see what happens - subplot(232)
- subplot(235)
- subplot(233)
- subplot(234)
- subplot(236)
39Homework 2 placing 4 plots on a page
- Lets say we have 4 plots (choose your own) and
want to arrange them on paper in the following
styles - Across the page in one row
- Vertical in one column
- In a matrix, 2x2 tiles on a page
40Special surfaces cylinder and sphere
- sphere(n) will generate a plot of unit sphere
using (n1)2 points. Another usage is - x,y,zsphere(25)
- surf(x,y,z)
- Similarly, we can generate a cylinder of radius 1
using cylinder.
41Generalized cylinder
- Think of a cylinder with changing cross section
42How to do it?
- Usage
- Cylinder(radius) where radius is the growing
cross sectional radius described by a vector
43Homework 3
- Plot zsin(sqrt(x2y2)). Plot it using
- mesh
- surf, surfl, surfc
- Experiment with shading flat, faceted,
interpolated - Experiment with lighting directions. For good
effect type colormap(copper) after bringing up
the plot.