Slicing and Dicing: univariate slices of a bivariate function
MAT329
Andy Long, Spring, 2016

(* I define the function as g, but then set f as either g(x,y) or g(y,x), depending on which I'd rather view. The code below does not treat the x and y axes symmetrically, unfortunately. *)
Clear[xmin,xmax,ymin,ymax,zmax,zmin]
g[x_,y_]:=x y /(x^2+y^2)
{xmin,xmax,ymin,ymax,zmin,zmax}={-1,1,-1,1,-1.5,1.5};
g[x_,y_]:=Sin[x]y^2
{xmin,xmax,ymin,ymax,zmin,zmax}={-3,3,-3,3,-10,10};
(* Set the plotting region *)
RA={{xmin,xmax},{ymin,ymax}};
RC={{xmin,xmax},{zmin,zmax}};
(* to reverse the axes switch the order of the next two lines: *)
f[x_,y_]:=g[y,x]
f[x_,y_]:=g[x,y]
(* The function f's surface won't change in the following, so we can define it outside the Manipulate command: *)
Plot1=Plot3D[f[x,y],{x,xmin,xmax},{y,ymin, ymax},AxesLabel->Automatic,BoxRatios->{1,1,1.2},ViewPoint->Front];
Manipulate[
m=c/(1-c^2);
scale=Sqrt[m^2+1];
Deltax=-m b/scale;
Deltay=b/scale;
xm=(ymin-Deltay)/m-Deltax;
xM=(ymax-Deltay)/m-Deltax;
tmp=Min[xm,xM];
xM=Max[xm,xM];
xm=tmp;
pxmin=Max[xmin,xm];
pxmax=Min[xmax,xM];
PlotA=Plot[m (x+Deltax)+Deltay,{x,xmin,xmax},AxesOrigin->{0,0},PlotRange->RA];
PlotB=ContourPlot[f[x,y],{x,xmin,xmax},{y,ymin,ymax},PlotRange->RA];
PlotC=Plot[f[x+Deltax,m (x+Deltax)+Deltay],{x,pxmin,pxmax},AxesOrigin->{0,0},PlotRange->RC];
(* Here's the slice along the surface: *)
PlotD=ParametricPlot3D[{x+Deltax,m (x+Deltax)+Deltay,f[x+Deltax,m (x+Deltax)+Deltay]},{x,pxmin,pxmax}];
{Show[Plot1,PlotD],Show[PlotB,PlotA],PlotC}
,{{b,0.1,"Shift (b)"},ymin, ymax} (* the biggest shifts should take us from the origin to the bottom of the y axis (ymin) or to the top (ymax) *)
,{{c,0.1,"Slope (c)"},-.99,.99} (* I keep the slope away from infinity this way... *)
]

Here’s a familiar example:

Clear[xmin,xmax,ymin,ymax,zmax,zmin]
g[x_,y_]:=x y /(x^2+2y^2)
{xmin,xmax,ymin,ymax,zmin,zmax}={-1,1,-1,1,-1.5,1.5};
(* Set the plotting region *)
RA={{xmin,xmax},{ymin,ymax}};
RC={{xmin,xmax},{zmin,zmax}};
(* to reverse the axes switch the order of the next two lines: *)
f[x_,y_]:=g[y,x]
f[x_,y_]:=g[x,y]
(* The function f's surface won't change in the following, so we can define it outside the Manipulate command: *)
Plot1=Plot3D[f[x,y],{x,xmin,xmax},{y,ymin, ymax},AxesLabel->Automatic,BoxRatios->{1,1,1.2},ViewPoint->Front];
Manipulate[
m=c/(1-c^2);
scale=Sqrt[m^2+1];
Deltax=-m b/scale;
Deltay=b/scale;
xm=(ymin-Deltay)/m-Deltax;
xM=(ymax-Deltay)/m-Deltax;
tmp=Min[xm,xM];
xM=Max[xm,xM];
xm=tmp;
pxmin=Max[xmin,xm];
pxmax=Min[xmax,xM];
PlotA=Plot[m (x+Deltax)+Deltay,{x,xmin,xmax},AxesOrigin->{0,0},PlotRange->RA];
PlotB=ContourPlot[f[x,y],{x,xmin,xmax},{y,ymin,ymax},PlotRange->RA];
PlotC=Plot[f[x+Deltax,m (x+Deltax)+Deltay],{x,pxmin,pxmax},AxesOrigin->{0,0},PlotRange->RC];
(* Here's the slice along the surface: *)
PlotD=ParametricPlot3D[{x+Deltax,m (x+Deltax)+Deltay,f[x+Deltax,m (x+Deltax)+Deltay]},{x,pxmin,pxmax}];
{Show[Plot1,PlotD],Show[PlotB,PlotA],PlotC}
,{{b,0.1,"Shift (b)"},ymin, ymax} (* the biggest shifts should take us from the origin to the bottom of the y axis (ymin) or to the top (ymax) *)
,{{c,0.1,"Slope (c)"},-.99,.99} (* I keep the slope away from infinity this way... *)
]