textbook: chapter 11 part : 3d-plotting cse 1010
TRANSCRIPT
Textbook: Chapter 11 Part : 3D-Plotting
CSE 1010
Demos … before we start … 1/3
3D-Plot and What Matlab Can Do …
>> teapotdemo
Demos … before we start … 2/3The Earth …
load topo.mat
[x,y,z] = sphere(50);
cla reset
axis square off
props.AmbientStrength = 0.1;
props.DiffuseStrength = 1;
props.SpecularColorReflectance = .5;
props.SpecularExponent = 20;
props.SpecularStrength = 1;
props.FaceColor= 'texture';
props.EdgeColor = 'none';
props.FaceLighting = 'phong';
props.Cdata = topo;
surface(x,y,z,props);
light('position',[-1 0 1]);
light('position',[-1.5 0.5 -0.5], 'color', [.6 .2 .2]);
view(3)
Demos … before we start … 3/3Matlab Logo …
L = 40*membrane(1,25);
logoFig = figure('Color',[0 0 0]);
logoax = axes('CameraPosition', [-193.4013 -265.1546 220.4819],...
'CameraTarget',[26 26 10], ...
'CameraUpVector',[0 0 1], ...
'CameraViewAngle',9.5, ...
'DataAspectRatio', [1 1 .9],...
'Position',[0 0 1 1], ...
'Visible','off', ...
'XLim',[1 51], ...
'YLim',[1 51], ...
'ZLim',[-13 40], ...
'parent',logoFig);
s = surface(L, ...
'EdgeColor','none', ...
'FaceColor',[0.9 0.2 0.2], ...
'FaceLighting','phong', ...
'AmbientStrength',0.3, ...
'DiffuseStrength',0.6, ...
'Clipping','off',...BackFaceLighting','lit', ...'SpecularStrength',1.1, ...'SpecularColorReflectance',1, ...'SpecularExponent',7, ...'Tag','TheMathWorksLogo', ...'parent',logoax);l1 = light('Position',[40 100 20], ...'Style','local', ...'Color',[0 0.8 0.8], ...'parent',logoax);l2 = light('Position',[.5 -1 .4], ...'Color',[0.8 0.8 0], ...'parent',logoax);
Demos … before we start … 3/3
Contents
Part 1 - 2D Plotting = 3D Plotting
Part 2 - Linear 3D Plotting
- Direct Linear Plotting
- Parametric Linear Plotting
Part 3 - 3D Mesh Plotting
Part 4 - 3D Surface Plotting
3D – Plotting
Part 1
When 2D Potting = 3D-Plotting
Linear Plotting
3D – Plotting – Part 1 When 2D Plotting = 3D Plotting
1 – Create a 2D Plot
2 – Click icon “Rotate 3D”
3 – Woow!
We can extend 2-plots by adding a set of z values
Syntax: Plot3 ( x, y, z, str)
str is an optional parameter,
It specifies color/line style
The default is “solid blue”
… Examples
Example
Plot3 ( x, y, z, str)str is an optional parameter,
specifies color/line styledefault solid blue
… Examples
3D-PLotting
Part 2
Linear 3D Plotting
3D – Plotting – Part 2Linear 3D Plotting
Linear 3-D Plotting looks like …
How do we do that …?
Linear 3D Plottingx = 0 : 0.1 : 3.* pi; % each plot has the same set of x values y1 = zeros(size(x)); % The y values for the first plot are all 0 z1 = sin(x); %
z2 = sin(2.*x); % The 2nd and 3rd plots are sin(x) at different freq.z3 = sin(3.*x); %
y3 = ones(size(x)); % The y values of the 2nd and 3rd plots y2 = y3./2; % are all 0.5 and 1 respectively
plot3(x,y1,z1, 'r', x,y2,z2, 'b', x, y3, z3, 'g')
grid on % activates the grid
xlabel('x-axis')ylabel('y-axis')zlabel('z-axis')
NOTE: .* and ./ :
3D-PLooting
Part 3
Parametric Plotting
3D – Plotting – Part 3Linear Parametric 3D Plot
3-D parametric plots allow the variables on each axis to be dependent on a separate, independent variable that defines a path in the plotting space
Two examples - The Spiral - 3D Motion of a particle
3D – Plotting – Part 3: Linear Parametric 3DExample 1 – The Spiral
The spiral as an example of a 3-D plot where two of the dimensions, x and y, are dependent on the third, independent parameter.
The independent parameter is the rotation angle, θ, varying from 0 to 10 π (five complete revolutions).
The x and y values are mapped as sin(θ) and cos(θ)—the classic means of describing a circle.
The spiral effect is accomplished by plotting θ on the z-axis.
theta = 0 : 0.1 : 10.*pi; % theta = θplot3(sin(theta),cos(theta),theta)title( 'parametric curve based on angle');grid on
Example 1 = The Spiral
This is a fully parametric plot:
- The values of ALL three coordinates are mappings of an independent parameter, t.
- This is a plot of the 3D motion of a particle receiving random impulses in all three axes.
Note the use of text anchored in x-y-z space to label points on the graph.
Script …
Example 2: 3D Motion of a Particle
N = 20;dvx = rand(1, N) - 0.5 % random v changesdvy = rand(1, N) - 0.5dvz = rand(1, N) - 0.5
vx = cumsum(dvx); % integrate to get vvy = cumsum(dvy);vz = cumsum(dvz);
x = cumsum(vx); % integrate to get posy = cumsum(vy);z = cumsum(vz);
plot3(x,y,z)grid ontitle(‘All 3 axes varying with parameter t')text(0,0,0, ‘Start');text(x(N),y(N),z(N), ‘End');
Part 4
Other Plot Capabilities
bar3(x, y)
bar3h(x, y)
stem3(x, y ,z)
scatter3(x, y, z)
pie3(y)
Other 3D Plot Capabilitie – Part 4bar3(x, y)
x = [ ]y = [ ]
for k = -5:5 T = 1 + k + (k * 2) + (k * 3) x = [x k] % stores values for x-axis y = [y T] % stores values for y-axisend
bar3(x, y)
xlabel('x-axis')ylabel('y-axis')zlabel('z-axis')
Other 3D Plot Capabilitie – Part 4bar3h(x, y)
x = [ ]y = [ ]
for k = -5:5 T = 1 + k + (k * 2) + (k * 3) x = [x k] % stores values for x-axis y = [y T] % stores values for y-axisend
bar3h(x, y) % Does not work well!!!
xlabel('x-axis')ylabel('y-axis')zlabel('z-axis')
Other 3D Plot Capabilitie – Part 4stem3(x, y,z,’fill’)
t = 0:0.2:10x = ty = sin(t)z = t .* (1.5) stem3(x,y,z,'fill')grid on xlabel('x-axis')ylabel('y-axis')zlabel('z-axis')
Other 3D Plot CapabilitiePart 4
scatter3(x, y,z,’filled’)[x,y,z] = sphere(16);X = [x(:)*.5 x(:)*.75 x(:)];Y = [y(:)*.5 y(:)*.75 y(:)];Z = [z(:)*.5 z(:)*.75 z(:)];S = repmat([1 .75 .5]*10,prod(size(x)),1);C = repmat([1 2 3],prod(size(x)),1);scatter3(X(:),Y(:),Z(:),S(:),C(:),'filled‘);view(-60,60) % view is explained later
% sphere: built-in function% repmat: replicate a matrix% prod(x): product of all the% items in x% sphere See next slide
sphere and peaks
sphere and peaks are 2 built-in Matlab functions
Their main use is to test the visualization (graphics) effects
sphere(n)The sphere function generates the x-, y-, and z-coordinates of a unit sphere for use with surf and mesh.
sphere generates a sphere consisting of 20-by-20 faces.sphere(n) draws a surf plot of an n-by-n sphere in the current figure. [X,Y,Z] = sphere(n) returns the coordinates of a sphere in three matrices
that are (n+1)-by-(n+1) in size.
You draw the sphere with surf(X,Y,Z) or mesh(X,Y,Z).
% Matlab script % From the Matlab website
% sphere(16) optional parm sphere axis equal
Peaks in brief Details
peaks is a function of two variables, obtained by translating and scaling Gaussian distributions, which is useful for demonstrating mesh, surf, pcolor, contour, etc.
Example of use:
Z = peaks; % returns a 49-by-49 matrix
% Matlab script
% From the Matlab website
[xx,yy,zz] = peaks(120);
surf(xx,yy,zz)
colormap 'default'
shading faceted
axis tight
title('peaks')
xlabel('x'),ylabel('y'),zlabel('z')
Matlab built-in function: peaks
Function Syntax
Z = peaks;
Z = peaks(n);
Z = peaks(V);
Z = peaks(X,Y);
peaks;
peaks(N);
peaks(V);
peaks(X,Y);
[X,Y,Z] = peaks;
[X,Y,Z] = peaks(n);
[X,Y,Z] = peaks(V);
Description
peaks is a function of two variables, obtained by translating and scaling Gaussian distributions, which is useful for demonstrating mesh, surf, pcolor, contour, etc.
Z = peaks; returns a 49-by-49 matrix.Z = peaks(n); returns an n-by-n matrix.Z = peaks(V); returns an n-by-n matrix, where n = length(V).Z = peaks(X,Y); evaluates peaks at the given X and Y (which must be the same size) and returns a matrix the same size.peaks(...) (with no output argument) plots the peaks function with surf.
> > help peaks … or go to:http://www.mathworks.com/access/helpdesk/help/techdoc/ref/peaks.html
Other 3D Plot Capabilitie – Part 4pie3(y)
y = [ 23 56 87 12 9 ]
pie3(y)
Surface Plots
Matlab Rendering Capabilities
3D-Plot: Surface Plots
4 Basic concepts:1 – plaid2 – meshgrid3 – mesh4 - surf
Plaid
The “plaid” is a Matlab concept, NOT a Matlab functionThe underlying surface is referred to as plaid because of its
conceptual similarity to a Scottish tartan pattem.To design such a pattern, one needs only to specify the color
sequence of the horizontal and vertical threads.In the same way, we specify a plaid by defining vectors of the row
and column data configurations.The simplest surface plots are obtained by defining a z value for
each point on an x-y plaid.
meshgrid(x,y)
meshgrid(x,y)
… accepts the x1xm and y1xm vectors that bound the edges of the plaid, and replicates the rows and columns appropriately to produce xxnxm and yy1xm, containing the x and y values (respectively) of the complete plaid
This enables us in general to compute mappings for the 3-D coordinates of the figure we want to plot
mesh(xx,yy)
mesh(xx,yy) plots the surface as white facets outlined by colored lines
The line coloring uses one of many color maps (listed in Appendix A of the textbook), where the color is selected in proportion to the parameter
You can turn the white facets transparent with the command “hidden off”.
surf(xx,yy,zz)
surf (xx, yy, zz)
plots the surface as colored facets outlined by black lines
The line coloring by default is selected in proportion to the zz parameter
The lines can be removed by using one of a number of shading commands listed in Appendix A of the textbook
Also on the next slide
Shading Commands
shading < value >: shades a surface plot with one color per grid section
SHADING controls the color shading of SURFACE and PATCH objects.
SURFACE and PATCH objects are created by the functions SURF, MESH, PCOLOR, FILL, and FILL3
shading flat sets the shading of the current graph to flat. shading interp sets the shading to interpolated. shading faceted sets the shading to faceted, which is the default.
Read: > > >> help shading
More on Mesh and Surface PlotsCommon Rendering tools Available in Matlab
Mesh mesh(x,y,z)
Mesh Curtain meshz(x,y,z)
Mesh Contour meshc(x,y,z)
Waterfal waterfall(x,y,z)
3D Contour Plot contour3(x,y,z,n)
2D Contour Plot contour(x,y,z,n)
Surf surf(x,y,z)
Surface and contour surfc(x,y,z)
Surface and lighting surfl(x,y,z)
Details and Examples
More on Mesh and Surface PlotsCommon Rendering tools Available in
Matlab
The MESH Series
In the following slides we use the same script to plot:
x = -3:0.25:3;y = -3:0.25:3; [X, Y] = meshgrid(x,y) % build the plaid Z = (1.8) .^ (-1.4) * sqrt((X .^2) + (Y .^2)) .* cos (0.5 * Y ) .* sin(X);
The only line of code that is different is the Matlab function used to plot our [X,Y] values in the red box
Mesh mesh(x,y,z)x = -3:0.25:3;
y = -3:0.25:3;
[X, Y] = meshgrid(x,y) % build the plaid
Z = (1.8) .^(-1.4) * sqrt((X.^2) + (Y .^2)) .* cos (0.5 * Y) .* sin(X);
mesh(X,Y,Z)
xlabel('x-coordinate');
ylabel('y-coordinate');
zlabel('z-coordinate');
Mesh Curtain meshz(x,y,z)x = -3:0.25:3;y = -3:0.25:3;
[X, Y] = meshgrid(x,y) % build the plaid
Z = (1.8) .^(-1.4) * sqrt((X.^2) + (Y .^2)) .* cos (0.5 * Y) .* sin(X);
meshz(X,Y,Z)
xlabel('x-coordinate');ylabel('y-coordinate');zlabel('z-coordinate');
%Draws a curtain around%the mesh
Mesh Contour meshc(x,y,z)x = -3:0.25:3;y = -3:0.25:3;
[X, Y] = meshgrid(x,y) % build the plaid
Z = (1.8) .^(-1.4) * sqrt((X.^2) + (Y .^2)) .* cos (0.5 * Y) .* sin(X);
meshc(X,Y,Z)
xlabel('x-coordinate');ylabel('y-coordinate');zlabel('z-coordinate');
% Draws a contour plot% beneath the mesh
Mesh Waterfall waterfall(x,y,z)x = -3:0.25:3;
y = -3:0.25:3;
[X, Y] = meshgrid(x,y) % build the plaid
Z = (1.8) .^(-1.4) * sqrt((X.^2) + (Y .^2)) .* cos (0.5 * Y) .* sin(X);
waterfall(X,Y,Z)
xlabel('x-coordinate');
ylabel('y-coordinate');
zlabel('z-coordinate');
% Draws a mesh
% in one direction
% only
3D Contour contour3(x,y,z,n)x = -3:0.25:3;
y = -3:0.25:3;
[X, Y] = meshgrid(x,y) % build the plaid
Z = (1.8) .^(-1.4) * sqrt((X.^2) + (Y .^2)) .* cos (0.5 * Y) .* sin(X);
contour3(X,Y,Z,50)
xlabel('x-coordinate');
ylabel('y-coordinate');
zlabel('z-coordinate');
% n is the number of
% contour levels
% optional
2D Contour contour(x,y,z,n)x = -3:0.25:3;
y = -3:0.25:3;
[X, Y] = meshgrid(x,y) % build the plaid
Z = (1.8) .^(-1.4) * sqrt((X.^2) + (Y .^2)) .* cos (0.5 * Y) .* sin(X);
contour(X,Y,Z,20)
xlabel('x-coordinate');
ylabel('y-coordinate');
zlabel('z-coordinate');
% n is the number of
% contour levels
% optional
% input is 3D
% output is 2D
More on Mesh and Surface PlotsCommon Rendering tools Available in
Matlab
The SURF (Surface) Series
Surface – Simple Version surf(x,y,z)x = -3:0.25:3;y = -3:0.25:3;
[X, Y] = meshgrid(x,y) % build the plaid
Z = (1.8) .^(-1.4) * sqrt((X.^2) + (Y .^2)) .* cos (0.5 * Y) .* sin(X);
surf(X,Y,Z)
xlabel('x-coordinate');ylabel('y-coordinate');zlabel('z-coordinate');
% % % %
Surface with parameters surf(x,y,z,c)
% Color a sphere with the pattern of +1s and -1s
% in a Hadamard matrix
k = 5;
n = 2^k-1;
[x,y,z] = sphere(n);
c = hadamard(2^k);
surf(x,y,z,c);
colormap([1 1 0;0 1 1])
axis equal
% Example from the
% Matlab website
The “sinc” function (sin(r)/r) where r = radius we need to add eps to avoid inf when dividing by zero
» [xx,yy]= meshgrid(-4.*pi:pi./5:4.*pi);
» R=sqrt(xx.^2 + yy.^2)+eps; % radius
» zz=sin(R)./R;
» surf(xx,yy,zz)
» axis tight
eps: smallest possible difference between 2 floating point numbers
inf: infinity
mesh(X,Y,Z) versus surf(X,Y,Z)
Surface and Contour Plot surfc(x,y,z)x = -3:0.25:3;y = -3:0.25:3;
[X, Y] = meshgrid(x,y) % build the plaid
Z = (1.8) .^(-1.4) * sqrt((X.^2) + (Y .^2)) .* cos (0.5 * Y) .* sin(X);
surfc(X,Y,Z)
xlabel('x-coordinate');ylabel('y-coordinate');zlabel('z-coordinate');
% % % %
Surface Plot with Lighting surfl(x,y,z)x = -3:0.25:3;y = -3:0.25:3;
[X, Y] = meshgrid(x,y) % build the plaid
Z = (1.8) .^(-1.4) * sqrt((X.^2) + (Y .^2)) .* cos (0.5 * Y) .* sin(X);
surfl(X,Y,Z)
xlabel('x-coordinate');ylabel('y-coordinate');zlabel('z-coordinate');
% % % %
Surface Plot with Triangles trisurf(tri,x,y,z)
[x,y]=meshgrid(1:15,1:15);
tri = delaunay(x,y); % Matlab built-in function
z = peaks(15);
trisurf(tri,x,y,z)
% Example from the
% Matlab website
Misc. Examples
The Cube - Introduction
Example 1
Uses the surf function
Listing
The Cube – Listing (textbook, p. 264)
xx = [-1 -1 1 1 -1 % A-B-C-D-A
-1 -1 1 1 -1] % D-E-F-G-D
yy = [ 1 -1 -1 1 1 % A-B-C-D-A
1 -1 -1 1 1] % D-E-F-G-D
zz = [ 1 1 1 1 1 % A-B-C-D-A
-1 -1 -1 -1 -1] % D-E-F-G-D
subplot(1, 2, 1)
surf(xx, yy, zz)
axis equal
shading interp
view(-36, 44)
axis off
xx = [ 0 0 0 0 0 % P-P-P-P-P
-1 -1 1 1 -1 % A-B-C-D-A
-1 -1 1 1 -1 % D-E-F-G-D
0 0 0 0 0] % Q-Q-Q-Q-Q
yy = [ 0 0 0 0 0 % P-P-P-P-P
1 -1 -1 1 1 % A-B-C-D-A
1 -1 -1 1 1 % D-E-F-G-D
0 0 0 0 0] % Q-Q-Q-Q-Q
zz = [ 1 1 1 1 1 % P-P-P-P-P
1 1 1 1 1 % A-B-C-D-A
-1 -1 -1 -1 -1 % D-E-F-G-D
-1 -1 -1 -1 -1] % Q-Q-Q-Q-Q
subplot(1, 2, 2)
surf(xx, yy, zz)
axis equal
shading interp
view(-36, 44)
% axis off
The Cube – 3D - Plot (p. 264)
Here we use the function meshgrid(x,y) to define the plaid
This plot is created in three parts:1. Develop the underlying plaid specifying the x-y location of every point on the x-y plane2. Calculate the z values from the plaid3. Call some function that will accept the plaid and
these z values to produce the required plot
meshgrid(x,y,z,C)
Example: The Parabolic Dish (pp. 264-266)
% Parabolic dish
x = -3:3;
y = x;
[xx,yy]= meshgrid(x,y); % generates the plaid
zz = xx.^2 + yy.^2; % mapping of the z coordinates
mesh(xx,yy,zz) % color is proportional to mesh height
axis tight
title('z = x^2 + y^2')
xlabel('x'),ylabel('y'),zlabel('z')
NOTE:
Many more possibilities
Read: >> help mesh
Recommended Exercises
pp. 266-267: Exercise 11.2
Working with Colors
3D – Basic PeaksColors (p. 267)
[xx,yy,zz] = peaks(30); % Matlab peaks
surf(xx,yy,zz)
colormap 'default'
shading interp
axis tight
title('peaks')
xlabel('x'),ylabel('y'),zlabel('z')
More on Colors
Colors in Matlab (for 2D and 3D)
pcolor
colormap
shading
alpha
pcolor
% Example
n = 6;r = (0:n)'/n;
theta = pi*(-n:n)/n;
X = r*cos(theta);
Y = r*sin(theta);
C = r*cos(2*theta);
pcolor(X,Y,C)axis equal tight
In Matlab, pcolor - pseudocolor plot - is a rectangular array of cells with colors determined by C. MATLAB creates a pseudocolor plot using each set of four adjacent points in C to define a surface rectangle (i.e., cell).
Complete description at:
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/pcolor.html
Syntax:
pcolor(C)pcolor(X,Y,C)pcolor(axes_handles,...)h = pcolor(...)
Explanations
More about Colormap (Matlab web page):http://www.mathworks.com/access/helpdesk/help/techdoc/ref/colormap.html
Built-in Colors
User Defined Colors
Next Slide
User Defined Colors
See Presentation:
CSE1010_2009Fall_W-13_Ch11=ColorsInMatlab.ppt
Shading% From the Matlab website% Compare flat, faceted, interpolated-shaded shape
subplot(3,1,1)sphere(16)axis squareshading flattitle('Flat Shading')
subplot(3,1,2)sphere(16)axis squareshading facetedtitle('Faceted Shading')
subplot(3,1,3)sphere(16)axis equalshading interptitle('Interpolated Shading')
alpha
% Set transparency properties for objects in current axes
surf(peaks);
alpha(0.5);
More on … alpha
plot(get(gcf,'Alphamap'))
plot(alphamap('vup'))alphamap('increase',.4)plot(get(gcf,'Alphamap'))
[x,y,z] = meshgrid(-1.25:.1:-.25,-2:.2:2,-2:.1:2);v = x.*exp(-x.^2-y.^2-z.^2);
h = slice(x,y,z,v,[-1 -.75 -.5],[],[0]);alpha('color')set(h,'EdgeColor','none','FaceColor',... 'interp', 'FaceAlpha','interp')
alphamap('rampdown')alphamap('increase',.1)colormap(hsv)
comet3: animation of a linear plot
t = -10*pi:pi/250:10*pi;
comet3((cos(2*t).^2).*sin(t),(sin(2*t).^2).*cos(t),t);
Drawnow: animationof a surface
x = 0:pi/100:4*pi;y = x
[X,Y]= meshgrid(x,y);
z = 3*sin(X) + cos(Y);
h = surf (z);
axis tight; set (gca, 'nextplot', 'replacechildren');
shading interp; colormap (jet)
m = 1;
for k = 0 : pi/100 : 2* pi
z = (sin(X) + cos (Y) ) .* sin (k);
set (h, 'Zdata',z);
M(m) = getframe; m = m + 1;
movie(M,2); drawnow
End
% Note: File: animation02.m
A better 3D Plot: Color and Lighting of the Peaks
Textbook: p. 268
Let’s change the parameter to peaks to 120, and add the line light angle (60, 45) at the bottom of the script.
This illuminates the surface with a light at the specified azimuth and elevation angle (in degrees).
[xx,yy,zz] = peaks(120);
surf(xx,yy,zz)
lightangle(60, 45)
colormap 'default'
shading interp
axis tight
title('peaks')
xlabel('x'),ylabel('y'),zlabel('z')
% Illumination of the surface with
% a light at the specified azimuth
% and elevation angle (degrees)
view(azimuth,elevation)
The position of the viewer (the viewpoint) determines the orientation of the axes. You specify the viewpoint in terms of azimuth and elevation, or by a point in three-dimensional space.
view(az,el) and view([az,el]) set the viewing angle for a three-dimensional plot.
The default view angles are:
az = 20º
el = 30º
Surface Plotting – Parametric Surfaces
Working with polar coordinates Examples: The cylinder (uses ONE angle) and the Sphere (uses TWO angles)
Listing
The Cylinder (p. 270)
facets = 120; len = 2; radius = 1; % Constants to define the smoothness of the cylinder thr = linspace(0, 2*pi, facets); % Definition of a plaid using x and theta xr = [0 len]; [x, th] = meshgrid( xr, thr );
y = radius * cos(th);
z = radius * sin(th);
surf(x, y, z);
shading interp
axis equal,axis tight,axis off
lightangle(60, 45) % Illuminate the figure alpha(0.8) % Sets the transparency of the surface so that view(-50, 35) % a portion of the hidden details can show through
The Sphere (textbook p. 271)
facets = 120; radius = 1;
thr = linspace(0, 2*pi, facets); % range of theta
phir = linspace(0, pi, facets); % range of phi
[th, phi] = meshgrid( thr, phir ); % builds the plaid in theta and phi
x = radius * cos(phi);
y = radius * sin(phi) .* cos(th);
z = radius * sin(phi) .* sin(th);
surf(x, y, z);
shading interp
colormap copper
axis equal, axis tight, axis off
lightangle(60, 45)
The Ellipsoid
Effects:
Meshing / No MeshingShading parameters
Ellipsoid – mesh view
% From the Matlab website
[x, y, z] = ellipsoid(0,0,0,5.9,3.25,3.25,30);
surfl(x, y, z)
% shading = not used
colormap spring
axis equal
Ellipsoid – faceted flat% From the Matlab website
[x, y, z] = ellipsoid(0,0,0,5.9,3.25,3.25,30);
surfl(x, y, z)
shading flat
colormap spring
axis equal
Ellipsoid - Interpolation
% From the Matlab website
[x, y, z] = ellipsoid(0,0,0,5.9,3.25,3.25,30);
surfl(x, y, z)
shading interp
colormap spring
axis equal
Ellipsoid – mesh only
% From the Matlab websiteclc; clear;
[x, y, z] = ellipsoid(0,0,0,5.9,3.25,3.25,30);
surfl(x, y, z)
mesh(x, y, z)
colormap spring
axis equal
Bodies of Rotation - pp. 272 - 276
Rotating Continuous Functions
Bodies of rotation are created by rotating a linear curve about a specified axis, that is, by rotating a general function v = f (u) defined over a range of u values about the x
or z axes.
Rotating Continuous Functions
Rotating the v = u { 2 } about the x and z axes
% set up the plaid
facets = 100;
x = linspace(0, 5, facets);
th = linspace(0, 2*pi, facets);
[xx tth] = meshgrid(x, th);
% rotate about the x axis
subplot(1, 2, 1)
rr = xx.^2;
yy = rr .* cos(tth);
zz = rr .* sin(tth);
surf(xx, yy, zz, xx);
shading interp, axis tight
xlabel('x'), ylabel('y'), zlabel('z')
title('x^2 rotated about the x axis')
% rotate about the z axis
subplot(1, 2, 2)
rr = xx;
zz = rr.^2;
xx = rr .* cos(tth);
yy = rr .* sin(tth);
surf(xx, yy, zz);
shading interp, axis tight
xlabel('x'), ylabel('y'), zlabel('z')
title('x^2 rotated about the z axis')
Rotating Discrete Functions
“2D profile” of a fictitious machine part and the picture created when that profile is rotated about the x-axis.
The figure is generated by the code shown on the next slide
x = [0 0 3 3 1.75 1.75 2 2 1.75 1.75 3 4 ...
5.25 5.25 5 5 5.25 5.25 3 3 6 6];
y = [0 .5 .5 .502 .502 .55 .55 1.75 1.75 ...
2.5 2.5 1.5 1.5 1.4 1.4 ...
.55 .55 .502 .502 .5 .5 0];
subplot(1, 2, 1)
plot(x, y)
axis ([-1 7 -1 5]), axis off
title('2-D profile')
facets = 200;
subplot(1, 2, 2)
[xx tth] = meshgrid( x, linspace(0, 2*pi, facets) );
radius = meshgrid( y, 1:facets);
yy = radius .* cos(tth);
zz = radius .* sin(tth);
surf(xx, yy, zz);
shading interp
axis square, axis tight, axis off
colormap copper
lightangle(60, 45)
alpha(0.8)
title('rotated object')
Other 3-D Surface Plot Capabilities
Other 3-D Surface Plot Capabilities 1/2
MATLAB can also create special-purpose plots with the following functions:
alpha (x) sets the transparency of the surfaces: 0 <= x <= 1, where 0 means completely transparent and 1 is opaque.
The options available can be studied with >> help alpha
contour (z) produces a contour plot of the plaid surface defined by z. The options available can be studied with >> help bar3
[x,y, z] = cylinder(n) constructs the meshgrid for a cylinder with n facets in each direction. For more options, see >> help cylinder
[x,y,z] = ellipsoid(n) constructs the meshgrid for an ellipsoid with n facets in each direction. For more options, see >> help ellipsoid
lightangle (az , el) sets the angle of a light source (angles in degrees). For more options, see >> help lightangle
Other 3-D Surface Plot Capabilities 2/2 meshc (x, y, z) makes a mesh plot with contours below. For more options, see >> help meshc.
meshz (x, y, z) makes a mesh plot with vertical line extensions For more options, see >> help meshz.
pie3 (y) makes a 3-D pie chart of the values in y. For more options, see >> help pie3.
[x,y,z] = sphere(n) constructs the meshgrid for a sphere with n facets in each direction. For more options, see >> help sphere.
surfc (x, y, z) makes a surface plot with contours below. For more options, see >> help surfc
surf z (x, y, z) makes a surface plot with vertical line extensions. For more options, see >> help surf z.
waterfall (x, y, z) makes a mesh plot with vertical line extensions only in the x direction. For more options, see» help surf z.
Assembling Compound Surfaces
We can assemble more complex solid bodies by constructing simple surfaces and concatenate the data before submitting it to the rendering machine.
Example: A Solid Disk (from the textbook)
%basic parameters facets = 200; len = 2; radius = 3; radial = [0, radius]; th = linspace(0, 2*pi, facets); along = [0 len]; % build the front face [r1, tth] = meshgrid( radial, th ); x1 = zeros(size(r1)); y1 = r1.*cos(tth); z1 = r1.*sin(tth); % build the curved surface [l, tth] = meshgrid(along, th); x2 = l; y2 = radius*cos(tth); z2 = radius*sin(tth);
% build the back face x3 = len*ones(size(r1)); [r3, tth] = meshgrid(radial(end:-1:1), th); y3 = r3.*cos(tth); z3 = r3.*sin(tth); % assemble and draw the three parts x = [x1 x2 x3]; y = [y1 y2 y3]; z = [z1 z2 z3]; surf(x, y, z); shading interp colormap winter axis equal, axis tight, axis off
The Klein Bottle (textbook, p. 279)
http://www.mathworks.com/products/matlab/demos.html?file=/products/demos/shipping/matlab/xpklein.html
Visualizing Geographic Data (p. 283)
Example (from the textbook):
You have been given two files of data:
atlanta.txt, which presents the streets of Atlanta in graphical form, and
ttimes.txt, which gives the travel times between Atlanta suburbs and the city center.
You have been asked to present these data sets in a manner that will help to visualize and validate the data
Analyzing the Data
First we proceed to determine the nature of the data by opening the files in a text editor and examining their format and content.
1. Determine the file format: The first step is to open the data files in a plain text editor (the MATLAB editor would work fine). The format appears to be consistent with that of a text file delimited by tab characters. Since there are no strings in the file, it should be suitable to be read using MATLAB's built-in dlmread(...) function.
2. Discern the “street map” file content:
2.The table on the next slides shows the first few lines of the file atlanta.txt simplified by omitting certain irrelevant columns. 3.The numbers in columns 3—6 are pairs, the first of the pair being a large negative number, and the second a smaller positive number.4.Assuming that each row of this file is a street segment, these could be the x-y coordinates of the ends of a line.
2.A little thought confirms this guess when we realize that the latitude of Atlanta is —84º 42' relative to the Greenwich meridian, and its longitude is 330 65'— clearly, the values in these columns are 1,000,000 times the latitude and longitude of points within the city, probably each end of street segments.3.Column 7 contains numbers mostly in the range 1—6, which could indicate the type of street. We could explore this idea by coloring each line according to that value
3. Discern the “travel time” file content:
Table 11 .2 shows the first few lines of the file ttimes .txt simplified by omitting certain irrelevant columns.
The same latitude/longitude values occur in columns 4 and 5, but they are not repeated, suggesting that the data in this file are in a different form.
Examining the first two columns, the numbers in column 2 cycle repeatedly from 1 to 75, with column 1 counting the number of cycles up to 75.
Furthermore, the values in column 5 are the same whenever column 1 is the same, and the values in column 4 are the same whenever the value in column 2 matches.
This seems to be much like the plaid that results from a meshgrid(...) function call.
The values in column 6 then become evident—they would be the z values of the plaid, and it seems reasonable to assume that they represent the travel time in minutes.
Street Map Data (part)
… - 84546080.00 33988480.00 - 84558400.00 33995480.00 1.00
… - 84243880.00 33780010.00 - 84249980.00 33800840.00 1.00
… - 84243590.00 33780060.00 - 84249740.00 33800840.00 1.00
… - 84509920.00 33944340.00 - 84517200.00 33958190.00 1.00
… - 84510420.00 33944930.00 - 84516490.00 33957280.00 1.00
… - 84252840.00 33895840.00 - 84247360.00 33899290.00 1.00
… - 84247360.00 33899290.00 - 84240250.00 33903630.00 1.00
… - 84240250.00 33903630.00 - 84216090.00 33911010.00 1.00
… - 84216090.00 33911010.00 - 4203990.00 33913990.00 1.00
% draw the streets raw = dlmread('atlanta.txt'); streets = raw(:,3:7); [rows, cols] = size(streets) colors = 'rgbkcmo'; for in = 1: rows x = streets(in,[1 3])/1000000; y = streets(in,[2 4])/1000000; col = streets(in,5); col(col < 1) = 7; col(col > 6) = 7; plot(x,y,colors(col)); hold on end
% plot the travel times
tt = dlmread('ttimes.txt');
[rows,cols] = size(tt)
for in = 1:rows
r = tt(in, 1); c = tt(in, 2);
xc(r,c) = tt(in, 4)/1000000;
yc(r,c) = tt(in, 5)/1000000;
zc(r,c) = tt(in, 6);
end
surf(xc, yc, zc)
shading interp
alpha(.5)
grid on
axis tight
xlabel('Longitude')
ylabel('Latitude')
zlabel('Travel Time (min)')
view(-30, 45)
User Interaction with PlotsVideo at:
http://www.mathworks.com/products/demos/shipping/matlab/WhatsNewR2008a_GraphicsAndGUIBuilding.html
Brushing (textbook, p. 279)Linking a Plot to the Source Data… more: see video
Brushing Data Items
Brush Tool:
Script:
th = linspace(1, 20*pi, 500) y = sin(th)plot(th,y)
Linking a Plot to the Source Data
Summary
Basic two-dimensional line plots are accomplished by using plot(x,y)
■ Two-dimensional parametric plots are accomplished by using plot(x,y), where both x and y are dependent on another independent variable
■ Three-dimensional line and parametric plots are accomplished by using plot3(x,y,z)
■ Basic three-dimensional surface plots are accomplished by building a plaid using meshgrid(x,y), computing the zz layer as a function of xx and yy, then plotting the surface using mesh(xx, yy, zz) or surf(xx, yy, zz)
■ Parametric surface plots, like parametric line plots, are achieved by building the plaid with two independent variables and making xx, yy, and zz functions of those independent variables
■ Bodies of rotation are a special case of parametric surface plots where one of the independent variables is an angle with values between 0 and 2 (radians).