Navigation

This Sage document is one of the tutorials
developed for the MAA PREP Workshop “Sage: Using Open-Source
Mathematics Software with Undergraduates” (funding provided by NSF DUE
0817071). It is licensed under the Creative Commons
Attribution-ShareAlike 3.0 license (CC BY-SA).

In the first tutorial, we defined functions using notation similar to
that one would use in (say) a calculus course.

There is a useful variant on this - defining expressions involving
variables. This will give us the opportunity to point out several
important, and sometimes subtle, things.

In the cell below, we define an expression \(FV\) which is the
future value of an investment of $100, compounded continuously. We
then substitute in values for \(r\) and \(t\) which calculate
the future value for \(t=5\) years and \(r=5\%\) nominal
interest.

sage: var('r,t')(r, t)sage: FV=100*e^(r*t)

sage: FV(r=.05,t=5)128.402541668774

The previous cells point out several things to remember when working
with symbolic expressions. Some are fairly standard.

An asterisk (*) signifies multiplication. This should be how you
always do multiplication.

Although it is possible to allow implicit multiplication, this can
easily lead to ambiguity.

We can access the most important constants; for instance, \(e\)
stands for the constant \(2.71828...\). Likewise, pi (or
\(\pi\)) and \(I\) (think complex numbers) are also defined.

Of course, if you redefine \(e\) to be something else, all bets
are off!

However, two others may be unfamiliar, especially if you have not used
much mathematical software before.

You must tell Sage what the variables are before using them in a
symbolic expression.

We did that above by typing var('r,t').

This is automatically done with the \(f(x)\) notation, but
without that it is necessary, so that Sage knows one intends t (for
instance) is a symbolic variable and not a number or something else.

If you then wish to substitute some values into the expression, you
must explicitly tell Sage which variables are being assigned which
values.

For instance, above we used FV(r=.05,t=5) to indicate the precise
values of \(r\) and \(t\).

Notice that when we define a function, we don’t need to specify which
variable has which value. In the function defined below, we have
already specified an order.

sage: FV2(r,t)=100*e^(r*t)sage: FV2(.05,5)128.402541668774

In this case it is clear that \(r\) is first and \(t\) is second.

But with FV=100*e^(r*t), there is no particular reason \(r\) or
\(t\) should be first.

sage: FV(r=.05,t=5);FV(t=5,r=.05)128.402541668774128.402541668774

This is why we receive a deprecation error message when we try to do
\(FV\) without explicitly mentioning the variables.

sage: FV(5,.05)doctest:...: DeprecationWarning: Substitution using function-call syntax and unnamed arguments is deprecated and will be removed from a future release of Sage; you can use named arguments instead, like EXPR(x=..., y=...)See http://trac.sagemath.org/5930 for details.128.402541668774

In this case, the outcome is the same, since \(rt=tr\)! Of course,
in most expressions, one would not be so lucky, as the following example
indicates.

sage: y=var('y')sage: G=x*y^2sage: G(1,2);G(2,1)42

Also remember that when we don’t use function notation, we’ll need to
define our variables.

One of the great things we can do with expressions is manipulate them.
Let’s make a typical expression.

sage: z=(x+1)^3

In the cells below, you’ll notice something new: the character #.
In Sage (and in Python ), anything on a
single line after the number/pound sign (the octothorp ) is ignored. We say that
# is a comment character. We use it below to mention alternative
ways to do the same thing.

sage: expand(z)# or z.expand()x^3 + 3*x^2 + 3*x + 1

sage: y=expand(z)sage: y.factor()# or factor(y)(x + 1)^3

In the previous cell, we assigned the expression which is the
expansion of \(z\) to the variable \(y\) with the first line.
After that, anything we want to do to the expansion of \(z\) can be
done by doing it to \(y\).

There are more commands like this as well. Notice that \(z\) will
no longer be \((x+1)^3\) after this cell is evaluated, since we’ve
assigned \(z\) to a (much more complex) expression.

One of the other basic uses of mathematics software is easy plotting.
Here, we include a brief introduction to the sorts of plotting which
will prepare us to use Sage in calculus. (There will be a separate
tutorial for more advanced plotting techniques.)

Recall that we can generate a plot using fairly simple syntax. Here, we
define a function of \(x\) and plot it between \(-1\) and
\(1\).

We can give the plot a name, so that if we want to do something with the
plot later, we don’t have to type out the entire plot command.
Remember, this is called assigning the plot to the name/variable.

In the next cell, we give the plot the name \(P\).

sage: P=plot(f,(x,-1,1))

One plot is nice, but one might want to superimpose plots as well. For
instance, the tangent line to \(f\) at \(x=0\) is just the line
\(y=1\), and we might want to show this together with the plot.

So let’s plot this line in a different color, and with a different style
for the line, but over the same interval.

Try rotating the plot above by clicking and dragging the mouse inside
of the plot.

Also, right-click (Control-click if you have only one mouse button)
just to the right of the plot to see other options in a menu.

If you have a wheel on your mouse or a multi-touch trackpad, you can
scroll to zoom.

You can also right-click to see other options, such as

spinning the plot,

changing various colors,

and even making the plot suitable for viewing through 3D glasses
(under the “style”, then “stereographic” submenus),

When using the plot3d command, the first variable range specified is
plotted along the usual “x” axis, while the second range specified is
plotted along the usual “y” axis.

The plot above is somewhat crude because the function is not sampled
enough times - this is fairly rapidly changing function, after all. We
can make the plot smoother by telling Sage to sample the function using
a grid of 300 by 300 points. Sage then samples the function at 90,000
points!

sage: plot3d(g,(x,-5,5),(y,-5,5),plot_points=300)Graphics3d Object

As with 2D plots, we can superimpose 3D plots by adding them together.

Note that in this one, we do not define the functions, but only use
expressions (see the first set of topics in this tutorial), so it is
wisest to define the variables ahead of time.