Mathematical Principles for Scientific Computing and Visualization
http : // www.farinhansford.com/books/scv
by Gerald Farin and Dianne Hansford

This notebook demonstrates contour and density plotting.

Figures  14.12, 14.13, and  14.14were created with this notebook.

See Chapter 14 : Visualizing Scalar Values over 2D for details.

Updated May 2008

(* Set the output folder -- folder this file lives in *)
SetDirectory[ToFileName[Extract["FileName"/.NotebookInformation[EvaluationNotebook[]],{1},FrontEnd`FileName]]];

(* Create the function to contour *)
f[x_,y_]:=Sin[(x^2)+(y^2)];

(* Create a plane z=0.9. We will intersect the surface with this plane to highlight one contour *)
f0[x_,y_]:=0.9; 

(* Create a plot of the function over a square domain with extentets [-pi/2,pi/2] *)
plot1=Plot3D[f[x,y],{x,-(π/2),(π/2)},{y,-(π/2),(π/2)},Mesh->False,PlotPoints->30,BaseStyle->{"FontFamily"->"Helvetica","FontSize"->11}]

(* Output the last plot to a postscript file *)
 (* Export["funct_example.eps",%] *)

(* Create a contour plot of the function *)
(* Play with the number of contours displayed *)
(* You can try changing the settings -- PlotPoints->Automatic,MaxRecursion->Automatic -- to small numbers, say 5 and 1 resepectively and see the contours looking less smooth. Without adequate evaluation, contouring methods cannot produce accurate results. *)

numcontours = 15;

Print["Contour plot of the function -- ",numcontours," z=constant contours"]

contplot=ContourPlot[f[x,y],{x,-(π/2),(π/2)},{y,-(π/2),(π/2)},Contours->numcontours,ContourShading->True,PlotPoints->Automatic,MaxRecursion->Automatic, BaseStyle->{"FontFamily"->"Helvetica","FontSize"->11}]

(* Output the contour plot to a postscript file *)
(* Export["contour.eps",contplot]; *)

(* Create a density plot of the function *)
(* Try modifying PlotPoints and MaxRecusion to get a feel for how the visualization technique works. *)
Print["Density plot of the function "]
density=DensityPlot[f[x,y],{x,-(π/2),(π/2)},{y,-(π/2),(π/2)}, PlotPoints->Automatic,MaxRecursion->Automatic,BaseStyle->{"FontFamily"->"Helvetica","FontSize"->11}]

(* Output to postscript *)
(* Export["density.eps",density]; *)


(* Create the surface z=0.9 *)
plot2=Plot3D[f0[x,y],{x,-(π/2),(π/2)},{y,-(π/2),(π/2)},Mesh->False,PlotPoints->5,BaseStyle->{"FontFamily"->"Helvetica","FontSize"->11}];

(* Combine the sine function to contour and the plane plots *)

Print["Sine function intersected with a plane producing a contour"]
Print[" -- 2 views -- "]

Picturetop=Show[plot1,plot2,ViewPoint->{0.`,0.`,3.384`},Axes->False]

Picture=Show[plot1,plot2,ViewPoint->{-1.902`,-1.902`,2.053`},Axes->False]

(* Output to postscript files *)
(* Export["sinx2y2_plane_top.eps",Picturetop]; *) (* Export["sinx2y2_plane.eps",Picture];*)

(* Let's play with another function *)

f1[x_,y_]:=(x^2)+(y^2);Plot3D[f1[x,y],{x,-1,1},{y,-1,1},Mesh->True,PlotPoints->3] 

DensityPlot[f1[x,y],{x,-1,1},{y,-1,1},PlotPoints->5]