Sage Days 20.5: Exercises
system:sage


<p style="text-align: right;">3 May 2010</p>
<h1 style="text-align: center;">Introduction to Sage</h1>
<p style="text-align: right;">&nbsp;</p>
<h2><span style="text-decoration: underline;">Navigating the help system</span></h2>
<p>There are various ways of getting help in Sage.</p>
<h2>Reference Manual</h2>
<p>Click the link <span style="text-decoration: underline;"><span style="color: #0000ff;">Help</span></span> at the top-right of this  page. This will bring up documentation for the notebook interface, as  well as links to the reference manual.</p>

{{{id=175|

///
}}}

{{{id=1|

///
}}}

<h2>Tab completion</h2>
<p>Start typing something and press the TAB key. The interface tries to  complete it with a command name. If there is more than one completion,  then they are all presented to you.</p>

{{{id=174|

///
}}}

{{{id=55|

///
}}}

<p><span id="cell_outer_1">
<div id="cell_text_1">
<h2>? : documentation and examples</h2>
<p>To see documentation and examples for a command, type ? at then end of the command name and press the TAB key.</p>
</div>
</span></p>

{{{id=11|

///
}}}

{{{id=154|

///
}}}

<h2><span style="text-decoration: underline;">Exercises</span></h2>
<p style="text-align: left;"><strong>Exercise A:</strong> What is the largest prime factor of <span id="cell_outer_4"><span id="cell_outer_10">600851475143?</span></span></p>

{{{id=46|

///
}}}

{{{id=44|

///
}}}

<p><strong>Exercise B:</strong> Create the <strong>Permutation</strong> 51324 and assign it to the variable <strong>p</strong>.</p>
<ol>
<li>What is the <em>inverse</em> of <strong>p</strong> ?</li>
<li>Does <strong>p</strong> have the <em>pattern</em> 123 ? What about 1234 ? And 312 ? (even if you don't know what a pattern is, you should be able to find a command that does this).</li>
</ol>

{{{id=61|

///
}}}

{{{id=51|

///
}}}

<p><strong>Exercise C:</strong> Use the <strong>matrix</strong> command to create the following matrix.</p>
<p>$$M = \left(\begin{array}{rrrr}<br />10 &amp; 4 &amp; 1 &amp; 1 \\<br />4 &amp; 6 &amp; 5 &amp; 1 \\<br />1 &amp; 5 &amp; 6 &amp; 4 \\<br />1 &amp; 1 &amp; 4 &amp; 10<br />\end{array}\right)$$</p>
<ol>
<li>Find the <em>determinant</em> of the matrix.</li>
<li>Find the <em>echelon form</em> of the matrix.</li>
<li>Find the <em>eigenvalues</em> of the matrix.</li>
<li>Find the <em>kernel</em> of the matrix.</li>
<li>Find the <em>LLL decomposition</em> of the matrix.</li>
<li>Create the vector $v = (1,-1,-1,1)$.</li>
<li>Compute the products: $M*v$ and $v*M$.</li>
</ol>
<p style="text-align: left;"><span style="color: #3366ff;"><strong>NOTE: Vectors in Sage are row vectors. A command such as </strong><strong><span style="color: #000000;">eigenspaces</span> might not return what you expect, so it is best to specify </strong><strong><span style="color: #000000;">eigenspaces_left</span> or </strong><strong><span style="color: #000000;">eigenspaces_right</span> instead. Same thing for kernel (<span style="color: #000000;">left_kernel</span> or <span style="color: #000000;">right_kernel</span>), and so on.<br /></strong></span></p>

{{{id=49|

///
}}}

{{{id=65|

///
}}}

<h2><span style="text-decoration: underline;">Some Plotting</span></h2>

<p>The <strong>plot</strong> command allows you to draw plots of functions. Type <strong>plot(&lt;tab key&gt;</strong> for the documentation (or look in the reference manual). Here is a simple example.</p>

{{{id=155|
var('x')   # make sure x is a symbolic variable
plot(sin(x^2), (x,0,10))
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

<p>Here is a more complicated plot. Try to change every single input to the plot command in some way, evaluating to see what happens.</p>

{{{id=157|
P = plot(sin(x^2), (x,-2,2), rgbcolor=(0.8,0,0.2), thickness=3, linestyle='--', fill='axis')
show(P, gridlines=True)
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

<p>Above we used the <strong>show</strong> command to show a plot after it was created. Try putting the cursor right after <strong>P.show(</strong> and pressing <strong>tab</strong> to get a list of the options for how you can change the values of the given inputs.</p>

{{{id=156|

///
}}}

{{{id=70|

///
}}}

<p>Plotting multiple functions at once is as easy as adding them together.</p>

{{{id=168|
P1 = plot(sin(x), (x,0,2*pi))
P2 = plot(cos(x), (x,0,2*pi), rgbcolor='red')
P1 + P2
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

{{{id=169|

///
}}}

{{{id=161|

///
}}}

<h3>Symbolic Expressions</h3>
<p>Here is an example of a symbolic function.</p>

{{{id=160|
f(x) = x^4 - 8*x^2 - 3*x + 2
///
}}}

{{{id=172|
f(x)
///
x^4 - 8*x^2 - 3*x + 2
}}}

{{{id=158|
f(-3)
///
20
}}}

{{{id=173|

///
}}}

<h2><span style="text-decoration: underline;">Exercises</span></h2>
<p><strong>Exercise D:</strong></p>
<ol>
<li>Define the symbolic function $f(x) = x \sin(x^2)$.</li>
<li><strong>plot</strong> $f$ on the domain $[-3,3]$ and colour it red.</li>
<li>Use the <strong>find_root</strong> method to numerically approximate the root of $f$ on the interval $[1,2]$.</li>
<li>Compute the tangent line to $f$ at $x=1$.</li>
<li>Plot $f$ and the tangent line to $f$ at $x=1$ in one image.</li>
</ol>

{{{id=100|

///
}}}

{{{id=94|

///
}}}

<p><strong>Exercise E (Advanced):</strong></p>
<ol>
<li>Solve the following equation for $y$ $$y = 1 + x y^2$$ There are two solutions, take the one for which $\lim_{x\to0}y(x) = 1$. (<em>Don't forget to create the variables $x$ and $y$!</em>)</li>
<li>Expand $y$ as a truncated Taylor series around $0$ and containing $n=10$ terms.</li>
<li>Do you recognize the coefficients of the Taylor series expansion? You might want to use&nbsp; <a href="http://www.research.att.com/~njas/sequences/">Sloane's Online Encyclopedia of Integer Sequences</a>, or better yet, Sage's command <strong>sloane_find</strong> </li>
</ol>

{{{id=147|

///
}}}

{{{id=146|

///
}}}