Ask Your Question
1

I want to plot power series with symbolic functions

asked 2012-04-24 16:16:55 +0100

kcrisman gravatar image

updated 2012-04-24 21:57:51 +0100

See this worksheet on sagenb.org.

var('x')
f = sin
def P(n,x):
    return sum([(-1)^k*x^(2*k+1)/factorial(2*k+1) for k in range(n)]) 

sinplot = plot(f(x),(x,-2*pi,2*pi),color='red')

@interact
def _(n=(1..10)):
    seriesplot = plot(P(n,x),(x,-2*pi,2*pi),color='blue')
    html('$P(%s,x) = %s$'%(latex(n),latex(P(n,x))))
    show(sinplot+seriesplot,ymin=-4,ymax=4)

Notice that they have to define a Python function in order for this to work. I could not get this to work with a Sage callable function like P(n,x) = sum([...]) for the life of me. I tried lambdas, everything.

Now, likely either

  • I've already answered this question somewhere else on the Internet, or
  • It's not possible.

But I'd like confirmation of this. It's really annoying that one has to use a Python function to do this.

edit retag flag offensive close merge delete

Comments

Yes sage callable is giving an error, but a lambda function works well too.

Shashank gravatar imageShashank ( 2012-04-24 19:08:02 +0100 )edit

Hmm, I couldn't get a lambda to work, at least not in conjunction with a callable. Can you post that as an answer? (Though I won't accept it, since I want to know how to jerry-rig the callable, I'd upvote it.)

kcrisman gravatar imagekcrisman ( 2012-04-24 21:57:14 +0100 )edit

3 Answers

Sort by ยป oldest newest most voted
1

answered 2012-04-24 22:11:57 +0100

Shashank gravatar image

This code with a lambda function works. As you mentioned in the comment, it is still not what you want.

var('x')
n=var('n')
assume(n,'integer')
f = sin
P = lambda n,x:sum([(-1)^k*x^(2*k+1)/factorial(2*k+1) for k in range(n)]) 

sinplot = plot(f(x),(x,-2*pi,2*pi),color='red')

@interact
def _(n=(1..10)):
    seriesplot = plot(P(n,x),(x,-2*pi,2*pi),color='blue')
    html('$P(%s,x) = %s$'%(latex(n),latex(P(n,x))))
    show(sinplot+seriesplot,ymin=-4,ymax=4)
edit flag offensive delete link more
1

answered 2012-04-30 08:24:52 +0100

ndomes gravatar image

What's about assigning a python function to a sage callable function?

var('x')
f = sin
def P(n,x):
    return sum([(-1)^k*x^(2*k+1)/factorial(2*k+1) for k in range(n)]) 

print 'type(P)',type(P)

sinplot = plot(f(x),(x,-2*pi,2*pi),color='red')

@interact
def _(n=(1..10)):
    p(x) = P(n,x)
    print 'type(p)',type(p)
    print  'p.parent()', p.parent()
    seriesplot = plot(p,(-2*pi,2*pi),color='blue')
    html('$P(%s,x) = %s$'%(latex(n),latex(p(x))))
    show(sinplot+seriesplot,ymin=-4,ymax=4)
edit flag offensive delete link more

Comments

Hmm, good point. Of course, then I might as well do what the original worksheet I found did anyway... but still worth having as an option.

kcrisman gravatar imagekcrisman ( 2012-04-30 11:50:06 +0100 )edit
0

answered 2012-04-24 23:48:13 +0100

Jason Grout gravatar image

The best I could come up with was

var('x')
f = sin
# note the different, symbolic convention for calling sum,
# *not* the standard python give-it-a-list way.
P(n,x)=sum((-1)^k*x^(2*k+1)/factorial(2*k+1), k, 0, n-1)

sinplot = plot(f(x),(x,-2*pi,2*pi),color='red')

@interact
def _(n=(1..10)):
    seriesplot = plot(P(n,x),(x,-2*pi,2*pi),color='blue')
    html('$P(%s,x) = %s$'%(latex(n),latex(P(n,x))))
    show(sinplot+seriesplot,ymin=-4,ymax=4)

However, I was still getting errors. It's odd that this doesn't work:

sage: var('i,x,k,n')
(i, x, k, n)
sage: a=(-1)^k*x^(2*k+1)/factorial(2*k+1)
sage: b=a.sum(k,0,n-1)
sage: b.subs(n=3,x=5)
sum((-1)^k*5^(2*k + 1)/factorial(2*k + 1), k, 0, 2)
sage: (b.subs(n=3,x=5)).n()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/Users/grout/<ipython-input-14-3dd82cd01b78> in <module>()
----> 1 (b.subs(n=3,x=5)).n(
      2 )

/Users/grout/sage-trees/sage-5.0.beta12/local/lib/python2.7/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression._numerical_approx (sage/symbolic/expression.cpp:18440)()

TypeError: cannot evaluate symbolic expression numerically
edit flag offensive delete link more

Comments

I was hoping to avoid using the symbolic sum like this, because it will likely invoke Maxima, but I guess this is more or less what I was looking for. But you are right that I can't get it to work.

kcrisman gravatar imagekcrisman ( 2012-04-25 11:50:09 +0100 )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: 2012-04-24 16:16:55 +0100

Seen: 1,112 times

Last updated: Apr 30 '12