Skip to main content
1-Visitor
October 10, 2010
Solved

How to sum a range variable

  • October 10, 2010
  • 3 replies
  • 35049 views

I want to sum up all the elements of a given range. see image

Best answer by RichardJ

See the attached

3 replies

3-Newcomer
October 11, 2010

I think this might be a bug, because if you pass a range explicity, i.e., 1,2..3, then the function works, but if you pass it by reference, i.e., j, where j:=1,2..3, then you get the error message, and it is the called function that is complaining. The summation works standalone, but when it is in a function, the summation complains that the passed variable is not a range, though is clearly is.

~R~

1-Visitor
October 11, 2010

While the behavior is buggy, the function definition is unclear to me. Making the summand a range seems to me to be not a valid construct. What does

sum(0..n) from 0..n mean exactly?

Since it does accept the range definition directly, it should accept it by reference, so that's a bug. The construct was allowed in M11, resulting in an array of the summation of the range, i.e., n*(n+1)/2 n+1 times, That's rather pointless, and so maybe that's why it was changed, but it doesn't throw a useful error message.

TTFN

19-Tanzanite
October 11, 2010
Since it does accept the range definition directly, it should accept it by reference, so that's a bug. The construct was allowed in M11,

Sort of. The range variable actually has to be defined before the function defintion, because the summation variable and the variable to be summed are not the same, even if they have common names:

range sum of range - MC11.gif

resulting in an array of the summation of the range, i.e., n*(n+1)/2 n+1 times, That's rather pointless, and so maybe that's why it was changed, but it doesn't throw a useful error message.

Thanks. Now I understand what it is doing in MC11. In the image above, the sum over all i, k+1 times. As you say, not a very useful answer!

1-Visitor
October 11, 2010

Something like the attached?

Simple solution though - don't use a range variable.

Mike

3-Newcomer
October 11, 2010

You've stumbled into the murky waters of range variables in Mathcad. In some ways, it is nice that they work the way they do, and that they are distinct from vectors. At other times, they are annoying [to me] to work wth, because I expect a certain behavior and don't get it.

In the attached file, I show the examples that I could think of, and conclude that there is no programmatic solution needed. Let us know what you think...

...and if you want to know the number of elements in the range, replace the summation argument k in the last example with 1, and the result is the number of elements in the range.

~R~

1-Visitor
October 11, 2010

converting my entire worksheet into vector is going to be a chore.

But i guess it will be worth it. these range variables are becoming a nuisance.

one thing against vectors. the worksheet becomes less readable, with tons of 0's and 1's floating around.

I noticed even if you create a vector you still have to use two indexes to pick an element from the vector, just like what you would use for a 2D array.

for eg

i:=(1 2 3 4)

to pick 2 i have to do i[0,1

i cannot do i[1

1-Visitor
October 11, 2010

one thing against vectors. the worksheet becomes less readable, with tons of 0's and 1's floating around.


Not sure what you mean by that, can you post an example?

I noticed even if you create a vector you still have to use two indexes to pick an element from the vector, just like what you would use for a 2D array.

for eg

i:=(1 2 3 4)

to pick 2 i have to do i[0,1]

Not true have a look at the attached image.

Btw how do i find sum of a vector. or sum of one column of a 2D array

Examples of both also included.

Mike