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

Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X

Translate the entire conversation x

Invalid Array Index (variable in index)

Devin_2020
8-Gravel

Invalid Array Index (variable in index)

Hello,

 

I am using Mathcad Prime 10.0.0.0.  On sheet 3 I am trying to call in column 2 (via data[i,2] in the first IF statement) which has a text name for the value.  If the value equates to something in my IF statements, I want my program to calculate the H_f or Friction Head.  I want the program to go down the array one at a time and at the end output the sum of all the individual friction heads added together, so I gave the array the indices of i,2 with i going from 0 (first value in the submatrix 'data') to p-1 (end of the rows in the main Matrix 'System').  I am not sure why I am receiving an error which states "This array index is invalid.  The index must be an integer, not less than origin, and not greater than the last element.".  Is there any chance you all may be able to assist?  Can a variable not be in the Array index?  If not, how do I go down my list?  Should I have individual vectors (single column arrays) for each column in the matrix and call them out that way?  Seems more time intensive if I go that route.

 

V/r,

DM

ACCEPTED SOLUTION

Accepted Solutions
Werner_E
25-Diamond I
(To:Devin_2020)

Your for loop runs from 0 to end=5, but the last row index of your matrix "data" is just 4. So when i=5 the error is thrown.

 

EDIT: Had not looked at your new sheet.

In your new sheet variable "end",  which you still use as end value of your for-loop, is not defined at all - hence the error.

 

You increment variable "a" in the for loop in each cycle, so "i" and "a" end up having the same value in each loop cycle. So "i+a" is equivalent to "2*i".

No idea what you are about to achieve, but the row index you use must never exceed the last allowable row index (which is 4 in case of your matrix "data").

 

EDIT 2: Your pdf shows a different file as the one you posted!! Here you have to change p-1 to p-2. After all p is the number of rows of the matrix "System". The matrix "data" has one row less, so the last allowable index is p-2.

The program in your pdf still increments variable a without using it. You may delete that expression.

The same goes for the statement i <-- i+1 at the end. It has no effect an is not used in any way.

View solution in original post

9 REPLIES 9
Werner_E
25-Diamond I
(To:Devin_2020)

You are incrementing variable "a" in your loop, so eventually the expression "i+a" which you use as row index for matrix "data" exceeds the value 4 (index of the last row in "data") and so you are trying to access a non existent row - this is what the error message tries you to tell.

 

Are you here with two different accounts? Its noticeable that the sheet posted here
Help with program

contains the same structure and the same errors (starting with the calculation of ‘end’) as the one posted here.

Werner, thank you for the response.  I had asked someone at PTC for assistance and it appears they handed off my question/program to  JH_9781973.  I read that thread, but it wasn't focused on my main concern.

 

I removed the [i+a, j+b] and made my index [i, #]  .  I also changed the end of the for loop from p to p-1.  Do you know why I get an error for having i as my row when I am incrementing i?

Werner_E
25-Diamond I
(To:Devin_2020)

Your for loop runs from 0 to end=5, but the last row index of your matrix "data" is just 4. So when i=5 the error is thrown.

 

EDIT: Had not looked at your new sheet.

In your new sheet variable "end",  which you still use as end value of your for-loop, is not defined at all - hence the error.

 

You increment variable "a" in the for loop in each cycle, so "i" and "a" end up having the same value in each loop cycle. So "i+a" is equivalent to "2*i".

No idea what you are about to achieve, but the row index you use must never exceed the last allowable row index (which is 4 in case of your matrix "data").

 

EDIT 2: Your pdf shows a different file as the one you posted!! Here you have to change p-1 to p-2. After all p is the number of rows of the matrix "System". The matrix "data" has one row less, so the last allowable index is p-2.

The program in your pdf still increments variable a without using it. You may delete that expression.

The same goes for the statement i <-- i+1 at the end. It has no effect an is not used in any way.

Werner, thank you!!  I was counting up a for no reason now, so I deleted that line.  I also ensured that my completion of the for loop was at (p-2) which now enables the program to work.  I am getting a wonky answer, but I am getting an answer!  Now I need to work through my if statements to ensure my calcs are proper.

Werner_E
25-Diamond I
(To:Devin_2020)

Are you sure that your expression

Werner_E_0-1741187216184.png

is correct?

According to what you wrote with

Hf=V2/2g*ft(L/D) where V=Q/[(D/2)^2*pi ]

I would have rather expected it to be (partially) squared

Werner_E_3-1741187750467.png

or simplified

Werner_E_4-1741187761526.png

 

 

Werner, that is a good catch.  I was going through my sheet and noticed the error as well.  I've added some additional steps to make it more clear (to me) what I am solving for.  I tried to nest an if loop for Reynolds number within the X program.  It says I am utilizing the wrong units, but I have looked at it several times and it appears that I am using the correct units and solving things properly.  Would you mind taking a peek?  please note that I have only built this out for if data [i,2] = "Pipe" and have a bunch of notes in hidden areas.  I have updated both the Mathcad file and the excel file and I've posted the file as a pdf too.

 

V/r,

Devin

Werner_E
25-Diamond I
(To:Devin_2020)

I stopped reading after the first three errors in your Prime sheet:

 

1) Definition of Re:

Variable "Vel" is not defined as Prime correctly states.
Furthermore its always better to avoid variable names which also are names of predefined functions or constants. "Re" is a function which returns the real part of a complex number. Its not forbidden to name a variable "Re" and Prime can distinguish between a  variable "Re" and the function "Re" via the labelling, but it can be quite a hassle to have to manually re-label in case you don't mean what Primes auto-labelling thinks you mean.

BTW, I noticed that your variable "System" is labelled as being "Function" (you can tell by the upright typeface use in contrary to the typeface used in your variable "Material"). Again this is not causing an error but I found it inconsistent and confusing and I guess it was not done on purpose.

 

2) Definition of ftcalc:

It fails because it uses "Re" which could not be defined as of the missing definition of "Vel".
In case you did not define "vel" on purpose and were thinking of about providing its value later, you have to define functions instead of variables. Like Re1(Vel)=:=.... and ftcalc(Vel):=....

 

3) Definition of X:

You still use the non-existing variable "end" in your program, so the calculation must fail.

I also noticed that you still use V and not V^2. Did you post the correct latest sheet?

 

BTW, using your new Excel sheet, you seem to only deal with two rows of valid data ?

Werner_E_0-1741192840417.png

 

 

Werner,

 

I do very much appreciate your help.  I did not know that about "Re".  I've deleted "Vel" to eliminate the error.  I have squared velocity, but that comes after I  calculate ftcalc.  It appears the program is assigning "Reynolds" units, but they should clear and the value should be unitless.  I wanted to compare Reynolds to numerical values, but Mathcad says the 'Units are not compatible'.  I found my error that I did not assign the units of "in" to datai,1!!

 

I have uploaded my new Revision 4 Prime file, PDF, and Excel if you would like to see.  You are correct that I am only evaluating two rows of data as I am trying to make this data set work prior to adding in my other data (which will be pipe fittings).  Looks like my program is working now with my two rows of data.  I will keep working on this to add piping fittings!

 

V/r,

Devin

Werner_E
25-Diamond I
(To:Devin_2020)

As I understand it, you don't experience any errors anymore.

 

 

In the calculation of X I would suggest to initialize H.f for good style.

Werner_E_0-1741200339676.png

If H.f is not defined in the worksheet in front of the program, its does not matter as in a program an undefined variable which is used at the left and right side of an assignment is assumed to be zero.

You may see the need for initialization if you define H.f in the worksheet in front of your program

Werner_E_1-1741200421467.png

 

 

Announcements

Top Tags