First time here? Check out the FAQ!

Ask Your Question
2

Detecting series divergence automatically

asked 13 years ago

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!

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
4

answered 13 years ago

kcrisman gravatar image

updated 13 years ago

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

Preview: (hide)
link

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 ( 13 years ago )

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

kcrisman gravatar imagekcrisman ( 13 years ago )

Thanks! This works great!

calc314 gravatar imagecalc314 ( 13 years ago )
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 ( 13 years ago )

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: 13 years ago

Seen: 1,777 times

Last updated: Nov 16 '11