# I want to plot power series with symbolic functions

 1 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. asked Apr 24 '12 kcrisman 6784 ● 14 ● 67 ● 152 Yes sage callable is giving an error, but a lambda function works well too. Shashank (Apr 24 '12)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 (Apr 24 '12)

 1 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) posted Apr 24 '12 Shashank 1580 ● 7 ● 22 ● 56
 1 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) posted Apr 30 '12 ndomes 546 ● 7 ● 16 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 (Apr 30 '12)
 0 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/ in () ----> 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 posted Apr 24 '12 Jason Grout 3225 ● 7 ● 27 ● 72 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 (Apr 25 '12)

[hide preview]