︠70d42de2-a80d-45a8-ad60-2186b302540bi︠ %html





Calculus in Sage

William Stein

2009-05-16 at Sage Days 16

 

 


︡934334b2-1234-4de3-b30b-fc91f7b61416︡{"html": "


\n


\n


\n


\n

Calculus in Sage

\n

William Stein

\n

2009-05-16 at Sage Days 16

\n

\n\n\n\n\n\n\n\n
\n

 

\n

\"\"

\n

 

\n
\n

\"\"

\n
\n

"}︡ ︠b1e1d81b-392d-4c69-9060-1565684b3501i︠ %html

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

︡88593d6f-fe94-4162-b0ac-5d95d0799e84︡{"html": "

Creating Symbolic Expressions

\r\n

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

\r\n

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

"}︡ ︠cc5c9756-4c36-4893-be1f-291a03e6b006︠ var('x y z epsilon') ︡5d0996b8-5a96-4351-9fe7-475b1bc0aec4︡{"stdout": "(x, y, z, epsilon)"}︡ ︠330eb9b5-ab03-471b-a000-e18d1e7db4ea︠ cos(x^3) - y^2*z + epsilon ︡593f35be-b644-4a3d-b57e-60498fdeaf6e︡{"stdout": "-y^2*z + cos(x^3) + epsilon"}︡ ︠364e4e4d-c732-4eef-91f8-2129b089c774i︠ %html

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

︡0ef1d048-006b-41ac-ac5e-aa7c8c5c23d1︡{"html": "

Examples: 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)$.

\n

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

"}︡ ︠648bf9fe-9457-484a-bf51-39b6a0c9d529︠ sin(x)^5*cos(x)^2 ︡c85b653f-a2b8-4419-8c11-df6fa4f3e8f5︡{"stdout": "cos(x)^2*sin(x)^5"}︡ ︠8dd8fce6-2c0a-4014-abc7-c102610c477d︠ x^3/(x^3+1) ︡df2d65cf-68bc-4bc8-b32a-fb9087f04038︡{"stdout": "x^3/(x^3 + 1)"}︡ ︠e517ff1c-b9e5-4650-867c-4bff1507b4ca︠ var('k,K,P') k*P * (1-P/K) ︡fd0e74a8-67d8-4ce5-91f9-98cc90506d92︡{"stdout": "k*P*(1 - P/K)"}︡ ︠2d988611-2836-4716-81d8-94ca2970e970i︠ %html

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.

︡15076d39-2842-4c68-80b6-4b442322645a︡{"html": "

Most standard functions are defined in Sage.  They are named lowercase, much like in Maple. E.g.,

\r\n

sin, cos, tan, sec, csc, cot, sinh, cosh, tanh, sech, csch, coth, log, exp, etc.

"}︡ ︠5485e6d8-89d3-4d6b-ae5e-9a9f753425c4︠ var('x,y') sin(x) + cos(y) - tan(x/y) + sec(x*csc(y))^3 ︡2acff958-a612-4b8b-93a2-ee3c44276ec5︡{"stdout": "sec(x*csc(y))^3 + cos(y) - tan(x/y) + sin(x)"}︡ ︠3b319090-4bf0-4a15-8a47-8be7427898a6i︠ %html

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

︡1e1c19b0-2b2a-4287-9f5c-e484e12fd029︡{"html": "

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

"}︡ ︠f06662b2-a919-4558-b41f-bbc112037aa5︠ var('x,y,theta') show(sin(x^cos(y)+theta) + coth(2*x) + log(3*x) * exp(y^3)) ︡881024f5-0109-4587-82d5-2df38c3b6c34︡{"html": "
{\\log \\left( {3 x} \\right) {e}^{{y}^{3} } } + \\sin \\left( {x}^{\\cos \\left( y \\right)} + \\theta \\right) + \\coth \\left( {2 x} \\right)
"}︡ ︠b13aeb3d-11c9-4fa6-a5e1-4652365c9d4ai︠ %html

Making substitutions

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

︡d2ef74bb-df8d-44ee-9210-34fb31118f75︡{"html": "

Making substitutions

\r\n

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

"}︡ ︠5200130b-6ed5-4b2a-871e-5d5d8ca56b9c︠ var('x,y') f = sin(x) + cos(y) - x^2 + y ︡29ed36d1-5c69-4540-9e5f-6c1263f11441︡︡ ︠b2f91c0d-10f8-4841-b9ff-87fb82eb7c95︠ f.subs(x=5) ︡8ed4f81c-55f7-40b2-9f14-df411424bc00︡{"stdout": "cos(y) + y + sin(5) - 25"}︡ ︠0bb5949f-c606-4945-8e51-511a936672d1︠ f.subs(x=y, y=x) ︡26da64fa-85ca-4d6d-b717-3becf1b6bd6d︡{"stdout": "sin(y) - y^2 + cos(x) + x"}︡ ︠ef95ffc0-f902-4f97-a2cd-754b4293eb77i︠ %html

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

︡f91e1b84-7a74-4e1f-aa8c-5086706fde72︡{"html": "

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

"}︡ ︠6650e282-a7ed-4275-8a6d-b187111184a4︠ var('x,y') f = x^3 + x*y f.subs(x=sin(y)-x) ︡917d16a7-7650-41eb-9ac6-0e7a2b31f77b︡{"stdout": "(sin(y) - x)^3 + y*(sin(y) - x)"}︡ ︠68ada52d-a098-4278-9e0a-8081a5311c2fi︠ %html

Expanding Expressions

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

︡ec6f2f56-2454-431a-88ce-ee0713521ee9︡{"html": "

Expanding Expressions

\r\n

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

"}︡ ︠6e2225c7-ae93-47fc-b2c3-a974b8562796︠ var('x,y') f = (x+2*y)^3 f ︡20aaf512-4b31-479a-bd56-606b1400f3cb︡{"stdout": "(2*y + x)^3"}︡ ︠160eea6a-d322-4320-b26b-c3aa19b5c803︠ f.expand().show() # tip -- using show makes the output nicer ︡9be4068c-15ec-4676-8dda-0df679ed9553︡{"html": "
{8 {y}^{3} } + {{12 x} {y}^{2} } + {{6 {x}^{2} } y} + {x}^{3}
"}︡ ︠6fda4174-19aa-44c4-90d3-c166eabd610ci︠ %html

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

︡a2077412-aea3-429b-8e6c-266c621244f4︡{"html": "

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

"}︡ ︠4b8ee132-0fa7-4b04-b307-932e68893f1c︠ f = (2*sin(x) - cos(y))^5 f.expand() ︡f273278c-5993-4be4-9309-7335a564d584︡{"stdout": "-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"}︡ ︠3f6983ec-3c64-45e7-ac35-8c4f9c4a25e1i︠ %html

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.

 

︡89f6a86a-461f-40c7-a9b4-8554ffeb1f31︡{"html": "

Creating Symbolic Functions

\r\n

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.

\r\n

 

"}︡ ︠fdd0f99c-cd38-4d06-aec6-dea9dec8d170︠ f(x,y) = x^3 + y f ︡b320703e-8ae1-438a-bf99-33fcdd92cc95︡{"stdout": "(x, y) |--> y + x^3"}︡ ︠77c09fab-c5d3-4911-93f5-c1999c09e4ce︠ f(2,3) ︡2b248622-4306-41b4-8fb6-bd3bc7a02dc6︡{"stdout": "11"}︡ ︠85938322-8725-4299-99d0-cfe60a698c63︠ f(pi,e) ︡52c90818-47f7-416c-8da6-05963499dd1c︡{"stdout": "pi^3 + e"}︡ ︠1c734c0d-d704-489a-b29a-8dc37bfb0539i︠ %html

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$.

︡b79b50d3-1059-4632-933e-2a3445c98456︡{"html": "

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$.

"}︡ ︠6ae7426f-aa4f-42a0-b360-cb290e23f1fb︠ f(x) = x^3 + 1 f ︡5c09681a-34a5-44b4-a8db-4ca37b82f836︡{"stdout": "x |--> x^3 + 1"}︡ ︠644a8150-560a-4786-b510-08d66be8057c︠ f(x,y) = sin(x)-cos(y)/y f ︡9cbc7ce2-1b35-4842-8972-d9c5843a9347︡{"stdout": "(x, y) |--> sin(x) - cos(y)/y"}︡ ︠05f44685-1429-498c-92fd-fea363652143︠ f(a,x,theta) = a*x+theta^2 show(f) ︡bf53c2a2-b4ca-45d7-9569-130f48999d48︡{"html": "
\\left(a, x, \\theta \\right)\\ {\\mapsto}\\ {a x} + {\\theta}^{2}
"}︡ ︠298bb9c6-31ea-4cac-93c0-0b6817cfa591i︠ %html

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.

︡1b8550c3-f379-4185-aa52-655d70ab99d9︡{"html": "

2D Plotting

\r\n

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.

"}︡ ︠69b59d5a-62ba-44fe-8b03-df4d052e71cf︠ var('x') plot(sin(x^2), (x,-3,3)) ︡5631c9e6-167b-491e-bd41-407a7dc34bad︡︡ ︠6acbf78f-fefd-4b15-80b7-aab99ab62defi︠ %html

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

︡70a0a5e7-446a-46e6-bb05-6db7f865abaa︡{"html": "

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

"}︡ ︠6f671fb0-976f-4edf-8bd8-e237de54db9d︠ 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) ︡230b8f93-d7b5-47b9-b849-ae2d88882067︡︡ ︠61432a52-de2a-4fd6-af25-796a774aa1e1i︠ %html

Example: I made the following using the above interactive plotter with $\sin(x^2)$.

︡5593f43b-d1e0-4bd3-ab41-91fb00043f00︡{"html": "

Example: I made the following using the above interactive plotter with $\\sin(x^2)$.

\n

\"\"

"}︡ ︠6ad2bbca-c5c4-48e0-a714-895e9adb51c5i︠ %html

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 +.

︡52eba0ba-6a4a-4c34-841d-b51ef4dc41ef︡{"html": "

You can plot many other things, including polygons, parametric plots, polar plots, implicit plots, etc.:

\r\n

line, polygon, circle, text, polar_plot, parametric_plot, circle, implicit_plot

\r\n

You superimpose plots using +.

"}︡ ︠42b6d259-4d24-4252-9bac-53696d5370eb︠ 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) ︡32f4db47-a74a-437c-99cd-4161959af3e8︡︡ ︠3663ac83-68a6-4fde-a9b3-6b28036340afi︠ %html

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

︡255917ee-58dd-4f4d-a286-a42e8b744969︡{"html": "

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

"}︡ ︠24a57e41-25b5-4726-a4e3-9a7cd5ff2e44︠ 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) ︡536d795f-02f6-42fd-ab89-7b746b7fb0f7︡{"html": ""}︡ ︠1278886f-d211-48cf-b9e1-cc0973ee8c7ci︠ %html

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.

︡3c146dc3-e393-41f8-b2cc-8a79fa69532d︡{"html": "

3D Plotting

\n

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.

"}︡ ︠83ab90c8-46f1-4f34-8d2e-161e60e2542b︠ var('x,y') plot3d(sin(x-y)*cos(x-y^2),(x,-2,2),(y,-2,2)) ︡67e036ec-93e1-4e15-be77-97093fd9757b︡︡ ︠dc291e92-74e2-4cca-a5d5-5a65ceae5668i︠ %html

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

︡6bab7c3d-fd5f-4cab-b49f-85a871dfcfc7︡{"html": "

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

"}︡ ︠d39ec6b5-6a95-4b0a-95f9-e3ce1c5aa7d3︠ f(x,y) = 4*x*e^(-x^2-y^2) plot3d(f,(x,-2,2),(y,-2,2)) ︡c34dec1c-fe5e-4b49-ba47-1a53b3263c12︡︡ ︠3141f003-2d60-4c7c-b5f9-5ca500fe064ai︠ %html

Computing Integrals and Derivatives

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

︡b7e20b26-eb40-4c1f-8afa-ba2b4a9223a4︡{"html": "

Computing Integrals and Derivatives

\r\n

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

"}︡ ︠86fe2890-71e0-4fa4-8ee0-4a648bdfaeb0︠ var('x') integrate(x^2, x) ︡630a1ef6-b441-4e57-9729-8d144c99f8b9︡{"stdout": "x^3/3"}︡ ︠25719f9e-cce7-4621-bb62-a0831663500c︠ show(integrate(sin(x)+tan(2*x),x)) ︡2c59407d-9a8a-4601-88ef-7da34b23afbc︡{"html": "
\\frac{\\log \\left( \\sec \\left( {2 x} \\right) \\right)}{2} - \\cos \\left( x \\right)
"}︡ ︠b9d6ee5b-80db-4ed8-95ac-67bad7504e24︠ f = sin(x) - cos(y*x) + 1/(x^3+1) g = f.integrate(x) show(g) ︡67a78c20-6244-4eb8-a07b-8f1b7b599e6f︡{"html": "
\\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)
"}︡ ︠c45c014a-3981-4ab4-9f5d-a6605bb0da8b︠ bool(g.differentiate(x) == f) ︡cd146c39-a2b6-4ece-bfd1-c337caa22b6b︡{"stdout": "True"}︡ ︠a1c5f79f-8873-4bbf-8e35-29c76a1146b9︠ h = sin(x)*cos(x) show(h.taylor(x, 1, 2)) ︡dfc489e8-69c0-4109-92fd-987e10a1c5ac︡{"html": "
{\\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} }
"}︡ ︠7af462af-5517-4714-8d9a-e43a077b040a︠ @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) ︡0a57bf29-4096-4dea-92ee-a010cd501539︡︡ ︠2e912469-99c4-481b-ab32-569e245e8d73i︠ %html


︡40868ad6-bc1a-4954-80c4-82d96f20b9dd︡{"html": "


"}︡