Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168703
Image: ubuntu2004





Calculus in Sage

William Stein

2009-05-16 at Sage Days 16

 

 


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").

var('x y z epsilon')
(x, y, z, epsilon)
cos(x^3) - y^2*z + epsilon
-y^2*z + cos(x^3) + epsilon

Examples: Create the following expressions: sin5(x)cos2(x),x3x3+1,kP(1PK)\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.

sin(x)^5*cos(x)^2
cos(x)^2*sin(x)^5
x^3/(x^3+1)
x^3/(x^3 + 1)
var('k,K,P') k*P * (1-P/K)
k*P*(1 - P/K)

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.

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)

Example: Construct the symbolic expresion sin(xcos(y)+θ)+coth(2x)+log(3x)exp(y3)\sin(x^{\cos(y)} + \theta) + \coth(2x) + \log(3x)\cdot \exp(y^3)

var('x,y,theta') show(sin(x^cos(y)+theta) + coth(2*x) + log(3*x) * exp(y^3))
{\log \left( {3 x} \right) {e}^{{y}^{3} } } + \sin \left( {x}^{\cos \left( y \right)} + \theta \right) + \coth \left( {2 x} \right)

Making substitutions

Use the subs method to replace any variables by other variables.

var('x,y') f = sin(x) + cos(y) - x^2 + y
f.subs(x=5)
cos(y) + y + sin(5) - 25
f.subs(x=y, y=x)
sin(y) - y^2 + cos(x) + x

Example: Replace xx by sin(y)x\sin(y)-x in the expression x3+xyx^3 + x y.

var('x,y') f = x^3 + x*y f.subs(x=sin(y)-x)
(sin(y) - x)^3 + y*(sin(y) - x)

Expanding Expressions

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

var('x,y') f = (x+2*y)^3 f
(2*y + x)^3
f.expand().show() # tip -- using show makes the output nicer
{8 {y}^{3} } + {{12 x} {y}^{2} } + {{6 {x}^{2} } y} + {x}^{3}

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

f = (2*sin(x) - cos(y))^5 f.expand()
-cos(y)^5 + 10*sin(x)*cos(y)^4 - 40*sin(x)^2*cos(y)^3 + 80*sin(x)^3*cos(y)^2 - 80*sin(x)^4*cos(y) + 32*sin(x)^5

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.

 

f(x,y) = x^3 + y f
(x, y) |--> y + x^3
f(2,3)
11
f(pi,e)
pi^3 + e

Problem: Create the functions xx3+1,(x,y)sin(x)cos(y)/y,(a,x,θ)ax+θ2x\mapsto x^3 + 1, \qquad (x, y) \mapsto \sin(x) - \cos(y)/y, \qquad (a,x,\theta)\mapsto a x + \theta^2.

f(x) = x^3 + 1 f
x |--> x^3 + 1
f(x,y) = sin(x)-cos(y)/y f
(x, y) |--> sin(x) - cos(y)/y
f(a,x,theta) = a*x+theta^2 show(f)
\left(a, x, \theta \right)\ {\mapsto}\ {a x} + {\theta}^{2}

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.

var('x') plot(sin(x^2), (x,-3,3))

Here's a the same plot, but you can adjust many of the parameters to the plot command interactively.

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)

Example: I made the following using the above interactive plotter with sin(x2)\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 +.

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)

Example: 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.]

G = circle((0,0),1,rgbcolor='red') + circle((0,0),2,rgbcolor='green') G = G + circle((0,0),3,rgbcolor='blue') G.show(aspect_ratio=1)

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.   Bill Cauchois will demo 3d plotting much more in the next talk.

var('x,y') plot3d(sin(x-y)*cos(x-y^2),(x,-2,2),(y,-2,2))

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

f(x,y) = 4*x*e^(-x^2-y^2) plot3d(f,(x,-2,2),(y,-2,2))

Computing Integrals and Derivatives

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

var('x') integrate(x^2, x)
x^3/3
show(integrate(sin(x)+tan(2*x),x))
\frac{\log \left( \sec \left( {2 x} \right) \right)}{2} - \cos \left( x \right)
f = sin(x) - cos(y*x) + 1/(x^3+1) g = f.integrate(x) show(g)
\frac{-\sin \left( {x y} \right)}{y} - \frac{\log \left( {x}^{2} - x + 1 \right)}{6} + \frac{\tan^{-1} \left( \frac{{2 x} - 1}{\sqrt{ 3 }} \right)}{\sqrt{ 3 }} + \frac{\log \left( x + 1 \right)}{3} - \cos \left( x \right)
bool(g.differentiate(x) == f)
True
h = sin(x)*cos(x) show(h.taylor(x, 1, 2))
{\cos \left( 1 \right) \sin \left( 1 \right)} + {\left( -{\sin \left( 1 \right)}^{2} + {\cos \left( 1 \right)}^{2} \right) \left( x - 1 \right)} - {{{2 \cos \left( 1 \right)} \sin \left( 1 \right)} {\left( x - 1 \right)}^{2} }
@interact def ex_taylor(n=(1..10), h=sin(x)*cos(x)): h = SR(h) show(plot(h,-1,4,thickness=2) + plot(h.taylor(x,1,n),-1,4, color='red'), ymin=-1,ymax=1)