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

Community Tip - If community subscription notifications are filling up your inbox you can set up a daily digest and get all your notifications in a single email. X

Translate the entire conversation x

Why is mathcad altering this vector when the IF conditions are not met?

CN_13593194
10-Marble

Why is mathcad altering this vector when the IF conditions are not met?

I have a loop with two conditions in it, none of which are satisfied.

I created a variable 'a' and defined it as zero, and inside the loop, I made it change 'a' to 1 if the If conditions are satisfied. as shown in the figure below, a is still zero after the loop, meaning the conditions are not satisfied. However, the vector y4 is modified even though it only has equations to modify it inside the same conditions for variable 'a', which is unchanged:

CN_13593194_0-1750999913102.png

what am I missing here?

I attached the file. Using Mathcad Prime 9.0.00

thanks

ACCEPTED SOLUTION

Accepted Solutions

Lets keep to this simplification of your example:

Capture.JPG

 

what exactly is setting the variables to zero in your example?  good question

Prime has to make a distinction if "a" in the program is the global "a" defined above the program or a local "a" within the program.

"a" if passed into the program as a parameter then it is clearly a global one.

"a" if not defined on the left hand side of a assignment but is used on the right hand side of an assignment it is the global one.

In your arrangement "a" within the program is set as output to the "a" global one on completion of the program.  It is still not clear if it is "a" local or "a" global within the program.  You are assuming it is the global "a" and is thus defined in all cases.

Prime makes "a" within the program a local variable as it does not know any different and it has to make an assumption.  0 is the default value that is set to this "a" local variable as no condition sets it value to any other value.  So it returns "a" local as zero.

 

If you want it to be the global "a" do it like this

Capture2.JPG

Cheers

Terry

View solution in original post

15 REPLIES 15
LucMeekes
23-Emerald IV
(To:CN_13593194)

Note that both a and y4 are 0 after the loop.

What if you set a:=2 before the loop. Is it still 2 afterwards?

 

Success!

Luc

good catch! I just tested the example where a changed (I changed my IF conditions) and noticed a changed

 

but as you pointed out, in this example, if I set a = 2, a becomes zero afterwards.

 

I still dont understand why is that, what's logic?

 

CN_13593194_0-1751028681018.png

 

tj never less than tm so nothing is done except set tj.

Capture.JPG

"so nothing is done except set tj."

 

And that's the reason why the OP asks why y4 is changed...

 

Luc

Yes it sets the variables to zero so it is changed.

In its essence:-

Capture.JPG

Cheers

Terry

what exactly is setting the variables to zero in your example?

i dont see it being defined as zero anywhere

thanks

Lets keep to this simplification of your example:

Capture.JPG

 

what exactly is setting the variables to zero in your example?  good question

Prime has to make a distinction if "a" in the program is the global "a" defined above the program or a local "a" within the program.

"a" if passed into the program as a parameter then it is clearly a global one.

"a" if not defined on the left hand side of a assignment but is used on the right hand side of an assignment it is the global one.

In your arrangement "a" within the program is set as output to the "a" global one on completion of the program.  It is still not clear if it is "a" local or "a" global within the program.  You are assuming it is the global "a" and is thus defined in all cases.

Prime makes "a" within the program a local variable as it does not know any different and it has to make an assumption.  0 is the default value that is set to this "a" local variable as no condition sets it value to any other value.  So it returns "a" local as zero.

 

If you want it to be the global "a" do it like this

Capture2.JPG

Cheers

Terry

Interesting, I would have never thought that Prime does that intentionally

thank you for the detailed explanation.

Here is a work around without having to define a function:

Werner_E_0-1751034736140.png

The first line assigns the values of the global variables a and b (RHS of the assignment) to the local variables a and b (LHS of the assignment).

Variables used on the LHS of a local assignment in a program are ALWAYS considered local variables and do not affect global variables of the same name.

For variables used on the RHS Prime first looks if there is a local variable with this name and uses it. Only if no local variable with that name exists, Prime uses the global variable with this name.

When Prime scans the program before executing it it sees  the local(!) variables a and b used in the if condition and so it reserves space for them an assigns them the default value zero. It does not matter that the condition is not fulfilled when the program is executed and the local variables would not be used in that case.

You may think of the local variables as of variables with completely different names. So the effect you experience may become clearer that way:

Werner_E_1-1751035787219.png

 

great! very useful, I will do that, thanks

I tried using  a similar approach as suggested in my mathcad file (mathcad prime 9.0.0.0)

and it does not seem to work, even though I dont believe the value of tt4 is being set to 0 by any IF checks:

CN_13593194_0-1751475421307.png

 

any support will be greatly appreciated!

 

Werner_E
25-Diamond I
(To:CN_13593194)

I used "try...on error..." to find out that the error is thrown because t1=0.197 seconds, but t4 = unitless zero! It happens when j=8001.

I have not traced as to why this happens but its sure very bad programming style to mix global variables and local variables with the same name and thats basically what you do, perhaps unintentionally.
If a local variable is for some reason automatically initialized by Prime, than to unitless zero and not zero seconds!

Try to initialize all variables used in the program inside the program yourself.

If you really need to use all in the program and want to initialize global variables in front of the program and want them to have the same name inside the program, then put something like [t1  t2 t3 ] <-- [t1  t2  t3] s your first program line.

When I do this at least for the first four t-values, the program runs without error but I would strongly suggest to initialize ALL variables used in the program!

Werner_E_0-1751491218243.png

Werner_E_0-1751491644139.png

BTW, "y" should be initialised to 0 seconds, not just 0.

you're right, I am not used to having a number being changed to zero just because a condition is not met, it does not make sense to me

so when I write my codes, it's hard to bypass that logic.

 

anyway, I was trying to assign t4 to tt4 when checking the If condition because if the value of tt4 is changed, it shouldn't matter because I always reassign t4 to tt4 everything I check. but anyway, there is something wrong there

 

I thought I just had to initialize the variables outside the loop, which is what I did (by assigning the values of zero)

 

ok, I added that line in the beginning of the loop and now it works

thanks!

Werner_E
25-Diamond I
(To:CN_13593194)


@CN_13593194 wrote:

you're right, I am not used to having a number being changed to zero just because a condition is not met, it does not make sense to me



There is no number changed to zero because of a condition not met!

Again - variables which are defined globally in front of a program can be used but only ON THE RIGHT HAND SIDE of a definition and ONLY IF THERE IS NO LOCAL VARIABLE WITH THE SAME NAME.

Variables you use at the left hand side of an assignment ALWAYS are local and once you use (anywhere) a variable on the left hand side of an assignment you create a local variable and any global variable with the same name is hidden, overrules by the local one. You cannot access the global variable in any way from within the program.

In your program you have assignments where t1, t4, etc. are used on the left hand side and so they are considered local variables which have nothing to do with the global variables of the same name (unless you assign the local variables the values from the global variables which is what the first line in my fix does.

Without that initialisation if you use t4 somewhere in one on branches you have in your program before it actually got assigned a value, not the value of the global t4 is used but rather Prime initialises that variable with a dimensionless zero - hence the unit mismatch error.

 

Incidentally, I have already noticed several times that although this logic works in principle, it has not always been implemented consistently and coherently in Prime, so that there are recurring illogical effects that contradict this logic.

Consequence: Avoid the usage of global variables in programs. If you need to hand the value of worksheets variables to a program to use, make the program a function with these variables as arguments.

What should be avoided in any place is using global and local variables with the same name or using uninitialized variables for further calculations.

 

thank you for the detailed explanation. I will try my best to do this from now on: What should be avoided in any place is using global and local variables with the same name or using uninitialized variables for further calculations

Announcements


Top Tags