《科学计算科学计算 (26).pdf》由会员分享,可在线阅读,更多相关《科学计算科学计算 (26).pdf(15页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、4 4.5 5 3 3-D D SurfaceSurface PlotsPlotsDefining 2-D and 3-D grid coordinatesCreating 3-D surface and mesh plotsCommonly used function for demonstratingCreating 3-D surface plot of expressionIn MATLAB,there are two ways to generate matrix storing 2-D grid coordinates.(1)Generate a matrix by matrix
2、operations x=2:6;y=(3:8);X=ones(size(y)*x;Y=y*ones(size(x);1 1Defining 2Defining 2-D and 3D and 3-D Grid CoordinatesD Grid CoordinatesMatrix X and Y are used to store 2-D grid coordinates of each grid.Matrix X and Y are the 2-D grid coordinate matrices of the gridded surface.(2)Use the meshgrid func
3、tionX,Y=meshgrid(x,y);Input arguments x and y are vectors,and output arguments X and Y are matrices.x=2:1:6;y=(3:1:8);X,Y=meshgrid(x,y);The grid matrix generated by the third command is the same as that generated by method 1.1 1Defining 2Defining 2-D and 3D and 3-D Grid CoordinatesD Grid Coordinates
4、Substitute the x-coordinate and y-coordinate of each point in function,we obtain matrix z.Each column vector in the matrix x,y,and z corresponds to the coordinates of points on a line.x=2:6;y=(3:8);X,Y=meshgrid(x,y);Z=randn(size(X);plot3(X,Y,Z)grid on;Example 1 Draw a space curve.(1)Basic Syntaxmesh
5、(x,y,z,c)surf(x,y,z,c)The x and y define a grid in the x-y plane.The z specifies the height of the surface plot at each x-y coordinate,and the c specifies the surface color at different heights.The c is according to the z when c is omitted.2 2Creating 3Creating 3-D Surface and Mesh PlotsD Surface an
6、d Mesh PlotsExample 2 Create a 3-D surface plot.=2 2t=-2:0.2:2;X,Y=meshgrid(t);Z=X.*exp(-X.2-Y.2);subplot(1,3,1)mesh(X,Y,Z);subplot(1,3,2)surf(X,Y,Z);subplot(1,3,3)plot3(X,Y,Z);grid on(2)Other format of the mesh funtion and the surf functionmesh(z,c)surf(z,c)When the arguments x and y are omitted,th
7、e plot use the column indices of the elements in z as the x-coordinates,the row indices of the elements in z as the y-coordinates,t=1:5;z=0.5*t;2*t;3*t;mesh(z);2 2Creating 3Creating 3-D Surface and Mesh PlotsD Surface and Mesh Plots(3)Other functions for plotting surfaceMeshcCreate a mesh plot with
8、a contour plot underneathmeshz Create a mesh plot with curtainsurfc Create a surface plot with a contour plot underneathsurfl Create a surface plot with colormap-based lighting2 2Creating 3Creating 3-D Surface and Mesh PlotsD Surface and Mesh PlotsExample 3 Create 3-D surface plots in four ways.=()+
9、(),x0,2,y1,3x,y=meshgrid(0:0.1:2,1:0.1:3);z=(x-1).2+(y-2).2-1;subplot(2,2,1);meshc(x,y,z);title(meshc(x,y,z)subplot(2,2,2);meshz(x,y,z);title(meshz(x,y,z)subplot(2,2,3);surfc(x,y,z);title(surfc(x,y,z)subplot(2,2,4);surfl(x,y,z);title(surfl(x,y,z)(1)The sphere functionx,y,z=sphere(n)Return the x-,y-,
10、and z-coordinates of a sphere.The returned sphere has a radius equal to 1 and consists of 20-by-20 faces.So The function returns the x-,y-,and z-coordinates as three 21-by-21 matrices.(2)The cylinder functionx,y,z=cylinder(R,n)Return the x-,y-,and z-coordinates of a cylinder.The returned cylinder ha
11、s a radius equal to 1,20 equally spaced points around its circumference,and bases parallel to the xy-plane.The function returns the x-,y-,and z-coordinates as three 21-by-21 matrices.3 3Commonly Used Function for Demonstrating Commonly Used Function for Demonstrating Example 4 Plots cylinder,vase an
12、d conical surface using the returned coordinates of the cylinder function.subplot(1,3,1);x,y,z=cylinder;surf(x,y,z);subplot(1,3,2);t=linspace(0,2*pi,40);x,y,z=cylinder(2+cos(t),30);surf(x,y,z);subplot(1,3,3);x,y,z=cylinder(0:0.2:2,30);surf(x,y,z);Example 5 Create two vertical intersecting plots of t
13、he same diameter using the returned coordinates of the cylinder function.x,y,z=cylinder(1,60);z=-1*z(2,:);z(2,:);surf(x,y,z)hold onsurf(y,z,x)axis equal p1=peaks(10);p2=peaks;p3=peaks(-3:0.2:3);x,y=meshgrid(-2:0.1:2,0:0.1:5);p4=peaks(x,y);3 3Commonly Used Function for Demonstrating Commonly Used Fun
14、ction for Demonstrating(3)The peaks functionpeaks(n)Return an n-by-n matrix.peaks(V)Return an n-by-n matrix,where n=length(V).peaks(x,y)Evaluate peaks at the given x and y(which must be the same size)and return a matrix the same size.peaksReturn a 49-by-49 matrix.fsurf(funx,funy,funz,uvlims)fmesh(fu
15、nx,funy,funz,uvlims)Plot the parametric surface defined by x=funx(u,v),y=funy(u,v),z=funz(u,v)over over the specified interval.Argument uvlims specify the plotting interval for u and v,specified as a vector of form umin umax vmin vmax.The default interval for u and vis-5 5.4 4Creating 3Creating 3-D Surface Plot of ExpressionD Surface Plot of Expressionfunx=(u,v)u.*sin(v);funy=(u,v)-u.*cos(v);funz=(u,v)v;fsurf(funx,funy,funz,-5 5-5-2)hold onfmesh(funx,funy,funz,-5 5-2 2)hold off=sin=uv=v-55 5,5,-55 22Example 6 Plotting a spiral surface.
限制150内