Skip to main content
15-Moonstone
September 22, 2025
Solved

Trouble with Summation

  • September 22, 2025
  • 4 replies
  • 954 views

I am trying to evaluate the sum shown below. I get "This array index is invalid for this array" but I don't see it. I would have thought that if the upper value in the sum is less than the lower, MC would give zero. That is not the case. Maybe this is the problem but, if so, I don't see how to solve it. As always, any help would be appreciated.

JohnRudnicki_0-1758573930376.png

 

Best answer by Werner_E

OK, this was a tricky one which took some time until I realized whats the cause for the difference.

The error occurs because Luc's  attempt does not prevent the summation from 1 to 0 from being executed. It only sets the nominators of the summands used in the process to C0. The approach therefore only works because C0 is fortunately set to zero and therefore these sums, which should actually be avoided, only add zeros. That is also why I do not particularly like this approach - it would not work if C has to be initialized with a value different from zero.
So when m runs through the values 1 and 0, the last summand gives us the expression 0/0 for m=0. This is undefined, and therefore it is perfectly fine for Mathcad to issue an error message here.
In Mathcad 14/15, however, there is an option in the worksheet options to set 0/0 to be treated as 0.

"Tools" - "Worksheet Options..." then tab "Calculation"

Werner_E_0-1758749383990.png

In the sheet  you posted the option 0/0 = 0 was not checked and that's the cause Mathcad treated 0/0 the correct mathematical way and threw an error 😉

ConcLM only works if this option is enabled (and if C0 is always initialized to 0). Of course it still fails for Maxk=1, which is why I included the branch before the loop in my version. This may also solve your problem in the other sheet, but I would rather prefer ConcSB over ConcLM.

 

According you attempts with ConcWExx in the other sheet: If you initialize C with just one value instead of 2 (like I did using the stack function) you must assure that you create a vector, not a scalar.

So I guess this is what you are trying to achieve

Werner_E_5-1758751656812.png

The last line would not be mandatory but I feel its good style to explicitly state a return value.

Werner_E_6-1758753565778.png

Find also similar SB and LM versions in the attached sheet. Personally I would settle for SB. SB also does not avoid the unwanted summations to be done, but it explicitly adds plain zeros (not only setting the nominators to zero). So this version (like the WE) also works if the option 0/0=0 is not set.

 

I was also curious concerning efficiency wrt calc time.

Werner_E_7-1758753663971.png

WE and SB can be considered to be equivalent. The effect with LM can probably be explained by the many calls to the external function “lim”

 

 

4 replies

23-Emerald V
September 22, 2025

I haven’t got MM15, I’m afraid.

 

However, if you (and, presumably, your calculation) expect the sum over m to be zero when k = 0, can you just put the summation inside an if function that returns zero when k = 0 otherwise the sum?

 

Stuart

23-Emerald V
September 22, 2025

Now I'm at a Mathcad laptop again ... 

 

Something like below:

 

2020 04 04 E.png

 

Stuart

25-Diamond I
September 22, 2025

I am missing your worksheet!

The sum operator is not implemented in Mathcad in a way conforming to mathematical standards. If the upper summation limit is lower than the lower one, the result should be zero ('empty sum') -> https://en.wikipedia.org/wiki/Summation#Formal_definition

But the implementation in Mathcad unfortunately was done by programmers who thought that the summation operator should work like a for-loop.

So when in your program k=0, the sum runs from 1 down to 0 and consists of two summands, the first with m=1 and the second with m=0. With m=1 your calculation tries to access C with index -1, which of course does not exist. with m=0 you would try to access C with index 1 which also does not yet exist, its just about to be created.

You may cope with this problem using an if-statement but in your case I guess its easier to simply predefine C[1 along with C[0 and let k run from 1 to Maxk (or better Maxk-1, see below). If-statements may still be necessary to cover the cases MaxK=0 and Maxk=1 where the k-loop should not be executed.

If Maxk is meant to be the last index in the zero based result matrix, the k-loop should just run up to Maxk-1.

Werner_E_1-1758577441822.png

 

Stuarts suggestion of using the if-function makes it a bit shorter at the price of the  if-function to be evaluated multiple times (in the k-loop). But unless you intend to create vectors with millions of entries I guess this is negligible

Werner_E_2-1758577569886.png

 

.

 

 

 

23-Emerald IV
September 22, 2025

Note that the sum works either way:

LucMeekes_0-1758576126774.png

If you limit the computed index with:

LucMeekes_1-1758576154385.png

like this:

LucMeekes_2-1758576169250.png

you should get:

LucMeekes_3-1758576183385.png

 

Success!
Luc

 

15-Moonstone
September 23, 2025

Thanks so much for your suggestions and explanations.  Your programs are giving the kind of numbers that I expect.  I also tried Luc's suggestion. I think I typed it in correctly but I am getting a "divide by zero error". He does get a numerical value but it is slightly different from the other programs. Any additional comments? (Sorry I did not post the worksheet last time, but I did this time.)

25-Diamond I
September 23, 2025

@JohnRudnicki wrote:

Thanks so much for your suggestions and explanations.  Your programs are giving the kind of numbers that I expect.  I also tried Luc's suggestion. I think I typed it in correctly but I am getting a "divide by zero error". He does get a numerical value but it is slightly different from the other programs. Any additional comments? (Sorry I did not post the worksheet last time, but I did this time.)


I did not experience the "divide by zero" error in the sheet you posted!? Where did you experienced that error?

 

Luc made a typo in the first summand. It should read k-m rather than k+1.

Luc's program only returns the last value. If you need he whole list simply add C as the last program line.

Furthermore Luc's loop still runs up to Maxk while my suggestion was to use Maxk-1 as upper limit. So he is doing one more iteration.

Werner_E_0-1758664888057.png

 

15-Moonstone
September 24, 2025

Thanks for the additional comments:

 

Hmm. The worksheet I posted was MC11 (for Luc) and I do not get the "divide by zero" error in it either. But I do get it in my original MC14 worksheet.

JohnRudnicki_0-1758717707467.png