Tutorial: 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.

{{{id=5| var('x y z epsilon') /// (x, y, z, epsilon) }}} {{{id=6| cos(x^3) - y^2*z + epsilon /// -y^2*z + cos(x^3) + 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)$.

Note that you must put in an asterisk (*) for multiplication.

{{{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 /// sec(x*csc(y))^3 + cos(y) - tan(x/y) + sin(x) }}} {{{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) /// cos(y) + y + sin(5) - 25 }}} {{{id=25| f.subs(x=y, y=x) /// sin(y) - y^2 + cos(x) + 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 /// (2*y + x)^3 }}} {{{id=30| f.expand().show() # tip -- using show makes the output nicer ///
{8 {y}^{3} } + {{12 x} {y}^{2} } + {{6 {x}^{2} } y} + {x}^{3}
}}} {{{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 /// (x, y) |--> y + x^3 }}} {{{id=9| f(2,3) /// 11 }}} {{{id=21| f(pi,e) /// (pi + 2*e)^3 }}} {{{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| /// }}}

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=40| var('x') plot(sin(x^2), (x,-3,3)) /// }}} {{{id=42| /// }}} {{{id=23| /// }}} {{{id=24| /// }}}