Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168766
Image: ubuntu2004

Functions of several variables: z=f(x,y)z=f(x,y), w=f(x,y,z)w=f(x,y,z)

Functions of several variables are used to keep track of things like temperature, wind speed, barometric pressure, altitude, profit, and much more. Pretty much any time you want to understand a variable which changes, then functions of several varialbes are used to model this.

To plot a function f(x,y)f(x,y), use plot3d.

Functions of several variables are used to keep track of things like temperature, wind speed, barometric pressure, altitude, profit, and much more. Pretty much any time you want to understand a variable which changes, then functions of several varialbes are used to model this.
f(x,y)=x^2-y^2
plot3d(f, (x,-3,3), (y,-3,3))

Here we plot the function with its traces.  We plot the traces as parametric curves in 3d.

xtrace=parametric_plot((0,y,f(x=0)), (x,-3,3), (y,-3,3),color='red') ytrace=parametric_plot((x,0,f(y=0)), (x,-3,3), (y,-3,3),color='red') plot3d(f, (x,-3,3), (y,-3,3))+xtrace+ytrace

And here is a contour plot (make sure you evaluate the definition of f(x,y)f(x,y) above).  Here, the shading helps us to know which values are higher and which values are lower.  In this plot, higher values are lighter.

contour_plot(f(x,y), (x,-3,3), (y,-3,3))

Or with specific contour lines (where f(x,y)=1f(x,y)=1, f(x,y)=3f(x,y)=3, and f(x,y)=4f(x,y)=4).  Here, the shading only happens between the contour lines; the white outside of the contour lines doesn't indicate anything.

contour_plot(f(x,y), (x,-3,3), (y,-3,3),contours=[1,3,4])

We can also plot 3d contour plots of functions of the form w=f(x,y,z)w=f(x,y,z) using implicit_plot3d.

var('x,y,z') c=1 implicit_plot3d(x^2 - y^2 + z^2, (x,-3,3), (y,-3,3), (z,-3,3))

Vector Fields

var('x,y') plot_vector_field( (-y,x), (x,-3,3), (y,-3,3))

To plot 3d vector fields, we first define a function which plots them.

%auto def plot_vector_field3d(vec, xrange, yrange, zrange, plot_points=[5,5,5], center_arrows=False,**kwds): xvar, xmin, xmax = xrange yvar, ymin, ymax = yrange zvar, zmin, zmax = zrange if not isinstance(plot_points, (list, tuple)): plot_points = [plot_points]*3 ff, gg, hh = fast_float(vec, xvar, yvar, zvar) xpoints = [xmin..xmax, step=float(xmax-xmin)/(plot_points[0]-1)][0:plot_points[0]] ypoints = [ymin..ymax, step=float(ymax-ymin)/(plot_points[1]-1)][0:plot_points[1]] zpoints = [zmin..zmax, step=float(zmax-zmin)/(plot_points[2]-1)][0:plot_points[2]] points = [vector((i,j,k)) for i in xpoints for j in ypoints for k in zpoints] vectors = [vector((ff(*point), gg(*point), hh(*point))) for point in points] # scale the vectors max_len = max(v.norm() for v in vectors) scaled_vectors = [v/max_len for v in vectors] if center_arrows: return sum([plot(v,**kwds).translate(p-v/2) for v,p in zip(scaled_vectors, points)]) else: return sum([plot(v,**kwds).translate(p) for v,p in zip(scaled_vectors, points)])
var('x,y,z') plot_vector_field3d( (-x+y,-y*z+1,z), (x,-3,3), (y,-3,3),(z,-3,3),thickness=2)

Parametric surfaces

You can see many examples of parametric plots in the Sage documentation, either by typing "parametric_plot3d?" in a cell, or going here: http://sagemath.org/doc/reference/sage/plot/plot3d/parametric_plot3d.html#sage.plot.plot3d.parametric_plot3d.parametric_plot3d

k = 1.2; k_2 = 1.2; a = 1.5 f = (k^u*(1+cos(v))*cos(u), k^u*(1+cos(v))*sin(u), k^u*sin(v)-a*k_2^u)
parametric_plot(f, (u,0,6*pi), (v,0,2*pi), plot_points=[40,40], texture=(0,0.5,0),mesh=True)
u, v = var('u,v') fx = cos(u)*sin(2*v) fy = sin(u)*sin(2*v) fz = sin(v) parametric_plot3d([fx, fy, fz], (u, 0, 2*pi), (v, 0, 2*pi), frame=False, color="green",mesh=True)