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?

edit retag close merge delete

Sort by » oldest newest most voted

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.

more