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

Community Tip - Did you know you can set a signature that will be added to all your posts? Set it here! X

Returning multiple variables from a while loop

efarquharson
1-Visitor

Returning multiple variables from a while loop

Hi,

I have been trying to get a while loop to augment multiple matrices, previously defined globally,  until convergence is reached. I then want to access these matrices outside of the loop to perform further calculations. I have been having no luck, and am not even sure if I have defined the loop correctly to draw from the global variables. If you have any ideas, i would be incredibly appreciative.
Thanks so much,

7 REPLIES 7
LucMeekes
23-Emerald III
(To:efarquharson)

Your function Convergence() takes alot of parameters, but it is ill defined.

Success!
Luc

Great, Thanks Luc.

If I were to create this loop without parameters, would I be able to access the global variables from within the loop? Would I have to locally assign them to another variable within the loop?

Thanks so much,

LucMeekes
23-Emerald III
(To:efarquharson)

In programs, Mathcad works with variables much like ordinary programming languages do.

If you use a variable, and it is defined (and has a valid value) within the program, that value will be used.

If it is not defined within the program, Mathcad will use a global variable (defined left/above the start of the program) if it exists.

If it can't find the variable in either way, an error results. Here is a simple example of a function that results in a value dependent on a global, and a local variable and a parameter.

Now if you change the function like this:

You see that the global parameter is not used, but it still exists outside of the function.

Success!
Luc

Thanks so much again, Luc. I really can't tell you how much I appreciate your help.

I am coming across another error that is making me think this loop isn't taking my global variables:

Capture55.JPG

Mathcad isn't recognizing b_uc_cp as a matrix, but if i check its value outside:

Capture77.JPG

It seems to be a matrix.

Do you have any idea what might be going on?

Thanks so much again

for some reason, if I call each variable within the loop it works.

Weird.
Thanks so much again luc!

LucMeekes
23-Emerald III
(To:efarquharson)

The b_uc_cp matrix has a unit 'm' (it's a unit, that's why it is coloured blue)

{And to comply with ISO standards it should be upright (roman) font instead of slanted (italic) font, but that's another discussion}

In the condition for the while loop, you call the max() function, the first argument to that is 0.1*m, where 'm' is not displayed as a unit. So I guess Prime takes 'm' to be a variable. Change the 'label' of that 'm' to be 'unit' and see if that helps.

To change the 'label', put the cursor on the 'm' and choose from the menu:

Success!
Luc

LucMeekes
23-Emerald III
(To:efarquharson)

Then, in the first iteration, the condition for the while loop should come out as:

so:

The result '1' here means true, so the body of the while statement is executed.

It would also be executed if

Are you sure that's what you want?

Luc

Announcements

Top Tags