Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168742
Image: ubuntu2004

Project MOSAIC Joint Math Meetings

Ancillary Workshop:

Sage, Calculus, and Modeling

Presenting:

Karl-Dieter Crisman, Gordon College (MA)

Abstract:

This introduction to Sage will be short and sweet.  We'll talk a little bit about how to use the notebook and the range of functionality, but focus mostly on calculus and a little bit of doing modeling.   I'll also show how to use R in the notebook, and perhaps a bit of collaborative functionality in the notebook.

 

 

Thank you to the organizers for inviting me to give this talk!  

We'll do the following:

  • Briefly show how to log in to Sage and start a worksheet
  • Demonstrate a few of the basic commands and range of functionality in Sage
  • Talk about calculus and a bit of modeling
  • Show how to use R in the notebook
  • Maybe talk about collaboration if there is time.

We'll be fairly quick about it, as it's best to learn while doing, so let's get started!

 

There are many Sage servers out there, and some allow anyone to create an account.  

  • The 'official' family of servers at the University of Washington, like sagenb.org, are a great resource.
  • Two other examples I like are the clemix server at Clemson U. and the Groundhog Day one at KAIST in Seoul.
  • There are also many servers at individual colleges that allow only on-campus account creation, and the dahl.calvin.edu server for this workshop.

Basically, there is not so much to do:

  • First, create an account like on a million other websites, and then log in.
  • Next, make a new worksheet (there should be a link for this on the upper left).
  • I'll show you live how to evaluate and create a new mathematics cell.

If you're in this workshop, you should be able to do this fairly easily.

 

So what can you do with Sage?  Well... what do you want to do?

factorial(35)
10333147966386144929666651337523200000000

We can do various plots, as one would expect.

plot(sin(x),(x,-2*pi,2*pi))+plot(diff(sin(x),x),(x,-2*pi,2*pi),color='red',linestyle='--')

There is lots of functionality in various topics.  The graph editor is nice.

graph_editor()

But allows for more advanced usage!

G.automorphism_group()

Interactive examples are fun. I love doing this myself, and is the primary way I use Sage in the classroom.  Can you find the pattern?

@interact def _(p=(7,prime_range(100))): try: T = two_squares(p) html("The prime {0} can be written as ${1}^2+{2}^2$.".format(p,T[0],T[1])) except: html("Looks like the prime {0} can't be written as a sum of squares.".format(p))

 

 

Naturally, you are here to talk about calculus.   Sage has the usual functionality you'd expect from a computer algebra system.

Do you remember how to calculate 11+x2dx  ?\int \frac{1}{1+x^2}\, dx\; ?

integrate(1/(1+x^2),x)

We can do computations inside of the things we are trying to learn about, and typeset them correctly.  (Incidentally, to get nice formulas in between the computational cells, just double click on one of the text areas!)

f(x) = 1/(1+x^2) P = plot(f,(x,-10,10),legend_label='$f(x)=\\frac{1}{1+x^2}$') Q = plot(diff(f,x),(x,-10,10),color='green',legend_label="$f\'(x)=%s$"%latex(diff(f,x)(x))) P+Q

I might want to find help.  Tabbing and the question mark are your ways to success.

density
var('y') g(x,y) = x^2 - y^2 + x^2/4 P3 = density_plot(g,(x,-2,2),(y,-2,2),cmap='jet') Slope = plot_slope_field(g,(x,-2,2),(y,-2,2)) P3+Slope

We can also try to find fits for models of various types, using one of the many components of Sage - in this case, SciPy.

R = [[1,2],[3.45,4],[6,5],[4,3],[1.5,2.3]] var('a,b') model(x) = a*x+b find_fit(R,model)
points(R)+plot(model(a=find_fit(R,model)[0].rhs(),b=find_fit(R,model)[1].rhs()),(x,0,10),color='red')

Because Python, the programming language Sage is based on, is a very powerful tool, we have everything we need to import data.

import csv data = [row for row in csv.reader(open( DATA+'blastdata.csv'))] headers = data[0] data = data[1:]

Now we'll make two possible models for the data from the bomb blast in the earlier talk Danny gave.

var('a,b,c,d') MODEL1(x) = a + b*sqrt(x)+c*x MODEL2(x) = d*x^(0.4)
fit1 = find_fit(data,MODEL1,solution_dict=True) fit2 = find_fit(data,MODEL2,solution_dict=True)

We've now found fits for each model.  Now we just extract the information and plot!

pts = points(data) P1 = plot(MODEL1.subs(fit1),(x,0,.07),color='green') P2 = plot(MODEL2.subs(fit2),(x,0,.07),color='red') pts+P1+P2

Like Danny said, which one is better?

 

As another example of what can be done, let's see how to use R from the notebook.  All I have to do is insert '%r' at the beginning of a cell, and then everything is done in R.  (It's also possible to have a whole worksheet evaluate in a given system.)

Here, I'll first load the library from the MOSAIC project's R package.  You'll need internet for this, of course.

%r install.packages('mosaic');
also installing the dependency ‘Hmisc’ trying URL 'http://cran.r-project.org/src/contrib/Hmisc_3.9-0.tar.gz' Content type 'application/x-gzip' length 572432 bytes (559 Kb) opened URL ================================================== downloaded 559 Kb trying URL 'http://cran.r-project.org/src/contrib/mosaic_0.2-3.tar.gz' Content type 'application/x-gzip' length 839835 bytes (820 Kb) opened URL ================================================== downloaded 820 Kb * installing *source* package ‘Hmisc’ ... ** libs gcc -std=gnu99 -I/Users/karl-dietercrisman/Downloads/sage-4.7.2/local/lib/R/include -I/Users/karl-dietercrisman/Downloads/sage-4.7.2/local/include -fPIC -I/Users/karl-dietercrisman/Downloads/sage-4.7.2/local/include -L/Users/karl-dietercrisman/Downloads/sage-4.7.2/local/lib/ -c Hmisc.c -o Hmisc.o sage_fortran -fPIC -g -O2 -c cidxcn.f -o cidxcn.o sage_fortran -fPIC -g -O2 -c cidxcp.f -o cidxcp.o sage_fortran -fPIC -g -O2 -c hoeffd.f -o hoeffd.o sage_fortran -fPIC -g -O2 -c jacklins.f -o jacklins.o sage_fortran -fPIC -g -O2 -c largrec.f -o largrec.o gcc -std=gnu99 -I/Users/karl-dietercrisman/Downloads/sage-4.7.2/local/lib/R/include -I/Users/karl-dietercrisman/Downloads/sage-4.7.2/local/include -fPIC -I/Users/karl-dietercrisman/Downloads/sage-4.7.2/local/include -L/Users/karl-dietercrisman/Downloads/sage-4.7.2/local/lib/ -c mChoice.c -o mChoice.o gcc -std=gnu99 -I/Users/karl-dietercrisman/Downloads/sage-4.7.2/local/lib/R/include -I/Users/karl-dietercrisman/Downloads/sage-4.7.2/local/include -fPIC -I/Users/karl-dietercrisman/Downloads/sage-4.7.2/local/include -L/Users/karl-dietercrisman/Downloads/sage-4.7.2/local/lib/ -c nstr.c -o nstr.o gcc -std=gnu99 -I/Users/karl-dietercrisman/Downloads/sage-4.7.2/local/lib/R/include -I/Users/karl-dietercrisman/Downloads/sage-4.7.2/local/include -fPIC -I/Users/karl-dietercrisman/Downloads/sage-4.7.2/local/include -L/Users/karl-dietercrisman/Downloads/sage-4.7.2/local/lib/ -c ranksort.c -o ranksort.o sage_fortran -fPIC -g -O2 -c rcorr.f -o rcorr.o gcc -std=gnu99 -I/Users/karl-dietercrisman/Downloads/sage-4.7.2/local/lib/R/include -I/Users/karl-dietercrisman/Downloads/sage-4.7.2/local/include -fPIC -I/Users/karl-dietercrisman/Downloads/sage-4.7.2/local/include -L/Users/karl-dietercrisman/Downloads/sage-4.7.2/local/lib/ -c string_box.c -o string_box.o sage_fortran -fPIC -g -O2 -c wclosest.f -o wclosest.o gcc -std=gnu99 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Users/karl-dietercrisman/Downloads/sage-4.7.2/local/lib/ -o Hmisc.so Hmisc.o cidxcn.o cidxcp.o hoeffd.o jacklins.o largrec.o mChoice.o nstr.o ranksort.o rcorr.o string_box.o wclosest.o -L/Users/karl-dietercrisman/Downloads/sage-4.7.2/local/lib/gcc/i686-apple-darwin8/4.2.3/x86_64 -L/Users/karl-dietercrisman/Downloads/sage-4.7.2/local/lib/gcc/i686-apple-darwin8/4.2.3 -L/Users/karl-dietercrisman/Downloads/sage-4.7.2/local/lib/gcc -L/usr/local/lib -lgfortran -L/Users/karl-dietercrisman/Downloads/sage-4.7.2/local/lib/R//lib -lR -Wl,-framework -Wl,CoreFoundation ** R ** inst ** preparing package for lazy loading Loading required package: splines ** help *** installing help indices ** building package indices ... * DONE (Hmisc) * installing *source* package ‘mosaic’ ... ** R ** data ** moving datasets to lazyload DB ** demo ** inst ** preparing package for lazy loading Loading required package: survival Loading required package: splines Hmisc library by Frank E Harrell Jr Type library(help='Hmisc'), ?Overview, or ?Hmisc.Overview') to see overall documentation. NOTE:Hmisc no longer redefines [.factor to drop unused levels when subsetting. To get the old behavior of Hmisc type dropUnusedLevels(). Attaching package: 'Hmisc' The following object(s) are masked from package:survival : untangle.specials The following object(s) are masked from package:base : format.pval, round.POSIXt, trunc.POSIXt, units Creating a generic for "mean" in package "mosaic" (the supplied definition differs from and overrides the implicit generic in package "base": Classes: "nonstandardGenericFunction", "standardGeneric") In method for function "var": expanding the signature to include omitted arguments in definition: data = "missing" Creating a new generic function for "print" in "mosaic" ** help *** installing help indices ** building package indices ... * DONE (mosaic) The downloaded packages are in ‘/private/var/folders/Yy/YytEJm5VEB0+pBRD7JNLe++++TQ/-Tmp-/RtmpC1m0k6/downloaded_packages’ Updating HTML index of packages in '.Library'
%r library(mosaic);
Loading required package: lattice Loading required package: grid Loading required package: Hmisc Loading required package: survival Loading required package: splines Hmisc library by Frank E Harrell Jr Type library(help='Hmisc'), ?Overview, or ?Hmisc.Overview') to see overall documentation. NOTE:Hmisc no longer redefines [.factor to drop unused levels when subsetting. To get the old behavior of Hmisc type dropUnusedLevels(). Attaching package: 'Hmisc' The following object(s) are masked from package:survival : untangle.specials The following object(s) are masked from package:base : format.pval, round.POSIXt, trunc.POSIXt, units Attaching package: 'mosaic' The following object(s) are masked from package:Hmisc : do The following object(s) are masked from package:stats : binom.test, D, median, prop.test, sd, var The following object(s) are masked from package:base : max, mean, min, print, print.function, sample

We can use the functions from the package immediately.

%r do(5)*3
result 1 3 2 3 3 3 4 3 5 3

Graphics work, too, if you create a graphics device.  Here, I'm using the D and fplot functions from the MOSAIC project's R package.

%r quartz() fplot(sin,xlim=c(-2*pi,2*pi)) fplot(D(sin),xlim=c(-2*pi,2*pi))

If you want to do a whole worksheet in R (or GAP, or Octave, or even Mathematica, if you own it) that is supported as well.

 

Finally, how might one use Sage in terms of pedagogy?  Here, the answer is just as broad as your own interests, and I'll focus on collaboration.  Here are several ideas.

  • Basic 'cheat sheet' of functionality for checking answers or doing computations

For example, in a calculus class, just giving a worksheet to upload with prototypes of syntax could help a lot for routine problems.

h(x) = sin(e^(-x^2)) derivative( h, x); show(derivative( h, x ), x))
  • Students working together on a project or lab can share their worksheets with each other and/or you, allowing for easy collaboration.  

Just click the 'Share' button at the top and fill in the usernames of the people who should share the worksheet!  Some people use this for turning in labs, for instance.

  • Publishing a worksheet so others can try it.

With this option, anyone on the server can look at your worksheet, and edit a copy for themselves.  This is especially effective when you want to post something for students to try out or use for homework, or to make easily available to colleagues elsewhere.

In fact, I'm going to publish THIS VERY WORKSHEET right now on the server for this workshop!  And then you can download it and try it yourself.

 

Well, I think that's all we have time for.  Please see www.sagemath.org for more discussion of download options for all platforms, places to try out Sage, and discussion forums.  We value your feedback!

This talk is also available at http://www.sagenb.org/home/pub/3932 .

Questions?

var('x,y') plot3d(sin(x^2+y^2),(x,-3,3),(y,-3,3),plot_points=200)