PREP Tutorial, Symbolics and Basic Plotting
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 Symbolics and Plotting</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;</p>
<p>This tutorial has the following sections:</p>
<ul>
<li><a href="#Symbolic">Symbolic Expressions</a></li>
<li><a href="#2DPlotting">Basic 2D Plotting</a></li>
<li><a href="#3DPlotting">Basic 3D Plotting</a></li>
</ul>
<p>This tutorial assumes that one is familiar with the basics of Sage. &nbsp;We provide a (very) brief refresher.</p>
<ol>
<li>Make sure the syntax below for defining a function and getting a value makes sense.</li>
<li>Then evaluate the cell by clicking the "evaluate" link, or by pressing Shift-Enter (hold down Shift while pressing the Enter key).</li>
</ol></div>

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

<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 id="Symbolic" style="font-size: 2em;">Symbolic Expressions</h1>
</div>
<p>In addition to defining <em>functions</em> like we did above, we can just define <em>expressions</em> involving variables.&nbsp; In the cell below, we define an expression $FV$ which is the future value of an investment of \$100, compounded continuously.&nbsp; We then substitute in values for $r$ and $t$ which calculate the future value for $t=5$ years and $r=5\%$ nominal interest.</p>
<ul>
</ul>

{{{id=56|
var('r,t')
FV=100*e^(r*t)
FV(r=.05,t=5)
///
128.402541668774
}}}

<p>Some things to remember when working with symbolic expressions are:</p>
<ul>
<li>you must tell Sage what the variables are before using them in a symbolic expression.&nbsp; We did that above by typing "var('r,t')"</li>
<li>an asterisk ("*") signifies multiplication</li>
<li>the letter $e$ stands for the constant $2.71828...$ (unless you redefine it!).&nbsp; Likewise, "pi" (or $\pi$) and $I$ (think complex numbers) are also defined.</li>
<li>when you want to substitute some values into the expression, you must explicitly tell Sage which variables are being assigned which values.</li>
</ul>
<p>Notice that when we define a function, we don't need to specify which variable has which value, since when we define a function, we must specify the order:</p>

{{{id=136|
FV2(r,t)=100*e^(r*t)
FV2(.05,5)
///
128.402541668774
}}}

<p>Next, let's do some algebraic manipulations with expressions.</p>
<p>In the cells below, you'll notice something new: the number sign "#".&nbsp; In Sage (and in <a href="http://www.python.org" target="_blank">Python</a>), anything on a single line after the number sign is ignored.&nbsp; We say that "#" is a comment character.&nbsp; We use it below to mention alternative ways to do the same thing.</p>

{{{id=58|
z = (x+1)^3
///
}}}

{{{id=81|
expand(z) # or z.expand()
///
x^3 + 3*x^2 + 3*x + 1
}}}

{{{id=82|
y = expand(z)
y.factor() # or factor(y)
///
(x + 1)^3
}}}

<p>In the previous cell, we <em>assigned</em>&nbsp;the expression which is the expansion of $z$ to the variable $y$ with the first line. &nbsp;After that, anything we want to do to the expansion of $z$ can be done by doing it to $y$.</p>
<p>There are more commands like this as well. &nbsp;Notice that $z$ will no longer be $(x-1)^3$ after this cell is evaluated, since we've assigned $z$ to a (much more complex) expression.</p>

{{{id=84|
z = ((x - 1)^(3/2) - (x + 1)*sqrt(x - 1))/sqrt((x - 1)*(x + 1))
z.simplify_full()
///
-2/sqrt(x + 1)
}}}

{{{id=140|
z.simplify_rational() # Just simplify the fraction part of the expression by combining it.
///
-2*sqrt(x - 1)/sqrt(x^2 - 1)
}}}

<p>You can see various methods for simplifying an expression by using tab completion.&nbsp; Put your cursor at the end of the next cell (after the "simplify") and press tab to see lots of different methods.&nbsp; Remember that you can use the question mark (e.g., "z.simplify_rational?") to get help about a particular method.</p>

{{{id=142|
z.simplify
///
((x - 1)^(3/2) - sqrt(x - 1)*(x + 1))/sqrt((x - 1)*(x + 1))
}}}

<p>Don't forget, you can get nicely typeset versions of the output in several ways. &nbsp;One option is to click the 'Typeset' button at the top, while another is to use the "show" command.</p>

{{{id=86|
show(z.simplify_rational())
///
<html><div class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\frac{-2 \, \sqrt{x - 1}}{\sqrt{x^{2} - 1}}</div></html>
}}}

<p>Another Sage command that is useful is the "solve" command.&nbsp; Here, we solve the equation $x^2=-1$.&nbsp; In the solve command, we type an equals sign in the equation as two equal signs (since the single equals sign means assignment, as we've done above).&nbsp; We also denote the variable we'd like to solve for.</p>

{{{id=87|
solve(x^2==-1,x) # solve x^2==-1 for x
///
[x == -I, x == I]
}}}

{{{id=89|
solve(z==0,x)
///
[x == 1]
}}}

<h1 id="2DPlotting" style="font-size: 2em;">Basic 2D Plotting</h1>
<p>Before going into how Sage can be used for various calculus topics, we include a brief introduction to plotting the sorts of things which can be quite useful in calculus. &nbsp;There will be a separate tutorial for seeing more advanced plotting techniques.</p>
<p>Recall that we can generate a plot using fairly simple syntax:</p>

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

<p>We can give the plot a name, so that if we want to do something with the plot later, we don't have to type out the entire plot command.&nbsp; In the next cell, we give the plot the name $P$.</p>

{{{id=19|
P=plot(f,(x,-1,1))
///
}}}

<p>You can superimpose plots by adding them together. &nbsp;For instance, observe that the tangent line to $f$ at $x=0$ is just the line $y=1$. &nbsp;Let's plot this, in a different color, and with a different style for the line, but over the same interval. &nbsp;Notice that we can get the plot $Q$ to show up by putting it in a separate line by itself.</p>

{{{id=23|
Q=plot(1,(x,-1,1),color="red", linestyle="--")
Q
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

<p>To show both plots superimposed on each other, we simply add them.</p>

{{{id=27|
P+Q
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

<p>Suppose we wanted to zoom in on this.&nbsp; We could create another plot.&nbsp; Another way to zoom in is to set the plot window using the "show" command:</p>

{{{id=31|
(P+Q).show(xmin=-.1,xmax=.1,ymin=.99,ymax=1.01)
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

<p>Since the axes no longer cross in the frame of reference, Sage shows a short gap between the horizontal and vertical axes.&nbsp;</p>
<p>There are many options one can pass in for various purposes. &nbsp;Pay very close attention to which ones require quotes around the values (such as the color option when we made a "red" line) and which ones do not (such as the xmin in the previous plot, where the minimum x value was just $-.1$). &nbsp;Usually (though not always) quotes are required for option values which are words or strings of characters, and not required for numerical values.</p>
<p>Perhaps one of the most useful of these options is the "axes_labels" option to get labels for the axes; as with the word processor, using dollar signs (like in Latex) to make the labels typeset nicely. &nbsp;Here we need both quotes and brackets for proper syntax, since there are two axes to label and the labels are not actually numbers.</p>

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

<p>One final note is that plots of functions with vertical asymptotes may need their vertical viewing range set manually to be useful.</p>

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

<p>Remember, you can use the command "plot?" to find out about most of the options demonstrated above.</p>
<p>Below are a few plotting options that you can experiment with.&nbsp; Just evaluate the cell below and play with the sliders, buttons, color picker, etc., to change the plot options.&nbsp; This uses a feature of Sage called "interacts", and is a very powerful way to engage a student in exploring a problem.</p>

{{{id=151|
var('x')
@interact
def plot_example(f=sin(x^2),r=range_slider(-5,5,step_size=1/4,default=(-3,3)), 
                 color=color_selector(widget='colorpicker'),
                 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
                 ):
    show(plot(f, (x,r[0],r[1]), color=color, 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)
///
<html><!--notruncate--><div padding=6 id="div-interact-151"> <table width=800px height=20px bgcolor="#c5c5c5"
                 cellpadding=15><tr><td bgcolor="#f9f9f9" valign=top align=left><table><tr><td align=right><font color="black">f&nbsp;</font></td><td><input type="text" value="sin(x^2)" size=80 onchange="interact(151, '_interact_.update(\'151\', \'f\', 1, _interact_.standard_b64decode(\''+encode64(this.value)+'\'), globals()); _interact_.recompute(\'151\');')"></input></td></tr>
<tr><td align=right><font color="black">r&nbsp;</font></td><td><table><tr><td>
        <div id="slider-r-151" style="margin:0px; margin-left: 1.0em; margin-right: 1.0em; width: 20.0em;"></div>
        </td></tr><tr><td><font color="black" id="slider-r-151-lbl"></font></td></tr></table><script>(function()
    {
        var values = ["-5","-19/4","-9/2","-17/4","-4","-15/4","-7/2","-13/4","-3","-11/4","-5/2","-9/4","-2","-7/4","-3/2","-5/4","-1","-3/4","-1/2","-1/4","0","1/4","1/2","3/4","1","5/4","3/2","7/4","2","9/4","5/2","11/4","3","13/4","7/2","15/4","4","17/4","9/2","19/4","5"];
        var pos = [8, 32];
        var sel = '#slider-r-151';
        var updatePos = function()
        {
            pos[0]=$(sel).slider('values', 0);
            pos[1]=$(sel).slider('values', 1);
            if(values!=null) $(sel+'-lbl').text('('+values[pos[0]]+', '+values[pos[1]]+')');
        };
        setTimeout(function()
        {
            $(sel).slider(
            {
                range: true,
                step: 1,
                min: 0,
                max: 40,
                values: [8, 32],
                change: function(e,ui){ updatePos(); interact(151, '_interact_.update(\'151\', \'r\', 2, _interact_.standard_b64decode(\''+encode64(pos[0]+' '+pos[1])+'\'), globals()); _interact_.recompute(\'151\');'); },
                slide: updatePos
            });
            updatePos();
        }, 1);
    })();</script></td></tr>
<tr><td align=right><font color="black">color&nbsp;</font></td><td>
            <table>
              <tr>
                <td>
                  <input type="text"
                         id="color-selector-color-151"
                         name="color"
                         onchange="interact(151, '_interact_.update(\'151\', \'color\', 3, _interact_.standard_b64decode(\''+encode64(this.value)+'\'), globals()); _interact_.recompute(\'151\');'); $(this).css({backgroundColor: this.value}); $('#color-selector-color-151-picker').ColorPickerSetColor(this.value.slice(1)); $('#color-selector-color-151-picker div').css({backgroundColor: this.value});"
                         value="#0000ff"
                         style="" />
                </td>
                <td>
                  <div id="color-selector-color-151-picker" style="position: relative; width: 36px; height: 36px; background: url(/javascript/jquery/plugins/colorpicker/images/select.png);">
                    <div style="position: absolute; top: 3px; left: 3px; width: 30px; height: 30px; background: url(/javascript/jquery/plugins/colorpicker/images/select.png) center; background-color: #0000ff"></div>
                  </div>
                </td>
              </tr>
            </table><script>
            setTimeout(function () {
                var def = '#0000ff'.slice(1), div = $('#color-selector-color-151-picker div'),
                    input = $('#color-selector-color-151'), picker = $('#color-selector-color-151-picker');
                input.css({
                    backgroundColor: '#0000ff',
                    // Should be good enough:
                    color: (parseInt(def.slice(0, 2), 16) + parseInt(def.slice(2, 4), 16) + parseInt(def.slice(4, 6), 16)) / 3 > 127 ? '#000000' : '#ffffff'
                });
                picker.ColorPicker({
                    color : '#0000ff',
                    onShow : function (pkr) {
                        $(pkr).css({zIndex: '10'}).show();
                        return false;
                    },
                    onChange : function (hsb, hex, rgb) {
                        color = '#' + hex;
                        if (input.val() !== color) {
                            input.val(color);
                            input.css({
                                backgroundColor: color,
                                color: hsb.b > 50 ? '#000000' : '#ffffff'
                            });
                            div.css({backgroundColor: color});
                            interact(151, '_interact_.update(\'151\', \'color\', 3, _interact_.standard_b64decode(\''+encode64(color)+'\'), globals()); _interact_.recompute(\'151\');');
                        }
                    },
                    onSubmit : function (hsb, hex, rgb, el) {
                        $(el).ColorPickerHide();
                    }
                });
            }, 1);
            </script></td></tr>
<tr><td align=right><font color="black">thickness&nbsp;</font></td><td><table><tr><td>
        <div id="slider-thickness-151" style="margin:0px; margin-left: 1.0em; margin-right: 1.0em; width: 15.0em;"></div>
        </td><td><font color="black" id="slider-thickness-151-lbl"></font></td></tr></table><script>(function(){ var values = ["1","2","3","4","5","6","7","8","9","10"]; setTimeout(function() {
    $('#slider-thickness-151').slider({
        step: 1, min: 0, max: 9, value: 2,
        change: function (e,ui) { var position = ui.value; if(values!=null) $('#slider-thickness-151-lbl').text(values[position]); interact(151, '_interact_.update(\'151\', \'thickness\', 4, _interact_.standard_b64decode(\''+encode64(position)+'\'), globals()); _interact_.recompute(\'151\');'); },
        slide: function(e,ui) { if(values!=null) $('#slider-thickness-151-lbl').text(values[ui.value]); }
    });
    if(values != null) $('#slider-thickness-151-lbl').text(values[$('#slider-thickness-151').slider('value')]);
    }, 1); })();</script></td></tr>
<tr><td align=right><font color="black">adaptive_recursion&nbsp;</font></td><td><table><tr><td>
        <div id="slider-adaptive_recursion-151" style="margin:0px; margin-left: 1.0em; margin-right: 1.0em; width: 15.0em;"></div>
        </td><td><font color="black" id="slider-adaptive_recursion-151-lbl"></font></td></tr></table><script>(function(){ var values = ["0","1","2","3","4","5","6","7","8","9","10"]; setTimeout(function() {
    $('#slider-adaptive_recursion-151').slider({
        step: 1, min: 0, max: 10, value: 5,
        change: function (e,ui) { var position = ui.value; if(values!=null) $('#slider-adaptive_recursion-151-lbl').text(values[position]); interact(151, '_interact_.update(\'151\', \'adaptive_recursion\', 5, _interact_.standard_b64decode(\''+encode64(position)+'\'), globals()); _interact_.recompute(\'151\');'); },
        slide: function(e,ui) { if(values!=null) $('#slider-adaptive_recursion-151-lbl').text(values[ui.value]); }
    });
    if(values != null) $('#slider-adaptive_recursion-151-lbl').text(values[$('#slider-adaptive_recursion-151').slider('value')]);
    }, 1); })();</script></td></tr>
<tr><td align=right><font color="black">adaptive_tolerance&nbsp;</font></td><td><table><tr><td>
        <div id="slider-adaptive_tolerance-151" style="margin:0px; margin-left: 1.0em; margin-right: 1.0em; width: 15.0em;"></div>
        </td><td><font color="black" id="slider-adaptive_tolerance-151-lbl"></font></td></tr></table><script>(function(){ var values = ["0.00100000000000000","0.00300200400801603","0.00500400801603206","0.00700601202404810","0.00900801603206413","0.0110100200400802","0.0130120240480962","0.0150140280561122","0.0170160320641283","0.0190180360721443","0.0210200400801603","0.0230220440881764","0.0250240480961924","0.0270260521042084","0.0290280561122245","0.0310300601202405","0.0330320641282565","0.0350340681362726","0.0370360721442886","0.0390380761523046","0.0410400801603207","0.0430420841683367","0.0450440881763527","0.0470460921843688","0.0490480961923848","0.0510501002004008","0.0530521042084169","0.0550541082164329","0.0570561122244489","0.0590581162324650","0.0610601202404810","0.0630621242484970","0.0650641282565131","0.0670661322645291","0.0690681362725451","0.0710701402805612","0.0730721442885772","0.0750741482965932","0.0770761523046093","0.0790781563126253","0.0810801603206413","0.0830821643286574","0.0850841683366734","0.0870861723446894","0.0890881763527055","0.0910901803607215","0.0930921843687375","0.0950941883767536","0.0970961923847696","0.0990981963927857","0.101100200400802","0.103102204408818","0.105104208416834","0.107106212424850","0.109108216432866","0.111110220440882","0.113112224448898","0.115114228456914","0.117116232464930","0.119118236472946","0.121120240480962","0.123122244488978","0.125124248496994","0.127126252505010","0.129128256513026","0.131130260521042","0.133132264529058","0.135134268537074","0.137136272545090","0.139138276553106","0.141140280561122","0.143142284569138","0.145144288577154","0.147146292585170","0.149148296593186","0.151150300601203","0.153152304609219","0.155154308617235","0.157156312625251","0.159158316633267","0.161160320641283","0.163162324649299","0.165164328657315","0.167166332665331","0.169168336673347","0.171170340681363","0.173172344689379","0.175174348697395","0.177176352705411","0.179178356713427","0.181180360721443","0.183182364729459","0.185184368737475","0.187186372745491","0.189188376753507","0.191190380761523","0.193192384769539","0.195194388777555","0.197196392785571","0.199198396793587","0.201200400801603","0.203202404809619","0.205204408817635","0.207206412825651","0.209208416833668","0.211210420841684","0.213212424849700","0.215214428857716","0.217216432865732","0.219218436873748","0.221220440881764","0.223222444889780","0.225224448897796","0.227226452905812","0.229228456913828","0.231230460921844","0.233232464929860","0.235234468937876","0.237236472945892","0.239238476953908","0.241240480961924","0.243242484969940","0.245244488977956","0.247246492985972","0.249248496993988","0.251250501002004","0.253252505010020","0.255254509018036","0.257256513026052","0.259258517034068","0.261260521042084","0.263262525050100","0.265264529058116","0.267266533066132","0.269268537074148","0.271270541082165","0.273272545090181","0.275274549098197","0.277276553106213","0.279278557114229","0.281280561122245","0.283282565130261","0.285284569138277","0.287286573146293","0.289288577154309","0.291290581162325","0.293292585170341","0.295294589178357","0.297296593186373","0.299298597194389","0.301300601202405","0.303302605210421","0.305304609218437","0.307306613226453","0.309308617234469","0.311310621242485","0.313312625250501","0.315314629258517","0.317316633266533","0.319318637274549","0.321320641282565","0.323322645290581","0.325324649298597","0.327326653306613","0.329328657314630","0.331330661322646","0.333332665330662","0.335334669338678","0.337336673346694","0.339338677354710","0.341340681362726","0.343342685370742","0.345344689378758","0.347346693386774","0.349348697394790","0.351350701402806","0.353352705410822","0.355354709418838","0.357356713426854","0.359358717434870","0.361360721442886","0.363362725450902","0.365364729458918","0.367366733466934","0.369368737474950","0.371370741482966","0.373372745490982","0.375374749498998","0.377376753507014","0.379378757515030","0.381380761523046","0.383382765531062","0.385384769539078","0.387386773547094","0.389388777555111","0.391390781563127","0.393392785571143","0.395394789579159","0.397396793587175","0.399398797595191","0.401400801603207","0.403402805611223","0.405404809619239","0.407406813627255","0.409408817635271","0.411410821643287","0.413412825651303","0.415414829659319","0.417416833667335","0.419418837675351","0.421420841683367","0.423422845691383","0.425424849699399","0.427426853707415","0.429428857715431","0.431430861723447","0.433432865731463","0.435434869739479","0.437436873747495","0.439438877755511","0.441440881763527","0.443442885771543","0.445444889779559","0.447446893787576","0.449448897795592","0.451450901803608","0.453452905811624","0.455454909819640","0.457456913827656","0.459458917835672","0.461460921843688","0.463462925851704","0.465464929859720","0.467466933867736","0.469468937875752","0.471470941883768","0.473472945891784","0.475474949899800","0.477476953907816","0.479478957915832","0.481480961923848","0.483482965931864","0.485484969939880","0.487486973947896","0.489488977955912","0.491490981963928","0.493492985971944","0.495494989979960","0.497496993987976","0.499498997995992","0.501501002004008","0.503503006012024","0.505505010020040","0.507507014028056","0.509509018036072","0.511511022044088","0.513513026052104","0.515515030060120","0.517517034068136","0.519519038076152","0.521521042084168","0.523523046092184","0.525525050100200","0.527527054108216","0.529529058116232","0.531531062124248","0.533533066132264","0.535535070140280","0.537537074148296","0.539539078156312","0.541541082164328","0.543543086172344","0.545545090180360","0.547547094188376","0.549549098196392","0.551551102204408","0.553553106212424","0.555555110220440","0.557557114228456","0.559559118236472","0.561561122244488","0.563563126252504","0.565565130260520","0.567567134268536","0.569569138276552","0.571571142284568","0.573573146292584","0.575575150300600","0.577577154308616","0.579579158316632","0.581581162324648","0.583583166332663","0.585585170340679","0.587587174348695","0.589589178356711","0.591591182364727","0.593593186372743","0.595595190380759","0.597597194388775","0.599599198396791","0.601601202404807","0.603603206412823","0.605605210420839","0.607607214428855","0.609609218436871","0.611611222444887","0.613613226452903","0.615615230460919","0.617617234468935","0.619619238476951","0.621621242484967","0.623623246492983","0.625625250500999","0.627627254509015","0.629629258517031","0.631631262525047","0.633633266533063","0.635635270541079","0.637637274549095","0.639639278557111","0.641641282565127","0.643643286573143","0.645645290581159","0.647647294589175","0.649649298597191","0.651651302605207","0.653653306613223","0.655655310621239","0.657657314629255","0.659659318637271","0.661661322645287","0.663663326653303","0.665665330661319","0.667667334669335","0.669669338677351","0.671671342685367","0.673673346693383","0.675675350701399","0.677677354709414","0.679679358717430","0.681681362725446","0.683683366733462","0.685685370741478","0.687687374749494","0.689689378757510","0.691691382765526","0.693693386773542","0.695695390781558","0.697697394789574","0.699699398797590","0.701701402805606","0.703703406813622","0.705705410821638","0.707707414829654","0.709709418837670","0.711711422845686","0.713713426853702","0.715715430861718","0.717717434869734","0.719719438877750","0.721721442885766","0.723723446893782","0.725725450901798","0.727727454909814","0.729729458917830","0.731731462925846","0.733733466933862","0.735735470941878","0.737737474949894","0.739739478957910","0.741741482965926","0.743743486973942","0.745745490981958","0.747747494989974","0.749749498997990","0.751751503006006","0.753753507014022","0.755755511022038","0.757757515030054","0.759759519038070","0.761761523046086","0.763763527054102","0.765765531062118","0.767767535070134","0.769769539078149","0.771771543086165","0.773773547094181","0.775775551102197","0.777777555110213","0.779779559118229","0.781781563126245","0.783783567134261","0.785785571142277","0.787787575150293","0.789789579158309","0.791791583166325","0.793793587174341","0.795795591182357","0.797797595190373","0.799799599198389","0.801801603206405","0.803803607214421","0.805805611222437","0.807807615230453","0.809809619238469","0.811811623246485","0.813813627254501","0.815815631262517","0.817817635270533","0.819819639278549","0.821821643286565","0.823823647294581","0.825825651302597","0.827827655310613","0.829829659318629","0.831831663326645","0.833833667334661","0.835835671342677","0.837837675350693","0.839839679358709","0.841841683366725","0.843843687374741","0.845845691382757","0.847847695390773","0.849849699398789","0.851851703406805","0.853853707414821","0.855855711422837","0.857857715430853","0.859859719438869","0.861861723446884","0.863863727454900","0.865865731462916","0.867867735470932","0.869869739478948","0.871871743486964","0.873873747494980","0.875875751502996","0.877877755511012","0.879879759519028","0.881881763527044","0.883883767535060","0.885885771543076","0.887887775551092","0.889889779559108","0.891891783567124","0.893893787575140","0.895895791583156","0.897897795591172","0.899899799599188","0.901901803607204","0.903903807615220","0.905905811623236","0.907907815631252","0.909909819639268","0.911911823647284","0.913913827655300","0.915915831663316","0.917917835671332","0.919919839679348","0.921921843687364","0.923923847695380","0.925925851703396","0.927927855711412","0.929929859719428","0.931931863727444","0.933933867735460","0.935935871743476","0.937937875751492","0.939939879759508","0.941941883767524","0.943943887775540","0.945945891783556","0.947947895791572","0.949949899799588","0.951951903807604","0.953953907815620","0.955955911823635","0.957957915831651","0.959959919839667","0.961961923847683","0.963963927855699","0.965965931863715","0.967967935871731","0.969969939879747","0.971971943887763","0.973973947895779","0.975975951903795","0.977977955911811","0.979979959919827","0.981981963927843","0.983983967935859","0.985985971943875","0.987987975951891","0.989989979959907","0.991991983967923","0.993993987975939","0.995995991983955","0.997997995991971","1.00000000000000"]; setTimeout(function() {
    $('#slider-adaptive_tolerance-151').slider({
        step: 1, min: 0, max: 499, value: 4,
        change: function (e,ui) { var position = ui.value; if(values!=null) $('#slider-adaptive_tolerance-151-lbl').text(values[position]); interact(151, '_interact_.update(\'151\', \'adaptive_tolerance\', 6, _interact_.standard_b64decode(\''+encode64(position)+'\'), globals()); _interact_.recompute(\'151\');'); },
        slide: function(e,ui) { if(values!=null) $('#slider-adaptive_tolerance-151-lbl').text(values[ui.value]); }
    });
    if(values != null) $('#slider-adaptive_tolerance-151-lbl').text(values[$('#slider-adaptive_tolerance-151').slider('value')]);
    }, 1); })();</script></td></tr>
<tr><td align=right><font color="black">plot_points&nbsp;</font></td><td><table><tr><td>
        <div id="slider-plot_points-151" style="margin:0px; margin-left: 1.0em; margin-right: 1.0em; width: 15.0em;"></div>
        </td><td><font color="black" id="slider-plot_points-151-lbl"></font></td></tr></table><script>(function(){ var values = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59","60","61","62","63","64","65","66","67","68","69","70","71","72","73","74","75","76","77","78","79","80","81","82","83","84","85","86","87","88","89","90","91","92","93","94","95","96","97","98","99","100"]; setTimeout(function() {
    $('#slider-plot_points-151').slider({
        step: 1, min: 0, max: 99, value: 19,
        change: function (e,ui) { var position = ui.value; if(values!=null) $('#slider-plot_points-151-lbl').text(values[position]); interact(151, '_interact_.update(\'151\', \'plot_points\', 7, _interact_.standard_b64decode(\''+encode64(position)+'\'), globals()); _interact_.recompute(\'151\');'); },
        slide: function(e,ui) { if(values!=null) $('#slider-plot_points-151-lbl').text(values[ui.value]); }
    });
    if(values != null) $('#slider-plot_points-151-lbl').text(values[$('#slider-plot_points-151').slider('value')]);
    }, 1); })();</script></td></tr>
<tr><td align=right><font color="black">linestyle&nbsp;</font></td><td><table style="border:1px solid #dfdfdf; background-color:#efefef;">
<tr><td><button style="border-style:inset;" value="0" onclick="$('BUTTON', this.parentNode).css('border-style', 'outset'); $(this).css('border-style', 'inset'); interact(151, '_interact_.update(\'151\', \'linestyle\', 8, _interact_.standard_b64decode(\''+encode64(this.value)+'\'), globals()); _interact_.recompute(\'151\');')">-</button>
<button style="border-style:outset;" value="1" onclick="$('BUTTON', this.parentNode).css('border-style', 'outset'); $(this).css('border-style', 'inset'); interact(151, '_interact_.update(\'151\', \'linestyle\', 8, _interact_.standard_b64decode(\''+encode64(this.value)+'\'), globals()); _interact_.recompute(\'151\');')">--</button>
<button style="border-style:outset;" value="2" onclick="$('BUTTON', this.parentNode).css('border-style', 'outset'); $(this).css('border-style', 'inset'); interact(151, '_interact_.update(\'151\', \'linestyle\', 8, _interact_.standard_b64decode(\''+encode64(this.value)+'\'), globals()); _interact_.recompute(\'151\');')">-.</button>
<button style="border-style:outset;" value="3" onclick="$('BUTTON', this.parentNode).css('border-style', 'outset'); $(this).css('border-style', 'inset'); interact(151, '_interact_.update(\'151\', \'linestyle\', 8, _interact_.standard_b64decode(\''+encode64(this.value)+'\'), globals()); _interact_.recompute(\'151\');')">:</button>
</td></tr></table></td></tr>
<tr><td align=right><font color="black">gridlines&nbsp;</font></td><td><input type="checkbox"  width=200px onchange="interact(151, '_interact_.update(\'151\', \'gridlines\', 9, _interact_.standard_b64decode(\''+encode64(this.checked)+'\'), globals()); _interact_.recompute(\'151\');')"></input></td></tr>
<tr><td align=right><font color="black">fill&nbsp;</font></td><td><input type="checkbox"  width=200px onchange="interact(151, '_interact_.update(\'151\', \'fill\', 10, _interact_.standard_b64decode(\''+encode64(this.checked)+'\'), globals()); _interact_.recompute(\'151\');')"></input></td></tr>
<tr><td align=right><font color="black">frame&nbsp;</font></td><td><input type="checkbox"  width=200px onchange="interact(151, '_interact_.update(\'151\', \'frame\', 11, _interact_.standard_b64decode(\''+encode64(this.checked)+'\'), globals()); _interact_.recompute(\'151\');')"></input></td></tr>
<tr><td align=right><font color="black">axes&nbsp;</font></td><td><input type="checkbox" checked width=200px onchange="interact(151, '_interact_.update(\'151\', \'axes\', 12, _interact_.standard_b64decode(\''+encode64(this.checked)+'\'), globals()); _interact_.recompute(\'151\');')"></input></td></tr>
</table><div id="cell-interact-151"><?__SAGE__START>
        <table border=0 bgcolor="white" width=100% height=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>
                 </tr></table></div>
                 </html>
}}}

<h1 id="3DPlotting" style="font-size: 2em;">Basic 3D Plotting</h1>
<p>There are several mechanisms for viewing three-dimensional plots in Sage, but we will stick to the default option in the notebook interface, which is via Java applets from the program <a href="http://jmol.sourceforge.net/" target="_blank">Jmol</a>.</p>
<p>Plotting a 3D plot is similar to plotting a 2D plot, but we need to specify ranges for two variables instead of one.</p>

{{{id=69|
g(x,y)=sin(x^2+y^2)
plot3d(g,(x,-5,5),(y,-5,5))
///
}}}

<p>There is a lot you can do with the 3D plots. &nbsp;Try rotating the plot above by clicking and dragging the mouse inside of the plot.&nbsp; Also, right-click (Control-click if you have only one mouse button) inside of the plot to see other options in a menu.&nbsp; If you have a wheel on your mouse, you can scroll the mousewheel to zoom.&nbsp; You can also right-click to see other options, such as spinning the plot, changing various colors, and even making the plot suitable for viewing through 3D glasses (under the "style", then "stereographic" submenus)</p>
<p>When using the "plot3d" command, the first variable range specified is plotted along the usual "x" axis, while the second range specified is plotted along the usual "y" axis.</p>
<p>The plot above is somewhat crude because the function is not sampled enough times.&nbsp; We can make the plot smoother by telling Sage to sample the function using a grid of 300 by 300 points.&nbsp; Sage then samples the function at 90,000 points!</p>

{{{id=74|
plot3d(g,(x,-5,5),(y,-5,5),plot_points=300)
///
}}}

<p>As with 2D plots, we can superimpose 3D plots by adding them together.</p>

{{{id=148|
var('x,y') 
b = 2.2 
P=plot3d(sin(x^2-y^2),(x,-b,b),(y,-b,b), opacity=.7)
Q=plot3d(0, (x,-b,b), (y,-b,b), color='red')
P+Q
///
}}}

<p>We close with a cool plot that we define implicitly as a 3D contour plot.</p>

{{{id=145|
var('x,y,z') 
T = golden_ratio 
p = 2 - (cos(x + T*y) + cos(x - T*y) + cos(y + T*z) + cos(y - T*z) + cos(z - T*x) + cos(z + T*x)) 
r = 4.78 
implicit_plot3d(p, (x, -r, r), (y, -r, r), (z, -r, r), plot_points=50, color='yellow')
///
}}}

<p>The next tutorial will use all that you have learned about Sage basics, symbolics, and plotting in a specific mathematical venue - the calculus sequence!</p>

{{{id=147|

///
}}}