SharedUebungen_Mathe1_WS17 / Intro to Sage.sagewsOpen in CoCalc
Author: Darius Mottaghy

#Introduction to Sage

Sage is a powerful set of computational tools that allow you to use the computer for various applications in

  • Precalculus (e.g., functions, graphing),
  • Calculus (e.g., limits, derivatives, integrals),
  • and beyond (e.g., Differential Equations, Linear Algebra).

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.

###Basic Arithmetic

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.

1+1
2
109-87
22
22*93
2046
88/22
4

Exponents:

4^3
64
4^(1/2)
2

The same symbol is used for subtraction and negative numbers:

-9+22
13
3*-6
-18

You can type multiple inputs before hitting "Run."

1+8 2-5 4*6
9 -3 24

The outputs are produced in the same order as the inputs.

If you want to access the last output, use _

3*4 _+5
12 17

In the example above, _ =12=12, since that was the last output.

12-10 7*_
2 14

In this example, _ =2=2, 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:

2*4^5+3
2051

245+3=21024+3=2048+3=20512\cdot4^5+3=2\cdot1024+3=2048+3=2051

(2*4)^5+3
32771

(24)5+3=85+3=32768+3=32771(2\cdot4)^5+3=8^5+3=32768+3=32771

2*(4^5+3)
2054

2(45+3)=2(1024+3)=21027=20542\cdot(4^5+3)=2\cdot(1024+3)=2\cdot1027=2054

2*4^(5+3)
131072

245+3=248=265536=1310722\cdot4^{5+3}=2\cdot4^8=2\cdot65536=131072


Be careful with fractions:

12/3+5
9

123+5=9\frac{12}{3}+5=9

12/(3+5)
3/2

123+5=32\frac{12}{3+5}=\frac{3}{2}

###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.

###Question 1

12+547\displaystyle\frac{12+5}{4-7} [Answer: 173-\frac{17}{3}]

###Question 2

42×51(7+4)\displaystyle 4^{2\times 5-1}-(7+4) [Answer: 262133]

###Decimal Approximations

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:

11/4
11/4

Since 11 and 4 are both integers, Sage tries to keep everything as integers, so it returns the fraction 114\frac{11}{4}. If you want a decimal instead, Sage has a decimal approximation function called "N" (short for "numerical_approximation").

N(11/4)
2.75000000000000

Compare the following:

17/5 N(17/5)
17/5 3.40000000000000

###Scientific Notation

For numbers that are very large or very close to 0, Sage employs scientific notation.

Recall, for example, 1.5×104=150001.5\times10^4=15000 and 1.5×104=0.000151.5\times10^{-4}=0.00015.

In Sage, scientific notation is indicated using the letter "e".

Here we'll see the examples of 1.5×10251.5\times10^{25} and 1.5×10251.5\times10^{-25}.

1.5*10^(25)
1.50000000000000e25
1.5*10^(-25)
1.50000000000000e-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.

1.25325428941968e-17

At first, this looks like a number around 1141\frac{1}{4}. But this is actually a number close to 00:

1.25325428941968e-17=1.25325428941968×1017=0.000000000000000012532542894196801.25325428941968\textrm{e-}17 =1.25325428941968\times 10^{-17}=0.0000000000000000125325428941968\approx 0.

###Standard Functions

Sage includes the standard functions you learned in precalculus, including trigonometric, exponential, and logarithmic functions.

Square root:

sqrt(2)
sqrt(2)

Note that Sage keeps it exact using integers. To convert to a decimal, use N().

N(sqrt(2))
1.41421356237310

However, notice that if you start with a decimal, Sage will output a decimal.

sqrt(2.5)
1.58113883008419

Compare with the following:

sqrt(5/2) N(sqrt(5/2))
sqrt(5/2) 1.58113883008419

Other roots:

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

2^(1/3) N(2^(1/3))
2^(1/3) 1.25992104989487

Here's a fourth root.

8^(1/4) N(8^(1/4))
8^(1/4) 1.68179283050743

Trig functions:

Notice how Sage keeps it exact again.

Note that pi = π\pi.

Another note: Sage assumes all angles are measured in radians.

sin(3) N(sin(3))
sin(3) 0.141120008059867
sin(pi) sin(3*pi/2)
0 -1

In the example below, note the 5*pi/4 - the multiplication must be explicit - Sage will not accept 5pi/4.

sin(5*pi/4) sin(pi/12)
-1/2*sqrt(2) -1/12*sqrt(6)*(sqrt(3) - 3)

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.

show(sin(5*pi/4)) show(sin(pi/12))
122\displaystyle -\frac{1}{2} \, \sqrt{2}
1126(33)\displaystyle -\frac{1}{12} \, \sqrt{6} {\left(\sqrt{3} - 3\right)}
cos(pi/7) N(cos(pi/7))
cos(1/7*pi) 0.900968867902419
tan(1/2) N(tan(1/2)) tan(0.5)
tan(1/2) 0.546302489843790 0.546302489843790
sec(2) N(sec(2))
sec(2) -2.40299796172238
csc(pi/4) N(csc(pi/4))
sqrt(2) 1.41421356237310
cot(pi/3) N(cot(pi/3))
1/3*sqrt(3) 0.577350269189626
show(cot(pi/3))
133\displaystyle \frac{1}{3} \, \sqrt{3}

Inverse trig functions:

arcsin(.5); arccos(.25); arctan(pi/2)
0.523598775598299 1.31811607165282 arctan(1/2*pi)

Let's convert that last one to a decimal. Remember that _ accesses the last output.

N(_)
1.00388482185389

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, e2.71828e\approx2.71828).

Notice Sage's use of exact values.

e^3
e^3
N(e^3)
20.0855369231877
exp(3)
e^3
10^6
1000000
3^(3/5) N(3^(3/5)) 3^0.6
3^(3/5) 1.93318204493176 1.93318204493176

In Sage, the function "log" is the logarithm base e, not base 10 as on your calculator.

ln(e^2) log(e^2)
2 2
log(10^3) N(log(10^3))
log(1000) 6.90775527898214

Sage can also handle logarithms with other bases: log(x,b) = logbx\log_b x

Here's log10103=3\log_{10}10^3=3

log(10^3,10)
3

Here's log2180010.81\log_2 1800\approx 10.81. Notice Sage gives an exact answer in terms of log unless you ask otherwise.

log(1800,2) N(log(1800,2))
log(1800)/log(2) 10.8137811912170

###Explicit Multiplication

When we write mathematical expressions, we often omit the multiplication symbol. For example, when we write 10cos(2)10\cos(2) we mean 10cos(2)10\cdot\cos(2).

When we write 3(4+8)3(4+8) we mean 3(4+8)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.]

10cos(2)
Error in lines 1-1 Traceback (most recent call last): File "/projects/a8975d68-235e-4f21-8635-2051d699f504/.sagemathcloud/sage_server.py", line 879, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "<string>", line 1 10cos(Integer(2)) ^ SyntaxError: invalid syntax

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."

3(4+8)
Error in lines 1-1 Traceback (most recent call last): File "/projects/a8975d68-235e-4f21-8635-2051d699f504/.sagemathcloud/sage_server.py", line 879, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> TypeError: 'sage.rings.integer.Integer' object is not callable

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 log(4)\log(4) is function notation, just like sin(4)\sin(4) or f(4)f(4) [for some function ff].

###Assignment (part 2 of 3):

Find decimal approximations for the following expressions using Sage.

###Question 3

5sin(37)+log2(arcsin(13))\displaystyle \sqrt{5\sin(3^7)}+\log_{2}\left(\arcsin\left(\frac{1}{3}\right)\right) [Answer: 0.08005\approx-0.08005]

###Question 4

cos(π7)12008+2e4/3\displaystyle \cos\left(\frac{\pi}{7}\right)-\sqrt[8]{1200}+2\cdot e^{4/3} [Answer: 6.06227\approx 6.06227]

###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 f(x)=5x2+5f(x)=5x^2+5.

Caution: Don't forget to type 5*x^2 - the multiplication must be explicit.

f(x)=5*x^2+5

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.

f(2)
25
f(14)
985
f(a+1)
Error in lines 1-1 Traceback (most recent call last): File "/projects/a8975d68-235e-4f21-8635-2051d699f504/.sagemathcloud/sage_server.py", line 865, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> NameError: name 'a' is not defined

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.

###Declaring Variables

To declare a variable called a, type %var a and hit "Run." You can now use the variable a anywhere in the worksheet.

%var a f(a+1)
5*(a + 1)^2 + 5

Here we use a variable called b.

%var b f(b)
5*b^2 + 5

You can use new variables in function definitions without declaring them.

g(t)=t^3-2*t+4 g(3)
25
h(w)=sqrt(w)-w^2 h(4)
-14

You do not have to declare variables that are set equal to a number.

z=22 f(z)
2425

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.

xyz=a+b f(xyz)
5*(a + b)^2 + 5

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.

%var xyz f(xyz)
5*xyz^2 + 5

You can also declare multiple variables at once - just separate with commas:

%var x1, x2, x3 f(x1) g(x2) h(x3)
5*x1^2 + 5 x2^3 - 2*x2 + 4 -x3^2 + sqrt(x3)
%var u, v, w f(u)*g(v)/h(w)
-5*(v^3 - 2*v + 4)*(u^2 + 1)/(w^2 - sqrt(w))

###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.

f(x)=3x25x+1f(x)=3x^2-5x+1

g(t)=2t19t2+4g(t)=\frac{2t-1}{9t^2+4}

###Question 5

f(8)f(8) [Answer: 153]

###Question 6

f(x+h)f(x)h\displaystyle\frac{f(x+h)-f(x)}{h} [Answer: (3*(h + x)^2 - 3*x^2 - 5*h)/h]

###Question 7

g(21)g(21) [Answer: 41/3973]

###Question 8

g(t12)g(t-12) [Answer: (2*t - 25)/(9*(t - 12)^2 + 4)]

###Adding Comments

If you want to add a comment to your input code, then type #. Anything after the # will be ignored when the input is run.

#This line will be ignored. 1+5-8 #If I leave off the # sign, Sage will return an error. It would try to interpret this text as a command.
-2
This line will not be ignored. 1+5-8
Error in lines 1-1 Traceback (most recent call last): File "/projects/a8975d68-235e-4f21-8635-2051d699f504/.sagemathcloud/sage_server.py", line 879, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "<string>", line 1 This line will not be ignored. ^ SyntaxError: invalid syntax

###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.

33+1 22-9
34 13