Ask Your Question

Comparing Sage and Mathematica for an Introductory Course

asked 2012-05-07 01:25:09 +0200

jaia gravatar image

I'm working with a committee that's revising the undergraduate math curriculum for UCLA biology majors. We're getting rid of a lot of paper and pencil calculation that's hardly ever used in mathematical biology and introducing a lot of modeling, dynamical systems and computer work. Earlier, we decided to use Mathematica for the course, but I recently learned about Sage and like the idea of using a free program with similar features to Mathematica.

While the students will be learning some programming during the course, that's not our emphasis and they need to be able to use a fairly high level of interactivity from the beginning. (For example, they should be able to interactively manipulate function parameters in the first or at worst second lab session.) Since I'm very much a newcomer to Sage, I want to ask more experienced users how hard it would be to reproduce the functionality of this Mathematica notebook in Sage. I've seen the slider examples, but they use quite a bit of code. An equivalent to Equation Trekker, which allows you to specify initial condition for a two-ODE system by clicking on the phase plane, would also be very useful. Has anyone coded such a thing for Sage?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2012-05-07 10:43:47 +0200

Jason Grout gravatar image

updated 2012-05-07 10:48:53 +0200

See this cell for the main calculations in that notebook.

# 2d vector plot
f(x,y)=(1.25*x-0.25*x*y, 0.05*x*y-0.1*y)
plot_vector_field(f, (x, 0, 10), (y, 0, 10)).show()

# 3d vector plot
plot_vector_field3d(g, (x,-3,3),(y,-3,3),(z,-3,3)).show()

# interactive sin plot
def _(a=(0,5)):

# interactive vector plot
def _(r=(1,4), a=(0.01,0.5), c=(0.01,0.5), d=(0.01,0.5)):
    f(x,y)=(r*x - a*x*y, a*c*x*y - d*y)

You might also be interested in this worksheet, which involves some custom code at the top, which I hope we can polish and submit to Sage sometime.

Right now, we don't have an easy way to make a plot clickable to create initial conditions. You can set initial conditions using sliders, though. Help is welcome to make plots clickable!

You might be interested in the MAA PREP course, which introduces faculty to using Sage in the classroom, helps them design interacts, etc. See

edit flag offensive delete link more


Thanks, that looks really good. Since the clickable phase plane will only be used for a few weeks of a year-long course, that probably doesn't justify Mathematica use, especially since XPP has the same functionality. BTW, there's a Python XPP interface that may be useful to people trying to code clickable plots. One thing I'm concerned about now is jumpiness. When I use sliders, say in the sin plot example, there's a noticeable delay between my moving the slider and seeing the new plot. This happens even when I run Sage on my own computer. Can anything be done about it? (I'm using Firefox 12.0.)

jaia gravatar imagejaia ( 2012-05-07 16:17:23 +0200 )edit

How long is the delay you are talking about?

Jason Grout gravatar imageJason Grout ( 2012-05-07 19:45:33 +0200 )edit

Maybe a second or so, maybe a bit less. Definitely noticeable. Part of it seems to be the plot refreshing.

jaia gravatar imagejaia ( 2012-05-08 03:11:22 +0200 )edit

Yes, that delay is normal. The calculation is done on the server, a new image is saved on the server, then the new image is downloaded. We are working on a way to make things faster by using javascript plots, but it will probably be a while before that is in Sage.

Jason Grout gravatar imageJason Grout ( 2012-05-08 05:16:29 +0200 )edit

@jaia I wrote an ad-hoc javascript solution for the delay a while ago - see my answer to . It isn't nearly good enough for distribution, but assuming you don't care about generality, it shouldn't take too much fiddling to make it do what you want.

chaesloc2 gravatar imagechaesloc2 ( 2012-05-09 20:58:58 +0200 )edit

answered 2012-05-07 12:46:34 +0200

calc314 gravatar image

updated 2012-05-07 12:47:39 +0200

I used Sage with my advanced math modeling course this semester and did a fair amount of biology in there. Generally, I did not ask the students to do much "from the ground up" coding, although most of them in that group could have. I focused on making worksheets that the students could interact with fairly easily. You can see some samples published at the links below. Most of these use code similar to what Jason mentions above. I've added some options that were needed for my class (nullclines, direction fields, access to the solution coordinates, etc.). This part of the code is lengthy, but it is hidden using "%hide" and automatically executed using "%auto".

Note that the SIR Lab ( ) uses the SciPy leastsqrs function to fit the data. This is a useful tool for a course in which you want students to fit data but not have to deal with the details.

Budworm model:

Modeling a parachutist:


Solving systems:


Vector Fields:

Advection-Diffusion-Reaction Equation:

edit flag offensive delete link more


Now I finally know who @calc314 is! You have been quietly building a substantial presence here :) Maybe someday I'll get to teach such a course - great resources!

kcrisman gravatar imagekcrisman ( 2012-05-07 22:48:16 +0200 )edit

Thanks! :)

calc314 gravatar imagecalc314 ( 2012-05-15 22:16:32 +0200 )edit

For each of the above links I getting a page with the message "The worksheet does not exist". How do I see any of the links? I want my daughter who is in highschool to take a look at these.

matrimc gravatar imagematrimc ( 2012-12-07 18:19:15 +0200 )edit

I'm not sure - I think that @calc314 must have taken them down, because that server does have public worksheets, but these particular ones are no longer up. Actually, calc314, if you could put all of those together into one resource that would make an excellent "thematic tutorial" for using Sage in modeling...

kcrisman gravatar imagekcrisman ( 2012-12-08 08:09:14 +0200 )edit

Yes, I took them down when I had to move much of my work off of that server before the next class of PREP workshop folks started. I'll work on getting those together for a "thematic tutorial." Might be a few days before I can pull this off, though.

calc314 gravatar imagecalc314 ( 2012-12-09 00:43:56 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2012-05-07 01:25:09 +0200

Seen: 2,296 times

Last updated: May 07 '12