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.Sun, 10 Apr 2011 02:01:53 -0500creating a fourier series animationhttp://ask.sagemath.org/question/8062/creating-a-fourier-series-animation/Hello,
I want to create an animation of sum((-1)^(n-1)*sin(n*x)/n,n,1 , k)
and show that as the number of terms 'k' in the partial sums increase, the sum converges to x/2 i.e looks more and more like the graph of x/2.
My first try is
**a = animate([(sum((-1)^(n-1)*sin(n*x)/n,n,1 , k))] for k in range (1,50,1))**
But that returns me an error
*Traceback (click to the left of this block for traceback)
...
AttributeError: 'int' object has no attribute '_maxima_'*
Also I want to include a static graph of x/2 in the background of the animation so that it is easy to observe that the series converges to x/2. how to do this?Fri, 08 Apr 2011 22:04:43 -0500http://ask.sagemath.org/question/8062/creating-a-fourier-series-animation/Answer by DSM for <p>Hello,
I want to create an animation of sum((-1)^(n-1)<em>sin(n</em>x)/n,n,1 , k)</p>
<p>and show that as the number of terms 'k' in the partial sums increase, the sum converges to x/2 i.e looks more and more like the graph of x/2.</p>
<p>My first try is </p>
<p><strong>a = animate([(sum((-1)^(n-1)<em>sin(n</em>x)/n,n,1 , k))] for k in range (1,50,1))</strong></p>
<p>But that returns me an error </p>
<p><em>Traceback (click to the left of this block for traceback)
...
AttributeError: 'int' object has no attribute '_maxima_'</em></p>
<p>Also I want to include a static graph of x/2 in the background of the animation so that it is easy to observe that the series converges to x/2. how to do this?</p>
http://ask.sagemath.org/question/8062/creating-a-fourier-series-animation/?answer=12274#post-id-12274As for the AttributeError, that's happening because the range function returns Python ints and the function expects Sage Integers. They're slightly different types, with different behaviour; usually in Sage you want Integers, so I seldom use range myself when writing Sage code. You can replace range with any of srange, xsrange, sxrange, IntegerRange, or write (1..49) instead to fix it, anything which returns an Integer.
[It should probably be considered a bug that this doesn't happen automatically here.]
To put a line in the background, though, I think it's easiest to forget that the animate command accepts functions and treat it merely as a tool to join plots together. That is, first you want to make the plots that you want, and *then* get animate to combine them. You can use the animate coercion as a shortcut sometimes, but when you try to start adding extra features it's much easier to work with plots as normal.
For example, something like:
n = var("n")
frames = []
xr = (x, 0, 1)
for k in srange(1, 50):
g = plot((sum((-1)^(n-1)*sin(n*x)/n,n,1,k)), xr, color="blue", legend_label='k = %d' % k)
g += plot(x/2, xr, color="green", legend_label="x/2")
frames.append(g)
a = animate(frames, ymin=0.0, ymax=1.0, legend_loc=(0.2,0.8))
a.show()
You could do the above in two lines if you really wanted to, but I think the above is clearer and more easily extensible.
Sat, 09 Apr 2011 04:49:19 -0500http://ask.sagemath.org/question/8062/creating-a-fourier-series-animation/?answer=12274#post-id-12274Comment by ebs for <p>As for the AttributeError, that's happening because the range function returns Python ints and the function expects Sage Integers. They're slightly different types, with different behaviour; usually in Sage you want Integers, so I seldom use range myself when writing Sage code. You can replace range with any of srange, xsrange, sxrange, IntegerRange, or write (1..49) instead to fix it, anything which returns an Integer.</p>
<p>[It should probably be considered a bug that this doesn't happen automatically here.]</p>
<p>To put a line in the background, though, I think it's easiest to forget that the animate command accepts functions and treat it merely as a tool to join plots together. That is, first you want to make the plots that you want, and <em>then</em> get animate to combine them. You can use the animate coercion as a shortcut sometimes, but when you try to start adding extra features it's much easier to work with plots as normal.</p>
<p>For example, something like:</p>
<pre><code>n = var("n")
frames = []
xr = (x, 0, 1)
for k in srange(1, 50):
g = plot((sum((-1)^(n-1)*sin(n*x)/n,n,1,k)), xr, color="blue", legend_label='k = %d' % k)
g += plot(x/2, xr, color="green", legend_label="x/2")
frames.append(g)
a = animate(frames, ymin=0.0, ymax=1.0, legend_loc=(0.2,0.8))
a.show()
</code></pre>
<p>You could do the above in two lines if you really wanted to, but I think the above is clearer and more easily extensible.</p>
http://ask.sagemath.org/question/8062/creating-a-fourier-series-animation/?comment=21877#post-id-21877 it works so well! thanks:) Sun, 10 Apr 2011 02:01:53 -0500http://ask.sagemath.org/question/8062/creating-a-fourier-series-animation/?comment=21877#post-id-21877Answer by ebs for <p>Hello,
I want to create an animation of sum((-1)^(n-1)<em>sin(n</em>x)/n,n,1 , k)</p>
<p>and show that as the number of terms 'k' in the partial sums increase, the sum converges to x/2 i.e looks more and more like the graph of x/2.</p>
<p>My first try is </p>
<p><strong>a = animate([(sum((-1)^(n-1)<em>sin(n</em>x)/n,n,1 , k))] for k in range (1,50,1))</strong></p>
<p>But that returns me an error </p>
<p><em>Traceback (click to the left of this block for traceback)
...
AttributeError: 'int' object has no attribute '_maxima_'</em></p>
<p>Also I want to include a static graph of x/2 in the background of the animation so that it is easy to observe that the series converges to x/2. how to do this?</p>
http://ask.sagemath.org/question/8062/creating-a-fourier-series-animation/?answer=12275#post-id-12275when I type your code into sage,
I get this error. how do I fix this?
**
Traceback (click to the left of this block for traceback)
...
TypeError: unsupported operand parent(s) for '-': '<type
'function'>' and 'Integer Ring'
**
how do I fix this?
Sat, 09 Apr 2011 06:08:20 -0500http://ask.sagemath.org/question/8062/creating-a-fourier-series-animation/?answer=12275#post-id-12275Comment by DSM for <p>when I type your code into sage,
I get this error. how do I fix this?</p>
<hr/>
<p>Traceback (click to the left of this block for traceback)
...
TypeError: unsupported operand parent(s) for '-': '<type 'function'="">' and 'Integer Ring'</p>
<hr/>
<p>how do I fix this?</p>
http://ask.sagemath.org/question/8062/creating-a-fourier-series-animation/?comment=21885#post-id-21885Ah, sorry! I forgot that I'd redefined "n" as a variable; I've corrected the original. By the way, if you're replying to an answer, usually we enter it using the "post a comment" button.Sat, 09 Apr 2011 06:22:48 -0500http://ask.sagemath.org/question/8062/creating-a-fourier-series-animation/?comment=21885#post-id-21885