Sage Quickstart for Multivariable Calculus

This Sage worksheet was developed for the MAA PREP Workshop "Sage: Using Open-Source Mathematics Software with Undergraduates" (funding provided by NSF DUE 0817071).

 

Vector Calculus

In Sage, vectors are primarly linear algebra objects, but they are slowly becoming simultaneously analytic continuous functions. (Mostly due to the efforts of Jason Grout.)

Dot Product, Cross Product

{{{id=2| v = vector(RR, [1.2, 3.5, 4.6]) w = vector(RR, [1.7,-2.3,5.2]) v*w /// }}} {{{id=1| v.cross_product(w) /// }}}

Lines and Planes

Intersect $x-2y-7z=6$ with $\frac{x-3}{2}=\frac{y+4}{-3}=\frac{z-1}{1}$

{{{id=6| # designed with intersection at t = 2, i.e. (7, -10, 3) var('t, x, y') line = parametric_plot3d([2*t+3, -3*t-4, t+1], (t, 0, 4),color='red') plane = plot3d((1/5)*(-12+x-2*y), (x, 4, 10), (y, -13,-7), opacity=0.5) intersect=point3d([7,-10,3],color='black') line+plane+intersect /// }}}

Vector-Valued Functions

{{{id=9| var('t') r(t)=(2*t-4, t^2, (1/4)*t^3) /// }}} {{{id=10| rprime(t)=list(r.diff(t)) /// }}} {{{id=13| rprime.norm() /// }}} {{{id=11| velocity(t)=list((1/rprime.norm())*rprime) /// }}} {{{id=12| velocity(4) /// }}}

Notice: the syntax is not  numerical_integral(f(t), (t, 0, 1)).

{{{id=14| arc_length = numerical_integral(rprime.norm(), 0,1) arc_length /// }}}

Functions of Several Variables

{{{id=33| # import matplotlib.cm; matplotlib.cm.datad.keys() # 'Spectral', 'summer', 'blues' g(x,y)=e^-x*sin(y) contour_plot(g, (x, -2, 2), (y, -4*pi, 4*pi), cmap = 'Blues', contours=10, colorbar=True) /// }}}

Partial Differentiation

{{{id=18| # Hass, Weir, Thomas, University Calculus, Exercise 12.7.35 # # Local min at (4, -2) # var('x y') f(x, y) = x^2 + x*y + y^2 - 6*x + 2 /// }}} {{{id=25| fx(x,y)= f.diff(x) fy(x,y) = f.diff(y) print fx print fy /// }}} {{{id=26| f.gradient() /// }}} {{{id=27| solve([fx==0, fy==0], (x, y)) /// }}} {{{id=28| H = f.hessian() H /// }}} {{{id=29| print "f_xx: ", H(4,-2)[0,0] print "D: ", H(4,-2).det() /// }}}

Multiple Integration

Vvolume under $f(x,y)=x^2y$ above the triangle bounded by $y=0$, $x=3$ and $y=4x$.

{{{id=39| var('x y') f(x,y)=x^2*y /// }}} {{{id=37| # dy dx integrate( integrate(f(x,y), (y, 0, 4*x)), (x, 0, 3) ) /// }}} {{{id=35| # dx dy integrate( integrate(f(x,y), (x, y/4, 3)), (y, 0, 12) ) /// }}} {{{id=31| var('u v') surface = plot3d(f(x,y), (x, 0, 3.2), (y, 0, 12.3), color = 'blue', opacity=0.3) domain = parametric_plot3d([3*u, 4*(3*u)*v,0], (u, 0, 1), (v, 0,1), color = 'green', opacity = 0.75) image = parametric_plot3d([3*u, 4*(3*u)*v, f(3*u, 12*u*v)], (u, 0, 1), (v, 0,1), color = 'green', opacity = 1.00) surface+domain+image /// }}}