All published worksheets from http://sagenb.org
Image: ubuntu2004
Using Sage for Calculus
Contents
1.1 Numbers
1.2 Variables and Expressions
1.3 Substitution
1.4 Solving Equations
2 Plotting
2.1 Functions of one variable
2.2 Functions of two variables
2.3 Polar coordinates; parametric functions
3 Calculus
3.1 Limits
3.2 Differentiation
3.3 Implicit Differentiation
3.4 Integration
4 Getting Help
Introduction
Sage is a powerful mathematical calculator, often called a computer algebra system (CAS) or symbolic mathematics program, because unlike most calculators it can do algebra and calculus symbolically. For example, it can expand the product into the normal representation of this polynomial, and it can factor the result back into the original form. It can compute the derivative or antiderivative of . It can also serve as a numerical calculator, but again unlike most calculators it can do integer and rational number arithmetic exactly, no matter how many digits are involved. If it is undesirable or impossible to express a result exactly, Sage can do numerical approximation. Finally, Sage can plot curves and surfaces in two and three dimensions.
To learn how to use some of Sage’s features, you should work through this tutorial step by step. As you go, you may alter the Sage commands and type new ones to experiment; you will learn better and faster if you do this. However, some commands depend on the results of previous commands, so before you continue you may want to restore the original calculation. A good alternative is to experiment in a new command window, called a cell in Sage. If you hover your mouse just a bit above an existing cell you will see a blue bar appear; click on this bar and a new cell appears. The blocks of plain text between command cells are themselves cells, and you can open new command cells above them in the same way.
Sage remembers values from one cell to the next, but if you don’t execute any commands for a while these values are forgotten. You may have to return to an earlier cell and re-execute it to refresh a value.
1 Algebra
1.1 Numbers
To evaluate the contents of a command cell, click in the box; the outline should turn blue. Then hold down the shift key and press return. This takes some getting used to; if you press just return, Sage will simply start a new line. When this happens, just backspace and then press shift-return. If you prefer, you can click on the word Evaluate below the box.
Try evaluating the cell below. Then hover above the box and open a new cell. Then hover between the original cell and the following text (“You may type more than one command…”) and open a new cell there as well. Try evaluating some simple arithmetic expressions using “”, “”, “”, and “”.
You may type more than one command in a window by placing semicolons between them.
The ordinary precedence rules apply to mathematical operations: multiplication and division are done first, then addition and subtraction. Multiplication and division have the same precedence, as do addition and subtraction; a sequence of mixed multiplications and divisions is evaluated left to right, and likewise for additions and subtractions. Make sure you understand these:
You may of course use parentheses to alter the order of operations. Sometimes using extra parentheses is a good idea because it makes it easier to read and understand an expression.
Sage will do arithmetic with rational numbers without converting to decimal form:
Sage knows standard mathematical notation, including two symbols for exponentiation. The underscore character refers to the previous result.
You can force Sage to approximate values using decimal notation.
Sage understands , , and (the square root of ).
Sage’s normal output can be a bit hard to read when it is complicated, though it becomes easier with practice, and it can be useful for copying results and pasting them in to new commands. At the top of the worksheet is a checkbox labeled Typeset. Check the box and then evaluate the previous cell; the output will be more familiar. Alternately, you may use the jsmath function to typeset a single result. Uncheck the Typeset box at the top of the page and then execute the next box.
The function is an alternate form of .
1.2 Variables and Expressions
Sage can manipulate expressions involving variables as well as numbers. Variables can be assigned values, either actual numeric values or entire expressions.
For readability you can also put different commands on different lines; just press return without the shift at the end of the line. Note that only the last result will be displayed in this case, so generally you use this when the other lines produce no output.
We normally think of as a function, but it is not officially a function.
So that gives the result you expect, but only after a warning. Here’s how to make an official function:
You should get in the habit of defining functions this way. You can see the value of by evaluating it:
This notation indicates that Sage thinks of as a function, not merely an expression. Compare to this:
If you have a mere expression that you would like to be a function, you have a couple of options. You can redefine it, but the expression you have might be the result of many calculations, not a simple definition. To turn the previous into a function you can do this:
There’s no need to use a different name if you don’t need the original one:
Here’s something a little more realistic:
You may need to apply a function to a list of input values; here is something a bit easier than doing each one separately:
In ordinary mathematical notation you are used to leaving out some multiplication symbols; Sage does not allow this:
Type an asterisk between the 2 and the and evaluate that cell again.
We saw above how to expand and factor polynomials. If a polynomial has integer or rational coefficients, the “factor” command tries to factor it into polynomials with integer coefficients, perhaps times a rational number:
If Sage can’t do this, it leaves the polynomial alone, even if it has a “nice” factorization:
1.3 Substitution
We use “variables” in a variety of ways, to stand for “true variables”, for functions, or for unspecified constants. We might say, for example, , using all of these. If we try this, there is already a problem:
Although Sage understands that is a variable, we must tell it explicitly about others:
We can manipulate this as you would expect:
Notice that strangely the result of expand is a function but the result of factor is not!
We may then want to assign the constant a value. We can do this temporarily like this:
So we see that the definition of has not changed, it still contains the symbol . This happens even if we give a permanent value:
This is probably a bad idea, since it is at least confusing. If you would like a new function with replaced by 2 you can do something like this:
Let’s restore to its status as a variable with no value:
You can subsitute expressions for variables, not just values.
Make sure you understand how is defined. This substitution method works quite generally on expressions that may not have names:
1.4 Solving Equations
Sage will solve a limited class of equations exactly and a broader class approximately. The solve command will give exact answers if possible, including complex answers.
If you specify just a function, Sage assumes you want to set it equal to zero and solve.
Even when Sage can solve the equation exactly, you may want to see numerical approximations. You can use the n function on each solution, or with a bit of programming do them all at once. First we generate the solutions in a different form:
Then we can extract just the numerical values:
Then use the map function to apply n to each number on the list:
Or we could combine the last two steps:
If Sage can’t solve an equation it will simply reprint the equation. You can approximate a solution, but you have to limit the search to a range, which you might discover by graphing the function or by substituting some values into the function. For example, using , you might see that and , so there must be a root between and .
Sage will also solve systems of equations; note the square brackets forming a list of equations.
A bit more involved:
2 Plotting
2.1 Functions of one variable
Sage can do two and three dimensional graphing. There are many ways to adjust the plots that Sage produces; we’ll cover just the basics here.
It’s easy to plot ordinary one-variable functions.
The vertical scale makes it hard to see what’s going on in that graph; you can limit the vertical scale for a better look, using “ymin” and “ymax”.
Some functions have vertical asymptotes, but Sage will just connect the dots:
You can get rid of the spurious bit in the middle:
You can also put a list of excluded values in the brackets; try plotting between and .
If you right-click on a plot you can download it as a png image.
You can put multiple plots on the same axes:
It’s often better to do the plots separately but display them together:
Sage normally uses different scales on the two axes; you can force it to use the same scales to see the “true shape”.
Here’s a better example: the line doesn’t look like it has slope 1 in the first graph.
2.2 Functions of two variables
Sage will plot functions of two variables in 3D. You can rotate the resulting graph with the mouse: click and hold, then drag. You can change the appearance from a menu that appears when you right-click on the graph. Note especially the stereographic option—try the cross-eyed version. Click on the “Get Image” link for a view that you can download.
Sage will do parametric plots in two or three dimensions.
Plots in polar coordinates are really special examples of parametric plots.
You can also do parametric surface plots:
You may notice that the edges of this surface appear more polygonal than round; you can change the number of points in either direction on the plot. The direction is the one we want to increase; the direction is along radial lines, so a very small value is fine.
Here’s a fancier plot: a sphere inside a cylinder, and the cylinder is partially transparent.
Note that Sage has a bit of trouble where the cylinder and the sphere meet. You can fix this by cheating a little: make the cylinder just slightly bigger.
You can plot quite complicated surfaces using multiple parametric plots together. The intersection of three cylinders of the same size at right angles to one another forms a solid. Note that we have plotted each color as four surfaces when in fact one would do; try plotting just the red surface with a single plot, using the range and see what happens; can you explain what has gone wrong? You might try adding the original cylinders to this plot, colored appropriately, and partially transparent. First we set up some useful functions.
Now notice that each of the colors is essentially the same, with the coordinates plotted against different axes for the different colors.
And everyone’s favorite non-trivial limit:
You can specify one-sided limits in two ways.
3.2 Differentiation
Here’s a typical easy calculus problem: find a derivative, find critical values, find the -coordinates for each.
3.3 Implicit Differentiation
Sage has no built-in implicit differentiation command, but we can do it using differentiation, in essentially the way we do it by hand. We tell Sage that is a function of , then proceed essentially as usual. Here is the standard example computing for defined by . Note that we convert this to , which guarantees that the derivative of the right side is zero. (In this case the derivative of the right side, 1, was already zero, but in general it might be more complicated.)
3.4 Integration
The “integral” command computes an antiderivative. Here, we take the derivative to check the antiderivative.
If Sage can’t handle an integral, it does nothing.
We could of course do definite integration by substituting and subtracting, but the integral command will also do it.
Sage can do some improper integrals.
The function doesn’t have a “nice” antiderivative, though it can be expressed in terms of the “erf” function. For most values of , erf(x) can only be approximated, but an improper integral turns out to be quite nice. (The function is a Gaussian function, a “bell curve”.)
Sometimes the result is mysterious.
Sage can’t find antiderivatives of some functions. In such cases you can approximate the value of a definite integral using the function.
4 Getting Help
If you click on “Help” at the top of the Sage window, you will find a number of options. There is a tutorial that is longer and more complete than this introduction, and there is a comprehensive reference manual. If you know the command you want help with you can enter “commandname?” in a command box and execute it. Google can also find helpful pages; generally you should search for “sagemath” plus some description of the topic you’re interested in.