Given a function $z=f(x,y)$, this code will do several things. *It will construct several different types of contour plots of the function. *It will plot the gradient field $\vec \nabla f$. *It will plot the level curves and gradient plot on the same axes. *If you uncomment the 3D portion, it will also construct a 3D graph of the surface. This requires that you have Java installed. When you are ready, just click "Evaluate" or type "Shift+Enter" while your cursor is in the input box.
var('x','y') #Define your variables
f(x,y) = 9-x^2-2*y^2 #State the function
xbounds = (x,-3,3)
ybounds = (y,-3,3)
#This gives you a generic contour plot (a plot consisting of several level curves)
pcontour=contour_plot(f,(x,-2,2), (y,-2,2),colorbar=True)
show(pcontour)
#This section allows you to plot as many level curves as you want
level_curve_values=[0,1,2,3,4,5,6,7,8]
p=implicit_plot(f(x,y)==level_curve_values[0],xbounds,ybounds)
for z in level_curve_values:
p+=implicit_plot(f(x,y)==z,xbounds,ybounds)
show(p)
#This section draws the gradient of your function.
gradf = f.diff()(x,y)
gradplot=plot_vector_field(gradf,xbounds,ybounds)
show(gradplot)
#This section combines your level curve plot and your gradient plot.
show(p+gradplot)
#Uncomment this section if you want a 3D image of the surface.
#p3D=plot3d(f,xbounds,ybounds)
#show(p3D)
Here's the command for plotting a vector field in Mathematica.
Plot3D[9-x^2-y^2,{x,-3,3},{y,-3,3}]
To get level curves in Mathematica, type
ContourPlot[z==9-x^2-y^2,{x,-3,3},{y,-3,3}]
{{page>get_sage}}
