User Tools

Site Tools


gibson:teaching:fall-2014:math445:lecture16-diary

This is an old revision of the document!


A PCRE internal error occured. This might be caused by a faulty plugin

====== 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> It is conventional to use capital letters (X,Y) for the matrix output of meshgrid from small letter (x,y) vector inputs. 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 in the region $-2 \leq x \leq 2$, $-3 \leq y \leq 3$. We can then evaluate a function $z = f(x,y)$ over that 2D array via elementwise matrix operations. For example, this Matlab code would evaluate $z = f(x,y) = x^2 + y^2$ <code matlab> Z = X.^2 + Y.^2 </code> ===== pcolor: pseudocolor plot ===== The ''pcolor'' function produces a pseudocolor or checkerboard plot of Z as a function of x,y. <code matlab> pcolor(X,Y,Z) colorbar axis equal axis tight xlabel('x'); ylabel('y'); title('z = x^2 + y^2') </code> You can modify the appearance of the pseudocolor with the ''shading'' command. Try ''shading flat'', ''shading interp'', and ''shading faceted''. ===== contour, contourf ===== The ''contour'' function plots contours or level curves of z=f(x,y). That is, it plots curves on which f(x,y) is constant. <code matlab> [X,Y] = meshgrid(x,y); Z = X.^2 + Y.^2; contour(X,Y,Z) xlabel('x'); ylabel('y'); title('z= x^2 + y^2') colorbar axis equal axis tight </code> As you can see, the level curves of $z = f(x,y) = x^2 + y^2$ are circles $x^2 + y^2 = c$. ''contourf'' is the same as ''contour'', except that the regions between contour lines are filled with color. ===== surf, surfc ===== The previous plots were all looking straight down at the (x,y) plane, with the value of z = f(x,y) encoded as a color. The ''surf'' function will plot z = f(x,y) in 3D, as a surface of height z over the (x,y) plane. <code matlab> surf(X,Y,Z) % draw z=f(x,y) as a surface over x,y xlabel('x'); ylabel('y'); zlabel('z') axis equal; axis tight </code> It's also possible to draw more complicated surfaces (surfaces that are not simple graphs of the form $z = f(x,y)$). Here's an example of how to draw a sphere by parameterizing its surface in terms of angles $\phi$ and $\theta$. <code matlab> % make mesh over theta, phi theta = linspace(0,2*pi,50); % angle between x and y phi = linspace(0,pi,25); % angle down from z axis [Theta, Phi] = meshgrid(theta, phi); % form 2D mesh in theta, phi % Parameterize surface of sphere in terms of theta, phi % (note that x^2 + y^2 + z^2 = 1) 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') axis equal </code> % 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);

gibson/teaching/fall-2014/math445/lecture16-diary.1416324303.txt.gz · Last modified: 2014/11/18 07:25 by gibson