ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 26 Aug 2015 14:04:52 -0500Using interacthttp://ask.sagemath.org/question/29364/using-interact/ I want to visualize these functions in the complex plane:
$$f_n(z)=\exp(z) - \sum_{k=0}^{n} \frac{z^k}{k!}$$
I tried this code, but it gives me an error.
z,k = var('z,k')
@interact
def _(n=(1..8)):
complex_plot(exp(z)- sum(z^k/factorial(k), k, 0, n), (-5, 5), (-5, 5))
I am new to Sage (I previously used Mathematica). I wrote the code based on this example: http://wiki.sagemath.org/interact/ . How can I fix that?
EDIT: I would also like to know why doesn't this work either
def myPlot(n):
complex_plot(exp(z)- sum(z^k/factorial(k), k, 0, n), (-5, 5), (-5, 5))
Then I type
myPlot(2)
in the notebook, but I get nothing. However, if I type:
complex_plot(exp(z)- sum(z^k/factorial(k), k, 0, 2), (-5, 5), (-5, 5))
I get the correct plot.
EDIT 2:
I tried
myPlot = lambda n: complex_plot(exp(z)- sum(z^k/factorial(k), k, 0, n), (-5, 5), (-5, 5))
And now I can evaluate it. However, I still can't use @Interact.
This doesn't make sense to me.Wed, 26 Aug 2015 12:32:31 -0500http://ask.sagemath.org/question/29364/using-interact/Answer by eric_g for <p>I want to visualize these functions in the complex plane:</p>
<p>$$f_n(z)=\exp(z) - \sum_{k=0}^{n} \frac{z^k}{k!}$$</p>
<p>I tried this code, but it gives me an error.</p>
<pre><code>z,k = var('z,k')
@interact
def _(n=(1..8)):
complex_plot(exp(z)- sum(z^k/factorial(k), k, 0, n), (-5, 5), (-5, 5))
</code></pre>
<p>I am new to Sage (I previously used Mathematica). I wrote the code based on this example: <a href="http://wiki.sagemath.org/interact/">http://wiki.sagemath.org/interact/</a> . How can I fix that?</p>
<p>EDIT: I would also like to know why doesn't this work either</p>
<pre><code>def myPlot(n):
complex_plot(exp(z)- sum(z^k/factorial(k), k, 0, n), (-5, 5), (-5, 5))
</code></pre>
<p>Then I type
myPlot(2)
in the notebook, but I get nothing. However, if I type:</p>
<pre><code>complex_plot(exp(z)- sum(z^k/factorial(k), k, 0, 2), (-5, 5), (-5, 5))
</code></pre>
<p>I get the correct plot.</p>
<p>EDIT 2:</p>
<p>I tried</p>
<pre><code>myPlot = lambda n: complex_plot(exp(z)- sum(z^k/factorial(k), k, 0, n), (-5, 5), (-5, 5))
</code></pre>
<p>And now I can evaluate it. However, I still can't use @Interact.</p>
<p>This doesn't make sense to me.</p>
http://ask.sagemath.org/question/29364/using-interact/?answer=29365#post-id-29365Hi,
Basically, you have to use the function `show()` on the graphic object resulting from `complex_plot()`. Note also the alignment of `@interact` and `def _(n=(1..8))` (alignment is crucial in Python). So the working code is
z,k = var('z,k')
@interact
def _(n=(1..8)):
g = complex_plot(exp(z)- sum(z^k/factorial(k), k, 0, n), (-5, 5), (-5, 5))
show(g)Wed, 26 Aug 2015 14:04:52 -0500http://ask.sagemath.org/question/29364/using-interact/?answer=29365#post-id-29365