Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168714
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.

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

Now let's solve an equation.

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

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.

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

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)

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.

f=(x^2+4*x+3)/(x^2-9) f.full_simplify()
(x + 1)/(x - 3)
f
(x^2 + 4*x + 3)/(x^2 - 9)
show(f) f.show() #both of these show f using a prettier output than the previous.
\frac{{(x^{2} + 4 \, x + 3)}}{{(x^{2} - 9)}}
\frac{{(x^{2} + 4 \, x + 3)}}{{(x^{2} - 9)}}
plot(f,(x,-2,4)) f.plot(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) f.plot(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

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()

var('h') f(x)=5*x^2+2*x-3 diffquotient=(f(x+h)-f(x))/h #when naming variables, use a descriptive name
diffquotient.show()
\frac{{(5 \, {(h + x)}^{2} - 5 \, x^{2} + 2 \, h)}}{h}
diffquotient.full_simplify().show()
5 \, h + 10 \, x + 2

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

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.

@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))