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

Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X

Modifying an external array within a loop

vgainov
1-Newbie

Modifying an external array within a loop

Why it does not work? (MathCad 14)

9 REPLIES 9

Fibbonachi.png

Please attach your worksheet (upper left - "Use advanced editor")

What does "not work".

Did you expect the nested for-loops to have any effect on the worksheet variable g31? Is hasn't.

Inside program structures everything on the left of a local assignment is treated as a local variable, so the variable g31 you change in your program has nothing to do with the WS variable you obviously wish to change.

Solution: Assign the program a variable by writing "ge31:=for j ....". Then as the first line of your program write "g31<-- g31". While this may look silly, keep in mind, that the left g31 is the uninitialized local variable, whereupon the right g31 is the worksheet variable. As from now on a local g31 exists, MC would use this local variable on the RHS of the subsequent local assigments, too.

At the end of your programm add "return g31"

This should do the job

In short: You cannot change a worksheet variable from within a program. Only way to do is to assign the program result to that variable.

Thank you very much, Werner, for your reply! Did you mean the following code (see attached fibbonachi_test.xcmd)?

Unfortunately, It still doesn't calculate a Fibbonachi sequence in each column.

Actually, I'm debugging a more complex program, and I tried to sort out with the issues using a simple example.

In another attached worksheet there is vector-function initialised by the program block, which contains a long while loop. Since certain moment of writing the program it has been beginning to generate an exception coloring the whole loop by red. Can you advice any proper way, how to debug such a complex code row by row in MathCAD?

Thank you very much, Werner, for your reply! Did you mean the following code (see attached fibbonachi_test.xcmd)?

Basically, yes.

Unfortunately, It still doesn't calculate a Fibbonachi sequence in each column.

There seemed to be a confusion with number one and lower case L in the otherwise statement. If you correct that it works.

In case of your test example you would not have to define g31 before the program and you would not need the g31<--g31 statement, because all elelements of the matrix are calculated and assigned by your program (see attached).

If your program would change only selected cells you will have to do as advised earlier.

As for your more complex file.

Debugging a larger program is not really comfortable in Mathcad.

First you should use right-click, Trace Error to find the origin of the error.

Then the only aids are toggling Debug and using the commands "pause" and "trace", the latter you have already used as I see.

The error in your program is because in the very first iteration step most values in your n2.old are zero and you get a divison by zero at the calculation of "cond". I trapped that error using "on error" and set cond to 1000 to keep the iteration running.

The iteration will stop after 9 iterations, so k is 9 and the matrices Pp1, Pp2 and Ps will have 11 columns. In your return vector you used the column selector k+2 which throws an index out of range error. I changed it to k+1.

I have included some comments in red and added some trace stetements to help me in the debugging - feel free to delete them.

EDITED: Added some more comments

Thanks a lot Werner, it was really huge help! Now it works. I've corrected the rest mistakes related to index mismatching. I included some comments in green about it, if it's interesting for you, I attach the new file here.

The problem is that when I transferred this file from my home laptop to office's computer the same exception as discribed in my previous post arised - coloring by red the whole while loop. It concerns both directly your file and my later corrected version, although both of them were working at my laptop. Trace Error function doesn't give any advise for position of error, and trace statements inside the loop doesn't give any output, it looks like the program sequence doesn't even enter the loop.

It seems that there may be some differences in Mathcad preferences, because I installed these v.14 from different distribs. Can you give any advise, where to look for possible mistake in preferences?

Glad you got it to run.

I am using MC15 and from the very beginning the only error I encounterd with your original file was the region which throwed the divison by zero.

If your whole program is red, there should be at leas an error message at the end if you click on it - what is it?

I can not think of any preference setting being responsible for the difference of behaviour on different installations.

The "Worsheet settings" are going along with the worksheet and should therefore be the same everywhere.

And the rest of the settings in "Preferences" do not affect calculations, as far as I see.

The only situation I know when a whole program gets red is when I interrupt the calculation via Esc-key.

EDIT: I found a setting which affects calculation, but its a worksheet option and it does not make the whole prog red.

Its under "Worksheet options"/Compatibility/"Local assignment results". If its set to MC11 (which I think is preferrable as MC12 was an inferiour version anyway) an error is thrown at the calculation of ny. I would correct that calculation anyway to clearly state what the prog should return. The "return" is not necessary, the ny is

ny.png

There is not the whole program in red, but the whole while loop. I use russian localized version, that's why I'm not sure about correct translation to English. The description of the error is "Destination of a call generated an exception".

Vladimir Gainov wrote:

There is not the whole program in red, but the whole while loop. I use russian localized version, that's why I'm not sure about correct translation to English. The description of the error is "Destination of a call generated an exception".

I remember I had a similar error message quite some time ago but I don't remember in which context and what caused the error.

I still don't think thats it because of different preferences and as I presume that MC is installed correctly on your office's computer there is not much what can be different. What about memory? But then, I worked on a comp with just 1 GB of mem and it worked and I guess there aren't much machines around with less.

Top Tags