All published worksheets from http://sagenb.org
Image: ubuntu2004
Introduction to Sage
The point to this worksheet is to introduce you to some of the basic commands in Sage. We'll use it to help us review topics from Algebra prior to starting our study of calculus.
Throughout this worksheet I have include blank cells in which you can type examples and try new things. To evaluate a cell, just click on the cell and then click evaluate. The simplest way I know of to use a new computer algebra system is to copy examples from someone else, and then change them to fit what you need. The purpose to the sheets I create this semester is to give you some place you can go to find examples relevant to what we are currently learning.
Basics - Variable, Solving, Factoring, Simplifying, Plotting
The first thing you should always do when beginning a new worksheet is to define the variables you plan to use. Since x and y are usually variables in our work, I'll start by stating they are variables. Without doing this, Sage will not let me use x and y without giving an error. The #auto command makes this cell evaluate every time you load this worksheet, so that you can use x and y as variables without having to evaluate this cell.
Now let's solve an equation.
You can quickly factor polynomials in Sage using factor. If you forget to put a times sign between two things, Sage will give you an error. There is work in progress to fix this, but currently you must always remember to always type in times symbols.
You can also use the factor command to simplify rational expressions, as it will factor both the numerator and denominator and then cancel common terms.
If you plan to use the same expression many times, you can save yourself lots of time by storing it as a variable. Here I store a rational expression as f, then simplify it, print it, use show() to make is appear pretty, and then plot it. Notice that you can often write f.[something] to perform commands.
Since the plot above had a vertical asymptote, you can use ymin and ymax to give a top and bottom range to your plot. Both commands below produce the same output.
Once you have defined a variable, if you want to remove it from Sage's memory use the del() function. The following command removes f from memory, so typing f from now on should give an error.
Function Notation
To use function notation in Sage (so that you can write something like f(2)) just write f(x)= before writing the function. Then you can use f(2), f(a), f(x+h) or any kind of function notation that you are familar with from algebra.
Difference Quotient $\dfrac{f(x+h)-f(x)}{h}
In the example below I compute the difference quotient of a function. If I don't define h to be a variable first, then I get an error. That is why I include var('h') on the first line. To simplify the difference quotient I use full_simplify()
Getting Pretty Output and Numerical Values
Sage will try to return symbolic answers when it can. Use .show() to get a pretty output. use .n() to get a numeric value. Below I show you mutliple ways to use the n() function.
More with Plotting
Let's look at some more plotting examples.
Trigonometry
Now let's look at trigonometry. Sage will give you an exact value, and simplify it when possible. Since the exact value of sin(2) is unknown, it just returns sin(2) unless you ask for a number.
This final example shows you a feature of Sage called interact, which allows you to create a slider to change variables.