analysis2_100308
system:sage


<h1><span style="font-size: xx-small;"><span style="font-weight: normal;"><span style="font-size: x-small;"><strong>Note:</strong> This worksheet is a modification of the worksheet found </span><a href="http://sagenb.org/home/pub/1139/" target="_blank"><span style="font-size: x-small;">here</span></a><span style="font-size: x-small;">.</span></span></span></h1>
<h2><span style="font-size: large;">Inserting new Sage cells</span></h2>
<p><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: medium;">You can insert a new cell above or below any existing cell or block of text. &nbsp;To insert a new cell, put your cursor above (respectively, below) the cell or block of text where you want to insert a new cell until you see a </span><span style="color: #cc99ff;"><span style="font-size: medium;"><span style="background-color: #0000ff;">purple</span> </span><span style="color: #000000;"><span style="font-size: medium;">horizontal line pop up. &nbsp;Click on this horizontal line to produce a new Sage cell. &nbsp;Try that just below right now and then type in some other complicated expression of numbers and then click "evaluate."</span></span></span></span></span></p>
<p><span style="font-size: large;"><strong>Inserting a new block of text</strong></span></p>
<p><span style="font-size: medium;">You can also insert a new block of text (also called an "html cell") above or below any existing Sage cell or other block of text. &nbsp;To insert a new html cell, you Shift+click the <span style="color: #cc99ff;"><span style="background-color: #0000ff;">purple</span> <span style="color: #000000;">horizontal line that pops up instead of just clicking on it. &nbsp;This will pop open a tiny text editor for you to type in. &nbsp;There are lots of things you can do inside an html block, but for now, we will keep it simple. &nbsp;You can always edit an existing html cell by simply double-clicking on the corresponding cell. &nbsp;After you are done editing an html cell, click "Save changes."</span></span></span></p>
<p>Add an html cell at the very top of this worksheet and type in your names.</p>
<h3>Getting Help</h3>
<p>Either one clicks help on the top right of the page or one tries <strong>search_src("my querie")</strong> to get an answer.</p>
<p>&nbsp;</p>

<h1>Introduction to Sage for Calculus 2</h1>
<p>This worksheed will introduce you to the basic properties and use of symbolic functions in Sage.</p>
<p>Throughout this worksheet I have include blank "Sage cells" in which you can type examples and try new things.&nbsp; To evaluate a cell, just click on the cell and then click "evaluate."</p>
<h2>Variables, Solving, Factoring, Simplifying</h2>
<p>The first thing you should always do when beginning a new worksheet is to define the variables you plan to use.&nbsp; Since $x$ and $y$ are usually variables in our work, I'll start by stating they are variables. &nbsp;(Note: Sage always assumes $x$ is variable, but I think this is the only one it assumes anything about.) &nbsp;Without doing this, Sage will not let me use $x$ and $y$ without giving an error. The <span style="font-family: 'andale mono', times;">%auto</span> command makes this cell evaluate every time you load this worksheet, so that you can use $x$ and $y$ as variables without having to evaluate this cell.</p>

{{{id=40|
%auto
var('x,y')
///
(x, y)
}}}

{{{id=0|
solve(x^2+3*x+2==0,x)
///
[x == -2, x == -1]
}}}

<p>You can quickly factor polynomials in Sage using <span style="font-family: symbol;">factor</span>. &nbsp;Evaluate each of the next two cells.</p>

{{{id=37|
factor(x^2+3*x+2)
///
(x + 1)*(x + 2)
}}}

{{{id=38|
factor(x^2+3x+2)                   #without the * between 3 and x, there is an error.
///
line 3
    factor(x**_sage_const_2 +3x+_sage_const_2 )                   #without the * between 3 and x, there is an error.
                              ^
SyntaxError: invalid syntax
}}}

<p><strong>Note: </strong></p>
<ol>
<li>You can use # to make comments inside a Sage cell. &nbsp;Sage will ignore anything to the right of #. &nbsp;For example, Sage ignored "<span style="font-family: symbol;">#without the * between 3 and x, there is an error."</span> in the previous cell.</li>
<li>You can make Sage display its output in nice format by checking the box at the top of the worksheet that says "typeset." &nbsp;Do that now and then re-evaluate the second to last cell.</li>
</ol>
<p>Now, you try one below.</p>
<p><strong>&nbsp;</strong></p>

{{{id=57|

///
}}}

<p>You can also use the factor command to simplify rational expressions, as it will factor both the numerator and denominator and then cancel common terms.</p>

{{{id=4|
factor((x^2+4*x+3)/(x^2-9))
///
(x + 1)/(x - 3)
}}}

<p>OK, your turn. &nbsp;Try a new one below.</p>

{{{id=126|

///
}}}

<h2>Expressions: plotting, differentiation and integration</h2>
<p>If you plan to use the same expression many times, you can save yourself lots of time by storing it as a variable.&nbsp; Here I store a rational expression as <strong>f</strong>, then simplify it, integrate it, differentiate it, print it, and then plot it.&nbsp; Notice that you can often write <strong><span style="font-family: symbol;">f.[something]</span></strong> to perform commands.</p>

{{{id=127|
f=(x^5-4*x+1)/(x^2-9)
///
}}}

{{{id=128|
f.partial_fraction()
///
9*x + 116/3/(x - 3) + 115/3/(x + 3) + x^3
}}}

{{{id=97|
f.integrate()
///
1/4*x^4 + 9/2*x^2 + 116/3*log(x - 3) + 115/3*log(x + 3)
}}}

{{{id=98|
g=f.diff()
g.full_simplify()
///
(3*x^6 - 45*x^4 + 4*x^2 - 2*x + 36)/(x^4 - 18*x^2 + 81)
}}}

{{{id=43|
plot(f,(x,-2,4))
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

<p>Since the expression has a vertical asymptote, you can use <strong>ymin</strong> and <strong>ymax</strong> to give a top and bottom range to your plot.</p>

{{{id=44|
plot(f,(x,-2,4),ymin=-10,ymax=10)
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

<h3></h3>
<p><strong><span style="font-family: symbol;">&nbsp;</span></strong></p>

<p>Deleting an expression works as follows:</p>

{{{id=7|
del(f)
///
}}}

{{{id=8|
f
///
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_30.py", line 4, in <module>
    exec compile(ur'f' + '\n', '', 'single')
  File "", line 1, in <module>
    
NameError: name 'f' is not defined
}}}

<h2>Functions: symbolic and Python functions</h2>
<p>To use (callable) functions in SAGE (so that you can write something like f(2)) just write <strong>f(x)=...</strong> before writing the function expression. Then you can use f(2), f(a), f(x+h) or any kind of function notation that you are familar with from algebra. Notice that <strong>f=...</strong> is an expression and not a callable function. Inserting arguments into expressions might be a problem.</p>

{{{id=10|
f(x)=5*x^2+4
///
}}}

{{{id=11|
f
///
x |--> 5*x^2 + 4
}}}

{{{id=12|
f(4)
///
84
}}}

{{{id=13|
a=4
///
}}}

{{{id=14|
f(a)
///
84
}}}

{{{id=16|
g(x)=x^2-3*x+2
f(g(x))
///
5*(x^2 - 3*x + 2)^2 + 4
}}}

{{{id=17|
g(f(x)).show() # nice output like if you click "typeset" on top
///
<html><div class="math">{(5 \, x^{2} + 4)}^{2} - 15 \, x^{2} - 10</div></html>
}}}

{{{id=18|
expand(g(f(x))).show()
g(f(x)).expand().show()    #Either method works.
///
<html><div class="math">25 \, x^{4} + 25 \, x^{2} + 6</div></html>
<html><div class="math">25 \, x^{4} + 25 \, x^{2} + 6</div></html>
}}}

<p>Functions as Expressions can be used for differentiation and integration and to define new piecewise objects. First we introduce two callable functions $ \sinh $ and $ \arcsin$&nbsp; which are already implemented in SAGE and calculate derivatives and (definite) integrals.</p>

{{{id=101|
f1(x)=sinh(x)
///
}}}

<p>Notice that $ f1 $ is a callable function, so is its indefinite integral. If we integrate $ f1(x) $ we obtain as a result an expression.</p>

{{{id=100|
f1.integrate()
///
x |--> cosh(x)
}}}

{{{id=139|
f1(x).integrate()
///
cosh(x)
}}}

{{{id=103|
f1.integrate(x,0,1)
///
cosh(1) - 1
}}}

{{{id=102|
f1.diff()
///
x |--> cosh(x)
}}}

{{{id=104|
f2(x)=arcsin(x)
///
}}}

{{{id=105|
f2.diff()
///
x |--> 1/sqrt(-x^2 + 1)
}}}

{{{id=106|
f2.plot()
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

{{{id=107|
f2.integrate()
///
x |--> x*arcsin(x) + sqrt(-x^2 + 1)
}}}

<p>We can also work with $ \arcsin $ as an expression, not as a callable function.</p>

{{{id=108|
f3=arcsin(x)
///
}}}

{{{id=109|
f3.diff()
///
1/sqrt(-x^2 + 1)
}}}

{{{id=110|
f3.integrate()
///
x*arcsin(x) + sqrt(-x^2 + 1)
}}}

{{{id=120|
numerical_integral(f3,0,1)
///
(0.57079632246586909, 8.9230242453635791e-07)
}}}

{{{id=121|
f3.integrate(0,1)
///
1/2*pi - 1
}}}

{{{id=111|
f3.plot()
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

<p>Next we introduce a Python function, which has no symbolic meaning anymore and can generically not be integrated and differentiated symbolically.</p>

{{{id=133|
def f4(z):
    if (z<0): return 0
    else: return 1
///
}}}

{{{id=135|
f4(x).integrate()
///
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_163.py", line 4, in <module>
    exec compile(ur'f4(x).integrate()' + '\n', '', 'single')
  File "", line 1, in <module>
    
AttributeError: 'sage.rings.integer.Integer' object has no attribute 'integrate'
}}}

<p>A numerical integration is certainly possibly!</p>

{{{id=136|
numerical_integral(f4,-1,1)
///
(1.0, 1.1102230246251565e-14)
}}}

<p>Last we introduce piecewise defined functions.</p>

<p>For subtlties on function expressions and callable functions see <a href="http://www.sagemath.org/doc/tutorial/tour_functions.html">here</a>.</p>
<p>&nbsp;</p>

<h2>Limits of Functions</h2>
<p>Now that you have played with functions, let's see if we can do a bit of calculus. Let us check some limiting values for different functions. We shall define the variables and functions for each of these cases. Try to verify all these limits using the known limit laws that you learnt in your calculus course.</p>

{{{id=31|
var('x')
f = (x^2 + x - 2)/(x^3 - 1)
limit(f, x=1)
///
1
}}}

{{{id=32|
var('x')
f = (sqrt(x^2 + 2) - 2)/(2*x - 1)
limit(f, x=infinity)
///
1/2
}}}

<p><img src="cells/35/sage95.png?1235878534" alt="" /></p>

{{{id=33|
var('x')
f = log(abs(x))
limit(f, x=0)
///
-Infinity
}}}

{{{id=34|
plot(f, [x, -3, 3])
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

<h2>Some Special Limits</h2>
<p>Let us verify some of the standard limit formulae using SAGE, as follows</p>
<p>&nbsp;</p>

{{{id=56|
var('x')
f = (e^x - 1)/x
limit(f, x=0)
///
1
}}}

{{{id=51|
plot(f, [x,-1,5]).show(ymin=0,ymax=5)
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

{{{id=58|
var('x')
f = sin(x)/x
limit(f, x=0)
///
1
}}}

{{{id=50|
plot(f, [x,-100,100]).show(ymin=-0.5,ymax=1)
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

{{{id=59|
var('x')
f = log(x+1)/x
limit(f, x=0)
///
1
}}}

{{{id=49|
plot(f, [x,-1,5]).show(ymin=0,ymax=5)
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

{{{id=48|
var('x')
f = (x + 1)^(1/x)
limit(f, x=0)
///
e
}}}

{{{id=41|
plot(f, [x,-1,5]).show(ymin=0,ymax=5)
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

<p>Cool, isn't it?</p>

<h2>Getting Pretty Output and Numerical Values</h2>
<p>Sage will try to return symbolic answers when it can.&nbsp; Use <strong>.show()</strong> to get a pretty output.&nbsp; use <strong>.n()</strong> to get a numeric value. Below I show you mutliple ways to use the <strong>n()</strong> function.</p>

{{{id=19|
sqrt(2)
///
sqrt(2)
}}}

{{{id=47|
sqrt(2).show()
///
<html><div class="math">\sqrt{2}</div></html>
}}}

{{{id=79|
sqrt(2).n()
///
1.41421356237310
}}}

{{{id=20|
n(sqrt(2))
///
1.41421356237310
}}}

{{{id=80|
sqrt(2).n(digits=50)
///
1.4142135623730950488016887242096980785696718753769
}}}

{{{id=21|
n(sqrt(3),digits=500)       #Just for fun let's look at 500 decimals.
///
1.7320508075688772935274463415058723669428052538103806280558069794519330169088000370811461867572485756756261414154067030299699450949989524788116555120943736485280932319023055820679748201010846749232650153123432669033228866506722546689218379712270471316603678615880190499865373798593894676503475065760507566183481296061009476021871903250831458295239598329977898245082887144638329173472241639845878553976679580638183536661108431737808943783161020883055249016700235207111442886959909563657970871684980729
}}}

<p>Try similar things with the number $e$.</p>

{{{id=81|
e.n(digits=500)
///
2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274274663919320030599218174135966290435729003342952605956307381323286279434907632338298807531952510190115738341879307021540891499348841675092447614606680822648001684774118537423454424371075390777449920695517027618386062613313845830007520449338265602976067371132007093287091274437470472306969772093101416928368190255151086574637721112523897844250569536967707854499699679468644549059879316368892300987931
}}}

<h2>More with Plotting</h2>

{{{id=24|
#this is the basic plotting command
plot(x^2-5*sin(x), (x, -5, 3))
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

{{{id=25|
#You can include multiple functions in a list
plot((x^2,sqrt(x),sin(x)), (x, 0, 1))
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

{{{id=26|
#You can also combine plots by adding them. This allows me to make each plot a different color.
plot(x^2, (x, 0, pi),color='red')+plot(sqrt(x), (x, 0, pi),color='green')+plot(sin(x), (x, 0, pi))
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

{{{id=85|

///
}}}

<h2>Trigonometric functions</h2>
<p>SAGE will give you an exact value, and simplify it when possible.</p>

{{{id=27|
cos(pi/2)
///
0
}}}

{{{id=28|
cos(pi/6)
///
1/2*sqrt(3)
}}}

{{{id=88|
sin(2)
///
sin(2)
}}}

{{{id=89|
tan(pi/2)
///
Infinity
}}}

{{{id=29|
cos(pi/12).show()
///
<html><div class="math">\frac{1}{12} \, {(\sqrt{3} + 3)} \sqrt{6}</div></html>
}}}

{{{id=64|
g=cos(x+3)
///
}}}

{{{id=65|
g.expand_trig()      #You can use Sage to help you remember your trig identities.
///
-sin(3)*sin(x) + cos(3)*cos(x)
}}}

{{{id=90|
#Let's graph a*sin(b*x+c)+d.  
a=3
b=4
c=pi/3
d=-2
f=a*sin(b*x+c)+d
f.plot(-pi,2*pi)
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

{{{id=93|

///
}}}

<h2>Sage Quick Reference: Calculus</h2>
<p>Reference sheets allow you to quickly access the most important structures in Sage, you find them <a href="http://wiki.sagemath.org/quickref?action=AttachFile&amp;do=get&amp;target=quickref-calc.pdf" target="_blank">here</a>.</p>

{{{id=140|

///
}}}