This Sage document is one of the tutorials developed for the MAA PREP Workshop “Sage: Using Open-Source Mathematics Software with Undergraduates” (funding provided by NSF DUE 0817071). It is licensed under the Creative Commons Attribution-ShareAlike 3.0 license (CC BY-SA).
This tutorial has the following sections. The first three are based on the topics encountered in a typical three-semester calculus sequence in the United States; the final section is a checkpoint of sorts.
The tutorial assumes that one is familiar with the basics of Sage, such as outlined in the previous tutorials.
For a refresher, make sure the syntax below for defining a function and getting a value makes sense; then evaluate the cell by clicking the “evaluate” link, or by pressing Shift-Enter (hold down Shift while pressing the Enter key).
sage: f(x)=x^3+1 sage: f(2) 9
We’ll use this function several times in this tutorial, so it’s important that it is evaluated!
The calculus is amazing not so much because it solves problems of tangency or area - individual solutions to these problems have been known before. It is amazing because it gives a remarkably comprehensive set of rules for symbolic manipulation for solving such problems in great generality!
Thus, the most typical use of a computer system like Sage in this context is simply to help check (or make less tedious) basic symbolic manipulation of things like derivatives. Let’s first show what our function \(f\) is again, then do things with it.
Something most (but not all) curricula for first-semester calculus do is spend a fair amount of time with limits.
What is the limit of this function as \(x\) approaches \(1\)?
sage: lim(f,x=1) x |--> 2
Sage gives the answer we expect. The syntax for limits is pretty straightforward, though it may differ slightly from that of other systems.
Since a limit may exist even if the function is not defined, Sage uses the syntax x=1 to show the input value approached in all situations.
sage: lim((x^2-1)/(x-1),x=1) 2
Next, we’ll return to the original function, and check the two directional limits.
The syntax uses the extra keyword dir.
- This is basically because otherwise we might have done something like let right=55 earlier, so instead Sage - and Python - requires us to put 'right' and '-' in quotes to make it clear they aren’t variables.
sage: lim(f,x=1,dir='-'); lim(f,x=1,dir='right'); f(1) x |--> 2 x |--> 2 2
By comparing with \(f(1)\), we see that \(f(x)\) is continuous at \(x=1\).
This cell also reminds us of something else:
What we spend the most time on in Calculus 1 is derivatives, and Sage is fully-featured here.
For example, here are three ways to get the basic, single-variable derivative of \(f(x)=x^3+1\).
sage: diff(f,x); derivative(f,x); f.derivative(x) x |--> 3*x^2 x |--> 3*x^2 x |--> 3*x^2
Naturally, Sage knows all of the derivatives you want.
sage: derivative(sinh(x^2+sqrt(x-1)),x) 1/2*(4*x + 1/sqrt(x - 1))*cosh(x^2 + sqrt(x - 1))
And maybe even knows those you don’t want. In this case, we put the computation inside show() since the output is so long.
A common question is why Sage might not check automatically if there is some “simpler” version. But simplifying an expression, or indeed, even defining what a ‘simple’ expression is, turns out to be a very hard technical and mathematical problem in general. Computers won’t solve every problem!
As a brief interlude, let’s consider an application of our ability to do some basic differential calculus.
First, as a reminder of plotting from the previous tutorial, try to plot the function \(f(x)\), together with its tangent line at \(x=1\), in the empty cells below. (If you can, do it without looking at the previous tutorial for a reminder.)
Did you get it?
Of course, in general we might want to see tangent lines at lots of different points - for instance, for a class demonstration.
So in the following cell, there are several auxiliary elements:
Finally, we plot everything together in the last line by adding \(P+Q\).
sage: P=plot(f,(x,-1,1)) sage: c=1/3 sage: fprime=derivative(f,x) sage: L(x)=fprime(c)*(x-c)+f(c) sage: Q=plot(L,(x,-1,1), color="red", linestyle="--") sage: P+Q Graphics object consisting of 2 graphics primitives
You may want to experiment by
Ideally, it would be extremely easy to change that parameter \(c\). In the cell below, we show our second example of a Sage “interact” (or “Sagelet”).
In this one, dragging a slider will show the tangent line moving.
sage: %auto sage: f(x)=x^3+1 sage: @interact sage: def _(c=(1/3,(-1,1))): ... P=plot(f,(x,-1,1)) ... fprime=derivative(f,x) ... L(x)=fprime(c)*(x-c)+f(c) ... Q=plot(L,(x,-1,1),color="red", linestyle="--") ... show(P+Q+point((c,f(c)), pointsize=40, color='red'),ymin=0,ymax=2)
A very sharp-eyed reader will also have noticed that the previous cell had %auto at the very top, and that it was not necessary to evaluate the cell to use it.
A final topic in Calculus 1 usually is basic integration. The syntax for indefinite integration is similar to that for differentiation.
sage: integral(cos(x),x) sin(x)
We don’t get the whole indefinite integral, just a convenient antiderivative.
Definite integration has similar syntax to plotting.
sage: integral(cos(x),(x,0,pi/2)) 1
Second-semester calculus is typically more challenging.
Nonetheless, Sage can handle this as well.
Sage includes a large number of indefinite integrals (via Maxima), though not all the ones you will find in a comprehensive table.
sage: h(x)=sec(x) sage: h.integrate(x) x |--> log(sec(x) + tan(x))
Since I defined h as a function, the answer I get is also a function. If I just want an expression as the answer, I can do the following.
sage: integrate(sec(x),x) log(sec(x) + tan(x))
Here is another (longer) example. Do you remember what command would help it look nicer in the browser?
sage: integrate(1/(1+x^5),x) 1/5*sqrt(5)*(sqrt(5) + 1)*arctan((4*x + sqrt(5) - 1)/sqrt(2*sqrt(5) + 10))/sqrt(2*sqrt(5) + 10) + 1/5*sqrt(5)*(sqrt(5) - 1)*arctan((4*x - sqrt(5) - 1)/sqrt(-2*sqrt(5) + 10))/sqrt(-2*sqrt(5) + 10) - 1/2*(sqrt(5) + 3)*log(2*x^2 - x*(sqrt(5) + 1) + 2)/(5*sqrt(5) + 5) - 1/2*(sqrt(5) - 3)*log(2*x^2 + x*(sqrt(5) - 1) + 2)/(5*sqrt(5) - 5) + 1/5*log(x + 1)
Some integrals are a little tricky, of course. If Sage doesn’t know the whole antiderivative, it returns as much of it as it (more properly, as Maxima) could do.
sage: integral(1/(1+x^10),x) 1/5*arctan(x) - 1/5*integrate((x^6 - 2*x^4 + 3*x^2 - 4)/(x^8 - x^6 + x^4 - x^2 + 1), x)
sage: integral(sinh(x^2+sqrt(x-1)),x) # long time (15s on sage.math, 2012) integrate(sinh(x^2 + sqrt(x - 1)), x)
This last one stumps other systems too.
However, if there is a special function which helps compute the integral, Sage will look for it. In the following case there is no elementary antiderivative, but the erf function helps us out.
sage: integral(e^(-x^2),x) 1/2*sqrt(pi)*erf(x)
Don’t forget, if this function is unfamiliar to you (as it might be to students trying this integral), Sage’s contextual help system comes to the rescue.
There are several ways to do definite integrals in Sage.
The most obvious one is simply turning
as indicated in the Calculus I section.
sage: integral(cos(x),(x,0,pi/2)) 1
The preferred syntax puts the variable and endpoints together in parentheses.
Just like with derivatives, we can visualize this integral using some of the plotting options from the plotting tutorial.
sage: plot(cos(x),(x,0,pi/2),fill=True,ticks=[[0,pi/4,pi/2],None],tick_formatter=pi) Graphics object consisting of 2 graphics primitives
It is possible to be completely symbolic in doing integration. If you do this, you’ll have to make sure you define anything that’s a symbolic variable - which includes constants, naturally.
sage: var('a,b') (a, b) sage: integral(cos(x),(x,a,b)) -sin(a) + sin(b)
On the numerical side, sometimes the answer one gets from the Fundamental Theorem of Calculus is not entirely helpful. Recall that \(h\) is the secant function.
sage: integral(h,(x,0,pi/8)) 1/2*log(sin(1/8*pi) + 1) - 1/2*log(-sin(1/8*pi) + 1)
Here, just a number might be more helpful. Sage has several ways of numerical evaluating integrals.
The first one, using the n or N function for numerical approximation, was also mentioned in the introductory tutorial.
sage: N(integral(h,(x,0,pi/8))) 0.403199719161512
The second function, numerical_integral, uses a powerful numerical program (the GNU Scientific Library).
sage: numerical_integral(h,0,pi/8) (0.4031997191615114, 4.476416117355069e-15)
To access just the number, one asks for the ‘zeroth’ element of this sequence of items. This is done with the following bracket notation.
sage: numerical_integral(h,0,pi/8) 0.4031997191615114
Notice that we began counting at zero. This is fairly typical in computer programs (though certainly not universal).
To aid readability (more important than one might think), we often assign the numerical integral to a variable, and then take the zeroth element of that.
sage: ni = numerical_integral(h,0,pi/8) sage: ni 0.4031997191615114
Finally, the .nintegrate() method from Maxima gives even more extra information.
sage: h(x).nintegrate(x,0,pi/8) (0.4031997191615114, 4.47641611735507e-15, 21, 0)
Second-semester calculus usually also covers various topics in summation. Sage can sum many abstract series; the notation is similar to plotting and integration.
sage: var('n') # Don't forget to declare your variables n sage: sum((1/3)^n,n,0,oo) 3/2
This is the geometric series, of course.
The next one is the famous result that a row of Pascal’s triangle is a power of 2 -
which has many pleasing combinatorial interpretations.
sage: k = var('k') # We already declared n, so now we just need k sage: sum(binomial(n,k), k, 0, n) 2^n
Do you remember what to do to see how we typed the nice sum in the text above? That’s right, we can double-click the text area/cell to see this.
Sage also can compute Taylor polynomials.
Taylor expansions depend on a lot of things. Whenever there are several inputs, keeping syntax straight is important. Here we have as inputs:
In the next cell, we call \(g(x)\) the Taylor polynomial in question.
sage: g(x)=taylor(log(x),x,1,6); g(x) -1/6*(x - 1)^6 + 1/5*(x - 1)^5 - 1/4*(x - 1)^4 + 1/3*(x - 1)^3 - 1/2*(x - 1)^2 + x - 1
Notice how close the approximation is to the function on this interval!
sage: plot(g,(x,0,2))+plot(log(x),(x,0,2),color='red') Graphics object consisting of 2 graphics primitives
We have already seen three-dimensional plotting, so it is not surprising that Sage has support for a variety of multivariable calculus problems.
We will often need to define all variables other than \(x\).
sage: var('y') y sage: f(x,y)=3*sin(x)-2*cos(y)-x*y
Above, we have defined a typical function of two variables.
Below, we use the separating semicolons to demonstrate several things one might do with such a function, including:
sage: f.gradient(); f.hessian(); f.diff(x,y) (x, y) |--> (-y + 3*cos(x), -x + 2*sin(y)) [(x, y) |--> -3*sin(x) (x, y) |--> -1] [ (x, y) |--> -1 (x, y) |--> 2*cos(y)] (x, y) |--> -1
In an effort to make the syntax simpler, the gradient and Hessian are also available by asking for a total derivative. We also ask for nicer output again.
sage: show(f.diff()); show(f.diff(2))
If we take the determinant of the Hessian, we get something useful for evaluating (the two-dimensional) critical points of \(f\).
These ideas are particularly helpful if one wants to plot a vector field.
The following example is of the gradient. The vector plotted in the cell below is the unit vector in the direction \((1,2)\).
sage: P=plot_vector_field(f.diff(), (x,-3,3), (y,-3,3)) sage: u=vector([1,2]) sage: Q=plot(u/u.norm()) sage: P+Q Graphics object consisting of 2 graphics primitives
Rather than actually figure out the unit vector in that direction, it’s easier to let Sage compute it by dividing the vector by its norm.
The directional derivative itself (in that direction, at the origin) can also be computed in this way.
sage: (f.diff()*u/u.norm())(0,0) 3/5*sqrt(5)
Another useful type of plot in these situations is a contour plot.
Notice that the one below uses several options. Try to correlate the options with features of the graphic.
sage: y = var('y') sage: contour_plot(y^2 + 1 - x^3 - x, (x,-pi,pi), (y,-pi,pi),\ ....: contours=[-8,-4,0,4,8], colorbar=True, labels=True, label_colors='red') Graphics object consisting of 1 graphics primitive
In this one, we have used options to:
(Incidentally, the True and False valued options are some of the few non-numerical ones that do not need quotes.)
This is another good time to remind us we must explicitly ask for \(y\) to be a variable here, as will be the case a few more times.
As you gain experience in Sage, we will slowly explain less and less of the syntax of commands in these tutorials. You can think of places where not everything is explained as a mini-quiz.
For example, the next example shows how one currently does a multiple integral. What have we done here?
sage: integrate(integrate(f,(x,0,pi)),(y,0,pi)) 6*pi - 1/4*pi^4
Answer: notice that integrate(f,(x,0,pi)) has been itself placed as the function inside integrate(...,(y,0,pi)).
We could use a 3D plot to help visualize this; these were already mentioned in the symbolics and plotting tutorial.
sage: plot3d(f,(x,0,pi),(y,0,pi),color='red')+plot3d(0,(x,0,pi),(y,0,pi)) Graphics3d Object
In addition to multivariate calculus, Calculus 3 often covers parametric calculus of a single variable. Sage can do arbitrary parametric plots, with fairly natural syntax.
This plot shows the tangent line to the most basic Lissajous curve at \(t=1\). The commands should be strongly reminiscent of the ones at the beginning of this tutorial.
sage: t = var('t') sage: my_curve(t)=(sin(t), sin(2*t)) sage: PP=parametric_plot( my_curve, (t, 0, 2*pi), color="purple" ) sage: my_prime=my_curve.diff(t) sage: L=my_prime(1)*t+my_curve(1) # tangent line at t=1 sage: parametric_plot(L, (t,-2,2))+PP Graphics object consisting of 2 graphics primitives
Before moving out of the calculus world, it is good to have a sort of miniature exam.
In the cell below, we have plotted and given:
We assume you have never seen several of the commands before. Can you nonetheless figure out which commands are doing each piece, and what their syntax is? How would you look for help to find out more?
sage: y = var('y') sage: Plot1=plot_slope_field(2-y,(x,0,3),(y,0,20)) sage: y = function('y',x) # declare y to be a function of x sage: h = desolve(diff(y,x) + y - 2, y, ics=[0,7]) sage: Plot2=plot(h,0,3) sage: show(expand(h)); show(Plot1+Plot2)
Ready to see the answers? Don’t peek until you’ve really tried it.
In this cell we do the following:
- Notice we have here once again used # to indicate a comment.
- In this case, in order to use common terminology, we now have told Sage y is no longer a variable, but instead a function (abstract) of the variable x.
As you gain experience, you will see how to glean what you are looking for from examples in the documentation like this - which is one of the real goals of these tutorials.
Congratulations! You are now armed with the basics of deploying Sage in the calculus sequence.