PREP Tutorial, Calculus
system:sage


<div style="color: #000000; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: #ffffff; background-position: initial initial; margin: 8px;">
<h1 style="font-size: 2em;">Sage Tutorial for Calculus</h1>
<p>This&nbsp;<a href="http://www.sagemath.org" target="_blank">Sage</a>&nbsp;worksheet is one of the tutorials developed for the MAA PREP Workshop "Sage: Using Open-Source Mathematics Software with Undergraduates" (funding provided by NSF DUE 0817071). &nbsp;It is licensed under the Creative Commons Attribution-ShareAlike 3.0 license (<a href="http://creativecommons.org/licenses/by-sa/3.0/" target="_blank">CC BY-SA</a>).</p>
<p>This tutorial has the following sections. &nbsp;The first three are based on the topics encountered in a typical three-semester calculus sequence in the United States; the final section is a checkpoint of sorts.</p>
<ul>
<li><a href="#Calc1">Calculus 1</a></li>
<li><a href="#Calc2">Calculus 2</a></li>
<li><a href="#Calc3">Calculus 3</a></li>
<li><a href="#Exam">'Exam'</a></li>
</ul>
<p>The tutorial assumes that one is familiar with the basics of Sage, such as outlined in the previous tutorials. &nbsp;</p>
<p>For a refresher, make sure the syntax below for defining a function and getting a value makes sense; then evaluate the cell by clicking the "evaluate" link, or by pressing Shift-Enter (hold down Shift while pressing the Enter key).</p>
</div>

{{{id=134|
f(x)=x^3+1
f(2)
///
9
}}}

<p>We'll use this function several times in this tutorial, so it's important that it is evaluated!</p>
<p>&nbsp;</p>
<div style="color: #000000; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: #ffffff; margin: 8px;">
<h1 id="Calc1" style="font-size: 2em;">Calculus 1</h1>
</div>
<p>The calculus is amazing not so much because it solves problems of tangency or area - individual solutions to these problems have been known before. &nbsp;It is amazing because it gives a remarkably comprehensive set of rules for symbolic manipulation for solving such problems in great generality!</p>
<p>Thus, the most typical use of a computer system like Sage in this context is simply to help check (or make less tedious) basic symbolic manipulation of things like derivatives. &nbsp;Let's first show what our function $f$ is again, then do things with it.</p>
<ul>
</ul>

{{{id=91|
show(f)
///
<html><div class="math">\newcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ x^{3} + 1</div></html>
}}}

<p>Something most (but not all) curricula for first-semester calculus do is spend a fair amount of time with limits. &nbsp;</p>
<p>What is the limit of this function as $x$ approaches $1$? &nbsp;</p>

{{{id=138|
lim(f,x=1)
///
2
}}}

<p>Sage gives the answer we expect. &nbsp;&nbsp;The syntax for limits is pretty straightforward, though it may differ slightly from that of other systems. &nbsp;</p>
<p>Since a limit may exist even if the function is not defined, Sage uses the syntax $x=1$ to show the input value approached in all situations.</p>

{{{id=159|
lim((x^2-1)/(x-1),x=1)
///
2
}}}

<p>Next, we'll return to the original function, and check the two directional limits.</p>
<p>The syntax uses the extra <em>keyword</em>&nbsp;"dir".</p>
<ul>
<li>Notice that we use dir='right', not dir=right. &nbsp;</li>
<li>It's also okay to use dir='+', like we use dir='-' below.   
<ul>
<li>(This is basically because otherwise we might have done something like let right=55 earlier, so instead Sage - and Python - requires us to put 'right' and '-' in quotes to make it clear they aren't variables.)</li>
</ul>
</li>
</ul>

{{{id=140|
lim(f,x=1,dir='-'); lim(f,x=1,dir='right'); f(1)
///
2
2
2
}}}

<p>By comparing with $f(1)$, we see that $f(x)$ is continuous at $x=1$. &nbsp;</p>
<p>This cell also reminds us of something else:</p>
<ul>
<li>By separating several commands with semicolons, we can tell Sage to evaluate all of them while still seeing all their outputs.</li>
</ul>
<p>&nbsp;</p>
<p>What we spend the most time on in Calculus 1 is derivatives, and Sage is fully-featured here. &nbsp;</p>
<p>For example, here are three ways to get the basic, single-variable derivative of $f(x)=x^3+1$.&nbsp;</p>

{{{id=111|
diff(f,x); derivative(f,x); f.derivative(x)
///
x |--> 3*x^2
x |--> 3*x^2
x |--> 3*x^2
}}}

<p>Naturally, Sage knows all of the derivatives you want.</p>

{{{id=114|
derivative(sinh(x^2+sqrt(x-1)),x)
///
1/2*(4*x + 1/sqrt(x - 1))*cosh(sqrt(x - 1) + x^2)
}}}

<p>And maybe even knows those you don't want. &nbsp;In this case, we put the computation inside "show()" since the output is so long.&nbsp;</p>

{{{id=120|
show(derivative(sinh(x^2+sqrt(x-1)),x,3))
///
<html><div class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\frac{1}{8} \, {\left(4 \, x + \frac{1}{\sqrt{x - 1}}\right)}^{3} \cosh\left(\sqrt{x - 1} + x^{2}\right) - \frac{3}{8} \, {\left(\frac{1}{{\left(x - 1\right)}^{\frac{3}{2}}} - 8\right)} {\left(4 \, x + \frac{1}{\sqrt{x - 1}}\right)} \sinh\left(\sqrt{x - 1} + x^{2}\right) + \frac{3 \, \cosh\left(\sqrt{x - 1} + x^{2}\right)}{8 \, {\left(x - 1\right)}^{\left(\frac{5}{2}\right)}}</div></html>
}}}

<p>A common question is why Sage might not check automatically if there is some "simpler" version. &nbsp;But simplifying an expression, or indeed, even defining what a 'simple' expression is, turns out to be a very hard technical and mathematical problem in general.&nbsp;&nbsp; Computers won't solve every problem!</p>
<p>&nbsp;</p>
<p>As a brief interlude, let's consider an application of our ability to do some basic differential calculus.</p>
<p>First, as a reminder of plotting from the previous tutorial, try to plot the function $f(x)$, together with its tangent line at $x=1$, in the empty cells below. &nbsp;(If you can, do it without looking at the previous tutorial for a reminder.)</p>

{{{id=151|

///
}}}

{{{id=152|

///
}}}

<p>Did you get it? &nbsp;</p>
<p>Of course, in general we might want to see tangent lines at lots of different points - for instance, for a class demonstration. &nbsp;</p>
<p>So in the following cell, there are several auxiliary elements:</p>
<ul>
<li>We define the plot $P$ of the original function.</li>
<li>There is a parameter $c=1/3$, which is the $x$-value where we want a tangent line. &nbsp;</li>
<li>We let $fprime$ be the derivative function simply by declaring it equal to the derivative.</li>
<li>We let $L$ be the tangent line defined by the point-slope formula at $x=c$.</li>
<li>We make $Q$ be the plot of this line.</li>
</ul>
<p>Finally, we plot everything together in the last line by adding $P+Q$. &nbsp;</p>

{{{id=149|
P=plot(f,(x,-1,1))
c=1/3
fprime=derivative(f,x)
L(x)=fprime(c)*(x-c)+f(c)
Q=plot(L,(x,-1,1), color="red", linestyle="--")
P+Q
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

<p>You may want to experiment by</p>
<ul>
<li>Changing $c$ to some other value, or &nbsp;</li>
<li>Changing the function $f$, or&nbsp;</li>
<li>Changing the colors, or&nbsp;</li>
<li>Changing something else (like the "linestyle" used for the tangent line). &nbsp;</li>
</ul>
<p>&nbsp;</p>
<p>Ideally, it would be <em>extremely</em>&nbsp;easy to change that parameter $c$. &nbsp;In the cell below, we show our second example of a Sage "interact" (or "Sagelet"). &nbsp;</p>
<p>In this one, dragging a slider will show the tangent line moving. &nbsp;</p>
<ul>
<li>Future tutorials will explain this process in more detail; here it's just as an example.</li>
<li>However, the reader will note how very similar the code for the two cells is.</li>
</ul>
<p>&nbsp;</p>

{{{id=154|
%auto
f(x)=x^3+1
@interact
def _(c=(1/3,(-1,1))):
    P=plot(f,(x,-1,1))
    fprime=derivative(f,x)
    L(x)=fprime(c)*(x-c)+f(c)
    Q=plot(L,(x,-1,1),color="red", linestyle="--")
    show(P+Q+point((c,f(c)), pointsize=40, color='red'),ymin=0,ymax=2)
///
<html><!--notruncate--><div padding=6 id="div-interact-154"> <table width=800px height=20px bgcolor="#c5c5c5"
                 cellpadding=15><tr><td bgcolor="#f9f9f9" valign=top align=left><table>
<tr><td colspan=3><table><tr><td align=right><font color="black">c&nbsp;</font></td><td><table><tr><td>
        <div id="slider-c-154" style="margin:0px; margin-left: 1.0em; margin-right: 1.0em; width: 15.0em;"></div>
        </td><td><font color="black" id="slider-c-154-lbl"></font></td></tr></table><script>(function(){ var values = ["-1.0","-0.99599198396793587","-0.99198396793587174","-0.9879759519038076","-0.98396793587174347","-0.97995991983967934","-0.97595190380761521","-0.97194388777555107","-0.96793587174348694","-0.96392785571142281","-0.95991983967935868","-0.95591182364729455","-0.95190380761523041","-0.94789579158316628","-0.94388777555110215","-0.93987975951903802","-0.93587174348697388","-0.93186372745490975","-0.92785571142284562","-0.92384769539078149","-0.91983967935871735","-0.91583166332665322","-0.91182364729458909","-0.90781563126252496","-0.90380761523046083","-0.89979959919839669","-0.89579158316633256","-0.89178356713426843","-0.8877755511022043","-0.88376753507014016","-0.87975951903807603","-0.8757515030060119","-0.87174348697394777","-0.86773547094188364","-0.8637274549098195","-0.85971943887775537","-0.85571142284569124","-0.85170340681362711","-0.84769539078156297","-0.84368737474949884","-0.83967935871743471","-0.83567134268537058","-0.83166332665330645","-0.82765531062124231","-0.82364729458917818","-0.81963927855711405","-0.81563126252504992","-0.81162324649298578","-0.80761523046092165","-0.80360721442885752","-0.79959919839679339","-0.79559118236472925","-0.79158316633266512","-0.78757515030060099","-0.78356713426853686","-0.77955911823647273","-0.77555110220440859","-0.77154308617234446","-0.76753507014028033","-0.7635270541082162","-0.75951903807615206","-0.75551102204408793","-0.7515030060120238","-0.74749498997995967","-0.74348697394789554","-0.7394789579158314","-0.73547094188376727","-0.73146292585170314","-0.72745490981963901","-0.72344689378757487","-0.71943887775551074","-0.71543086172344661","-0.71142284569138248","-0.70741482965931834","-0.70340681362725421","-0.69939879759519008","-0.69539078156312595","-0.69138276553106182","-0.68737474949899768","-0.68336673346693355","-0.67935871743486942","-0.67535070140280529","-0.67134268537074115","-0.66733466933867702","-0.66332665330661289","-0.65931863727454876","-0.65531062124248463","-0.65130260521042049","-0.64729458917835636","-0.64328657314629223","-0.6392785571142281","-0.63527054108216396","-0.63126252505009983","-0.6272545090180357","-0.62324649298597157","-0.61923847695390744","-0.6152304609218433","-0.61122244488977917","-0.60721442885771504","-0.60320641282565091","-0.59919839679358677","-0.59519038076152264","-0.59118236472945851","-0.58717434869739438","-0.58316633266533024","-0.57915831663326611","-0.57515030060120198","-0.57114228456913785","-0.56713426853707372","-0.56312625250500958","-0.55911823647294545","-0.55511022044088132","-0.55110220440881719","-0.54709418837675305","-0.54308617234468892","-0.53907815631262479","-0.53507014028056066","-0.53106212424849653","-0.52705410821643239","-0.52304609218436826","-0.51903807615230413","-0.51503006012024","-0.51102204408817586","-0.50701402805611173","-0.5030060120240476","-0.49899799599198347","-0.49498997995991934","-0.4909819639278552","-0.48697394789579107","-0.48296593186372694","-0.47895791583166281","-0.47494989979959867","-0.47094188376753454","-0.46693386773547041","-0.46292585170340628","-0.45891783567134214","-0.45490981963927801","-0.45090180360721388","-0.44689378757514975","-0.44288577154308562","-0.43887775551102148","-0.43486973947895735","-0.43086172344689322","-0.42685370741482909","-0.42284569138276495","-0.41883767535070082","-0.41482965931863669","-0.41082164328657256","-0.40681362725450843","-0.40280561122244429","-0.39879759519038016","-0.39478957915831603","-0.3907815631262519","-0.38677354709418776","-0.38276553106212363","-0.3787575150300595","-0.37474949899799537","-0.37074148296593123","-0.3667334669338671","-0.36272545090180297","-0.35871743486973884","-0.35470941883767471","-0.35070140280561057","-0.34669338677354644","-0.34268537074148231","-0.33867735470941818","-0.33466933867735404","-0.33066132264528991","-0.32665330661322578","-0.32264529058116165","-0.31863727454909752","-0.31462925851703338","-0.31062124248496925","-0.30661322645290512","-0.30260521042084099","-0.29859719438877685","-0.29458917835671272","-0.29058116232464859","-0.28657314629258446","-0.28256513026052033","-0.27855711422845619","-0.27454909819639206","-0.27054108216432793","-0.2665330661322638","-0.26252505010019966","-0.25851703406813553","-0.2545090180360714","-0.25050100200400727","-0.24649298597194313","-0.242484969939879","-0.23847695390781487","-0.23446893787575074","-0.23046092184368661","-0.22645290581162247","-0.22244488977955834","-0.21843687374749421","-0.21442885771543008","-0.21042084168336594","-0.20641282565130181","-0.20240480961923768","-0.19839679358717355","-0.19438877755510942","-0.19038076152304528","-0.18637274549098115","-0.18236472945891702","-0.17835671342685289","-0.17434869739478875","-0.17034068136272462","-0.16633266533066049","-0.16232464929859636","-0.15831663326653223","-0.15430861723446809","-0.15030060120240396","-0.14629258517033983","-0.1422845691382757","-0.13827655310621156","-0.13426853707414743","-0.1302605210420833","-0.12625250501001917","-0.12224448897795503","-0.1182364729458909","-0.11422845691382677","-0.11022044088176264","-0.10621242484969851","-0.10220440881763437","-0.098196392785570241","-0.094188376753506109","-0.090180360721441977","-0.086172344689377844","-0.082164328657313712","-0.07815631262524958","-0.074148296593185448","-0.070140280561121315","-0.066132264529057183","-0.062124248496993058","-0.058116232464928932","-0.054108216432864807","-0.050100200400800682","-0.046092184368736556","-0.042084168336672431","-0.038076152304608306","-0.034068136272544181","-0.030060120240480052","-0.026052104208415923","-0.022044088176351794","-0.018036072144287665","-0.014028056112223537","-0.010020040080159408","-0.0060120240480952799","-0.0020040080160311519","0.002004008016032976","0.0060120240480971039","0.010020040080161233","0.014028056112225362","0.01803607214428949","0.022044088176353619","0.026052104208417748","0.030060120240481877","0.034068136272546005","0.038076152304610131","0.042084168336674256","0.046092184368738381","0.050100200400802507","0.054108216432866632","0.058116232464930757","0.062124248496994883","0.066132264529059015","0.070140280561123147","0.07414829659318728","0.078156312625251412","0.082164328657315544","0.086172344689379676","0.090180360721443809","0.094188376753507941","0.098196392785572073","0.10220440881763621","0.10621242484970034","0.11022044088176447","0.1142284569138286","0.11823647294589273","0.12224448897795687","0.126252505010021","0.13026052104208513","0.13426853707414926","0.1382765531062134","0.14228456913827753","0.14629258517034166","0.15030060120240579","0.15430861723446992","0.15831663326653406","0.16232464929859819","0.16633266533066232","0.17034068136272645","0.17434869739479059","0.17835671342685472","0.18236472945891885","0.18637274549098298","0.19038076152304712","0.19438877755511125","0.19839679358717538","0.20240480961923951","0.20641282565130364","0.21042084168336778","0.21442885771543191","0.21843687374749604","0.22244488977956017","0.22645290581162431","0.23046092184368844","0.23446893787575257","0.2384769539078167","0.24248496993988083","0.24649298597194497","0.2505010020040091","0.25450901803607323","0.25851703406813736","0.2625250501002015","0.26653306613226563","0.27054108216432976","0.27454909819639389","0.27855711422845802","0.28256513026052216","0.28657314629258629","0.29058116232465042","0.29458917835671455","0.29859719438877869","0.30260521042084282","0.30661322645290695","0.31062124248497108","0.31462925851703522","0.31863727454909935","0.32264529058116348","0.32665330661322761","0.33066132264529174","0.33466933867735588","0.33867735470942001","0.34268537074148414","0.34669338677354827","0.35070140280561241","0.35470941883767654","0.35871743486974067","0.3627254509018048","0.36673346693386893","0.37074148296593307","0.3747494989979972","0.37875751503006133","0.38276553106212546","0.3867735470941896","0.39078156312625373","0.39478957915831786","0.39879759519038199","0.40280561122244612","0.40681362725451026","0.41082164328657439","0.41482965931863852","0.41883767535070265","0.42284569138276679","0.42685370741483092","0.43086172344689505","0.43486973947895918","0.43887775551102332","0.44288577154308745","0.44689378757515158","0.45090180360721571","0.45490981963927984","0.45891783567134398","0.46292585170340811","0.46693386773547224","0.47094188376753637","0.47494989979960051","0.47895791583166464","0.48296593186372877","0.4869739478957929","0.49098196392785703","0.49498997995992117","0.4989979959919853","0.50300601202404938","0.50701402805611351","0.51102204408817764","0.51503006012024177","0.5190380761523059","0.52304609218437004","0.52705410821643417","0.5310621242484983","0.53507014028056243","0.53907815631262657","0.5430861723446907","0.54709418837675483","0.55110220440881896","0.5551102204408831","0.55911823647294723","0.56312625250501136","0.56713426853707549","0.57114228456913962","0.57515030060120376","0.57915831663326789","0.58316633266533202","0.58717434869739615","0.59118236472946029","0.59519038076152442","0.59919839679358855","0.60320641282565268","0.60721442885771681","0.61122244488978095","0.61523046092184508","0.61923847695390921","0.62324649298597334","0.62725450901803748","0.63126252505010161","0.63527054108216574","0.63927855711422987","0.64328657314629401","0.64729458917835814","0.65130260521042227","0.6553106212424864","0.65931863727455053","0.66332665330661467","0.6673346693386788","0.67134268537074293","0.67535070140280706","0.6793587174348712","0.68336673346693533","0.68737474949899946","0.69138276553106359","0.69539078156312772","0.69939879759519186","0.70340681362725599","0.70741482965932012","0.71142284569138425","0.71543086172344839","0.71943887775551252","0.72344689378757665","0.72745490981964078","0.73146292585170491","0.73547094188376905","0.73947895791583318","0.74348697394789731","0.74749498997996144","0.75150300601202558","0.75551102204408971","0.75951903807615384","0.76352705410821797","0.76753507014028211","0.77154308617234624","0.77555110220441037","0.7795591182364745","0.78356713426853863","0.78757515030060277","0.7915831663326669","0.79559118236473103","0.79959919839679516","0.8036072144288593","0.80761523046092343","0.81162324649298756","0.81563126252505169","0.81963927855711582","0.82364729458917996","0.82765531062124409","0.83166332665330822","0.83567134268537235","0.83967935871743649","0.84368737474950062","0.84769539078156475","0.85170340681362888","0.85571142284569301","0.85971943887775715","0.86372745490982128","0.86773547094188541","0.87174348697394954","0.87575150300601368","0.87975951903807781","0.88376753507014194","0.88777555110220607","0.89178356713427021","0.89579158316633434","0.89979959919839847","0.9038076152304626","0.90781563126252673","0.91182364729459087","0.915831663326655","0.91983967935871913","0.92384769539078326","0.9278557114228474","0.93186372745491153","0.93587174348697566","0.93987975951903979","0.94388777555110392","0.94789579158316806","0.95190380761523219","0.95591182364729632","0.95991983967936045","0.96392785571142459","0.96793587174348872","0.97194388777555285","0.97595190380761698","0.97995991983968112","0.98396793587174525","0.98797595190380938","0.99198396793587351","0.99599198396793764","1.0"]; setTimeout(function() {
    $('#slider-c-154').slider({
        step: 1, min: 0, max: 499, value: 333,
        change: function (e,ui) { var position = ui.value; if(values!=null) $('#slider-c-154-lbl').text(values[position]); interact(154, '_interact_.update(\'154\', \'c\', 1, _interact_.standard_b64decode(\''+encode64(position)+'\'), globals()); _interact_.recompute(\'154\');'); },
        slide: function(e,ui) { if(values!=null) $('#slider-c-154-lbl').text(values[ui.value]); }
    });
    if(values != null) $('#slider-c-154-lbl').text(values[$('#slider-c-154').slider('value')]);
    }, 1); })();</script></td>
</tr></table></td></tr>
<tr><td></td><td style='width: 100%;'><div id="cell-interact-154"><?__SAGE__START>
        <table border=0 bgcolor="white" width=100%>
        <tr><td bgcolor="white" align=left valign=top><pre><?__SAGE__TEXT></pre></td></tr>
        <tr><td  align=left valign=top><?__SAGE__HTML></td></tr>
        </table><?__SAGE__END></div></td><td></td></tr>
<tr><td colspan=3></td></tr>
</table></td>
                 </tr></table></div>
                 </html>
}}}

<p>A very sharp-eyed reader will also have noticed that the previous cell had "%auto" at the very top, and that it was not necessary to evaluate the cell to use it.</p>
<ul>
<li>The command "%auto" allows us to have a cell, especially an interactive one, all loaded up as soon as we start - particularly convenient for a classroom situation.</li>
<li>Such instructions are called <em>percent directives</em>. &nbsp;Most are documented in the notebook help one can access at the top of any worksheet.</li>
</ul>
<p>&nbsp;</p>
<p>A final topic in Calculus 1 usually is basic integration. &nbsp;The syntax for indefinite integration is similar to that for differentiation.</p>

{{{id=141|
integral(cos(x),x)
///
sin(x)
}}}

<p>We don't get the whole indefinite integral, just a convenient antiderivative. &nbsp;</p>
<ul>
<li>(If you were to get a different answer 'by hand', remember that being an antiderivative means the answer is correct <em>up to a constant </em>- and deciding whether this is the case with two expressions has the same problems as the issue of "simplification" above.)</li>
</ul>
<p>&nbsp;</p>
<p>Definite integration has similar syntax to plotting.</p>

{{{id=143|
integral(cos(x),(x,0,pi/2))
///
1
}}}

<div style="color: #000000; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: #ffffff; background-position: initial initial; margin: 8px;">
<div style="color: #000000; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: #ffffff; margin: 8px;">
<h1 id="Calc2" style="font-size: 2em;">Calculus 2</h1>
</div>
<p>Second-semester calculus is typically more challenging. &nbsp;</p>
<ul>
<li>One reason for that is that the computational problems are not so straightforward as computing derivatives and basic integrals.</li>
<li>Another reason is that the second semester is usually where the harder versions of problems from the first semester show up.</li>
</ul>
<p>Nonetheless, Sage can handle this as well. &nbsp;</p>
<p>&nbsp;</p>
<p>Sage includes a large number of indefinite integrals (via Maxima), though not all the ones you will find in a comprehensive table.</p>
</div>

{{{id=115|
h(x)=sec(x)
h.integrate(x)
///
x |--> log(tan(x) + sec(x))
}}}

<p>Since I defined $h$ as a function, the answer I get is also a function. &nbsp;If I just want an expression as the answer, I can do the following.</p>

{{{id=180|
integrate(sec(x),x)
///
log(tan(x) + sec(x))
}}}

<p>Here is another (longer) example. &nbsp;Do you remember what command would help it look nicer in the browser?</p>

{{{id=145|
integrate(1/(1+x^5),x)
///
1/5*(sqrt(5) - 1)*sqrt(5)*arctan((4*x - sqrt(5) - 1)/sqrt(-2*sqrt(5) + 10))/sqrt(-2*sqrt(5) + 10) + 1/5*(sqrt(5) + 1)*sqrt(5)*arctan((4*x + sqrt(5) - 1)/sqrt(2*sqrt(5) + 10))/sqrt(2*sqrt(5) + 10) - 1/10*(sqrt(5) - 3)*log((sqrt(5) - 1)*x + 2*x^2 + 2)/(sqrt(5) - 1) - 1/10*(sqrt(5) + 3)*log(-(sqrt(5) + 1)*x + 2*x^2 + 2)/(sqrt(5) + 1) + 1/5*log(x + 1)
}}}

<p>Some integrals are a little tricky, of course. &nbsp;If Sage doesn't know the whole antiderivative, it returns as much of it as it (more properly, as Maxima) could do.</p>

{{{id=146|
integral(1/(1+x^10),x)
///
1/5*arctan(x) - 1/5*integrate((x^6 - 2*x^4 + 3*x^2 - 4)/(x^8 - x^6 + x^4 - x^2 + 1), x)
}}}

{{{id=116|
integral(sinh(x^2+sqrt(x-1)),x)
///
integrate(sinh(sqrt(x - 1) + x^2), x)
}}}

<p>This last one stumps other systems too.</p>
<p>However, if there is a special function which helps compute the integral, Sage will look for it. &nbsp;&nbsp;In the following case there is no elementary antiderivative, but the "erf" function helps us out.</p>

{{{id=117|
integral(e^(-x^2),x)
///
1/2*sqrt(pi)*erf(x)
}}}

<p>Don't forget, if this function is unfamiliar to you (as it might be to students trying this integral), Sage's contextual help system comes to the rescue.</p>

{{{id=147|
erf?
///
<html><!--notruncate-->

<div class="docstring">
    
  <p><strong>File:</strong> /home/sageserver/sage/local/lib/python2.6/site-packages/sage/functions/other.py</p>
<p><strong>Type:</strong> &lt;class &#8216;sage.functions.other.Function_erf&#8217;&gt;</p>
<p><strong>Definition:</strong> erf(*args, coerce=True, hold=False)</p>
<p><strong>Docstring:</strong></p>
<blockquote>
<p>The error function, defined as
<span class="math">erf(x) = \frac{2}{\sqrt{\pi}} \int_0^x e^{-t^2} dt</span>.</p>
<p>Sage currently only implements the error function (via a call to
PARI) when the input is real.</p>
<p>EXAMPLES:</p>
<div class="highlight-python"><div class="highlight"><pre class="literal-block"><span class="gp">sage: </span><span class="n">erf</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="go">erf(2)</span>
<span class="gp">sage: </span><span class="n">erf</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">n</span><span class="p">()</span>
<span class="go">0.995322265018953</span>
<span class="gp">sage: </span><span class="n">loads</span><span class="p">(</span><span class="n">dumps</span><span class="p">(</span><span class="n">erf</span><span class="p">))</span>
<span class="go">erf</span>
</pre></div>
</div>
<p>The following fails because we haven&#8217;t implemented
erf yet for complex values:</p>
<div class="highlight-python"><div class="highlight"><pre class="literal-block"><span class="gp">sage: </span><span class="nb">complex</span><span class="p">(</span><span class="n">erf</span><span class="p">(</span><span class="mi">3</span><span class="o">*</span><span class="n">I</span><span class="p">))</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="nc">TypeError</span>: <span class="n-Identifier">unable to simplify to complex approximation</span>
</pre></div>
</div>
<p>TESTS:</p>
<p>Check if conversion from maxima elements work:</p>
<div class="highlight-python"><div class="highlight"><pre class="literal-block"><span class="gp">sage: </span><span class="n">merf</span> <span class="o">=</span> <span class="n">maxima</span><span class="p">(</span><span class="n">erf</span><span class="p">(</span><span class="n">x</span><span class="p">))</span><span class="o">.</span><span class="n">sage</span><span class="p">()</span><span class="o">.</span><span class="n">operator</span><span class="p">()</span>
<span class="gp">sage: </span><span class="n">merf</span> <span class="o">==</span> <span class="n">erf</span>
<span class="go">True</span>
</pre></div>
</div>
</blockquote>


</div>
</html>
}}}

<p>There are several ways to do definite integrals in Sage. &nbsp;</p>
<p>The most obvious one is simply turning $$\int f(x)dx$$ into $$\int_a^b f(x)dx\; ,$$ as indicated in the Calculus I section. &nbsp;</p>

{{{id=123|
integral(cos(x),(x,0,pi/2))
///
1
}}}

<p>The preferred syntax puts the variable and endpoints together in parentheses.</p>
<p>Just like with derivatives, we can visualize this integral using some of the plotting options from the plotting tutorial.</p>

{{{id=155|
plot(cos(x),(x,0,pi/2),fill=True,ticks=[[0,pi/4,pi/2],None],tick_formatter=pi)
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

<p>It is possible to be completely symbolic in doing integration. &nbsp;If you do this, you'll have to make sure you define anything that's a symbolic variable - which includes constants, naturally.</p>

{{{id=124|
var('a,b')
integral(cos(x),(x,a,b))
///
-sin(a) + sin(b)
}}}

<p>On the numerical side, sometimes the answer one gets from the Fundamental Theorem of Calculus is not entirely helpful. &nbsp;Recall that $h$ is the secant function.</p>

{{{id=125|
integral(h,(x,0,pi/8))
///
x |--> -1/2*log(-sin(1/8*pi) + 1) + 1/2*log(sin(1/8*pi) + 1)
}}}

<p>Here, just a number might be more helpful. &nbsp;Sage has several ways of numerical evaluating integrals.</p>
<ul>
<li>Doing a definite integral symbolically, then approximating it numerically</li>
<li>The <span style="font-family: 'courier new', courier;">numerical_integral</span> function</li>
<li>The .<span style="font-family: 'courier new', courier;">nintegrate</span> method</li>
</ul>
<p>The first one, using the n or N function for numerical approximation, was also mentioned in the introductory tutorial.</p>
<ul>
</ul>

{{{id=127|
N(integral(h,(x,0,pi/8)))
///
0.403199719161512
}}}

<p>The second function, "numerical_integral", uses a powerful numerical program (the GNU Scientific Library). &nbsp;</p>
<ul>
<li>Unfortunately, the syntax for this function is not &nbsp;yet&nbsp;consistent with the rest of Sage. &nbsp;</li>
<li>Helpfully, the output has two elements - the answer you desire, and its error tolerance. &nbsp;</li>
</ul>

{{{id=112|
numerical_integral(h,0,pi/8)
///
(0.40319971916151143, 4.4764161173550687e-15)
}}}

<p>To access just the number, one asks for the 'zeroth' element of this sequence of items. &nbsp;This is done with the following bracket notation.</p>

{{{id=176|
numerical_integral(h,0,pi/8)[0]
///
0.40319971916151143
}}}

<p>Notice that we began counting at zero. &nbsp;This is fairly typical in computer programs (though certainly not universal).</p>
<p>To aid readability (more important than one might think), we often assign the numerical integral to a variable, and then take the zeroth element of that.</p>

{{{id=178|
ni = numerical_integral(h,0,pi/8)
ni[0]
///
0.40319971916151143
}}}

<p>Finally, the ".nintegrate()" method from Maxima gives even more extra information. &nbsp;</p>
<ul>
<li>Notice again the period/dot needed to use this.</li>
<li>It is only possible to use "h(x)"; doing "h.nintegrate()" raises an error.</li>
</ul>

{{{id=129|
h(x).nintegrate(x,0,pi/8)
///
(0.40319971916151143, 4.4764161173550703e-15, 21, 0)
}}}

<p>Second-semester calculus usually also covers various topics in summation. &nbsp;Sage can sum many abstract series; the notation is similar to plotting and integration.</p>

{{{id=160|
var('n') # Don't forget to declare your variables
sum((1/3)^n,n,0,oo)
///
3/2
}}}

<p>This is the geometric series, of course. &nbsp;</p>
<p>The next one is the famous result that a row of Pascal's triangle is a power of 2 - $$\binom{n}{0}+\binom{n}{1}+\binom{n}{2}+\cdots+\binom{n}{n-1}+\binom{n}{n}=2^n\; ,$$ which has many pleasing combinatorial interpretations.</p>

{{{id=162|
var('k') # We already declared n, so now we just need k
sum(binomial(n,k), k, 0, n)
///
2^n
}}}

<p>Do you remember what to do to see how we typed the nice sum in the text above? That's right, we can double-click the text area/cell to see this.</p>
<p>&nbsp;</p>
<p>Sage also can compute Taylor polynomials. &nbsp;</p>
<p>Taylor expansions depend on a lot of things. &nbsp;Whenever there are several inputs, keeping syntax straight is important. &nbsp;Here we have as inputs:</p>
<ul>
<li>the function,</li>
<li>the variable,</li>
<li>the point around which we are expanding the function, and&nbsp;</li>
<li>the degree.</li>
</ul>
<p>In the next cell, we call $g(x)$ the Taylor polynomial in question.</p>

{{{id=164|
g(x)=taylor(log(x),x,1,6); g(x)
///
-1/6*(x - 1)^6 + 1/5*(x - 1)^5 - 1/4*(x - 1)^4 + 1/3*(x - 1)^3 - 1/2*(x - 1)^2 + x - 1
}}}

<p>Notice how close the approximation is to the function on this interval!</p>

{{{id=166|
plot(g,(x,0,2))+plot(log(x),(x,0,2),color='red')
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

<div style="color: #000000; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: #ffffff; margin: 8px;">
<h1 id="Calc3" style="font-size: 2em;">Calculus 3</h1>
</div>
<p>We have already seen three-dimensional plotting, so it is not surprising that Sage has support for a variety of multivariable calculus problems. &nbsp;</p>
<p><em>Warning</em>: we will usually need to define all variables other than $x$! &nbsp;</p>

{{{id=130|
var('y')
f(x,y)=3*sin(x)-2*cos(y)-x*y
///
}}}

<p>Above, we have defined a typical function of two variables. &nbsp;</p>
<p>Below, we use the separating semicolons to demonstrate several things one might do with such a function, including:</p>
<ul>
<li>The gradient vector of all $\frac{\partial f}{\partial x_i}$</li>
<li>The Hessian of all possible second derivatives</li>
<li>A double partial derivative of $f$ with respect to $x$, then $y$ (that is, $\frac{\partial f}{\partial y\partial x}$)</li>
</ul>

{{{id=132|
f.gradient(); f.hessian(); f.diff(x,y)
///
(x, y) |--> (-y + 3*cos(x), -x + 2*sin(y))
[(x, y) |--> -3*sin(x)        (x, y) |--> -1]
[       (x, y) |--> -1  (x, y) |--> 2*cos(y)]
(x, y) |--> -1
}}}

<p>In an effort to make the syntax simpler, the gradient and Hessian are also available by asking for a total derivative. &nbsp;We also ask for nicer output again.</p>

{{{id=168|
show(f.diff()); show(f.diff(2))
///
<html><div class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\left( x, y \right) \ {\mapsto} \ \left(-y + 3 \, \cos\left(x\right),\,-x + 2 \, \sin\left(y\right)\right)</div></html>
<html><div class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rr}
\left( x, y \right) \ {\mapsto} \ -3 \, \sin\left(x\right) & \left( x, y \right) \ {\mapsto} \ -1 \\
\left( x, y \right) \ {\mapsto} \ -1 & \left( x, y \right) \ {\mapsto} \ 2 \, \cos\left(y\right)
\end{array}\right)</div></html>
}}}

<p>If we take the determinant of the Hessian, we get something useful for evaluating (the two-dimensional) critical points of $f$.</p>

{{{id=183|
show(f.diff(2).det())
///
<html><div class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\left( x, y \right) \ {\mapsto} \ -6 \, \sin\left(x\right) \cos\left(y\right) - 1</div></html>
}}}

<p>These ideas are particularly helpful if one wants to plot a vector field.</p>
<p>The following example is of the gradient. &nbsp;The vector plotted in the cell below is the unit vector in the direction $(1,2)$.</p>

{{{id=170|
P=plot_vector_field(f.diff(), (x,-3,3), (y,-3,3))
u=vector([1,2])
Q=plot(u/u.norm())
P+Q
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

<p>Rather than actually figure out the unit vector in that direction, it's easier to let Sage compute it by dividing the vector by its norm.</p>
<p>The directional derivative itself (in that direction, at the origin) can also be computed in this way.</p>

{{{id=173|
(f.diff()*u/u.norm())(0,0)
///
3/5*sqrt(5)
}}}

<p>Another useful type of plot in these situations is a contour plot.</p>
<p>Notice that the one below uses several options.  Try to correlate the options with features of the graphic.</p>

{{{id=148|
var('y')
contour_plot(y^2 + 1 - x^3 - x, (x,-pi,pi), (y,-pi,pi), contours=[-8,-4,0,4,8], colorbar=True, labels=True, label_colors='red')
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

<p>In this one, we have used options to:</p>
<ul>
<li>Explicitly list the contours we want to show,</li>
<li>Label these contours,&nbsp;</li>
<li>Place a color bar on the side to show the different levels.</li>
</ul>
<p>(Incidentally, the "True" and "False" valued options are some of the few non-numerical ones that do <em>not</em>&nbsp;need quotes.)</p>
<p>This is another good time to remind us we must explicitly ask for $y$ to be a variable here, as will be the case a few more times.</p>
<p>&nbsp;</p>
<p>As you gain experience in Sage, we will slowly explain less and less of the syntax of commands in these tutorials. &nbsp; You can think of places where not everything is explained as a mini-quiz.</p>
<p>&nbsp;</p>
<p>For example, the next example shows how one currently does a multiple integral. &nbsp;What have we done here? &nbsp;</p>

{{{id=158|
integrate(integrate(f,(x,0,pi)),(y,0,pi))
///
(x, y) |--> 6*pi - 1/4*pi^4
}}}

<p>Answer: notice that "integrate(f,(x,0,pi))" has been itself placed as the function inside "integrate(...,(y,0,pi))".</p>
<p>&nbsp;</p>
<p>We could use a 3D plot to help visualize this; these were already mentioned in the symbolics and plotting tutorial.</p>

{{{id=175|
plot3d(f,(x,0,pi),(y,0,pi),color='red')+plot3d(0,(x,0,pi),(y,0,pi))
///
}}}

<p>In addition to multivariate calculus, Calculus 3 often covers parametric calculus of a single variable. &nbsp;Sage can do arbitrary parametric plots, with fairly natural syntax. &nbsp;</p>
<p>This plot shows the tangent line to the most basic Lissajous curve at $t=1$. &nbsp;The commands should be strongly reminiscent of the ones at the beginning of this tutorial. &nbsp;</p>

{{{id=59|
var('t')
my_curve(t)=(sin(t), sin(2*t))
PP=parametric_plot( my_curve, (t, 0, 2*pi), color="purple" )
my_prime=my_curve.diff(t)
L=my_prime(1)*t+my_curve(1) # tangent line at t=1
parametric_plot(L, (t,-2,2))+PP
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

<p>Two comments:</p>
<ul>
<li>After a while, you'll find that giving things names other than $f$ and $g$ becomes quite helpful in distinguishing things from each other. &nbsp;Use descriptive names! &nbsp;We have tried to do so here.</li>
<li>If you are adventurous, try turning this into an interactive cell along the lines of the single variable example earlier! &nbsp;</li>
</ul>
<p>&nbsp;</p>
<ul>
</ul>
<div style="color: #000000; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: #ffffff; margin: 8px;">
<h1 style="font-size: 2em;">'Exam'</h1>
</div>
<p>Before moving out of the calculus world, it is good to have a sort of miniature exam. &nbsp;</p>
<p>In the cell below, we have plotted and given:</p>
<ul>
<li>A slope field for a differential equation,</li>
<li>A solution to an initial value problem,</li>
<li>And a symbolic formula for that solution.</li>
</ul>
<p>We assume you have never seen several of the commands before. &nbsp;Can you nonetheless figure out which commands are doing each piece, and what their syntax is? &nbsp;How would you look for help to find out more?</p>
<ul>
</ul>

{{{id=92|
y = var('y')
Plot1=plot_slope_field(2-y,(x,0,3),(y,0,20)) 
y = function('y',x) # declare y to be a function of x
h = desolve(diff(y,x) + y - 2, y, ics=[0,7])
Plot2=plot(h,0,3) 
show(expand(h)); show(Plot1+Plot2)
///
<html><div class="math">\newcommand{\Bold}[1]{\mathbf{#1}}5 \, e^{\left(-x\right)} + 2</div></html>
Warning: divide by zero encountered in divide
Warning: invalid value encountered in multiply
Warning: invalid value encountered in multiply
Warning: divide by zero encountered in divide
Warning: invalid value encountered in multiply
Warning: invalid value encountered in multiply
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

<p>Ready to see the answers? &nbsp;Don't peek until you've really tried it.</p>
<p>&nbsp;</p>
<p>In this cell we do the following:</p>
<ul>
<li>Make sure that $y$ is indeed a variable for the first plot.</li>
<li>Create a slope field for the DE for appropriate inputs of $x$ and $y$, and give the plot the name $Plot1$.  
<ul>
<li>(The "divide by zero" warnings are spurious, and come from the package we use to create arrowheads, which thinks that arrowheads of size zero are naughty. &nbsp;This will be taken care of in an upcoming release of Sage, and hopefully in the upstream package as well. &nbsp;This is the open source development process at work.)</li>
</ul>
</li>
<li>Use the formalism of the function command to get ready for the DE. &nbsp;   
<ul>
<li>Notice we have here once again used "#" to indicate a comment. &nbsp;</li>
<li>In this case, in order to use common terminology, we now have told Sage $y$ is no longer a variable, but instead a function (abstract) of the variable $x$.</li>
</ul>
</li>
<li>Use the differential equation solving command, with&nbsp;<strong>I</strong>nitial&nbsp;<strong>C</strong>ondition<strong>S</strong>&nbsp;of 2 and 2. &nbsp;</li>
<li>Plot the solution and give it the name $Plot2$.</li>
<li>Show a simplification of the symbolic version of the solution (which we didn't know ahead of time!) as well as the sum of the two graphs - the solution against the slope field.</li>
</ul>
<p>As you gain experience, you will see how to glean what <em>you</em>&nbsp;are looking for from examples in the documentation like this - which is one of the real goals of these tutorials.</p>
<p>&nbsp;</p>
<p>Congratulations! &nbsp;You are now armed with the basics of deploying Sage in the calculus sequence. &nbsp;We hope you will also look at one or two of the other tutorials&nbsp;geared toward different interests in the typical undergraduate curriculum. Such topics will include:</p>
<ul>
<li>Linear Algebra</li>
<li>Discrete Math</li>
<li>Abstract Algebra</li>
<li>Numerical Analysis</li>
<li>Statistics</li>
<li>Number Theory</li>
</ul>
<p>Thank you and good luck!</p>

{{{id=133|

///
}}}