Skip to main content
1-Visitor
June 4, 2022
Solved

Unusual error

  • June 4, 2022
  • 4 replies
  • 3198 views

I have been trying to create a simple while loop and just can't figure out the error Mathcad keeps prompting. Can someone suggest what the issue is here? 

Best answer by terryhendicott

Hi,

Other way is to increase the elements of c in the while loop,

Capture.JPG

4 replies

ttokoro
21-Topaz I
21-Topaz I
June 4, 2022

Use i instead k.

image.pngimage.pngimage.png

t.t.
21-Topaz II
June 4, 2022

Hi,

Only one element in c at index 0.
The while loop enters once with k = 1 from start.
k-1 or zero index into c works
while loop adds 1 to index k=2
condition at loop start tries to index c at 1 which does not exist
hence the error message.
Solution increase the elements of c and make sure one is >= c_Critical to halt the loop
Also index t_series to get a series instead of just the last t.

 

21-Topaz II
June 5, 2022

Hi,

Other way is to increase the elements of c in the while loop,

Capture.JPG

25-Diamond I
June 5, 2022

Terry already explained in detail the reason for the error - you try to access elements of a vector c which simply aren't there as your vector only contains one single value.

It looks to me that you should create a new concentration value c_k in each iteration step before you increase k in your program. That way you program would add values to your vector c to use in the while comparison.

But as your program only returns a vector of time values, it would not be necessary to create a vector of concentration values at all. simply overwriting the current c value with a new one in each iteration step would be all thats necessary.

Its not clear what you actually want to achieve. If you just want to determine by iteration the time when the concentration exceeds a certain threshold, you would not need to create a vector of time values at all. Actually I suspect that you would not need a program to do the iteration but could calculate the desired time in an easier way.

So maybe they want to explain in a little more detail what exactly they want to calculate and how the concentration should change at each step.

BTW, the suggestion of @ttokoro seems to create an endless loop 😉

25-Diamond I
June 5, 2022

Could it be that you are just looking for something like this:

Werner_E_0-1654430678011.png

But of course you also could use a program which does a stepwise iteration to do the job:

Werner_E_0-1654431891821.png

 

 

1-Visitor
June 5, 2022

Thanks all, I didn't realise this forum was this active! The file I shared was just a snippet of the larger file I am developing  - apologies for the confusion.  Please see my responses:

 

@ttokoro I'm surprised that Mathcad didn't object to incrementing "k"  within the loop, even though it hasn't been initialised previously. I thought it would have flagged it as undefined variable? 

 

@terryhendicott  Thanks - now I realise that, while initialising the variables used in the logical conditions of while loop (before the loop) just gets the while loop started; unless they are fully defined WITHIN the loop, the conditions can't be evaluated at the next iteration.  Once I included the equations of all the variables in the while loop, this error goes away. 

 

@Werner_E The actual loop is a lot more exhaustive where, while the incoming flow rates are constant, the exiting flow rates of the fluids and dissolved species are a function of concentration of the previous iteration. At the end of the current iteration, the concentration changes and this further impacts both the flow rates in the next iteration and so on,  This circularity is the reason why I can't simply vectorise the calculations as you suggest and have to use a while loop, accompanied by the logical conditions to halt it as required. 

 

On another note, I also observed a peculiar behaviour. In the snippet below, I define zeroth element of vector b as 999. I then use the vector b in the program structure, with an expectation that since I start the loop at N=1, the zeroth element of vector b (i.e. 999) should be preserved. However I think Mathcad just disregards the definition of vector b outside the structure and defines the zeroth element as zero? This is in contrast to the constant "y" defined outside the program structure which is correctly passed on. This means that whenever there are vectors in the program structure, I have to define the zeroth element WITHIN the program structure, before the while condition in the example below?

OJ_10231311_6-1654450723831.png

 

            

OJ_10231311_7-1654450732167.png