Lecture 9-2
3-D Surface Plotting
Semester 1, 2012
School of Information Technologies
The University of Sydney, Australia
https://www.doczj.com/doc/4a9717545.html,.au/~engg1801
engg1801help@https://www.doczj.com/doc/4a9717545.html,.au
Jason Chan
j.chan@https://www.doczj.com/doc/4a9717545.html,.au
ENGG1801 Engineering Computing 1
Day Date (Jan 2013)Lecture topics Assessment
1Fri 4Introduction, Part 1: Excel–Basics
2Sat 5Functions, Plotting, Solving equations, File I/O
Sun 6---
3Mon 7Matrix algebra Lab Exam 1 (10%) [9-10am] 4Tue 8Part 2: Matlab–Basics, If statements, Arrays
5Wed 9Loops
6Thu 10Functions (1)
7Fri 11Functions (2)
8Sat 12Text & File I/O, Character strings Lab Exam 2 (15%) [9-11am] Sun 13---
9Mon 142-D and 3-D plotting, Surface plots
10Tue 15Creating movies from matrices, Matrix algebra
11Wed 16Matrix algebra, Interpolation and curve fitting
12Thu 17Polynomial regression, Images
13Fri 18Help for the Final Exam Lab Exam 3 (25%) [9-11am] Sat 19 –Sun 20---
Exam Mon 21---Final Exam (50%)
ENGG1801 Engineering Computing
2
?Surface plotting
–How to draw shapes and surfaces in 3-D
3
ENGG1801 Engineering Computing
4
?We can plot z = x 2+ y 2
ENGG1801 Engineering Computing
mesh()only shows the connecting lines -3
-2
-10
1
2
3
-2
2
51015x
z = x 2 + y 2
y
z
-3
-2
-10
1
2
3
-2
2
5
1015
x
z = x 2 + y 2
y
z
surf()shows the faces of the surfaces
% Set the x and y values
x = -3:3;
y = x;
% Create a matrix, 'xx', which contains the x
% values for all the points on the x-y plane
% (similar for 'yy')
[xx,yy] = meshgrid(x,y);
% Calculate the corresponding z values for each of % the (x,y) points on the x-y plane
zz = xx.^2 + yy.^2;
% Continued on next slide...
5
ENGG1801 Engineering Computing
6
% ... continued from previous slide
% Plot the mesh given the x, y and z values mesh(xx,yy,zz);
% Set the axes so that they reach only to the % limits of the values axis tight;
% Add labels
title('z = x^2 + y^2');xlabel('x');ylabel('y');zlabel('z');
ENGG1801 Engineering Computing
-3
-2
-10
1
2
3
-2
2
51015
x
z = x 2 + y 2
y
z
7
?Instead of mesh(), we can use the surf()function to plot a surface
% Plot the surface given the % x, y and z values surf(xx,yy,zz);
ENGG1801 Engineering Computing
-3
-2
-10
1
2
3
-2
2
51015
x
z = x 2 + y 2
y
z
?We can also plot one after the other on the same figure using pause:
% Plot the mesh
mesh(xx,yy,zz);
% Wait until the user presses something
pause;
% Now, overwrite the mesh and plot the
% surface on the same figure
surf(xx,yy,zz);
8
ENGG1801 Engineering Computing
?meshgrid()creates 2 matrices with the values of x and y for each point on the x-y plane
–It takes 2 arrays x and y and returns 2
matrices xx and yy
–The rows of xx are copies of x
–The columns of yy are copies of y
9
ENGG1801 Engineering Computing
10
>> x = -3:1:3
x = -3 -2 -1 0 1 2 3 >> y = x;
>> [xx,yy] = meshgrid(x,y)
xx = -3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3-3 -2 -1 0 1 2 3-3 -2 -1 0 1 2 3-3 -2 -1 0 1 2 3-3 -2 -1 0 1 2 3-3 -2 -1 0 1 2 3yy = -3 -3 -3 -3 -3 -3 -3
-2 -2 -2 -2 -2 -2 -2-1 -1 -1 -1 -1 -1 -10 0 0 0 0 0 01 1 1 1 1 1 12 2 2 2 2 2 23 3 3 3 3 3 3
ENGG1801 Engineering Computing
-3
-2
-10
1
2
3
-2
2
5
10
15
x
z = x 2 + y 2
y
z
11
1) Use meshgrid()to generate matrices of repeated rows of x and columns of y over the domain of the function
2)Calculate z = f(x,y)
3)Call either mesh()or surf()that accepts the x-y grid and z values to produce the plot
ENGG1801 Engineering Computing
[xx,yy] = meshgrid(x,y);mesh(xx,yy,zz)surf(xx,yy,zz)zz = xx.^2 + yy.^2;
12
?Matlab as 13 in-built colormaps ?User-defined colormaps can also be created ?The default is Jet
ENGG1801 Engineering Computing
-3
-2
-10
1
2
3
-2
2
510
15x
z = x 2 + y 2
y
z
-3
-2
-10
1
2
3
-2
2
5
10
15x
z = x 2 + y 2
y
z
-3
-2
-10
1
2
3
-2
2
5
10
15
x
z = x 2 + y 2
y
z
colormap bone colormap cool
colormap autumn
?Plot part of the top half of a sphere, defined by the equation:
x2+ y2+ z2= r2
where r is the radius
–Vary x and y from -1 to 1 with a step of 0.01
–Set the radius of the sphere r to 3
–The surface should be colored in gray shades –Name the plot “hemisphere” and label axes
13
ENGG1801 Engineering Computing
% Define r, x and y values
r = 3;
x = -1:0.01:1;
y = x;
% Create the x-y grid
[xx yy] = meshgrid(x, y);
% Calculate the z values using the x-y
% grid
zz = sqrt(r.^2 -xx.^2 -yy.^2)
14
ENGG1801 Engineering Computing
15
% Use grey shades for the surface colormap bone
% Generate the surface plot surf(xx,yy,zz) % Add a title and % labels
xlabel('x');ylabel('y');zlabel('z');
title('Hemisphere');
ENGG1801 Engineering Computing
16
?We can smooth the surface with:shading interp
ENGG1801 Engineering Computing
?Given 2 files:
atlanta.txt–coordinates of streets of Atlanta
ttimes.txt–travel times to the city centre
from different parts of Atlanta
?Visualize these datasets
17
ENGG1801 Engineering Computing
?Here are the first few lines of atlanta.txt
53423.00 53343.00 -84546100.00 33988160.00 -84556050.00 33993620.00 1.00 3025.00
54528.00 53351.00 -84546080.00 33988480.00 -84558400.00 33995480.00 1.00 3025.00
130081.00 128176.00 -84243880.00 33780010.00 -84249980.00 33800840.00 1.00 3025.00
130105.00 128192.00 -84243590.00 33780060.00 -84249740.00 33800840.00 1.00 3025.00
58150.00 71086.00 -84509920.00 33944340.00 -84517200.00 33958190.00 1.00 3025.00
58190.00 71102.00 -84510420.00 33944930.00 -84516490.00 33957280.00 1.00 3025.00
166861.00 168241.00 -84252840.00 33895840.00 -84247360.00 33899290.00 1.00 66.00
168241.00 169968.00 -84247360.00 33899290.00 -84240250.00 33903630.00 1.00 71.00
169968.00 169984.00 -84240250.00 33903630.00 -84216090.00 33911010.00 1.00 64.00
169984.00 173303.00 -84216090.00 33911010.00 -84203990.00 33913990.00 1.00 70.00?Each row corresponds to a street ?Columns 1, 2 and 8 are irrelevant ?Columns 3 and 4: the x and y coordinates of the beginning of the street
18
ENGG1801 Engineering Computing
53423.00 53343.00 -84546100.00 33988160.00 -84556050.00 33993620.00 1.00 3025.00
54528.00 53351.00 -84546080.00 33988480.00 -84558400.00 33995480.00 1.00 3025.00
130081.00 128176.00 -84243880.00 33780010.00 -84249980.00 33800840.00 1.00 3025.00
130105.00 128192.00 -84243590.00 33780060.00 -84249740.00 33800840.00 1.00 3025.00
58150.00 71086.00 -84509920.00 33944340.00 -84517200.00 33958190.00 1.00 3025.00
58190.00 71102.00 -84510420.00 33944930.00 -84516490.00 33957280.00 1.00 3025.00
166861.00 168241.00 -84252840.00 33895840.00 -84247360.00 33899290.00 1.00 66.00
168241.00 169968.00 -84247360.00 33899290.00 -84240250.00 33903630.00 1.00 71.00
169968.00 169984.00 -84240250.00 33903630.00 -84216090.00 33911010.00 1.00 64.00
169984.00 173303.00 -84216090.00 33911010.00 -84203990.00 33913990.00 1.00 70.00?Columns 5 and 6: the x and y coordinates of the end of the street
?Columns 3 to 6: need to be divided by
1,000,000 to convert to latitude / longitude
19
ENGG1801 Engineering Computing
53423.00 53343.00 -84546100.00 33988160.00 -84556050.00 33993620.00 1.00 3025.00
54528.00 53351.00 -84546080.00 33988480.00 -84558400.00 33995480.00 1.00 3025.00
130081.00 128176.00 -84243880.00 33780010.00 -84249980.00 33800840.00 1.00 3025.00
130105.00 128192.00 -84243590.00 33780060.00 -84249740.00 33800840.00 1.00 3025.00
58150.00 71086.00 -84509920.00 33944340.00 -84517200.00 33958190.00 1.00 3025.00
58190.00 71102.00 -84510420.00 33944930.00 -84516490.00 33957280.00 1.00 3025.00
166861.00 168241.00 -84252840.00 33895840.00 -84247360.00 33899290.00 1.00 66.00
168241.00 169968.00 -84247360.00 33899290.00 -84240250.00 33903630.00 1.00 71.00
169968.00 169984.00 -84240250.00 33903630.00 -84216090.00 33911010.00 1.00 64.00
169984.00 173303.00 -84216090.00 33911010.00 -84203990.00 33913990.00 1.00 70.00?Column 7: a number representing which type of street (e.g. quiet street, highway) –the values range from 1 to 6
20
ENGG1801 Engineering Computing