A Beginning of Sage

3138 days ago by kcrisman

A Beginning of Sage

Parts based on a worksheet by Mike May, S.J., 2010, licensed CC BY-NC-SA 3.0

The purpose of this worksheet is to learn just enough of Sage to be able to start using it (if you wish) in the course.

The first detail is to get an account!

Look at the upper left corner of your browser window. 

If you do not see the option to "Edit a copy" or "Edit this", but instead to "Log in to edit a copy", you are not yet logged in. Select "Log in to edit a copy".

Click on "Sign up for a new Sage Notebook account", and follow the instructions to do this; then log in with that account, and finally return to this page.

You should now see the option in the picture to "Edit a copy" or "Edit this". Select the "Edit" option; you will now have your own version of this worksheet you can use!


We now walk through how to actually do some mathematics!

The easiest operation is evaluating a cell that someone has already prepared for you.  Notice the two math cells pictured below, which also appear beneath the text region.

The same cells are below.


When the cursor in in the math region, it becomes active.  As the picture shows, an evaluate link appears.  You can evaluate the region by clicking the evaluate link.  When you do either, the cursor evaluates the contents of the cell, prints out the last result, and moves the cursor to the next math cell.

If you get bored with clicking, you can also click inside the cell, and then press 'Shift' and 'Enter' at the same time to evaluate the cell.

As you can see, we can use Sage to do easy calculations, or longer computations we would not want to do by hand.  To see the output, always highlight the cell (with up/down arrows or by clicking) and then evaluate it. 

Try evaluating the ones above or be brave and try something in the cell below!


Our main reason for looking at Sage today is to be able to compare how functions look when we mess with them, especially by moving and stretching. 

Below is a cell that can do movement for you. Click in it, evaluate it, and try out different transformations!

@interact def _(f=[sin(x),x^2,sqrt(x),(3/2)^x],a=(1,[-3..3]),b=(0,[-3..3])): html("This cell will plot $f(x)$ and $f(x+a)+b$ for you.") f(x)=f g(x)=f(x+a)+b fplot = plot(f,-5,5,legend_label='$f(x)$') newplot = plot(g,-5,5,legend_label='$f(x+%s)+%s$'%(a,b),color='red') show(fplot+newplot) 

Click to the left again to hide and once more to show the dynamic interactive window

In this cell, you can do different stretches.

@interact def _(f=[sin(x),x^2,sqrt(x),(3/2)^x],a=(1,[0,1,2,1/2,-1,-2]),b=(2,[0,1,2,1/2,-1,-2])): html("This cell will plot $f(x)$ and $bf(ax)$ for you.") f(x)=f g(x)=f(x*a)*b fplot = plot(f,-5,5,legend_label='$f(x)$') newplot = plot(g,-5,5,legend_label='$%sf(%sx)$'%(b,a),color='red') show(fplot+newplot) 

Click to the left again to hide and once more to show the dynamic interactive window

That's it for the main piece of today!  But if you think this could be a useful - or fun! - tool, read on.

It's also possible to do plots "by hand", which some of you may wish to do. In order to do that efficiently, we add one extra thing.

At this point, the main thing you might want to do is to plot functions (for instance, if you didn't have a graphing calculator, or if you realized that they are very wimpy).  The syntax is pretty simple, and you can reuse the same cell as often as you like.

plot(5*x^3, (x,-1,4)) 

Notice that you first type in "plot", a parenthesis, and the function, with (for now) $x$ as the variable, and using '^' for exponents and '*' for multiplication.  Then, after a comma, you put the variable and the part of the domain you'd like to examine in parentheses, separated by commas, and finally end with another parenthesis.  (You can think of it like a big function, only where the output isn't a number, but a plot).

Try plotting $x^3+\frac{1}{x}$ between $x=1$ and $x=2$ in the next cell, which is currently empty.


A useful skill to learn is to be able to add a math cell in between existing cells.

As you can see in the picture, when you have the cursor just above a math cell, a thin blue line appears.  Clicking while your cursor in in the blue line will cause another math cell to appear above the current cell.

Try this anywhere you want to on the worksheet, and calculate $2+4$ (or something more interesting, if you want).

In the following cell, I've typed $f(x)=x^2$, and then plotted $f$ between $-2$ and $2$.  Notice that I can just type $f$ in the plot command to do this.

f(x) = sin(x^3) plot(f(x), (x,-2,5)) 

If I wanted to them compare this with $f(x-2)$, I would just plot this, like below.  (Notice that I probably will want to expand my input range in order to see its whole behavior.)

plot(f(x-2), (x,-2,2)) 

There are two ways to compare the two plots.  I can put both functions together, inside of square brackets $[f(x-2),f]$:

plot([f(x-2),f], (x,-2,2)) 

Or I can add the plots themselves with the $+$ symbol.  (Notice that in the one below, I also add a color, to be able to distinguish them.)

plot(f, (x,-2,2)) + plot(f(x-2), (x,-2,2), color='red')