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.Sun, 02 Dec 2018 23:29:01 +0100Variable Not Found while Plotting Finite Sumhttps://ask.sagemath.org/question/44530/variable-not-found-while-plotting-finite-sum/Today, while working on a discrete mathematics homework assignement, I came across this strange function:
$$ P(n) = \sum_{k=0}^{n}{\frac{(-1)^k (n-k)!}{k! \cdot n!}} $$.
Part of the question was to evaluate $\lim{n \rightarrow \infty}{P(n)}$ and as I had a hard time doing this by hand I gave it a shot with sagemath. I didn't expect it to evaluate the limit for me (which I tried and it didnt) but I was hoping to plot the graph in a straightforward manner. What I did is
```
var('k n')
```
,
```
p(n) = sum( (factorial(n-k)*(-1)^k)/factorial(k), k, 0, n)/factorial(n)
```
and finally
```
plot(p, (n, 1, 10))
```
which comes back with `ValueError: Variable 'k' not found`.
I found [this] (ask.sagemath.org/question/24293/plotting-value-error-variable-not-found/) related question which however seems to deal with an entirely different problem in the end. There is also [this] (groups.google.com/forum/#!topic/sage-support/azeRkbvtass) question which is closer to the problem I have, however the issue there seems to be that `p(4).n()` doesn't seem to work which happily provides an answer in my case. I have tried `plot(p(n).n(), (n, 1, 10))` which comes back as `TypeError: cannot evaluate symbolic expression numerically`. (Sorry for the non linked links, I will properly set them as soon as I have enough karma.)
I ended up working around the issue using `plot_list([p(i) for i in range(10)])` which was fine since I was interested mostly in integers anyway.
But why is this function not accepted using the normal `plot`? I also read somewhere that using matplotlib might be a workaround, however that'd introduce so much uneccessary syntax to my code. Also, if the issue here is that sagemath can't find a closed expression of my function, shouldn't there be a `plot_n` function that simply evaluates an argument numerically?Sat, 01 Dec 2018 18:30:52 +0100https://ask.sagemath.org/question/44530/variable-not-found-while-plotting-finite-sum/Answer by Emmanuel Charpentier for <p>Today, while working on a discrete mathematics homework assignement, I came across this strange function:</p>
<p>$$ P(n) = \sum_{k=0}^{n}{\frac{(-1)^k (n-k)!}{k! \cdot n!}} $$.</p>
<p>Part of the question was to evaluate $\lim{n \rightarrow \infty}{P(n)}$ and as I had a hard time doing this by hand I gave it a shot with sagemath. I didn't expect it to evaluate the limit for me (which I tried and it didnt) but I was hoping to plot the graph in a straightforward manner. What I did is</p>
<p><code>
var('k n')
</code>
,
<code>
p(n) = sum( (factorial(n-k)*(-1)^k)/factorial(k), k, 0, n)/factorial(n)
</code>
and finally
<code>
plot(p, (n, 1, 10))
</code>
which comes back with <code>ValueError: Variable 'k' not found</code>.
I found [this] (ask.sagemath.org/question/24293/plotting-value-error-variable-not-found/) related question which however seems to deal with an entirely different problem in the end. There is also [this] (groups.google.com/forum/#!topic/sage-support/azeRkbvtass) question which is closer to the problem I have, however the issue there seems to be that <code>p(4).n()</code> doesn't seem to work which happily provides an answer in my case. I have tried <code>plot(p(n).n(), (n, 1, 10))</code> which comes back as <code>TypeError: cannot evaluate symbolic expression numerically</code>. (Sorry for the non linked links, I will properly set them as soon as I have enough karma.)</p>
<p>I ended up working around the issue using <code>plot_list([p(i) for i in range(10)])</code> which was fine since I was interested mostly in integers anyway.</p>
<p>But why is this function not accepted using the normal <code>plot</code>? I also read somewhere that using matplotlib might be a workaround, however that'd introduce so much uneccessary syntax to my code. Also, if the issue here is that sagemath can't find a closed expression of my function, shouldn't there be a <code>plot_n</code> function that simply evaluates an argument numerically?</p>
https://ask.sagemath.org/question/44530/variable-not-found-while-plotting-finite-sum/?answer=44538#post-id-44538`plot` is suited to the plotting of functions of a real variable, *a priori* defined in the plotting range.Your function is a function of a discrete variable (a suite), i. e. defined nowhere on the plotting range except of a set of null measure. For which `plot_list` is the "right" solution.Sun, 02 Dec 2018 23:29:01 +0100https://ask.sagemath.org/question/44530/variable-not-found-while-plotting-finite-sum/?answer=44538#post-id-44538