Ask Your Question

Detecting series divergence automatically

asked 2011-11-16 15:27:41 +0200

calc314 gravatar image

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.


edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2011-11-16 16:16:21 +0200

kcrisman gravatar image

updated 2011-11-16 22:15:29 +0200

Can you use a try/except block?

def check_convergence(exp):
        print sum(exp,x,1,oo)
    except ValueError, msg:
        if 'divergent' in str(msg):
            print "Diverges!"
            raise ValueError, msg


sage: check_convergence(1/x^2)
sage: check_convergence(1/x)
#0: simplify_sum(expr='sum(1/x,x,1,inf))

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)

because Maxima doesn't use the word diverge there, but evaluates it to infinity. Strange...

edit flag offensive delete link more



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

DSM gravatar imageDSM ( 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.

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

Thanks! This works great!

calc314 gravatar imagecalc314 ( 2011-11-18 21:28:55 +0200 )edit

@calc314: remember, be sure to accept answers you feel are right; we want 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!

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2011-11-16 15:27:41 +0200

Seen: 1,649 times

Last updated: Nov 16 '11