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.Sat, 01 Dec 2018 18:30:52 +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?HansZoellnerSat, 01 Dec 2018 18:30:52 +0100https://ask.sagemath.org/question/44530/TypeError: range() integer end argument expected, got sage.symbolic.expression.Expression.https://ask.sagemath.org/question/8836/typeerror-range-integer-end-argument-expected-got-sagesymbolicexpressionexpression/I am trying to generate some plots using the following function definition
var('p N M W C')
assume(C,'integer')
pc(p,W) = 1 - (1-p)^W
prc(p,M,W,C) = 1 - sum(binomial(M,i)*pc(p,W)^i*(1-pc(p,W))^(M-i) for i in range(C))
pcol(p,N,M,W,C) = 1 - (1-prc(p,M,W,C))^N
plot(pcol(p/2.0^33,2^20,2^10,8,1),p,0,1000)
However, I get range TypeError even when I have declared that C is integer type. See error listing below:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "_sage_input_34.py", line 10, in <module>
exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("dmFyKCdwIE4gTSBXIEMnKQphc3N1bWUoQywnaW50ZWdlcicpCnBjKHAsVykgPSAxIC0gKDEtcCleVwpwcmMocCxNLFcsQykgPSAxIC0gc3VtKGJpbm9taWFsKE0saSkqcGMocCxXKV5pKigxLXBjKHAsVykpXihNLWkpIGZvciBpIGluIHJhbmdlKEMpKQpwY29sKHAsTixNLFcsQykgPSAxIC0gKDEtcHJjKHAsTSxXLEMpKV5OCnBsb3QocGNvbChwLzIuMF4zMywyXjIwLDJeMTAsOCwxKSxwLDAsMTAwMCk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
File "", line 1, in <module>
File "/private/var/folders/04/ymgbq2r15hx_q1knz7jxlqzc0000gn/T/tmpcfnMcC/___code___.py", line 6, in <module>
__tmp__=var("p,M,W,C"); prc = symbolic_expression(_sage_const_1 - sum(binomial(M,i)*pc(p,W)**i*(_sage_const_1 -pc(p,W))**(M-i) for i in range(C))).function(p,M,W,C)
TypeError: range() integer end argument expected, got sage.symbolic.expression.Expression.
nrsaxenaFri, 30 Mar 2012 03:38:42 +0200https://ask.sagemath.org/question/8836/Easy (beginner) sum problem:"need a summation variable"?https://ask.sagemath.org/question/8741/easy-beginner-sum-problemneed-a-summation-variable/My native language is not english but I try my best to make you understand the problem.
I'd like to:
from k=1 to n+1 sum k^2
But when I try to solve it with sage, it gives me an error.
(k,n)=var('k,n')
show(sum(k,k^2,1,n+1))
It has a problem with k^2.
How to solve it?
Many thanks.pepeSun, 26 Feb 2012 16:05:55 +0100https://ask.sagemath.org/question/8741/summing over a list of variables?https://ask.sagemath.org/question/8222/summing-over-a-list-of-variables/Hi all,
I'm new to SAGE (I'm trying to move over from Mathematica), and I was wondering if someone could help me define the following function:
$$ r_k(x_1, x_2, x_3, x_4) = \left( \sum_{i=k+1}^{4} x_i^2 \right)^{1/2} $$
I figured I could do something like:
var('k,j', domain=NN)
x = [var('x_{index}'.format(index=i)) for i in range(4)]
r(k,x) = sqrt( sum( x[j]^2, j, k, 3 ) )
But this returns the error:
> TypeError: unable to convert x (=j) to
> an integer
Worse yet, when I enter sage:x, it returns x, and forgets x was a list of variables. And to complicate things, when I try
y = [1,2,3]
sum( y[j], j, 0, 1)
I get the error
>TypeError: unable to convert x (=j) to an integer
even after I do "clear_vars()" and try everything all over again.
Can anyone tell me what's going on?StevenPollackWed, 13 Jul 2011 12:26:39 +0200https://ask.sagemath.org/question/8222/