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 2, 2025

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.