#Introduction to Sage
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. Your assignment for this week is within this tutorial. There are 8 questions total.
###Input and Output
There are two regions in a SageMathCloud worksheet: input cells and output cells.
Output cells have an orange, vertical line next to them, while input cells do not. You type commands in an input cell, and then you push the green "Run" button on the top of the page (or use the keyboard shortcut [SHIFT]-[ENTER]). 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 click the "Run" button. If any commands you have typed in the input cell produce an output, this will appear right below the input cell. Most output will be static (you can't change it), but later in the semester we will produce output that can be manipulated.
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.
Sage can be used very much like your graphing calculator to do computations. Simply type an arithmetic expression in an input cell and hit "Run" (or use [SHIFT]-[ENTER]). Here are some examples.
The same symbol is used for subtraction and negative numbers:
You can type multiple inputs before hitting "Run."
The outputs are produced in the same order as the inputs.
If you want to access the last output, use _
In the example above, _ , since that was the last output.
In this example, _ , since that was the last output.
###Order of Operations
Like a calculator, Sage uses the standard order of operations, often called PEMDAS (parentheses, exponents, multiplication, division, addition, subtraction). Compare the following:
Be careful with fractions:
###Assignment (part 1 of 3):
Compute the following in Sage. Make sure you have enough parentheses to get the order of operations right. I have provided the answers so you can check.
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 . If you want a decimal instead, Sage has a decimal approximation function called "N" (short for "numerical_approximation").
Compare the following:
For numbers that are very large or very close to 0, Sage employs scientific notation.
Recall, for example, and .
In Sage, scientific notation is indicated using the letter "e".
Here we'll see the examples of and .
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 . But this is actually a number close to :
Sage includes the standard functions you learned in precalculus, including trigonometric, exponential, and logarithmic functions.
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 (don't forget parentheses).
Here's a fourth root.
Notice how Sage keeps it exact again.
Note that 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.
Inverse trig functions:
Let's convert that last one to a decimal. Remember that _ accesses the last output.
Exponential and logarithmic functions:
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, ).
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) =
Here's . 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 we mean .
When we write we mean .
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."
Make sure you explicitly type every multiplication, every time - no exceptions!
On the other hand, don't put multiplication where it doesn't belong. For example, there is no multiplication involved in log(4). This is not log*(4) - log is a function, and is function notation, just like or [for some function ].
###Assignment (part 2 of 3):
Find decimal approximations for the following expressions using Sage.
###Defining your own functions
You can define new functions with whatever name you want (some characters are not allowed) by typing function_name(x)=some_formula and then hitting "Run."
For example, let's define .
Caution: Don't forget to type 5*x^2 - the multiplication must be explicit.
Once a function has been defined (make sure you hit "Run"), 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 - check for a blue line segment above the definition.
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 hit "Run." 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:
###Assignment (part 3 of 3):
Define the following functions in Sage, and use them to evaluate the function values below. Don't forget you need explicit multiplications and use plenty of parentheses.
[Answer: (3*(h + x)^2 - 3*x^2 - 5*h)/h]
[Answer: (2*t - 25)/(9*(t - 12)^2 + 4)]
If you want to add a comment to your input code, then type #. Anything after the # will be ignored when the input is run.
###Dividing Input Cells
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.