Introduction to Sage
Sage is an open-source (and free) mathematical software system. SageMathCloud is a way to use Sage through a web browser, without having to install Sage on your own computer. We will use Sage in this course to explore some Calculus using software tools.
Basic Computations
Let's start with an easy calculation. Below you will see a Sage cell, with 2+2
already entered. Click inside the cell, then hit Shift-Return
to evaluate the cell and get the result:
(You can also click on a gray line to get a new Sage cell in which to enter your own calculation. Try it out!)
As the above example shows, Sage can be used as a calculator. But Sage is a very good calculator, which knows about all sorts of mathematical functions and constants. To see an example, let's start a new cell by clicking on a gray line below. Then type sin(pi/3)
and press [Shift]-[Enter] to see a little of what Sage can do.
That's really good! But if you want to see a numerical approximation to the number , you can use the Sage function n
. You can also use the symbol _
to refer to the last computation that Sage did. So if the last thing you computed was sin(pi/3)
above, you can type n(_)
to find a numerical approximation. (If you have entered something else since then, just type n(sin(pi/3))
in a new cell.)
If you want more accuracy, you can add the argument digits
inside the n
function, like this:
Sage is familiar with many functions you know about. A few notes about some special ones:
The inverse trig functions are written as
arcsin
,arctan
, etc.The natural logarithm can be written as
ln
or aslog
. If you want a logarithm to another base, such as base 10, specify the base with thebase
argument:log(x, base=10)
, for example.The natural exponential function, , can be written as
exp(x)
.
Practice Can you find (to at least six decimal places) the angle which makes equal to 1/3?
Algebra
Sage can do symbolic algebra, but you must first declare any variables you want to use. Let's declare both x
and t
to be symbolic variables with the statement below:
Here are a few examples of what Sage can do with algebraic expressions: (Note in the examples below, multiplication must be indicated with a *
; it cannot be represented by juxtaposition!)
It is also easy to define a simple function, so that you can reuse it many times, if you wish. Here's an example of creating a function :
When you run the cell above, nothing seems to happen, because all it does is define the function. But now you can use the function , if you wish:
There are many other types of algebraic problems that Sage can solve. See some of the references at the end if you would like to learn more.
Basic Calculus
Sage can also carry out some basic symbolic calculus. Here are a few examples:
Plotting
It is easy to plot a function in Sage. Here is an example of plotting the function on the interval :
Sometimes the graph does not show the sorts of details you would like. Try the following to plot on :
The values get so big that we cannot see much else. We can add the extra arguments ymin
and ymax
to the plot function to fix the window of the plot:
By the way, those vertical lines probably look like Sage is plotting the asymptotes. Actually, it's just the result of Sage "connecting the dots" as it plots the graph. We can adjust the behavior at these points using the detect_poles
option:
3D Plotting
We are more interested in plots of functions of several variables. Here's how to plot a surface:
A useful trick is to plot an implicit surface, where a function is equal to a given value. Here we can plot a sphere, by plotting where :
Sage is also capable of plotting parametric plots in space, using parametric_plot3d
:
To combine two plots, just add them:
Vectors and Matrices
Sage is familiar with vectors and vector operations. You can define a vector as a list of numbers using the vector
function:
Sage understands common operations, and even knows about dot and cross products:
Matrices are created using the matrix command, but you need to tell sage what kinds of entries the matrix should have. We will use rational numbers, indicated by QQ
. Then we enter the matrix as a list of lists. We define a matrix m
Programming
Sage is a full featured programming language. Here is a simple example of a function defined to test if a function is even or not:
If you are familiar with Python, Sage uses the same syntax.
This is text. We can include italic, or bold.
Writing Text
Finally, to write your responses to some Sage problems, you will want to be able to put in some text explanations. The easiest way to do this is using markdown, a simple language for formatted text. To start a markdown cell, put %md
as the first text in the cell. Then you can type any text you want, and when you evaluate that cell, it will mostly just turn in to text. Here are a few simple formatting commands you can add, if you wish
To type italicized text, surround the words with an asterisk:
*italicized*
To make text bold, use two asterisks:
**bold**
Trickier: You can format a polynomial like like this:
$f(x) = 3 x^3 - x^2 + 2x + 1$
(That last example is very tricky. There is an entire language for mathematical typesetting called which is being used there. You are not required to use this notation, but you are welcome to try if you wish.)
All of the text you see in this document is really in a markdown cell. If you double click on one of those cells, you can see exactly how I created that text, if you are interested.
(Alternatively, you can also start a cell with %html
, and Sage will recognize and render HTML as well.)
Other References
Here are some useful references if you would like to explore Sage further:
A lengthy (but very good) Sage Tutorial at San Diego State University.
Gregory Bard at University of Wisconsin, Stout, has a page of information about Sage, including some videos and his new book, Sage for Undergraduates.
Introduction to Sage
Sage is an open-source (and free) mathematical software system. SageMathCloud is a way to use Sage through a web browser, without having to install Sage on your own computer. We will use Sage in this course to explore some Calculus using software tools.
Basic Computations
Let's start with an easy calculation. Below you will see a Sage cell, with 2+2
already entered. Click inside the cell, then hit Shift-Return
to evaluate the cell and get the result:
(You can also click on a gray line to get a new Sage cell in which to enter your own calculation. Try it out!)
As the above example shows, Sage can be used as a calculator. But Sage is a very good calculator, which knows about all sorts of mathematical functions and constants. To see an example, let's start a new cell by clicking on a gray line below. Then type sin(pi/3)
and press [Shift]-[Enter] to see a little of what Sage can do.
That's really good! But if you want to see a numerical approximation to the number , you can use the Sage function n
. You can also use the symbol _
to refer to the last computation that Sage did. So if the last thing you computed was sin(pi/3)
above, you can type n(_)
to find a numerical approximation. (If you have entered something else since then, just type n(sin(pi/3))
in a new cell.)
If you want more accuracy, you can add the argument digits
inside the n
function, like this:
Sage is familiar with many functions you know about. A few notes about some special ones:
The inverse trig functions are written as
arcsin
,arctan
, etc.The natural logarithm can be written as
ln
or aslog
. If you want a logarithm to another base, such as base 10, specify the base with thebase
argument:log(x, base=10)
, for example.The natural exponential function, , can be written as
exp(x)
.
Practice Can you find (to at least six decimal places) the angle which makes equal to 1/3?
Algebra
Sage can do symbolic algebra, but you must first declare any variables you want to use. Let's declare both x
and t
to be symbolic variables with the statement below:
Here are a few examples of what Sage can do with algebraic expressions: (Note in the examples below, multiplication must be indicated with a *
; it cannot be represented by juxtaposition!)
It is also easy to define a simple function, so that you can reuse it many times, if you wish. Here's an example of creating a function :
When you run the cell above, nothing seems to happen, because all it does is define the function. But now you can use the function , if you wish:
There are many other types of algebraic problems that Sage can solve. See some of the references at the end if you would like to learn more.
Basic Calculus
Sage can also carry out some basic symbolic calculus. Here are a few examples:
Plotting
It is easy to plot a function in Sage. Here is an example of plotting the function on the interval :
Sometimes the graph does not show the sorts of details you would like. Try the following to plot on :
The values get so big that we cannot see much else. We can add the extra arguments ymin
and ymax
to the plot function to fix the window of the plot:
By the way, those vertical lines probably look like Sage is plotting the asymptotes. Actually, it's just the result of Sage "connecting the dots" as it plots the graph. We can adjust the behavior at these points using the detect_poles
option:
3D Plotting
We are more interested in plots of functions of several variables. Here's how to plot a surface:
A useful trick is to plot an implicit surface, where a function is equal to a given value. Here we can plot a sphere, by plotting where :
Sage is also capable of plotting parametric plots in space, using parametric_plot3d
:
To combine two plots, just add them:
Vectors and Matrices
Sage is familiar with vectors and vector operations. You can define a vector as a list of numbers using the vector
function:
Sage understands common operations, and even knows about dot and cross products:
Matrices are created using the matrix command, but you need to tell sage what kinds of entries the matrix should have. We will use rational numbers, indicated by QQ
. Then we enter the matrix as a list of lists. We define a matrix m
Programming
Sage is a full featured programming language. Here is a simple example of a function defined to test if a function is even or not:
If you are familiar with Python, Sage uses the same syntax.
This is text. We can include italic, or bold.
Writing Text
Finally, to write your responses to some Sage problems, you will want to be able to put in some text explanations. The easiest way to do this is using markdown, a simple language for formatted text. To start a markdown cell, put %md
as the first text in the cell. Then you can type any text you want, and when you evaluate that cell, it will mostly just turn in to text. Here are a few simple formatting commands you can add, if you wish
To type italicized text, surround the words with an asterisk:
*italicized*
To make text bold, use two asterisks:
**bold**
Trickier: You can format a polynomial like like this:
$f(x) = 3 x^3 - x^2 + 2x + 1$
(That last example is very tricky. There is an entire language for mathematical typesetting called which is being used there. You are not required to use this notation, but you are welcome to try if you wish.)
All of the text you see in this document is really in a markdown cell. If you double click on one of those cells, you can see exactly how I created that text, if you are interested.
(Alternatively, you can also start a cell with %html
, and Sage will recognize and render HTML as well.)
Other References
Here are some useful references if you would like to explore Sage further:
A lengthy (but very good) Sage Tutorial at San Diego State University.
Gregory Bard at University of Wisconsin, Stout, has a page of information about Sage, including some videos and his new book, Sage for Undergraduates.