Sage Days 9 -- Graphics Exercise Set 1
system:sage

<font color='black'><h1>Graphics Exercise Set 1</h1></font>

<br><br><br><hr>
<font color='darkred'><b>Exercise:</b> PLOTS</font>
<font color='#333'>
Draw plots of the following functions, where the $x$ ranges include
some interesting behavior of each function:
$$
  \sin(x), \exp(x), \sin(x\cos(x)), \sin(1/x), e^{e^x}, \tanh(x), \text{asin}(x)/\text{acos}(x), \lfloor x \rfloor
$$
</font>

{{{id=15|
x = var('x')
///
}}}

{{{id=14|
plot(sin(1/x), (-pi,pi), rgbcolor='orange', thickness=2, plot_points=10000)
///
}}}

{{{id=16|

///
}}}

{{{id=0|

///
}}}

{{{id=2|

///
}}}

{{{id=3|

///
}}}

{{{id=4|

///
}}}

<hr>


<br><br><br><hr>
<font color='darkred'><b>Exercise:</b> OPACITY</font>
<font color='#333'>

<ol>
<li> Draw a 100 random points of random sizes and random opacity. 

</ol> 

</font>

{{{id=18|
@interact
def f(num=(10..100)):
    set_random_seed(0)
    S = sum(point((random(), random()), alpha=random(), pointsize=2000) for _ in range(num))
    show(S,xmin=0,ymin=0)
///

<html><div padding=6 id='div-interact-18'> <table width=800px height=400px bgcolor='#c5c5c5'
                 cellpadding=15><tr><td bgcolor='#f9f9f9' valign=top align=left><table><tr><td align=right><font color="black">num&nbsp;</font></td><td><div id='slider-num-18' class='ui-slider-1' style='padding:0px;margin:0px;'><span class='ui-slider-handle'></span></div><script>setTimeout(function() { $('#slider-num-18').slider({
    stepping: 1, minValue: 0, maxValue: 90, startValue: 0,
    change: function () { var position = Math.ceil($('#slider-num-18').slider('value')); interact(18, "sage.server.notebook.interact.update(18, \"num\", 6, sage.server.notebook.interact.standard_b64decode(\""+encode64(position)+"\"), globals())"); }
});}, 1);</script></td></tr>
</table><div id='cell-interact-18'><?__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>
}}}

{{{id=19|

///
}}}

{{{id=24|

///
}}}

{{{id=25|

///
}}}

{{{id=26|

///
}}}

{{{id=27|

///
}}}

{{{id=28|

///
}}}

<br><table border=1 cellpadding=5 bgcolor='lightyellow'><tr><td>
<b>NOTE:</b> To insert new cells, click on the blue line that appears just above a cell, or
press alt-enter to evaluate a cell and create a new cell after it. 
</td></tr></table><br>

<hr>


<br><br><br><hr>
<font color='darkred'><b>Exercise:</b> DIGITS</font>
<font color='#333'>
Let $z$ be a number with decimal digits $d_i$, so that 
$$
    z = \sum d_i 10^{d_i}.
$$
For example, if $z = 15.182$, then $d_1 = 1$, $d_0 = 5$, $d_{-1}=1$, $d_{-2}=8$, and $d_{-3}=2$. 
<ol>
<li> Draw the <b><i>frequency histogram</i></b> of the sequence of the first 10000 
decimal digits of the following numbers: $1/7$, $\sqrt{2}$, $e$, $\pi$.  Make a conjecture. 
<li> Draw the <b><i>frequency histogram</i></b> of the sequence of the first 10000 
successive pairs of decimal digits of the following numbers: $1/7$, $\sqrt{2}$, $e$, $\pi$.
I.e., you have a sequence of $100$ possible numbers $d_i d_{i+1}$.  Make a conjecture.
<li> Define a <b><i>random walk</i></b> as follows. For any real number $z$, 
let $$f_z(X) = \sum_{n <= X} d_{n} - 9/2.$$
The random walk is then just the plot of the function $f_z(X)$ as a function of $X$.
Plot the random walks $f_z(X)$ for $z = 1/7, \sqrt{2}, e, \pi$.  
</ol>

</font>


<br><table border=1 cellpadding=5 bgcolor='lightyellow'><tr><td>
<b>NOTE:</b> For these sorts of interactive exploratory questions, the <tt>interact</tt> command
in Sage is incredibly useful.  All you do is put <tt>@interact</tt> on the line before the
definition of a function.  Then all the arguments to the function get turned into interactive
controls.  Type <tt>interact?</tt> in a blank input cell for extensive documentation about 
interact and the controls that are available. 
</td></tr></table><br>

{{{id=17|
@interact
def f(digits=(10,10^5)):
    digits = int(digits)
    print digits
    s = N(pi, digits=digits)
    d = list(str(s))
    d = [int(a) for a in str(s) if a != '.']
    T = finance.TimeSeries(d)   # just happens to be the best for this sort of thing!
    T.plot_histogram(10).show(frame=True)
///

<html><div padding=6 id='div-interact-17'> <table width=800px height=400px bgcolor='#c5c5c5'
                 cellpadding=15><tr><td bgcolor='#f9f9f9' valign=top align=left><table><tr><td align=right><font color="black">digits&nbsp;</font></td><td><div id='slider-digits-17' class='ui-slider-1' style='padding:0px;margin:0px;'><span class='ui-slider-handle'></span></div><script>setTimeout(function() { $('#slider-digits-17').slider({
    stepping: 1, minValue: 0, maxValue: 99, startValue: 0,
    change: function () { var position = Math.ceil($('#slider-digits-17').slider('value')); interact(17, "sage.server.notebook.interact.update(17, \"digits\", 3, sage.server.notebook.interact.standard_b64decode(\""+encode64(position)+"\"), globals())"); }
});}, 1);</script></td></tr>
</table><div id='cell-interact-17'><?__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>
CPU time: 0.00 s,  Wall time: 0.02 s
}}}

{{{id=20|
@interact
def f(digits=(100,10^5)):
    digits = int(digits)
    print digits
    s = N(e^2, digits=digits)
    d = list(str(s))
    d = [int(a) for a in str(s) if a != '.']
    T = finance.TimeSeries(d)
    T = T.add_scalar(-4.5)  # adds -4.5 to every entry
    T = T.sums()  # sequence of partial sums
    print T.hurst_exponent()
    T.plot().show(frame=True, figsize=[8,3])
///

<html><div padding=6 id='div-interact-20'> <table width=800px height=400px bgcolor='#c5c5c5'
                 cellpadding=15><tr><td bgcolor='#f9f9f9' valign=top align=left><table><tr><td align=right><font color="black">digits&nbsp;</font></td><td><div id='slider-digits-20' class='ui-slider-1' style='padding:0px;margin:0px;'><span class='ui-slider-handle'></span></div><script>setTimeout(function() { $('#slider-digits-20').slider({
    stepping: 1, minValue: 0, maxValue: 99, startValue: 0,
    change: function () { var position = Math.ceil($('#slider-digits-20').slider('value')); interact(20, "sage.server.notebook.interact.update(20, \"digits\", 4, sage.server.notebook.interact.standard_b64decode(\""+encode64(position)+"\"), globals())"); }
});}, 1);</script></td></tr>
</table><div id='cell-interact-20'><?__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>
CPU time: 0.00 s,  Wall time: 0.02 s
}}}

{{{id=21|

///
}}}

{{{id=22|

///
}}}

{{{id=23|

///
}}}

<hr>

{{{id=6|

///
}}}

{{{id=7|

///
}}}

{{{id=8|

///
}}}

{{{id=9|

///
}}}

{{{id=10|

///
}}}

{{{id=11|

///
}}}

{{{id=12|

///
}}}

{{{id=13|

///
}}}