cancel
Showing results for
Did you mean:
cancel
Showing results for
Did you mean:

4-Participant

## Unusual error

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?

1 ACCEPTED SOLUTION

Accepted Solutions
21-Topaz I
(To:OJ_10231311)

Hi,

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

7 REPLIES 7
19-Tanzanite
(To:OJ_10231311)

21-Topaz I
(To:OJ_10231311)

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 I
(To:OJ_10231311)

Hi,

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

24-Ruby V
(To:OJ_10231311)

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 😉

24-Ruby V
(To:Werner_E)

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

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

4-Participant
(To:Werner_E)

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?

24-Ruby V
(To:OJ_10231311)

The variable on the left hand side of an assignment inside a program always is a LOCAL variable. A variable on the right hand side may be a GLOBAL worksheet variable, unless there is a local variable with the same name.

Thats the reason b[0 is not 999 in the vector returned by the program (its used on the LHS only and so a new local variable is created) and also the reason why the global variable "y" is used (its used on the RHS of the assignment and there is no local variable "y" defined).
You may put the assignment b <-- b at the top of your program to turn the global variable b (right hand side) to the local variable b (left hand side) which then at the end is returned.

I find it generally better to put the assignments if the initial values (also the iteration counter N) inside the program at the top. So its all nice and tidy at the place where it belongs to and there is no need for N outside the program anyway.

From your description your problem sounds like a 2-compartment model which possibly could be described by a differential equation which may be solved numerically by a solve block with "odesolve". Not sure, though, of course.

Announcements