I want to sum up all the elements of a given range. see image
Solved! Go to Solution.
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~
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
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:
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!
Something like the attached?
Simple solution though - don't use a range variable.
Mike
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~
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
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
You have a good file to illustrate the pros and cons of ranges and vectors. I agree that changing your document to use vectors will be an effort, AND will make things a bit less clear in some ways. OTOH, you have defined functions w ranges, and I found /that/ confusing at first.
In the attached doc, you can see what I referred to in my last post regarding the sums of ranges and counting how many values are in the range.
As for the question re using multiple subscripts, you must use a vector, not a marix, to accomplish your goal. Please see the attached.
~R~
See the attached image. this looks just weird. can someone explain what is going on.
First, if you write
i:=0...2
f(i):=i^2
the "i" in te function definition has nothing to do with the range variable i. The i in the function definition is a formal parameter. You could make it anything you wanted.
f(xxyyzz):=xxyyzz^2
would work just as well.
If you then evaluate
f(i)=
if finds the last available definition of i, which is a range variable. A range variable is an iterator, and so it evaluates f(i) for each value of i. It therefore returns 0,1,4. You cannot do this though:
xx:=f(i)
because it returns multiple values (I would consider the fact that this does not give an error when f(i):=i to be a bug). You need somewhere to put those multiple values, for example a vector. So if, and only if, i is an integer sequence this is allowed:
xx[i:=f(i)
If the increment in range is not 1 though, then the vector xx will have zero's as some of the entries.
even if i do f(k):=k^2 and then call it with sum(f(i)) it doesn't work
if i do f(k) := k then use sum(f(i)) it works.
why would these two give different results
Which sum? In the attached, it works for range-sigma, because it is design to work with it.
But you have 3-sigmas: one for ranges (in the calculus toolbar), one for vectors (in the matrix toolbar) and other for expressions (also in the calculus toolbar).
Regards. Alvaro
Hi,
Can you tell me why this summation is not working? I have already defined E1, E2, E3 above.
TIA
@Abhishek71992 wrote:
Hi,
Can you tell me why this summation is not working?
Not without seeing how you defined the vector E (did you use vector indices, not literal ones?), how you set the value of ORIGIN (did you change it from the default 0 to 1?) and how your variables are labeled. We would need the worksheet to check the latter.
In any case it would be preferable that you open a new thread and attach your worksheet, too.
BTW, if your vector E only consists of the three elements, its easier to use the summation operator without any range i:
Thanks for your response. I have started a new thread and posted my program there.
This is the link for that program. Please see if you can find the error.
https://community.ptc.com/t5/PTC-Mathcad/How-to-use-summation-operator-in-mathcad-prime/m-p/683876#M...
TIA
As previously suggested you could use the pre-defined "ravel function" - simple and easy.
Mike
You realize that ravel and FillVec are just different versions of the same function, right (or possibly even the same function with different names)?
You realize that ravel and FillVec are just different versions of the same function, right (or possibly even the same function with different names)?
Yes, I was aware of that. I only used ravel because it was already in the collapsed area at the top of my worksheet.
I still don't understand my one would want to sun a range variable.
Mike
if you use a range variable in a function the answer is also a range of numbers. thats what i want to sum up.
For eg. my TotalPowerForDay(d) functions gives a range of answers if d is a range variable. correct.
TotalPowerForDay(1..31) = Table of numbers, each number represents the total for the day.
i just want to sum it up and calculate TotalPowerForMonth(m)
Jan := 1..31
Feb := 32..59
TotalPowerForMonth(m) := sum(TotalPowerForDay(m))
sum(TotalPowerForDay(Jan)) = works
TotalPowerForMonth(Jan) = doesn't work
Hence this post.
But now i'm moving my entire worksheet to vectors.
if you use a range variable in a function the answer is also a range of numbers.
No it's not. If I do
i:=0...2
f(k):=k^2
and then evaluate f(i) it gives me a series of answers, one for each value of i, because a range variable is an iterator. The result is not a range value though, it is just a series of answers displayed on the screen. It is not even possible for it to be a range value, because a range value is defined only by a start, an increment, and an end. That means it can only represent a sequence of values that are all separated by equal increments. It is not possible for a range value to represent the sequence 0,1,4. If you want to assign the values returned by f(i) to a variable you must do so explicitly, to a variable that is capable of holding multiple values (for example a vector).
V[i:=f(i)
I would reccommend re-reading Richards post on the 11-Oct-2010 21:53.
Have a look at the attached image. This is what Richard is trying to explain, I think.
Mike
I'll stick in my oar, but only to chat further...and to ask a question or two
First, thanks to Richard for the clarifications. On a related note, I'd like to see the Mathcad Help system updated on a regular basis - monthly, if necessary - with notes such as these. Help updates could be routed to folks who have shown an expertise in the related area, and those folks could comment just as we are doing here. The goal would be to use Help to answer more questions that folks have. Even someone with many years of experience (I am an example.) don't know all the answers.
One of the problems with ranges and vectors is that they are NOT explained all that well in Help, and (see the attached image) when novices interpret Help and experiment, the results can be misleading. In the attached image, the "list" (v1) looks like a table (T1) or a vector until one tries to use it.
It /looks/ as if the range is being passed in the formal parameter list, but I accept Richard's explanation (as I understand it) that the range is not being passed to a function, here f1, f2, & f3, but rather items from the list are passed in sequence to the function, which then does what the function asks for that element. Is this correct, Richard?
It seems that this could explain why the assignments to v in the last row work as they do. If that's true, then what is up with v2 and v3?
That all being said, I encourage Ziyad to work with the differences twixt ranges and e. Once one gets used to them, they are easier to understand. And I think that he will find the vectors more flexible in the future in his application, though I admit that I find a certain look of efficiency to his earlier use of ranges.
~R~
One of the problems with ranges and vectors is that they are NOT explained all that well in Help, and (see the attached image) when novices interpret Help and experiment, the results can be misleading. In the attached image, the "list" (v1) looks like a table (T1) or a vector until one tries to use it.
I agree 100%. Vectors, range variable, and tables can all look very similar, and that can confuse a novice. They need to be much better explained in the help. There needs to be a specific tutorial on the subject.
It /looks/ as if the range is being passed in the formal parameter list, but I accept Richard's explanation (as I understand it) that the range is not being passed to a function, here f1, f2, & f3, but rather items from the list are passed in sequence to the function, which then does what the function asks for that element. Is this correct, Richard?
That is exactly corrrect.
It seems that this could explain why the assignments to v in the last row work as they do. If that's true, then what is up with v2 and v3?
The question is not so much what is up with v2 and v3, but what is up with v1. If you apply f2 or f3 to a range the result cannot be a range, because the increments are not equal. If you write
v2:=f2(i)
the result also cannot be a vector, because although a vector can hold multiple results you have not specified where in the vector to put each result. If you do specify where to put the results it works
v2[i:=f2(i)
v2[(2-i):=f2(i)
are a couple of possibilities
Logically, it could return a scalar. So first it assigns 0 to v2, then it assigns 1, then it assigns 4. So what you would get is the last result. That would be consistent with what you would get if you used a loop in a program rather than a range variable:
That's not very useful though, so the designers evidently decided it should simply not be allowed. Except it is allowed in one special case, when f(i):=i. Not even f(i):=1*i is allowed though, so that one special case is a real anomaly!
So treat v1 as the unexpected result, not v2 and v3
even if i do f(k):=k^2 and then call it with sum(f(i)) it doesn't work
Out of curiosity, what are you expecting the function f(k):=k^2 to return if k is a range?
if i do f(k) := k then use sum(f(i)) it works.
It's a fluke. I don't know why that case works, but probably because some internal optimization recognizes that if I have
f(k):=k
i:=0...2
xx:=f(i)
the function call is not needed at all, and therefore what it actually does is xx:=k. It does not work for any other case though, even the most trivial:
So, in a nutshell, just don't do that.
But i guess it will be worth it. these range variables are becoming a nuisance.
Range variables are not a nuisance. They are useful if you use them for the things that range variables are good for. Use vectors for the things vectors are good for. Trying to substitute one for the other leads to problems because not only are they not the same thing, they are not even close to the same thing! Stop thinking about ranges as being something like vectors (a comment that applies to others that have responded to this thread too). Think about them an iterator, i.e. a loop variable, because that's what they are. They are not fundamentally any different to the standard programming construct
FOR i = 0 STEP 0.5 TO 12
Some stuff
NEXT
Mathcad has a similar construct that can be used to define a loop in a program. A range variable defines a loop at the worksheet level. It is defined by a start, an increment (defined as the second value minus the first), and an end. They do not contain a set of values. Would you ask the question "what's the sum of the FOR loop above"? I doubt it, because it a question that does not make much sense. You might ask the question "what is the sum of the values the loop variable will take on when the loop is executed?, but that is a different question. It makes no more sense to ask what the sum of a range variable is. All the "weird" results everyone thinks they are seeing are quite explainable if you think of a range variable as an iterator rather than a vector, or something similar to a vector that contains a set of values (see my other post).
one thing against vectors. the worksheet becomes less readable, with tons of 0's and 1's floating around.
I would need to see an example of what you mean.
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
Use a column vector, not a row vector. If you want to display a column vector horizontally, then transpose it.
Nice explanation as usual.
Think this should be your first topic in the "FAQ" document/blog.
When I first started using Mathcad I was shown by a colleague of mine how iterate calculations using a range variable and it took me a while to get away from using them. I find once you get your head around the difference in the two, you can selectively pick when to use a range variable or a range vector.
Mike