Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168732
Image: ubuntu2004

Welcome to Sage! Sage is a computer algebra system similar to what you would find on the TI-89 but with many more features. Plus it's free!

If you want to keep using Sage after you leave today, write down or send yourself an email with the URL of Sage's home page: http://www.sagemath.org/. There you can find links to download Sage as well as tutorials and other resources on how to use it.

With that, let's get started! To have Sage evaluate text that you have typed, simply press [SHIFT]+[ENTER] or click "evaluate". Click in the box below and press [SHIFT]+[ENTER] to see the result.

123*456

You can do any sort of basic computations using *, +, -, /, ^ (for exponents), and % (for modulus) with parentheses to group expressions together. What does the expression below evaluate to?

(67*9 - 450/3 - 4^2 - 17) / 10

Some functions only have text equivalents, like the square root function (sqrt), pi (pi) and the number e (e).

sqrt(pi + e)

Sage will only give you symbolic output unless you use the "float" function to get the decimal equivalent. If you ever forget the name of a function in Sage, you can start typing and then press [TAB] to see a list of functions that start with the letters you have typed. For example, if you type "fl" and then press [TAB] you will see "float" as one of the options.

float(sqrt(pi + e))

For practice, try computing the square root of e to the power of pi in the box below. What are the first three digits?

If you want more boxes for doing computations, move you mouse below a box until a thick blue line appears and then click on it.

Let's do something more exciting--plotting! The command for plotting is "plot", although there are a few different kinds of plots you can do. To see how to use a command, type the name of the command followed by a question mark and evaluate it. The plot command has a lot of useful examples.

plot?

Here are some plots. In the first example, x^2 + 1 is the function to plot, x is the variable, -10 is the minimum value of x, and 10 is the maximum value of x.

plot(x^2 + 1, x, -10, 10)
plot(sin(x), x, -pi, pi)
plot(abs(x), x, -5, 5)

One thing that's always fun is 3D plotting. Check out some of the examples below or try experimenting with your own! Click and drag on the 3D plots to rotate them.

var('y') # We need to "tell" Sage that y is a variable before we can use it plot3d(x^2 + y^2, (x, -2, 2), (y, -2, 2)) # The lower and upper limits for x and y are -2 and 2, respectively
plot3d(sin(pi*(x^2 + y^2))/2, (x, -1, 1), (y, -1, 1))

Something that's useful for pretty much any math class is being able to solve equations. Luckily Sage is good at this! As always, if you're unsure how to use a command, simply type the name of the command followed by a question mark and evaluate it.

solve([x + y == 20, 3*x - y == 4], x, y)
solve([sin(x) == 1], x)

For those of you in calculus, Sage allows you to define functions and compute derivatives and integrals. Try it!

f(x) = cos(x)^2 derivative(f(x), x)
g(x) = log(x) integrate(g(x), x)
integrate(e^(-x^2), x, 0, oo) # Use oo for infinity. Negative infinity is -oo

Sage allows you to store values in variables so that you can reuse them. Aside from using the "solve" function, we can also define a function for the quadratic formula and plug some values into it like this:

f(a, b, c) = [-b + sqrt(b^2 - 4*a*c)/(2*a), -b - sqrt(b^2 - 4*a*c)/(2*a)] # Compute the solutions of the quadratic equation 2*x^2 + 3*x - 4 = 0 a = 2 b = 3 c = -4 f(a, b, c)

Sage is built on a programming language called Python, so Sage can do anything that Python can do. For example, say that we want to sum up all numbers from 1 to 100. Sage makes this easy:

total = 0 for i in [1..100]: total = total + i total

What the above code does is to set the "total" variable to 0, and then for each value between 1 and 100, set i equal to that value and add i to the total. Putting "total" on a line by itself at the end prints it out.

Something that a lot of mathematicians care about is prime numbers. Sage has quite a few functions for working with prime numbers, such as the primes_first_n function. primes_first_n is a function that returns a list of all the prime numbers up to a given index. So primes_first_n(100), for example, will give you the first 100 prime numbers. In the example above, replace "[1..100]" with "primes_first_n(100)" and reevaluate to see what the sum of the first 100 prime numbers is.


If you get to the end here and there's still a lot of time left, try exploring the Sage website at http://www.sagemath.org/ or let me (Elliott) know and I'll give you some suggestions for what else to try in Sage. Make sure to email yourself the URL of the Sage website before you leave!