Upload
elinor-green
View
221
Download
1
Embed Size (px)
Citation preview
1999 BG Mobasseri 1 04/19/23
MATLAB GRAPHICS - PART IIADVANCED PLOTTING
June 4, ‘99
1999 BG Mobasseri 2 04/19/23
3-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)
1999 BG Mobasseri 3 04/19/23
PLOTTING 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?
1999 BG Mobasseri 4 04/19/23
3-D EQUATION
A circle can be parametrically described by
– x=cos(t)
– y=sin(t)
To make it rise from the ground plane, let z=t and run t from 0 to 10*pi.
1999 BG Mobasseri 5 04/19/23
Try it!
1999 BG Mobasseri 6 04/19/23
Try 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?
-30-20
-100
1020
3040
-30
-20
-10
0
10
20
300
5
10
15
20
25
30
35
1999 BG Mobasseri 7 04/19/23
DISPLAYING 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
1999 BG Mobasseri 8 04/19/23
mesh 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=- 8:0.5:8;
– y=x;
– [x,y]=meshgrid(x,y)
x and y are now matrices
mesh evaluates the function over the grid
1999 BG Mobasseri 9 04/19/23
EXMAPLE: 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.
– r2=x2+y2
r
1999 BG Mobasseri 10 04/19/23
Try it!
1999 BG Mobasseri 11 04/19/23
SOMBRERO
0
20
40
60
80
0
20
40
60
80-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
1999 BG Mobasseri 12 04/19/23
GENERATING 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
1999 BG Mobasseri 13 04/19/23
Try it!
MATLAB has a built-in function called peaks
How can you make the plot look smoother?
1999 BG Mobasseri 14 04/19/23
What 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
1999 BG Mobasseri 15 04/19/23
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
0
10
20
30
40
0
10
20
30
40-10
-5
0
5
10
1999 BG Mobasseri 16 04/19/23
CURTAIN PLOT
You can put your plot on a ‘pedestal’ by using meshz (Z)
05
1015
2025
30
0
10
20
30-10
-5
0
5
10
1999 BG Mobasseri 17 04/19/23
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
z
x
yelaz
1999 BG Mobasseri 18 04/19/23
DEFAULT VIEWPOINTS
In MATLAB, default viewpoints are az=- 37.5 and el=30 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
1999 BG Mobasseri 19 04/19/23
Working 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)
1999 BG Mobasseri 20 04/19/23
INTERPRETING 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
1999 BG Mobasseri 21 04/19/23
Homework#1
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
z =sin x2 + y2( )
1999 BG Mobasseri 22 04/19/23
GENERATING 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
1999 BG Mobasseri 23 04/19/23
Using 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.
1999 BG Mobasseri 24 04/19/23
Plotting 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
1999 BG Mobasseri 25 04/19/23
Try it!
Look closely and see if you can tell the difference between surf and mesh
1999 BG Mobasseri 26 04/19/23
mesh vs. surf
Display mesh and surf side-by-side
0
1020
30
4050
0
10
20
30
40
50-8
-6
-4
-2
0
2
4
6
8
10
0
1020
30
4050
0
10
20
30
40
50-8
-6
-4
-2
0
2
4
6
8
10
1999 BG Mobasseri 27 04/19/23
SOLID SHADING- shading
To plot solid looking shapes, as opposed to wiremeshes, shading command comes in
– shading flat
– shading faceted
– shading interp
05
1015
2025
05
1015
20250.3
0.4
0.5
0.6
0.7
0.8
0.9
1
1999 BG Mobasseri 28 04/19/23
Try 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
1999 BG Mobasseri 29 04/19/23
FLAT 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
1999 BG Mobasseri 30 04/19/23
3D 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 N=10
1999 BG Mobasseri 31 04/19/23
Try 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
1999 BG Mobasseri 32 04/19/23
FEW EXAMPLES
-1-0.5
00.5
1
-1
-0.5
0
0.5
10.3
0.4
0.5
0.6
0.7
0.8
0.9
1
20 contours
1999 BG Mobasseri 33 04/19/23
Can you get this?
Hint: contour displays its plots on the z=0 plane
-5
0
5
-5
0
50
2
4
6
8
10
12
14
16
18
20
1999 BG Mobasseri 34 04/19/23
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] where az and el are azimuth and elevation angles previously defined
1999 BG Mobasseri 35 04/19/23
Lighting example
Keep changing the s parameter and watch
1999 BG Mobasseri 36 04/19/23
WATERFALL PLOTS
An interesting effect can be generated by just plotting the rows of the Z matrix using
– waterfall(x,y,z)
-3-2
-10
12
3
-4
-2
0
2
4-10
-5
0
5
10
1999 BG Mobasseri 37 04/19/23
Putting 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)
1999 BG Mobasseri 38 04/19/23
Seeing subplot at work
Let’s say we want to partition the page into 3x2=6 tiles. Simply type the following in the command window and see what happens
– subplot(232)
– subplot(235)
– subplot(233)
– subplot(234)
– subplot(236)
1999 BG Mobasseri 39 04/19/23
Homework #2: placing 4 plots on a page
Let’s 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
1999 BG Mobasseri 40 04/19/23
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)
Similarly, we can generate a cylinder of radius 1 using cylinder.
1999 BG Mobasseri 41 04/19/23
Generalized cylinder
Think of a cylinder with changing cross section
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
10
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
1999 BG Mobasseri 42 04/19/23
How to do it?
Usage:
– Cylinder(radius) where radius is the growing cross sectional radius described by a vector
1999 BG Mobasseri 43 04/19/23
Homework #3
Plot z=sin(sqrt(x^2+y^2)). 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.