Plotting Surfaces on Mathematica

by Pantelis Bouboulis

ClearAll["@"];

1. Plotting functions

To plot a function on mathematica you can use the Plot3D command. Just define a function, use the Plot3D command and then click on the Evaluate Cells command of the Evaluate menu, or use shift+enter. you can use the examples presented here and change the function.
For example:

f1[x_,y_]=x^2-y^2;
Plot3D[f1[x,y],{x,-3,3},{y,-3,3},PerformanceGoal->"Quality"]

A more sophisticated function is

f2[x_,y_]=Sin[x^2-y^2];
Plot3D[f2[x,y],{x,-3,3},{y,-3,3},PerformanceGoal->"Quality"]

You can "play" with the options of the Plot3D command to change the appearance. For example, if you don't want to display the grid lines you can use the Mesh -> None option:

Plot3D[f2[x,y],{x,-3,3},{y,-3,3},PerformanceGoal->"Quality",Mesh->None]

You can change the color, add contours, edit the mesh grid e.t.c. You can see all the options at the documentation center of the "Help" menu. You can also use the PlotRange option to change the range of the z-axis. See the following example

g1[x_,y_]=1/(x^2+y^3);
Plot3D[g1[x,y],{x,-3,3},{y,-3,3},PerformanceGoal->"Quality",PlotRange->{-5,5}]

To plot functions that change value rapidly you must use the MaxRecursion option. Beware, that the number following must be an integer in the interval [0,15].

Plot3D[g1[x,y],{x,-3,3},{y,-3,3},PerformanceGoal->"Quality",PlotRange->{-5,5},MaxRecursion->8, ClippingStyle->None]

See the difference of the following graphs.

g2[x_,y_]=Sin[1/(x^2+y^2)];
Plot3D[g2[x,y],{x,-1,1},{y,-1,1},PerformanceGoal->"Quality",PlotRange->{-2,2}]

Plot3D[g2[x,y],{x,-1,1},{y,-1,1},PerformanceGoal->"Quality",PlotRange->{-2,2},MaxRecursion->5]

Beware that the MaxRecursion option may use an enormous ammount of your system's memory.

You can also create an animation over a family of functions. The following exaples, animates the function using values of a form 1 to 5 with step 0.2: The performance depends greatly on your coputer. You will need a Core2Duo or better processor to view it at a satisfactory quality.

h1[x_,y_,a_]=Exp[-(x^2+y^2)/(3a)]*Cos[(x^2+y^2)/a];
avalues=Join[Table[i,{i,0.2,3,0.1}]];
Animate[Plot3D[h1[x,y,a],{x,-5,5},{y,-5,5},PerformanceGoal->"Quality",Mesh->Automatic,PlotRange->{-1,1}],{a,avalues}]

Parametric Plots

With the ParametricPlot3d command you can plot surfaces and curves that are given in parametric form.
For example:

x[u_]=Sin[u];
y[u_]=Cos[u];
z[u_]=u/10;
ParametricPlot3D[{x[u],y[u],z[u]},{u,0,20}]

x[u_]=(4+Sin[10u])*Cos[u];
y[u_]=(4+Sin[10u])*Sin[u];
z[u_]=2Cos[10u];
ParametricPlot3D[{x[u],y[u],z[u]},{u,0,20}]

x[u_,v_]=Cos[u];
y[u_,v_]=Sin[u]+Cos[v];
z[u_,v_]=Sin[v];
ParametricPlot3D[{x[u,v],y[u,v],z[u,v]},{u,0,2 π}, {v,-π,π}]

You can even plot more objects in the same window, but this is a little tricky.

f4[x_,y_]=Sin[(x^2-y^2)/4];
obj1=Plot3D[f4[x,y],{x,-4,4},{y,-4,4},PerformanceGoal->"Quality",Mesh->None];
x[u_,v_]=(3+Cos[v])Cos[u];
y[u_,v_]=(3+Cos[v])Sin[u];
z[u_,v_]=2*Sin[v];
obj2=ParametricPlot3D[{x[u,v],y[u,v],z[u,v]},{u,0,2Pi},{v,0,2Pi},PlotStyle->Directive[Yellow,Specularity[White,20],Opacity[0.8]],Mesh->None];
Show[obj2,obj1]

Spherical coordinates

You may use the SpericalPlot3D command to create 3D graphs using spherical coordinates. For example

r[θ_,φ_]=Cos[2θ];
SphericalPlot3D[r[θ,φ],{θ,0,Pi},{φ,0,2Pi}]

r1[θ_,φ_]=1+Sin[φ]-Cos[θ];
SphericalPlot3D[r1[θ,φ],{θ,0,Pi},{φ,0,2Pi}]