Tutorial: Calculus and Linear Algebra in Sage
system:sage


<h1>Calculus, plotting &amp; interact</h1>

<h2>Some differentiating and plotting<br /></h2>
<p><strong>Exercise:</strong> Let $f(x) = x^4 + x^3 - 13 x^2 - x + 12$. Define $f$ as a symbolic function.</p>

{{{id=4|

///
}}}

{{{id=12|

///
}}}

<p>&nbsp;</p>
<p><strong>Exercise:</strong> Plot $f$ on the domain $-4.5 \leq x \leq 3.5$.</p>

{{{id=13|

///
}}}

{{{id=10|

///
}}}

<p>&nbsp;</p>
<p><strong>Exercise:</strong> Find numerical approximations for the <em>critical values</em> of $f$ by taking the derivative of $f$ and using the <strong>find_root</strong> method. (<em>Hint:</em> plot the derivative.)</p>

{{{id=15|

///
}}}

{{{id=40|

///
}}}

{{{id=43|

///
}}}

{{{id=42|

///
}}}

{{{id=37|

///
}}}

<p>&nbsp;</p>
<p><strong>Exercise:</strong> Find numerical approximations for the <em>critical values</em> of $f$ by taking the derivative of $f$ and using the <strong>roots(ring=RR)</strong> method. (Here, <strong>RR</strong> stands for the real numbers.) Are there any roots over the ring of rationals (<strong>QQ</strong>)?</p>

{{{id=38|

///
}}}

{{{id=16|

///
}}}

<p><strong>Exercise:</strong> Compute the equation $y = mx +b$ of the tangent line to the function $f$ at the points $x=-1$ and $x=2$.</p>

{{{id=7|

///
}}}

{{{id=6|

///
}}}

<p><strong><br /></strong></p>
<p><strong>Exercise:</strong> Write a function that takes $x$ as an argument and returns the equation of the tangent line to $f$ through the point $x$.</p>

{{{id=5|

///
}}}

{{{id=19|

///
}}}

<p><strong><br /> </strong></p>
<p><strong>Exercise:</strong> Write a function that takes $x$ as an argument and plots $f$ together with the the tangent line to $f$ through the point $x$. Make the line <span style="color: #ff0000;"><em>red</em></span>.</p>

{{{id=20|

///
}}}

{{{id=21|

///
}}}

<p><strong><br /> </strong></p>
<p><strong>Exercise:</strong> Convert the function you created above into an <strong>@interact</strong> object. Turn the argument $x$ into a <strong>slider</strong>. (<em>Hint:</em> see the documentation for <strong>interact</strong> for examples on creating <strong>sliders</strong>.)</p>

{{{id=22|

///
}}}

{{{id=23|

///
}}}

{{{id=24|

///
}}}

{{{id=25|

///
}}}

<h1>Linear Algebra</h1>
<h2>Vectors<br /></h2>
<p>To create a vector in Sage, use the <strong>vector</strong> command.</p>
<p><strong>Exercise:</strong> Create the vector $x = (1, 2, \ldots, 100)$.</p>

{{{id=18|

///
}}}

{{{id=119|

///
}}}

<p>
<table border="2" align="center">
<tbody>
<tr>
<td style="text-align: center;"><span style="font-size: large;"><span style="color: #ff0000;"><strong>Note: vectors in Sage are <em>row</em> vectors!</strong></span></span></td>
</tr>
</tbody>
</table>
</p>
<p><strong>Exercise: </strong>Create the vector $y = (1^2, 2^2, \ldots, 100^2)$.</p>

{{{id=110|

///
}}}

{{{id=27|

///
}}}

<p><strong>Exercise: </strong>Type <strong>x.</strong> and hit tab to see the available methods for vectors. Find the <em>norm</em> (length) of the vectors <strong>x</strong> and <strong>y</strong>.</p>

{{{id=112|

///
}}}

{{{id=111|

///
}}}

<p><strong>Exercise: </strong>Find the <em>dot product</em> of <strong>x</strong> and <strong>y</strong>.</p>

{{{id=169|

///
}}}

{{{id=170|

///
}}}

<p style="text-align: right;"><span style="font-size: small;">[The above problems are essentially the first problem on Exercise Set 1 of <a href="http://modular.math.washington.edu/">William Stein's</a> <a href="http://wiki.wstein.org/09/480b">Math 480b</a>.]</span></p>
<h2>Matrices</h2>
<p><strong>Exercise:</strong> Use the <strong>matrix</strong> command to create the following matrix over the rational numbers (hint: in Sage, <strong>QQ</strong> denotes the field of rational numbers).</p>
<p>$$\left(\begin{array}{rrrrrr}<br />3 &amp; 2 &amp; 2 &amp; 1 &amp; 1 &amp; 0 \\<br />2 &amp; 3 &amp; 1 &amp; 0 &amp; 2 &amp; 1 \\<br />2 &amp; 1 &amp; 3 &amp; 2 &amp; 0 &amp; 1 \\<br />1 &amp; 0 &amp; 2 &amp; 3 &amp; 1 &amp; 2 \\<br />1 &amp; 2 &amp; 0 &amp; 1 &amp; 3 &amp; 2 \\<br />0 &amp; 1 &amp; 1 &amp; 2 &amp; 2 &amp; 3<br />\end{array}\right)$$</p>
<ol>
<li>Find the <em>echelon form</em> of the above matrix.</li>
<li>Find the <em>right kernel</em> of the matrix.</li>
<li>Find the <em>eigenvalues</em> of the matrix.</li>
<li>Find the <em>left eigenvectors</em> of the matrix.</li>
<li>Find the <em>right eigenspaces</em> of the matrix.</li>
</ol>

{{{id=122|

///
}}}

{{{id=179|

///
}}}

{{{id=178|

///
}}}

{{{id=121|

///
}}}

{{{id=144|

///
}}}

<p><strong>Exercise: </strong>For what values of $k$ is the <em>determinant</em> of the following matrix $0$?</p>
<p>$$\left(\begin{array}{rrr}<br />1 &amp; 1 &amp; -1 \\<br />2 &amp; 3 &amp; k \\<br />1 &amp; k &amp; 3<br />\end{array}\right)$$</p>
<p style="text-align: right;"><span style="font-size: small;">[K. R. Matthews, <a href="http://www.numbertheory.org/book/">Elementary Linear Algebra</a>, Chapter 4, Problem 8]</span></p>

{{{id=183|

///
}}}

{{{id=182|

///
}}}

{{{id=155|

///
}}}

{{{id=154|

///
}}}

<p><strong>Exercise: </strong>Prove that the determinant of the following matrix is $-8$.</p>
<p>$$\left(\begin{array}{rrr}<br />{n}^{2}  &amp; {\left( n + 1 \right)}^{2}  &amp; {\left( n + 2<br />\right)}^{2}  \\<br />{\left( n + 1 \right)}^{2}  &amp; {\left( n + 2 \right)}^{2}  &amp;<br />{\left( n + 3 \right)}^{2}  \\<br />{\left( n + 2 \right)}^{2}  &amp; {\left( n + 3 \right)}^{2}  &amp;</p>
<p>{\left( n + 4 \right)}^{2}\end{array}\right)$$</p>
<p style="text-align: right;"><span style="font-size: small;">[K. R. Matthews, <a href="http://www.numbertheory.org/book/"><em>Elementary Linear Algebra</em></a>, Chapter 4, Problem 3] </span></p>

{{{id=185|

///
}}}

{{{id=184|

///
}}}

{{{id=141|

///
}}}

{{{id=133|

///
}}}

<p><strong>Exercise: </strong>Prove that if $a \neq c$, then the line through the points $(a,b)$ and $(c,d)$ is given by the following equation.<br /><br />$$\det\left(\begin{array}{rrr}<br />x &amp; y &amp; 1 \\<br />a &amp; b &amp; 1 \\<br />c &amp; d &amp; 1<br />\end{array}\right) = 0.$$</p>

{{{id=187|

///
}}}

{{{id=186|

///
}}}

{{{id=149|

///
}}}

{{{id=129|

///
}}}

<p><strong>Exercise: </strong>Find the determinant of the following matrices.</p>
<pre class="shrunk">$$<br />\left(\begin{array}{r}<br />1<br />\end{array}\right),<br />\left(\begin{array}{rr}<br />1 &amp; 1 \\<br />r &amp; 1<br />\end{array}\right),<br />\left(\begin{array}{rrr}<br />1 &amp; 1 &amp; 1 \\<br />r &amp; 1 &amp; 1 \\<br />r &amp; r &amp; 1<br />\end{array}\right),<br />\left(\begin{array}{rrrr}<br />1 &amp; 1 &amp; 1 &amp; 1 \\<br />r &amp; 1 &amp; 1 &amp; 1 \\<br />r &amp; r &amp; 1 &amp; 1 \\<br />r &amp; r &amp; r &amp; 1<br />\end{array}\right),<br />\left(\begin{array}{rrrrr}<br />1 &amp; 1 &amp; 1 &amp; 1 &amp; 1 \\<br />r &amp; 1 &amp; 1 &amp; 1 &amp; 1 \\<br />r &amp; r &amp; 1 &amp; 1 &amp; 1 \\<br />r &amp; r &amp; r &amp; 1 &amp; 1 \\<br />r &amp; r &amp; r &amp; r &amp; 1<br />\end{array}\right)$$<br /></pre>
<p>Make a conjecture about the determinant of an arbitrary matrix in this sequence. Can you prove it your conjecture?</p>
<p style="text-align: right;"><span style="font-size: small;">[Adapted from: K. R. Matthews, <a href="http://www.numbertheory.org/book/"><em>Elementary Linear Algebra</em></a>, Chapter 4, Problem 19] </span></p>

{{{id=181|

///
}}}

{{{id=180|

///
}}}

{{{id=194|

///
}}}

{{{id=128|

///
}}}

<p><strong>Exercise: </strong>What is the largest determinant possible for a $3\times3$ matrix whose entries are $1, 2, \dots, 9$ (each occurring exactly once, in any order). How many matrices $M$ achieve this maximum?</p>
<p>(<em>Hint:</em> You might find the command <strong>Permutations</strong> useful. The following code will construct all the lists that have the entries $1, 2, 3, 4$, each appearing exactly once.)</p>
<pre>for P in Permutations(4):<br />    L = list(P)<br />    print L<br /></pre>

{{{id=191|
for P in Permutations(4):
    L = list(P)
    print L
///
[1, 2, 3, 4]
[1, 2, 4, 3]
[1, 3, 2, 4]
[1, 3, 4, 2]
[1, 4, 2, 3]
[1, 4, 3, 2]
[2, 1, 3, 4]
[2, 1, 4, 3]
[2, 3, 1, 4]
[2, 3, 4, 1]
[2, 4, 1, 3]
[2, 4, 3, 1]
[3, 1, 2, 4]
[3, 1, 4, 2]
[3, 2, 1, 4]
[3, 2, 4, 1]
[3, 4, 1, 2]
[3, 4, 2, 1]
[4, 1, 2, 3]
[4, 1, 3, 2]
[4, 2, 1, 3]
[4, 2, 3, 1]
[4, 3, 1, 2]
[4, 3, 2, 1]
}}}

{{{id=190|

///
}}}

{{{id=189|

///
}}}