All published worksheets from http://sagenb.org
Image: ubuntu2004
Sage Calculus Commands
Tim McLarnan, Earlham College
Although our initial Introduction to Sage included discussion of a number of Sage functions related to calculus, you may not have noticed that fact if you didn't look carefully at what Sage can do. This handout is, in the first instance, just a summary of Sage's calculus-related functionality. As always, I'm only scratching the surface. The Sage help command (say derivative? to get help on derivative, for instance) offers a lot more information on any of the commands used in this Worksheet.
Basic Commands
Limits
Sage's command for limits is, logically enough, limit. Notice how Sage handles the limits below, distinguishing among infinite limits, undefined limits, and indeterminate limits for functions that are still locally bounded. Notice, also that Sage gets one of these limits wrong.
Our investigation of derivatives could have been speeded up substantially had we been willing to use Sage uncritically as a black box for taking limits. Here, for instance, are calculations of the derivatives of the sine, exponential, and cube root functions. It would be useful review to try to remember how we obtained these results in class. Are you sure Sage doesn't have bugs that affect these calculations?
These calculation show an annoyance in Sage: In order to compute some limits, it needs to know whether various quantities are positive and whether they are integers, even though the result of the calculation doesn't depend on the answers to these questions. We therefore end up having to use Sage's assume() function to give it various choices of sign and type for numbers.
Here's one last limit, just for fun. You might think about how to do it by hand.
Derivatives
Sage can also compute derivatives, either of expressions or of functions. derivative(), differentiate(), and diff() are three different names for the same function (or method) for doing this.
Sage can even handle differentiation rules for arbitrary functions, like this:
Integrals
Sage can also compute integrals, naturally using the function (or method) integral().
Big deal so far, but here's one I bet you can't do by hand, .
Ah, this is good! Sage should ask whether or not . After all, it makes a difference:
Sums
Sums were originally done in Sage with slightly unusual notation. [1..10] means the list [1,2,3,4,5,6,7,8,9,10]. For convenience, range(n) is the list of integers such that . (Watch the endpoints here!)
Sage now has a nicer notation for sums.
Sage can also handle sums with variable or infinite endpoints. Here's how to get Sage to compute the sums
$$
\sum_{k=1}^nk,\quad
\sum_{k=1}^nk^2,\quad
\sum_{k=1}^nk^3,\quad
\sum_{k=1}^\infty\frac1{k^2},\quad
\sum_{k=1}^\infty\frac1{k^4},\quad\text{and}\quad
\sum_{k=1}^\infty\frac1{k^3}.
$$
Sage can compute integrals using Riemann sums, though again, we have to use Maxima and we fall afoul of some problems with the Maxima interface.
The right hand Riemann sum for computing the integral
$$
\int_0^xt^3\,dt
$$
is
$$
\sum_{k=1}^n\left(\frac{kx}n\right)^3\cdot\frac xn,
$$
which Maxima can write and evaluate.
We now want to take the limit of this as in order to get the integral, right?
Bingo! So
$$
\int_0^xt^3\,dt=\frac{x^4}4,
$$
just as we would have suspected.
More Complicated Tools
As one learns to work with Sage, one finds more and more ways to visualize and to work with the ideas of calculus. Here, for instance, is a little interactive tool for illustrating the idea of the derivative of a function as the slope of the tangent line. The blue curve is the function , the dotted black curve is its derivative . Moving the slider moves a point along the curve and the tangent line at that point. As the tangent line moves, the green dot on the derivative curve traces out its slope.
And here are two functions for working with Riemann sums. right_riemann_plot plots a curve and its right-hand Riemann sum, and right_riemann_value gives the value of this sum.
Finally, here's a function for computing formal Riemann sums. At least for simple functions, we end up with expressions where Sage can take the limit to evaluate the integral, though notice that occasionally Sage wants to do something more omplex than we think it should.
Some Calculus Problems
As far as I can tell at this point, Sage doesn't have inert integrals like Maple, so some of the exercises in the Maple version of this worksheet which involve doing explicit changes of variable in inert integrals aren't so easy in Sage. Others of our exercises from the Maple version are easy. For instance, we can explicitly explore Taylor polynomials for the sine function by equating derivatives of with derivatives of a generic polynomial at , and we can then interactively plot the first few Taylor polynomials.