# 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!

edit retag close merge delete

Sort by ยป oldest newest most voted

Can 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...

more

1

Probably 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.. :-(

( 2011-11-16 18:09:33 +0200 )edit

Of course! But I was lazy and wanted to get this off before my next class. Edit above is better.

( 2011-11-16 22:15:52 +0200 )edit

Thanks! This works great!

( 2011-11-18 21:28:55 +0200 )edit
1

@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!

( 2011-11-19 07:34:33 +0200 )edit