**This is an old revision of the document!** ----
====== Math 445: 3D graphics ====== Matlab vocabulary * meshgrid * pcolor * contour, contourf * surf, surfc * quiver * shading (flat, faceted, or interp) * colorbar * axis (equal, tight) ====== meshgrid: ====== The ''meshgrid'' function is essential for Matlab's 3D graphics. Meshgrid creates 2D arrays of x,y data covering the x,y, plane, over which a function can be evaluated and graphed. Example: <code matlab> >> x = linspace(-2,2,5) x = -2 -1 0 1 2 >> y = linspace(-3,3,7) y = -3 -2 -1 0 1 2 3 >> [X,Y] = meshgrid(x,y) X = -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 Y = -3 -3 -3 -3 -3 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 </code> Observe how the output matrices X varies left to right, along the x axis, and Y varies up and down, along the y axis. Together they provide x,y, coordinates for a grid of points on the x,y plane. ===== pcolor: pseudocolor plot ===== % 3D graphics: plot z=f(x,y) versus x,y % need to evaluate f over aplane x,y % define simple function f(x,y) = 1/2 x^2 + y^2 x = linspace(-2,2,5) x = -2 -1 0 1 2 y = linspace(-3,3,6) y = -3.0000 -1.8000 -0.6000 0.6000 1.8000 3.0000 [X,Y] = meshgrid(x,y); X X = -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 Y Y = -3.0000 -3.0000 -3.0000 -3.0000 -3.0000 -1.8000 -1.8000 -1.8000 -1.8000 -1.8000 -0.6000 -0.6000 -0.6000 -0.6000 -0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 1.8000 1.8000 1.8000 1.8000 1.8000 3.0000 3.0000 3.0000 3.0000 3.0000 X X = -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 Y Y = -3.0000 -3.0000 -3.0000 -3.0000 -3.0000 -1.8000 -1.8000 -1.8000 -1.8000 -1.8000 -0.6000 -0.6000 -0.6000 -0.6000 -0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 1.8000 1.8000 1.8000 1.8000 1.8000 3.0000 3.0000 3.0000 3.0000 3.0000 % Now use componentwise arithmetic to generate z=f(x,y) Z = 0.5*X.^2 + Y.^2 Z = 11.0000 9.5000 9.0000 9.5000 11.0000 5.2400 3.7400 3.2400 3.7400 5.2400 2.3600 0.8600 0.3600 0.8600 2.3600 2.3600 0.8600 0.3600 0.8600 2.3600 5.2400 3.7400 3.2400 3.7400 5.2400 11.0000 9.5000 9.0000 9.5000 11.0000 % simplest 3D plot is a contour plot contour(X,Y,Z) % contour plot shows levels curves, Z = constant y = linspace(-3,3,20) y = Columns 1 through 7 -3.0000 -2.6842 -2.3684 -2.0526 -1.7368 -1.4211 -1.1053 Columns 8 through 14 -0.7895 -0.4737 -0.1579 0.1579 0.4737 0.7895 1.1053 Columns 15 through 20 1.4211 1.7368 2.0526 2.3684 2.6842 3.0000 x = linspace(-2,2,20) x = Columns 1 through 7 -2.0000 -1.7895 -1.5789 -1.3684 -1.1579 -0.9474 -0.7368 Columns 8 through 14 -0.5263 -0.3158 -0.1053 0.1053 0.3158 0.5263 0.7368 Columns 15 through 20 0.9474 1.1579 1.3684 1.5789 1.7895 2.0000 [X,Y] = meshgrid(x,y); Z = X.^2 + Y.^2; contour(X,Y,Z) xlabel('x') ylabel('y') colorbar title('z= x^2 + y^2') axis equal axis tight contourf(X,Y,Z) % filled contour plot axis equal axis tight colorbar pcolor(X,Y,Z) pcolor(X,Y,Z) % draw each matrix elem as a color colorbar shading flat % takes away grid lines shading interp % interpolate shading % 3D graphics clf() surf(X,Y,Z) % draw z=f(x,y) as a surface over x,y xlabel('x') ylabel('y') zlabel('z') shading flat shading interp % Parametric plot of sphere % make mesh over theta, phi theta = linspace(0,2*pi,50); phi = linspace(0,pi,25); % those are vectors, transform to matrix mesh [Theta, Phi] = meshgrid[theta, phi]; [Theta, Phi] = meshgrid[theta, phi]; | Error: Unbalanced or unexpected parenthesis or bracket. [Theta, Phi] = meshgrid(theta, phi); X = cos(Theta).*sin(Phi); Y = sin(Theta).*sin(Phi); Z = cos(Phi); % Draw parametrized surface of sphere with surf surf(X,Y,Z); axis equal xlabel('x'); ylabel('y'); zlabel('z') % use extra variable C for color coding C = X; surf(X,Y,Z,C); axis equal xlabel('x'); ylabel('y'); zlabel('z') C = sin(X); surf(X,Y,Z,C); axis equal % surfc : surfaceplot with contour on x,y surfc(X,Y,Z,C); axis equal % quiver plot % used to show vector fields load y.asc load x.asc load vx.asc load vy.asc clf() quiver(x,y,vx,vy); axis equal axis tight xlabel('x'); ylabel('y'); title('vector field vx,vy over plane x,y') % subplots % make an array of plots within a figure window clf() subplot(2,2,1) % 1st subplot in 2 x 2 array quiver(x,y,vx,vy); axis equal axis tight subplot(2,2,2) % 2nd subplot in 2 x 2 array surfc(X,Y,Z,C); axis equal subplot(2,2,3) % 2nd subplot in 2 x 2 array surf(X,Y,Z,Z); axis equal subplot(2,2,4) % 4th x = linspace(0,pi,20);