ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 30 Apr 2012 11:50:06 +0200I want to plot power series with symbolic functionshttps://ask.sagemath.org/question/8922/i-want-to-plot-power-series-with-symbolic-functions/See [this worksheet on sagenb.org](http://www.sagenb.org/home/pub/4482/).
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.Tue, 24 Apr 2012 16:16:55 +0200https://ask.sagemath.org/question/8922/i-want-to-plot-power-series-with-symbolic-functions/Comment by Shashank for <p>See <a href="http://www.sagenb.org/home/pub/4482/">this worksheet on sagenb.org</a>.</p>
<pre><code>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)
</code></pre>
<p>Notice that they have to define a <em>Python</em> function in order for this to work. I could not get this to work with a Sage callable function like <code>P(n,x) = sum([...])</code> for the life of me. I tried lambdas, everything.</p>
<p>Now, likely either</p>
<ul>
<li>I've already answered this question somewhere else on the Internet, or </li>
<li>It's not possible.</li>
</ul>
<p>But I'd like confirmation of this. It's really annoying that one has to use a Python function to do this.</p>
https://ask.sagemath.org/question/8922/i-want-to-plot-power-series-with-symbolic-functions/?comment=19872#post-id-19872Yes sage callable is giving an error, but a lambda function works well too. Tue, 24 Apr 2012 19:08:02 +0200https://ask.sagemath.org/question/8922/i-want-to-plot-power-series-with-symbolic-functions/?comment=19872#post-id-19872Comment by kcrisman for <p>See <a href="http://www.sagenb.org/home/pub/4482/">this worksheet on sagenb.org</a>.</p>
<pre><code>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)
</code></pre>
<p>Notice that they have to define a <em>Python</em> function in order for this to work. I could not get this to work with a Sage callable function like <code>P(n,x) = sum([...])</code> for the life of me. I tried lambdas, everything.</p>
<p>Now, likely either</p>
<ul>
<li>I've already answered this question somewhere else on the Internet, or </li>
<li>It's not possible.</li>
</ul>
<p>But I'd like confirmation of this. It's really annoying that one has to use a Python function to do this.</p>
https://ask.sagemath.org/question/8922/i-want-to-plot-power-series-with-symbolic-functions/?comment=19871#post-id-19871Hmm, 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.)Tue, 24 Apr 2012 21:57:14 +0200https://ask.sagemath.org/question/8922/i-want-to-plot-power-series-with-symbolic-functions/?comment=19871#post-id-19871Answer by Shashank for <p>See <a href="http://www.sagenb.org/home/pub/4482/">this worksheet on sagenb.org</a>.</p>
<pre><code>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)
</code></pre>
<p>Notice that they have to define a <em>Python</em> function in order for this to work. I could not get this to work with a Sage callable function like <code>P(n,x) = sum([...])</code> for the life of me. I tried lambdas, everything.</p>
<p>Now, likely either</p>
<ul>
<li>I've already answered this question somewhere else on the Internet, or </li>
<li>It's not possible.</li>
</ul>
<p>But I'd like confirmation of this. It's really annoying that one has to use a Python function to do this.</p>
https://ask.sagemath.org/question/8922/i-want-to-plot-power-series-with-symbolic-functions/?answer=13495#post-id-13495This 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)Tue, 24 Apr 2012 22:11:57 +0200https://ask.sagemath.org/question/8922/i-want-to-plot-power-series-with-symbolic-functions/?answer=13495#post-id-13495Answer by ndomes for <p>See <a href="http://www.sagenb.org/home/pub/4482/">this worksheet on sagenb.org</a>.</p>
<pre><code>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)
</code></pre>
<p>Notice that they have to define a <em>Python</em> function in order for this to work. I could not get this to work with a Sage callable function like <code>P(n,x) = sum([...])</code> for the life of me. I tried lambdas, everything.</p>
<p>Now, likely either</p>
<ul>
<li>I've already answered this question somewhere else on the Internet, or </li>
<li>It's not possible.</li>
</ul>
<p>But I'd like confirmation of this. It's really annoying that one has to use a Python function to do this.</p>
https://ask.sagemath.org/question/8922/i-want-to-plot-power-series-with-symbolic-functions/?answer=13511#post-id-13511What'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)Mon, 30 Apr 2012 08:24:52 +0200https://ask.sagemath.org/question/8922/i-want-to-plot-power-series-with-symbolic-functions/?answer=13511#post-id-13511Comment by kcrisman for <p>What's about assigning a python function to a sage callable function?</p>
<pre><code>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)
</code></pre>
https://ask.sagemath.org/question/8922/i-want-to-plot-power-series-with-symbolic-functions/?comment=19851#post-id-19851Hmm, 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.Mon, 30 Apr 2012 11:50:06 +0200https://ask.sagemath.org/question/8922/i-want-to-plot-power-series-with-symbolic-functions/?comment=19851#post-id-19851Answer by Jason Grout for <p>See <a href="http://www.sagenb.org/home/pub/4482/">this worksheet on sagenb.org</a>.</p>
<pre><code>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)
</code></pre>
<p>Notice that they have to define a <em>Python</em> function in order for this to work. I could not get this to work with a Sage callable function like <code>P(n,x) = sum([...])</code> for the life of me. I tried lambdas, everything.</p>
<p>Now, likely either</p>
<ul>
<li>I've already answered this question somewhere else on the Internet, or </li>
<li>It's not possible.</li>
</ul>
<p>But I'd like confirmation of this. It's really annoying that one has to use a Python function to do this.</p>
https://ask.sagemath.org/question/8922/i-want-to-plot-power-series-with-symbolic-functions/?answer=13496#post-id-13496The 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
Tue, 24 Apr 2012 23:48:13 +0200https://ask.sagemath.org/question/8922/i-want-to-plot-power-series-with-symbolic-functions/?answer=13496#post-id-13496Comment by kcrisman for <p>The best I could come up with was</p>
<pre><code>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)
</code></pre>
<p>However, I was still getting errors. It's odd that this doesn't work:</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/8922/i-want-to-plot-power-series-with-symbolic-functions/?comment=19867#post-id-19867I 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.Wed, 25 Apr 2012 11:50:09 +0200https://ask.sagemath.org/question/8922/i-want-to-plot-power-series-with-symbolic-functions/?comment=19867#post-id-19867