Tutorial: Symbolic Calculus system:sage

Creating Symbolic Expressions

Use the var command to define some symbolic variables.  You can separate the variables by commas or spaces in the var command.

Tip: Press shift-enter to evaluate an input cell (instead of clicking "evaluate").

{{{id=5| var('x y z epsilon') /// }}} {{{id=6| cos(x^3) - y^2*z + epsilon /// }}} {{{id=7| /// }}} {{{id=0| /// }}}

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| /// }}} {{{id=19| /// }}} {{{id=11| /// }}} {{{id=10| /// }}}

Most standard functions are defined in Sage.  They are named lowercase, much like in Maple. 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))^3 /// }}} {{{id=2| /// }}}

Problem: Construct the symbolic expresion $\sin(x^{\cos(y)} + \theta) + \coth(2x) + \log(3x)\cdot \exp(y^3)$. 

{{{id=17| /// }}} {{{id=26| /// }}}

Making substitutions

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) /// }}} {{{id=25| f.subs(x=y, y=x) /// }}} {{{id=31| /// }}}

Problem: Replace $x$ by $\sin(y)-x$ in the expression $x^3 + x y$.

{{{id=32| /// }}} {{{id=35| /// }}}

Expanding Expressions

To expand a symbolic expression with exponents, use the expand method.

{{{id=34| var('x,y') f = (x+2*y)^3 f /// }}} {{{id=30| f.expand().show() # tip -- using show makes the output nicer /// }}} {{{id=38| /// }}}

Problem: Expand the expression $(2\sin(x)  - \cos(y))^5$.

{{{id=37| /// }}} {{{id=16| /// }}} {{{id=15| /// }}}

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.

 

{{{id=13| f(x,y) = x^3 + y f /// }}} {{{id=9| f(2,3) /// }}} {{{id=21| f(pi,e) /// }}} {{{id=46| /// }}}

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| /// }}} {{{id=44| /// }}} {{{id=22| /// }}}

2D Plotting

Use the plot command to plot a function of 1 variable.  TIP: Type plot(<tab key> to find out much more about the plot command.

{{{id=51| var('x') plot(sin(x^2), (x,-3,3)) /// }}} {{{id=40| /// }}}

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)), 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, c=Color('blue') ): show(plot(f, (x,r[0],r[1]), color=c, 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) /// }}} {{{id=54| /// }}}

Problem: Use the above interactive plotter to draw the following plot of $\sin(x^2)$.

{{{id=57| /// }}} {{{id=56| /// }}}

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) /// }}} {{{id=52| /// }}}

Problem: Draw 3 concentric circles that are red, green and blue.  [Hints: Use rgbcolor, and give the aspect_ratio=1 option to the show command, as above.]

{{{id=48| /// }}} {{{id=42| /// }}}

3D Plotting

You can also plot functions of two variables using the plot3d command.  Also, there are line3d, sphere, text3d, cube, parametric_plot3d, etc. commands.

{{{id=65| var('x,y') plot3d(sin(x-y)*cos(x-y^2),(x,-2,2),(y,-2,2)) /// }}} {{{id=69| /// }}}

Problem: Draw a 3d plot of the function $4x e^{-x^2-y^2}$.

{{{id=73| /// }}} {{{id=72| /// }}} {{{id=68| /// }}}

Here we use the parametric_plot3d command to draw a 3d green transparent conchoid.

{{{id=63| var('u,v') k = 1.2; k_2 = 1.2; a = 1.5 f = (k^u*(1+cos(v))*cos(u), k^u*(1+cos(v))*sin(u), k^u*sin(v)-a*k_2^u) parametric_plot3d(f, (u,0,6*pi), (v,0,2*pi), color=(0,0.5,0), opacity=0.7) /// }}} {{{id=62| /// }}} {{{id=61| /// }}} {{{id=76| /// }}}

Computing Integrals and Derivatives

You can symbolically integrate or differentiate functions, compute limits, Taylor polynomials, etc.

{{{id=81| var('x') integrate(x^2, x) /// }}} {{{id=82| show(integrate(sin(x)+tan(2*x),x)) /// }}} {{{id=78| f = sin(x) - cos(y*x) + 1/(x^3+1) g = f.integrate(x) show(g) /// }}} {{{id=24| bool(g.differentiate(x) == f) /// }}} {{{id=79| h = sin(x)*cos(x) show(h.taylor(x, 1, 2)) /// }}} {{{id=84| @interact def ex_taylor(n=(1..10)): h(x) = sin(x)*cos(x) show(plot(h,-1,4,thickness=2) + plot(h.taylor(x,1,n),-1,4, color='red'), ymin=-1,ymax=1) /// }}} {{{id=88| /// }}}

Problem: Create an interact like the one above that allows you to input the function $h$ instead of it being hardcoded.

{{{id=87| /// }}} {{{id=86| /// }}} {{{id=85| /// }}}