I defined a function in Mathematica. It is a function of n, so f[n_]:=, but in the definition, I used a sum, $\sum_{k=0}^n$. So, the $k$ is just an index variable for the sum and no $k$ shows up in the final answer. As I was using this function I tried evaluating f[k-1] and got a weird answer, 0. I finally figured out that Mathematica was trying to do the sum $\sum_{k=0}^{k-1}$, or so I guess. So, my question is, is there a way to make the $k$ local so that this error never occurs? My fix for now was to change $k$ to $index$ and I will probably not use f[index] at any point.
Tell me more
×
Mathematics Stack Exchange is a question and answer site for
people studying math at any level and professionals in related fields. It's 100% free, no registration required.
|
The function you are looking for is called Module. So you can define
|
|||
|
|
|
An alternative to One may still run into trouble depending on how you choose to use Formal symbols elsewhere, but if you never use |
|||
|
|
|
|
|||||||||||
|
Sumfunction. However, $k$ is still $k$ so thatg[k-1]translates intoSum[Binomial[k-1,k],{k,0,k-1}]which will evaluate to 0 (because Binomial[k-1,k]=0). What you want is that the summation index gets a different name and therefore you needModule. WithModuletheg[k-1]is effectively translated intoSum[Binomial[k-1,k$],{k$,0,k-1}]. – Fabian Mar 24 '11 at 22:32Remove[k], I get the correct result. (c) Protecting g with eitherBlockorModulemakes it work correctly. Clearly, then, the help statement is not entirely correct. – whuber Mar 25 '11 at 2:52