Ask Your Question

Variable Not Found while Plotting Finite Sum

asked 2018-12-01 18:33:07 +0200

HansZoellner gravatar image

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] ( related question which however seems to deal with an entirely different problem in the end. There is also [this] (!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 flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2018-12-02 23:29:01 +0200

Emmanuel Charpentier gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2018-12-01 18:30:52 +0200

Seen: 386 times

Last updated: Dec 02 '18