Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168727
Image: ubuntu2004

Note: This worksheet is a modification of the worksheet found here.

Lab 2: Introduction to Sage

The point to this worksheet is to introduce you to some of the basic commands in Sage.  The simplest way I know of to learn how to use things like Sage is to copy examples from someone else, and then change them to fit what you need.

Throughout this worksheet I have include blank "Sage cells" in which you can type examples and try new things.  To evaluate a cell, just click on the cell and then click "evaluate."

Let's try a simple example.  In the empty cell below, type in some complicated expression of numbers (no variables right now) involving addition, subtraction, multiplication, and division.  Use * for multiplication and // for division.  When you are done, click "evaluate."  If you get an error, you must have typed something in incorrectly; just try again.

Variables, 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 xx and yy are usually variables in our work, I'll start by stating they are variables.  (Note: Sage always assumes xx is variable, but I think this is the only one it assumes anything about.)  Without doing this, Sage will not let me use xx and yy without giving an error. The %auto command makes this cell evaluate every time you load this worksheet, so that you can use xx and yy as variables without having to evaluate this cell.

%auto var('x,y')
(x, y)

Some Basics

Inserting new Sage cells

You can insert a new cell above or below any existing cell or block of text.  To insert a new cell, put your cursor above (respectively, below) the cell or block of text where you want to insert a new cell until you see a purple horizontal line pop up.  Click on this horizontal line to produce a new Sage cell.  Try that just below right now and then type in some other complicated expression of numbers and then click "evaluate."

Inserting a new block of text

You can also insert a new block of text (also called an "html cell") above or below any existing Sage cell or other block of text.  To insert a new html cell, you Shift+click the purple horizontal line that pops up instead of just clicking on it.  This will pop open a tiny text editor for you to type in.  There are lots of things you can do inside an html block, but for now, we will keep it simple.  You can always edit an existing html cell by simply double-clicking on the corresponding cell.  After you are done editing an html cell, click "Save changes."

Add an html cell at the very top of this worksheet and type in your names.

Declare another variable in the cell below.  Pick your favorite letter, but you should avoid ee.  Why?

Now let's solve an equation.

solve(x^2-4*x+3==0,x)
[x == 3, x == 1]

Now, I want you to try one.  Mimic the example above, being sure to use * for multiplication and two equal signs.  If you forget to put a times sign between two things, Sage will give you an error.

You can quickly factor polynomials in Sage using factor.  Evaluate each of the next two cells.

factor(x^2+3*x+2)
(x + 1)*(x + 2)
factor(x^2+3x+2) #without the * between 3 and x, there is an error.
line 4 factor(x**_sage_const_2 +3x+_sage_const_2 ) #without the * between 3 and x, there is an error. ^ SyntaxError: invalid syntax

Note:

  1. You can use # to make comments inside a Sage cell.  Sage will ignore anything to the right of #.  For example, Sage ignored "#without the * between 3 and x, there is an error." in the previous cell.
  2. You can make Sage display its output in nice format by checking the box at the top of the worksheet that says "typeset."  Do that now and then re-evaluate the second to last cell.

Now, you try one below.  Try to use the variable that you declared earlier.

 

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.

factor((x^2+4*x+3)/(x^2-9))
(x + 1)/(x - 3)

OK, your turn.  Try a new one below.

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, and then plot it.  Notice that you can often write f.[something] to perform commands.

f=(x^2+4*x+3)/(x^2-9) f.full_simplify()
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{{\left(x + 1\right)}}{{\left(x - 3\right)}}
f
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{{\left(x^{2} + 4 \, x + 3\right)}}{{\left(x^{2} - 9\right)}}
plot(f,(x,-2,4))

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.

plot(f,(x,-2,4),ymin=-10,ymax=10)

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.

del(f)
f
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/sage/sagenb/sage_notebook/worksheets/bmwoodruff/11/code/21.py", line 6, in <module> exec compile(ur'f' + '\n', '', 'single') File "", line 1, in <module> NameError: name 'f' is not defined

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.

f(x)=5*x^2+4
f(x)
5*x^2 + 4
f(4)
84
a=4
f(a)
84
g(x)=x^2-3*x+2 f(g(x))
5*(x^2 - 3*x + 2)^2 + 4
g(f(x)).show()
{(5 \, x^{2} + 4)}^{2} - 15 \, x^{2} - 10
expand(g(f(x))).show() g(f(x)).expand().show() #Either method works.
25 \, x^{4} + 25 \, x^{2} + 6
25 \, x^{4} + 25 \, x^{2} + 6

Experiment in the cells below.

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.

sqrt(2)
sqrt(2)
sqrt(2).show()
\sqrt{2}
sqrt(2).n()
1.41421356237310
n(sqrt(2))
1.41421356237310
sqrt(2).n(digits=50)
1.4142135623730950488016887242096980785696718753769
n(sqrt(2),digits=500) #Just for fun let's look at 500 decimals.
1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927557999505011527820605714701095599716059702745345968620147285174186408891986095523292304843087143214508397626036279952514079896872533965463318088296406206152583523950547457502877599617298355752203375318570113543746034084988471603868999706990048150305440277903164542478230684929369186215805784631115966687130130156185689872372

Try similar things with the number ee.

More with Plotting

Let's look at some more plotting examples.

#this is the basic plotting command plot(x^2, (x, -2, 3))
#You can include multiple functions in a list plot((x^2,sqrt(x),sin(x)), (x, 0, pi))
#You can also combine plots by adding them. This allows me to make each plot a different color. plot(x^2, (x, 0, pi),color='red')+plot(sqrt(x), (x, 0, pi),color='green')+plot(sin(x), (x, 0, pi))

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.

cos(pi/2)
0
cos(pi/6)
1/2*sqrt(3)
sin(2)
sin(2)
sin(2).n()
0.909297426825682
cos(pi/12).show()
\frac{1}{12} \, {(\sqrt{3} + 3)} \sqrt{6}
g=cos(x+y)
g.expand_trig() #You can use Sage to help you remember your trig identities.
-sin(x)*sin(y) + cos(x)*cos(y)
#Let's graph a*sin(b*x+c)+d. a=3 b=4 c=pi/3 d=-2 f=a*sin(b*x+c)+d f.plot(-pi,2*pi)

This final example shows you a feature of Sage called interact, which allows you to create a slider to change variables.  Drag the sliders around and experiment.

@interact def _(a=(-2,2),b=(1/2,3,1/4),c=(-pi,pi,pi/6),d=(-2,2)): show(plot(a*sin(b*x+c)+d,(x,0,6),ymin=-3,ymax=3))

Now, using the "Sage Quick Reference: Calculus" sheet that I gave you, see if you can do some differentiation and integration.  You can also access the reference sheet here.

I want you to do at least 3 different examples below.  Perhaps you can try some of your homework problems.  Ideally, I'd like to try some definite and indefinite integrals.

Note:  When Sage computes an indefinite integral, it omits the +C+C on the end.

When you are done, you should click "Save" at the top right of the worksheet.  If you click "Save & quit", the worksheet will close and you will be returned to your "home" directory.  Just click "Save" for now.

I want you to email me a copy of this worksheet.  To this, you should select "File..." at the top left of the worksheet.  Select "Save worksheet to file..." and save the worksheet some place on your computer (and remember where you put it).  Name the file something like, "Lab 2 (Tom, Dick, Jane)" (without the quotes).  Now, send that file to me as an attachment to an email.  Remember, I only need one worksheet per group.