ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 19 Nov 2011 07:34:33 +0100Detecting series divergence automaticallyhttps://ask.sagemath.org/question/8481/detecting-series-divergence-automatically/I'm writing an interact to let my students see graphs of sequences and of sequences of partial sums. But, I also want the interact to print the sum of the infinite series, if it converges.
Is there a way I can detect whether the sum converges within the interact? I need some automation here. I don't see that the "sum" command returns any sort of error value that flags whether the series converges or not. It just produces lots of errors.
Thanks!
Wed, 16 Nov 2011 15:27:41 +0100https://ask.sagemath.org/question/8481/detecting-series-divergence-automatically/Answer by kcrisman for <p>I'm writing an interact to let my students see graphs of sequences and of sequences of partial sums. But, I also want the interact to print the sum of the infinite series, if it converges.</p>
<p>Is there a way I can detect whether the sum converges within the interact? I need some automation here. I don't see that the "sum" command returns any sort of error value that flags whether the series converges or not. It just produces lots of errors.</p>
<p>Thanks!</p>
https://ask.sagemath.org/question/8481/detecting-series-divergence-automatically/?answer=12907#post-id-12907Can you use a try/except block?
def check_convergence(exp):
try:
print sum(exp,x,1,oo)
except ValueError, msg:
if 'divergent' in str(msg):
print "Diverges!"
else:
raise ValueError, msg
Then
sage: check_convergence(1/x^2)
1/6*pi^2
sage: check_convergence(1/x)
#0: simplify_sum(expr='sum(1/x,x,1,inf))
Oops!
I can't seem to get rid of that extra `#0` thingie. Maxima sometimes prints out extra stuff now with our ECL library interface.
Notice also
sage: check_convergence(1/22)
+Infinity
because Maxima doesn't use the word diverge there, but evaluates it to infinity. Strange...Wed, 16 Nov 2011 16:16:21 +0100https://ask.sagemath.org/question/8481/detecting-series-divergence-automatically/?answer=12907#post-id-12907Comment by DSM for <p>Can you use a try/except block?</p>
<pre><code>def check_convergence(exp):
try:
print sum(exp,x,1,oo)
except ValueError, msg:
if 'divergent' in str(msg):
print "Diverges!"
else:
raise ValueError, msg
</code></pre>
<p>Then</p>
<pre><code>sage: check_convergence(1/x^2)
1/6*pi^2
sage: check_convergence(1/x)
#0: simplify_sum(expr='sum(1/x,x,1,inf))
Oops!
</code></pre>
<p>I can't seem to get rid of that extra <code>#0</code> thingie. Maxima sometimes prints out extra stuff now with our ECL library interface.</p>
<p>Notice also </p>
<pre><code>sage: check_convergence(1/22)
+Infinity
</code></pre>
<p>because Maxima doesn't use the word diverge there, but evaluates it to infinity. Strange...</p>
https://ask.sagemath.org/question/8481/detecting-series-divergence-automatically/?comment=20888#post-id-20888Probably a better idea to catch whatever it is that sum throws (a ValueError?) than a bare except. And we really need to make that noise go away somehow.. :-(Wed, 16 Nov 2011 18:09:33 +0100https://ask.sagemath.org/question/8481/detecting-series-divergence-automatically/?comment=20888#post-id-20888Comment by calc314 for <p>Can you use a try/except block?</p>
<pre><code>def check_convergence(exp):
try:
print sum(exp,x,1,oo)
except ValueError, msg:
if 'divergent' in str(msg):
print "Diverges!"
else:
raise ValueError, msg
</code></pre>
<p>Then</p>
<pre><code>sage: check_convergence(1/x^2)
1/6*pi^2
sage: check_convergence(1/x)
#0: simplify_sum(expr='sum(1/x,x,1,inf))
Oops!
</code></pre>
<p>I can't seem to get rid of that extra <code>#0</code> thingie. Maxima sometimes prints out extra stuff now with our ECL library interface.</p>
<p>Notice also </p>
<pre><code>sage: check_convergence(1/22)
+Infinity
</code></pre>
<p>because Maxima doesn't use the word diverge there, but evaluates it to infinity. Strange...</p>
https://ask.sagemath.org/question/8481/detecting-series-divergence-automatically/?comment=20866#post-id-20866Thanks! This works great!Fri, 18 Nov 2011 21:28:55 +0100https://ask.sagemath.org/question/8481/detecting-series-divergence-automatically/?comment=20866#post-id-20866Comment by kcrisman for <p>Can you use a try/except block?</p>
<pre><code>def check_convergence(exp):
try:
print sum(exp,x,1,oo)
except ValueError, msg:
if 'divergent' in str(msg):
print "Diverges!"
else:
raise ValueError, msg
</code></pre>
<p>Then</p>
<pre><code>sage: check_convergence(1/x^2)
1/6*pi^2
sage: check_convergence(1/x)
#0: simplify_sum(expr='sum(1/x,x,1,inf))
Oops!
</code></pre>
<p>I can't seem to get rid of that extra <code>#0</code> thingie. Maxima sometimes prints out extra stuff now with our ECL library interface.</p>
<p>Notice also </p>
<pre><code>sage: check_convergence(1/22)
+Infinity
</code></pre>
<p>because Maxima doesn't use the word diverge there, but evaluates it to infinity. Strange...</p>
https://ask.sagemath.org/question/8481/detecting-series-divergence-automatically/?comment=20882#post-id-20882Of course! But I was lazy and wanted to get this off before my next class. Edit above is better.Wed, 16 Nov 2011 22:15:52 +0100https://ask.sagemath.org/question/8481/detecting-series-divergence-automatically/?comment=20882#post-id-20882Comment by kcrisman for <p>Can you use a try/except block?</p>
<pre><code>def check_convergence(exp):
try:
print sum(exp,x,1,oo)
except ValueError, msg:
if 'divergent' in str(msg):
print "Diverges!"
else:
raise ValueError, msg
</code></pre>
<p>Then</p>
<pre><code>sage: check_convergence(1/x^2)
1/6*pi^2
sage: check_convergence(1/x)
#0: simplify_sum(expr='sum(1/x,x,1,inf))
Oops!
</code></pre>
<p>I can't seem to get rid of that extra <code>#0</code> thingie. Maxima sometimes prints out extra stuff now with our ECL library interface.</p>
<p>Notice also </p>
<pre><code>sage: check_convergence(1/22)
+Infinity
</code></pre>
<p>because Maxima doesn't use the word diverge there, but evaluates it to infinity. Strange...</p>
https://ask.sagemath.org/question/8481/detecting-series-divergence-automatically/?comment=20863#post-id-20863@calc314: remember, be sure to accept answers you feel are right; we want ask.sagemath.org to be a searchable reference for answers, and this helps new users - as opposed to more experienced people like you :) - tell what's what. Good luck!Sat, 19 Nov 2011 07:34:33 +0100https://ask.sagemath.org/question/8481/detecting-series-divergence-automatically/?comment=20863#post-id-20863