3D Plotting

-- surface function
z(x,y) = sin(sqrt(x^2+y^2))/sqrt(x^2+y^2)

n = 3*pi
xmin=-n; xmax=n -- desired range of x and y values
ymin=-n; ymax=n
nx=40; ny=40 -- desired resolution

-- sample the function z(x,y) on an evenly spaced grid
mapx(i) = (i-1)*(xmax-xmin)/(nx-1) + xmin
mapy(i) = (i-1)*(ymax-ymin)/(ny-1) + ymin
zgrid[ix,iy] = z(mapx(ix),mapy(iy)) dim[nx,ny]

Zmin= -.2; Zmax= 1
-- Display a 3D projection of the surface

MathPad can display a "3D" projection of surface by using viewsurf. To enable this function quit MathPad, move the XFun file "viewsurf" into MathPad's Active XFuns folder and relaunch.

The surface is specified by a 2D array of z values. The 2D array represents z samples spaced uniformly in x and y. To sample a function over some arbitrary range of x and y values, you can scale array index values to generate the desired x and y values.

View Angle

The viewsurf XFun creates a new window named SURFACE to display the projection. The SURFACE window scrollbars can be used to change the view angles. The vertical bar controls the view elevation. The horizontal controls the view azimuth. The direction of the controls corresponds moving the back corner of the surface.


The xy scale is adjusted to fill the window width. The z scale can be controlled by variables Zmin and Zmax. If Zmin and/or Zmax are not specified they will autorange. The base of the surface is at Zmin. Values below Zmin are clipped and displayed as Zmin. Missing data is also displayed as Zmin. Zmax is used for scaling but the surface is not clipped at Zmax.

Click Info

When the mouse is clicked on a surface grid point, the corresponding z value is shown in an info line at the bottom of the plot.


The viewsurf function is primarily intended as an interactive way to view a surface. However, the surface view can be copied to the main plot window by setting the 2nd parameter viewsurf(zgrid,on). The surface is scaled to fit the plot window size at the time of evaluation. If the plot window is re-sized it may distort the projection. You can reevaluate the surface to restore correct scaling. To prevent distortion on printout you should use the zoom box and evaluate the surface with the plot window set to the printer aspect ratio.

The normal axis labels are not very meaningful for the surface projection. They are usually suppressed but can be controlled by the special variables Xticklabels=on; Yticklabels=on

Comparing image and viewsurf

Note that image and viewsurf are alternate ways to display the same data.

Xmin=xmin; Xmax=xmax;
Ymin=ymin; Ymax=ymax

image zgrid

The viewsurf display works best for seeing shapes of smooth surfaces. The image command is better for picking out sharp features.