Sage is a powerful set of computational tools that allow you to use the computer for various applications in
This semester, we will just begin to learn some of the many features of Sage, and we will use Sage to help us understand calculus better.
This tutorial will introduce you to the basics of Sage.
There are two regions in a Sage worksheet: input cells and output cells.
Output cells have a green, vertical line next to them, while input cells do not. You type commands in an input cell, and you run them by pressing [SHIFT]-[ENTER] (or press the play button [with the triangle] at the top of the page). To get a new input cell, move your cursor to the gray horizontal line above or below any other input cell. A blue highlight will appear. Click and another input cell will be inserted. Try it now.
An output cell will appear when you press [SHIFT]-[ENTER]. If any commands you have typed in the input cell produce an output, this will appear right below the input cell. If you click and drag, you can highlight multiple output cells and run them all by pressing [SHIFT]-[ENTER] once.
If an input cell has been run in the current session, then a small portion of the gray horizontal line above it will be blue. While input code is running, part of the horizontal line above it will flash green. If input code is waiting to run, part of the line will flash red.
Sage can be used very much like your graphing calculator to do computations. Simply type an arithmetic expression in an input cell and press [SHIFT]-[ENTER]. Here are some examples.
Exponents:
The same symbol is used for subtraction and negative numbers:
You can type multiple inputs before running.
One way to do this is to hit [ENTER] after each line.
The outputs are produced in the same order as the inputs.
Another way to do this is to put a semicolon between different inputs.
If you want to access the last output, use _
In the example above, _ $=12$, since that was the last output.
In this example, _ $=2$, since that was the last output.
Like a calculator, Sage uses the standard order of operations, often called PEMDAS (parentheses, exponents, multiplication, division, addition, subtraction). Compare the following:
$2\cdot4^5+3=2\cdot1024+3=2048+3=2051$
$(2\cdot4)^5+3=8^5+3=32768+3=32771$
$2\cdot(4^5+3)=2\cdot(1024+3)=2\cdot1027=2054$
$2\cdot4^{5+3}=2\cdot4^8=2\cdot65536=131072$
Be careful with fractions:
$\displaystyle\frac{12}{3}+5=9$
$\displaystyle\frac{12}{3+5}=\frac{3}{2}$
Complete Question 1 of the assignment.
Unlike calculators, Sage tries to return exact answers instead of decimal approximations. If the input involves only integers, then Sage tries to keep integers in the output as well. For example, since 11 divided by 4 is not a whole number, Sage doesn't do anything:
Since 11 and 4 are both integers, Sage tries to keep everything as integers, so it returns the fraction $\frac{11}{4}$. If you want a decimal instead, Sage has a decimal approximation function called "N" (short for "numerical_approximation").
Compare the following:
This issue does not arise if you don't start with integers. For example 3.1/4.6 returns a decimal approximation:
For numbers that are very large or very close to $0$, Sage employs scientific notation.
Recall, for example, $1.5\times10^4=15000$ and $1.5\times10^{-4}=0.00015$.
In Sage, scientific notation is indicated using the letter "e" (for "exponent"). This is similar to graphing calculators that use "E."
Here we'll see the examples of $1.5\times10^{25}$ and $1.5\times10^{-25}$.
This notation is similar to many graphing calculators.
When the number after the "e" is postive, move the decimal point that many spots to the right (adding extra zeros as necessary).
When the number after the "e" is negative, move the decimal point that many spots to the left (adding extra zeros as necessary).
Consider the example below.
At first, this looks like a number around $1\frac{1}{4}$. But this is actually a number close to $0$:
$1.25325428941968\textrm{e-}17 =1.25325428941968\times 10^{-17}=0.0000000000000000125325428941968\approx 0$.
Caution: This use of the letter "e" has nothing to do with the number $e\approx2.71828$ (the base of the natural logarithm).
Sage includes the standard functions you learned in precalculus, including trigonometric, exponential, and logarithmic functions.
The Sage command for square root is sqrt.
Note that Sage keeps it exact using integers. To convert to a decimal, use N().
However, notice that if you start with a decimal, Sage will output a decimal.
Compare with the following:
If you want a root other than the square root, use a fractional power. For example, here's a cube root (note the parentheses).
Don't forget the parentheses around 1/3. This is not the same as 2^1/3.
Here's a fourth root.
Notice how Sage keeps it exact again.
Note that pi = $\pi$.
Another note: Sage assumes all angles are measured in radians.
In the example below, note the 5*pi/4 - the multiplication must be explicit - Sage will not accept 5pi/4.
Notice that Sage knows many exact values for the trig functions - even some that you did not memorize in precalc.
These answers are kind of hard to read. You can make them look nicer by using the "show" command.
Sage uses arcsin, arccos, and arctan, not $\sin^{-1}$, $\cos^{-1}$, and $\tan^{-1}$.
Let's convert that last one to a decimal. Remember that _ accesses the last output.
For exponential functions, just use ^ as we did above.
The built-in function "exp" is the exponential base e; this is equivalent to "e^" (recall, $e\approx2.71828$).
Notice Sage's use of exact values.
In Sage, the function "log" is the logarithm base e, not base 10 as on your calculator.
Sage can also handle logarithms with other bases: log(x,b) = $\log_b x$
Here's $\log_{10}10^3=3$
Here's $\log_2 1800\approx 10.81$. Notice Sage gives an exact answer in terms of log unless you ask otherwise.
When we write mathematical expressions, we often omit the multiplication symbol. For example, when we write $10\cos(2)$ we mean $10\cdot\cos(2)$.
When we write $3(4+8)$ we mean $3\cdot(4+8)$.
Implicit multiplication like in the above examples is not allowed in Sage. Every multiplication must be explicitly typed using the asterisk * (Shift-8).
If we try to run 10cos(2) we get a "SyntaxError." [When you see lots of red in the output, that's an error of some sort. You can ignore everything at the beginning. The nature of the error is in the last line.]
We get an error if we try 3(4+8) as well. Sage thinks we are using function notation, but "3" can't be the name of a function, so we get a "TypeError."
Explicit multiplication is extremely important when you are multiplying binomials. Sage may not give you an error, but it will not give you the correct answer. Compare the following two examples:
Notice that Sage does something in the first example (no error indicated), but what it does is wrong (or, at least, not what we expect)! [When you do (x+2)(x+1) Sage actually substitutes x+1 for x in x+2.]
We know $(x+2)(x+1)=x^2+3x+2$, which is definitely not $x+3$.
The moral is:
Make sure you explicitly type every multiplication, every time - no exceptions!
Complete Question 2 of the assignment.
You can define new functions with whatever name you want (some characters are not allowed) by typing a function name followed by a variable name in parentheses, then an equal sign, then the formula for the function, and finally pressing [SHIFT]-[ENTER].
For example, let's define $f(x)=5x^2+5$.
Caution: Don't forget to type 5*x^2 - the multiplication must be explicit.
Once a function has been defined (make sure you run it), then you can use that function in other cells.
Note: If you close a worksheet and return to it later, you may have to run the function definition again before using it.
Oops, we got an error: "name 'a' is not defined."
Sage automatically defines one variable called "x." If you want to use another variable, you have to declare it using the "var" command.
To declare a variable called "a," type %var a and press [SHIFT]-[ENTER]. You can now use the variable "a" anywhere in the worksheet.
Here we use a variable called "b."
You can use new variables in function definitions without declaring them.
You do not have to declare variables that are set equal to a number.
In the example above, we do not have to declare z, since z is not really a variable at all, it's just a different way of writing 22.
You also don't have to declare a variable that is set equal to other variables.
Here xyz is just another name for a+b, and we already declared a and b above.
If we later declare xyz to be a variable, it will erase the previous assignment.
You can also declare multiple variables at once - just separate with commas:
Complete Question 3 of the assignment.
If you want to add a comment to your input code, then type #. Anything after the # will be ignored when the input is run.
If you want to divide a single input cell into multiple cells (i.e., add a horizontal gray line between them), then put your cursor at the place you want to divide the cell and hit [CTRL]-[;]. A horizontal gray line will be inserted above the line with the cursor.
Try splitting the input cell below.
If you want to download, delete, or rename a Sage worksheet, click on the check box to the left of the worksheet name in the "Files" list. These options will appear right above the list of files. Or, if the worksheet is open, click on the blue box with a blue "i" in a white circle (it's on the left side of the page), and you will see these options.
If you want to create a new Sage worksheet, click on the button with a + sign that says "New." Type the name you want in the box, and click the button that says "SageMath Worksheet."
If you want to print a worksheet, open it and click on the white button with a printer-shaped icon (it's next to the green "Save" button). This will produce an HTML file that you can open in any web browser for printing.
If you want to split your screen (so you can view two parts of a worksheet at the same time), click on the split view button: it's between the button with the two arrows and the button with the letter "A." If you click once, it splits the screen horizontally; if you click twice, it splits the screen vertically; if you click a third time, it returns to normal.
When you are finished with Sage, click on your name or email address (top right next to a gear icon), and then click "Sign Out."
By default, when you type an open parenthesis or other bracket, Sage automatically inserts a matching close bracket.
However, when you already have a close bracket, if you try to insert another one before it, then it will not insert another bracket (it will assume you are typing the one that is already there). So if you want a second close bracket, you have to put it after the existing one.
Personally, I get so irritated by this, that I have disabled auto close brackets. To do this, open the general settings by clicking on the gear icon with your name next to it (upper right). [Note: this is not the same as the project settings, which has a wrench icon in the upper left.] Then you can uncheck the box with "auto close brackets." The next time you open a Sage worksheet, it will not automatically close brackets.
P.S. I also disable the "Extra button bar," since I never use it and it just takes up space on my screen.