All published worksheets from http://sagenb.org
Image: ubuntu2004
Math 480b: May 5, 2010
Basic Symbolic Calculus in Sage
William Stein
University of Washington
Creating Symbolic Expressions
The "symbolic variable" or indeterminate x is predefined when you startup Sage:
Use the var command to define some symbolic variables. You can separate the variables by commas or spaces in the var command.
NOTE: If you want symbolic variables to just "magically" spring into existence when you use them, type automatic_names(True). To turn this off, type automatic_names(False).
Using automatic_names is usually a bad idea, since it can easily lead to subtle bugs:
Problem: Create the following expressions: .
Tip: that you must put in an asterisk (*) for multiplication.
Tip: You can edit the HTML between cells by double clicking on it. You can create new HTML areas by shift-clicking on the blue bar that appears just above a compute cell.
Most standard functions are defined in Sage. They are named lowercase, e.g.,
sin, cos, tan, sec, csc, cot, sinh, cosh, tanh, sech, csch, coth, log, exp, etc.
Problem: Construct the symbolic expresion .
Making substitutions
Use the subs method to replace any variables by other variables.
Problem: Replace by in the expression .
Expanding Expressions
To expand a symbolic expression with exponents, use the expand method (or function) -- we saw this above:
Problem: Expand the expression .
Symbolic Unit Conversions
As of Sage-4.4, Sage has a massive symbolic unit conversion package, which was written by David Ackerman (a UW undergrad) recently, as a funded student project after he took Math 480 (the Sage class) last year.
Creating Symbolic Functions
To create a symbolic function, use the notation f(x,y) = x^3 + y. A symbolic function is just like a symbolic expression, except you can call it without having to explicitly use subs or name variables and be sure that the order is what you want.
Problem: Create the functions .
2D Plotting
Use the plot command to plot a function of one variable. TIP: Type plot(<tab key> to find out much more about the plot command.
The plot command has many options:
Here's a the same plot, but you can adjust many of the parameters to the plot command interactively.
Problem: Use the above interactive plotter to draw the following plot of .
You can plot many other things, including polygons, parametric plots, polar plots, implicit plots, etc.:
line, polygon, circle, text, polar_plot, parametric_plot, circle, implicit_plot
You superimpose plots using +.
You can also do 3D plots, including parametric lines, parametric surfaces, implicit 3d plots, etc.
More Symbolic Calculus: Computing Integrals and Derivatives
You can symbolically integrate or differentiate functions, compute limits, Taylor polynomials, etc.
Symbolic Taylor Series
Other Options for Symbolic Calculus
-
Maxima -- a powerful symbolic computer algebra system, which is fully usable from Sage, and included in Sage. Sometimes you want to do something, e.g., solve some weird differential equation or evaluate a special functions, and you search the web, find how to do it in Maxima, and can then... paste and do the same in Sage via the Maxima interface.
- SymPy -- a powerful pure Python library for symbolic Calculus, written almost entirely by Physics graduate students. This can be installed into anything that you can run Python on, and provides a nice range of capabilities. Sage uses it some, and it is included in Sage.