Math 480b: May 5, 2010
Basic Symbolic Calculus in Sage
William Stein
University of Washington
{{{id=89| /// }}}
The "symbolic variable" or indeterminate x is predefined when you startup Sage:
{{{id=104| x /// x }}} {{{id=106| (x+2)^3 /// (x + 2)^3 }}} {{{id=150| parent(x) /// Symbolic Ring }}} {{{id=152| type(x) ///Use the var command to define some symbolic variables. You can separate the variables by commas or spaces in the var command.
{{{id=108| reset() x + y /// Traceback (most recent call last): File "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).
{{{id=93| automatic_names(True) /// }}} {{{id=92| expand( (fred + sam + verlkja + anything)^3 ) /// anything^3 + 3*anything^2*fred + 3*anything^2*sam + 3*anything^2*verlkja + 3*anything*fred^2 + 6*anything*fred*sam + 6*anything*fred*verlkja + 3*anything*sam^2 + 6*anything*sam*verlkja + 3*anything*verlkja^2 + fred^3 + 3*fred^2*sam + 3*fred^2*verlkja + 3*fred*sam^2 + 6*fred*sam*verlkja + 3*fred*verlkja^2 + sam^3 + 3*sam^2*verlkja + 3*sam*verlkja^2 + verlkja^3 }}}Using automatic_names is usually a bad idea, since it can easily lead to subtle bugs:
{{{id=109| theta^3 - x + y + thetta # "thetta" -- a typo! /// theta^3 + thetta - x + y }}} {{{id=0| automatic_names(False) /// }}} {{{id=96| x + y + askdf /// Traceback (most recent call last): File "Problem: Create the following expressions: $\sin^5(x)\cos^2(x), \qquad \displaystyle \frac{x^3}{x^3 + 1}, \qquad k\cdot P \cdot \left(1 - \frac{P}{K}\right)$.
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.
{{{id=20| var('k K P') /// (k, K, P) }}} {{{id=11| k * P * (1 - P/K) /// -(P/K - 1)*P*k }}} {{{id=10| implicit_multiplication(True) /// }}} {{{id=164| /// }}} {{{id=167| k P *(1 - P/K) /// -(P/K - 1)*P*k }}} {{{id=166| k K x sin(y) /// K*k*x*sin(y) }}} {{{id=165| implicit_multiplication(False) /// }}}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.
{{{id=8| var('x,y') sin(x) + cos(y) - tan(x/y) + sec(x*csc(y))^5 /// sec(x*csc(y))^5 + sin(x) + cos(y) - tan(x/y) }}} {{{id=95| /// }}} {{{id=2| /// }}}Problem: Construct the symbolic expresion $\sin(x^{\cos(y)} + \theta) + \coth(2x) + \log(3x)\cdot \exp(y^3)$.
{{{id=17| var('x, y, theta') show(sin(x^cos(y) + theta) + coth(2*x) + log(3*x) * exp(y^3)) ///Use the subs method to replace any variables by other variables.
{{{id=29| var('x,y') f = sin(x) + cos(y) - x^2 + y /// }}} {{{id=28| f.subs(x=5) /// y + sin(5) + cos(y) - 25 }}} {{{id=25| f.subs(x=y, y=x) /// -y^2 + x + sin(y) + cos(x) }}} {{{id=31| f /// -x^2 + y + sin(x) + cos(y) }}}Problem: Replace $x$ by $\sin(y)-x$ in the expression $x^3 + x y$.
{{{id=32| f = x^3 + x*y /// }}} {{{id=35| f.subs(x = sin(y)-x) /// -(x - sin(y))^3 - (x - sin(y))*y }}} {{{id=170| /// }}} {{{id=169| /// }}}To expand a symbolic expression with exponents, use the expand method (or function) -- we saw this above:
{{{id=34| var('x,y') f = (x+2*y)^3 f /// (x + 2*y)^3 }}} {{{id=30| f.expand().show() # tip -- using show makes the output nicer ///Problem: Expand the expression $(2\sin(x) - \cos(y))^5$.
{{{id=37| /// }}} {{{id=16| /// }}}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.
{{{id=121| a = units.length.rope /// }}} {{{id=171| a.convert(units.length.meter) /// 762/125*meter }}} {{{id=174| a = 170*units.mass.pound /// }}} {{{id=172| a.convert(units.mass.dalton) /// 4.64371586715522e28*dalton }}} {{{id=173| a.convert(units.mass.drachma) /// Traceback (most recent call last): File "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.
{{{id=102| f = y + x^3 /// }}} {{{id=103| f.subs(y=2,x=3) /// 29 }}} {{{id=179| preparse('f(y,x) = x^3 + y') /// '__tmp__=var("y,x"); f = symbolic_expression(x**Integer(3) + y).function(y,x)' }}} {{{id=13| f(y,x) = x^3 + y f /// (y, x) |--> x^3 + y }}} {{{id=9| f(2,3) /// 29 }}} {{{id=21| f(pi,e) /// pi + e^3 }}} {{{id=46| f._fast_float_ ///
Problem: Create the functions $x\mapsto x^3 + 1, \qquad (x, y) \mapsto \sin(x) - \cos(y)/y, \qquad (a,x,\theta)\mapsto a x + \theta^2$.
{{{id=45| f(x) = x^3 + 1 show(f) ///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.
{{{id=51| var('x') plot(sin(x^2), (x,-10,3)) ///The plot command has many options:
{{{id=111| plot(sin(x^2), (x,-3,3), thickness=5, color='orange', gridlines=True, frame=True) ///Here's a the same plot, but you can adjust many of the parameters to the plot command interactively.
{{{id=50| var('x') @interact def plot_example(f=sin(x^2),r=range_slider(-5,5,step_size=1/4,default=(-3,3)), color=color_selector(widget='colorpicker'), thickness=(3,(1..10)), adaptive_recursion=(5,(0..10)), adaptive_tolerance=(0.01,(0.001,1)), plot_points=(20,(1..100)), linestyle=['-','--','-.',':'], gridlines=False, fill=False, frame=False, axes=True ): show(plot(f, (x,r[0],r[1]), color=color, thickness=thickness, adaptive_recursion=adaptive_recursion, adaptive_tolerance=adaptive_tolerance, plot_points=plot_points, linestyle=linestyle, fill=fill if fill else None), gridlines=gridlines, frame=frame, axes=axes) ///
|
Problem: Use the above interactive plotter to draw the following plot of $\sin(x^2)$.
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 +.
{{{id=53| var('x') P = circle((0,0),1) + polar_plot(2 + 2*cos(x), (x, 0, 2*pi), rgbcolor='red')+ plot(sin(x^2),(x,0,4)) show(P, aspect_ratio=1) ///You can also do 3D plots, including parametric lines, parametric surfaces, implicit 3d plots, etc.
{{{id=117| var('x y') plot3d(sin(pi*(x^2+y^2))/2,(x,-1,1),(y,-1,1), opacity=.7) + octahedron(color='red') /// }}} {{{id=116| f = sin(x).plot() /// }}} {{{id=187| f.plot3d() /// }}} {{{id=76| /// }}}You can symbolically integrate or differentiate functions, compute limits, Taylor polynomials, etc.
{{{id=81| var('x') integrate(x^2, x) /// 1/3*x^3 }}} {{{id=82| integrate(sin(x)+tan(2*x),x) /// 1/2*log(sec(2*x)) - cos(x) }}} {{{id=130| integrate(sin(x)+tan(2*x),x, algorithm='sympy') /// 1/4*log(tan(2*x)^2 + 1) - cos(x) }}} {{{id=78| f = sin(x) - cos(y*x) + 1/(x^3+1) g = f.integrate(x) show(g) ///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.