Ask Your Question
2

Detecting series divergence automatically

asked 2011-11-16 08:27:41 -0600

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.

Thanks!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2011-11-16 09:16:21 -0600

kcrisman gravatar image

updated 2011-11-16 15:15:29 -0600

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

edit flag offensive delete link more

Comments

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

DSM gravatar imageDSM ( 2011-11-16 11:09:33 -0600 )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 15:15:52 -0600 )edit

Thanks! This works great!

calc314 gravatar imagecalc314 ( 2011-11-18 14:28:55 -0600 )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!

kcrisman gravatar imagekcrisman ( 2011-11-19 00:34:33 -0600 )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

Stats

Asked: 2011-11-16 08:27:41 -0600

Seen: 558 times

Last updated: Nov 16 '11