Skip to main content
10-Marble
June 27, 2025
Solved

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

  • June 27, 2025
  • 2 replies
  • 1377 views

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

Best answer by terryhendicott

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

2 replies

23-Emerald IV
June 27, 2025

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

netoprax10-MarbleAuthor
10-Marble
June 27, 2025

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

 

21-Topaz II
June 27, 2025

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

Capture.JPG

23-Emerald IV
June 27, 2025

"so nothing is done except set tj."

 

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

 

Luc

25-Diamond I
July 3, 2025

@netoprax 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.