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:
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)
.
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:
This means that we can try to use Sage to help us verify that something is a solution to a differential equation:
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.)
Plotting a Slope Field
Sage can plot slope fields using the command plot_slope_field(f(x, y), (x, xstart, xend), (y, ystart, yend))
, where f(x, y)
is the function is set equal to and we wish to view a window with between xstart
and xend
and between ystart
and yend
. (Here we are using and as the independent and dependent variables, respectively.)
Note that and need to be defined as variables in Sage for this to work.
The above shows a slope field for the equation on . (Solutions take the form .) However, it will probably look better if we make the - and -axes scaled the same by adding the option aspect_ratio=1
to the end of the command:
Note that you can combine two plots by adding them. It is sometimes useful to assign a plot to a variable so you can combine plots later. (Note also that assigning a plot to a variable keeps the plot from displaying when first generated.)
Exploration Problem
Plot a slope field for . (Try a region near the origin, like .)
Solve the differential equation (you know how to do this!), and plot several particular solutions on the slope field.
What patterns do you notice about the solutions? What conjectures do you have about other solutions to this equation? What evidence can you gather to support your conclusions?
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.