# 222 - Topic 02 - Contours

## 1348 days ago by Professor222

Contour Plot/Level Curves for 3D functions

John Travis

Mississippi College

This worksheet allows the user to graph one particular contour for a function $z = f(x,y)$.

Students can right-click on the images for printing onto paper.  By cutting out the 'light' part of each contour plot and stacking the dark parts on top of each other (perhaps after pasting onto thick paper) for varying values of $z_0$, and actual three dimensional solid view of the surface from the top will be generated.  By stacking the light pieces in the proper order, a solid view from the bottom will also be generated.

This is a good project for students to do in groups.  Constraints is that the surface the groups choose must include at least one peak and at least one valley and not grow too large on the boundary.

%auto ## Contour plot 3D solid construction project ## John Travis ## Spring 2011 var('x,y,z') ## Adjust the values of zmin and zmax to fit the input surface extremes ## to fit the input surface to be analyzed. This is not automated since the slider ## in the interact ranges between these values. zmin=-4 zmax = 4 @interact def _(surface = input_box(default=4*sin(x^3*y)/(x^2+y^2+1),label='$f(x,y) =$'), z_value=slider(zmin,zmax,1/20,0,label='$z_0$'), xmin=input_box(default=-3,label='$x_{min}$'), xmax=input_box(default=3,label='$x_{max}$'), ymin=input_box(default=-3,label='$y_{min}$'), ymax=input_box(default=3,label='$y_{max}$'), threed=('3D display',false), auto_update=true): F = plot3d(surface,(x,xmin,xmax),(y,ymin,ymax),color='lightgreen')+plot3d(z_value,(x,xmin,xmax),(y,ymin,ymax),color='blue') if threed: show(F,stereo='redcyan',spin=true,figsize=(6,9)) else: show(F,spin=true,perspective_depth=true,figsize=(6,9)) color_choice='gray' G = contour_plot(surface,(x,xmin,xmax),(y,ymin,ymax),contours=(-1000,z_value),cmap=color_choice,aspect_ratio=True) G.show(figsize=(6,9))

$f(x,y) =$
$z_0$
$x_{min}$
$x_{max}$
$y_{min}$
$y_{max}$
3D display
auto_update

## Click to the left again to hide and once more to show the dynamic interactive window

%auto ## Contour plot 3D solid construction project ## John Travis ## Spring 2011 var('x,y,z') ## Adjust the values of zmin and zmax to fit the input surface extremes ## to fit the input surface to be analyzed. This is not automated since the slider ## in the interact ranges between these values. zmin=-4 zmax = 4 @interact def _(surface = input_box(default=4*sin(x^3*y)/(x^2+y^2+1),label='$f(x,y) =$'), z_value=slider(zmin,zmax,1/20,0,label='$z_0$'), xmin=input_box(default=-3,label='$x_{min}$'), xmax=input_box(default=3,label='$x_{max}$'), ymin=input_box(default=-3,label='$y_{min}$'), ymax=input_box(default=3,label='$y_{max}$'), threed=('3D display',false), auto_update=true): F = plot3d(surface,(x,xmin,xmax),(y,ymin,ymax))+plot3d(z_value,(x,xmin,xmax),(y,ymin,ymax),color='yellow') if threed: show(F,stereo='redcyan',spin=true,figsize=(6,9)) else: show(F,spin=true,perspective_depth=true,figsize=(6,9)) color_choice='gray' G = contour_plot(surface,(x,xmin,xmax),(y,ymin,ymax),cmap='hsv',aspect_ratio=True) G.show(figsize=(6,9))

$f(x,y) =$
$z_0$
$x_{min}$
$x_{max}$
$y_{min}$
$y_{max}$
3D display
auto_update

## Click to the left again to hide and once more to show the dynamic interactive window

%auto ## Contour plot 3D solid construction project ## John Travis ## Spring 2011 var('x,y,z') ## Adjust the values of zmin and zmax to fit the input surface extremes ## to fit the input surface to be analyzed. This is not automated since the slider ## in the interact ranges between these values. @interact(layout=dict(top=[['surface'],['num']],left=[['xmin','xmax'],['ymin','ymax'],['zmin','zmax']])) def _(surface = input_box(default=4*sin(x^3*y)/(x^2+y^2+1),label='$f(x,y) =$'), num = slider(5,50,1,5), xmin=input_box(default=-3,label='$x_{min}$',width=5), xmax=input_box(default=3,label='$x_{max}$',width=5), ymin=input_box(default=-3,label='$y_{min}$',width=5), ymax=input_box(default=3,label='$y_{max}$',width=5), zmin=input_box(default=-2,label='$z_{min}$',width=5), zmax=input_box(default=1,label='$z_{max}$',width=5)): color_choice='gray' delz = (zmax-zmin)/num np1 = num+1 for k in range(1,np1): z_value = zmin+delz*k G = contour_plot(surface,(x,xmin,xmax),(y,ymin,ymax),contours=(-1000,z_value),cmap=color_choice,aspect_ratio=True)+text(str(z_value),(xmin,ymin),horizontal_alignment='left') G.show(figsize=(6,9))

$f(x,y) =$
num
 $x_{min}$ $x_{max}$ $y_{min}$ $y_{max}$ $z_{min}$ $z_{max}$

## Click to the left again to hide and once more to show the dynamic interactive window

# Here is some new experimental stuff grabbed from the Sage mailing lists # Might only work once we upgrade to version 6.0 f(x,y)=x^2-y^2 p = density_plot(f,(x,-3,3),(y,-3,3),cmap='jet') tracking = None xcoords,ycoords=[],[] def h(event): global tracking, xcoords, ycoords x,y = event.xdata, event.ydata ax = event.inaxes if ax is not None: xcoords.append(x) ycoords.append(y) if tracking is None: tracking, = ax.plot(xcoords, ycoords) else: tracking.set_xdata(xcoords) tracking.set_ydata(ycoords) ax.scatter([x], [y]) print "clicked on (%s,%s)"%(x,y) event.canvas.draw() html("<h2>Click me!</h2>") show(sys._sage_.InteractiveGraphics(p, renderer="matplotlib", events={'button_press_event': h}))

## Click me!

Traceback (click to the left of this block for traceback)
...
AttributeError: 'module' object has no attribute '_sage_'

## Click me!

Traceback (most recent call last):    xcoords,ycoords=[],[]
File "", line 1, in <module>

File "/tmp/tmpjHx2fF/___code___.py", line 26, in <module>
exec compile(u'show(sys._sage_.InteractiveGraphics(p, renderer="matplotlib", events={\'button_press_event\': h}))
File "", line 1, in <module>

AttributeError: 'module' object has no attribute '_sage_'