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

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

Problem with a while loop

jesusmor
10-Marble

Problem with a while loop

Hi everyone,

 

I´m programming a plate heat exchanger, the flow begin in single-phase and when certain conditions are met, it change to twin-phase.

 

I write a while loop, it works and everything is correct.

 

The first problem comes when I try to introduce some more line and assign a new variable name to it, it fails, I have to reuse a variable name already used inside the loop, it is not solved by declaring the variable outside the program, neither in the program but outside the loop or inside the loop. The failure it gives is the following "" Ldtoken, Ldftn and Ldvirtftn OpCodes cannot have DynamicMethod as their destination ".

 

The second problem appears in the result array, certain variables fail when appearing together in the result array, only if there are two or more of them, one at a time they do not fail. I have observed that the variables that fail are those found from line 17 in the loop, the previous ones do not fail, and it does not matter if I previously declared them outside the program, inside the program but outside the loop or inside the loop. The only solution is to put just one of those variables.

 

I use MC14

 

It's weird, right?

 

Thanks for your help.

 

Greetings, Jesús

ACCEPTED SOLUTION

Accepted Solutions

Hi everybody,

 

One guy give me a operative 15th version, I have installed and both sheets run whitout problem.

 

Which is the reason? Who knows, MC mania.

 

Thanks a lot for your time, I´m glad to meet you and it won´t be the last time I´ll come here.

 

Kind regards!!

 

jesusmor_0-1588958389108.png

 

View solution in original post

19 REPLIES 19

 
LucMeekes
23-Emerald III
(To:jesusmor)

Please attach your worksheet.

Without it, there's little we can do.

 

Success!
Luc

You´re reason, sorry!!

 

I send the attachment

LucMeekes
23-Emerald III
(To:jesusmor)

Your sheet contains a couple of programming constructs at the bottom, and to the far right. It is very hard to see what works, and what doesn't. As far as I can see the one to the left, called prog() is red, so I guess it doesn't work.

Could you just put two complete programs, one that works, the other with one extra line (as you say) that doesn't.

 

Success!
Luc

Hi Luc, thanks for your interest.

Yes, this worsheet has two constructions (one at bottom and one at right) that I´ve been using for test, efectively the construction that doesn't work is the one on the left.

It´s good idea to prepare two worsheets, one that work and another doesn´t work, for compare.

I prepare them and I send them.

See you.

Hi Luc,

 

I send both worsheets.

 

Thanks for your time.

Can you provide a screenshot where you get an error (quote: "The failure it gives is the following "Ldtoken, Ldftn and Ldvirtftn OpCodes cannot have DynamicMethod as their destination")?

 

Pic_1.png

 

LucMeekes
23-Emerald III
(To:VladimirN)

Vladimir,

 

I see you get an answer from that file. I assume you're using Mathcad 15 (the OP is using 14).

Maybe you can save your file under a new name and the OP can open it, and it will hopefully be without that error...?

 

Luc

Here it is. I cut out all the contents and pasted it into a new file. It seems that this error was fixed in one of the service releases.

LucMeekes
23-Emerald III
(To:jesusmor)

I see you've swapped a few expressions in the While loop, but that shouldn't get you into trouble, since all variables are defined before they're used.

I'm sorry, I have no clue at this moment.

 

And I see that Vladimir can run the file without an error. Strange...

 

Success!
Luc

 Luc,

 

Here are the files for the 11th version.

LucMeekes
23-Emerald III
(To:VladimirN)

The Not-fail document fails in Mathcad 11 for two reasons:

1. The unit bar is not defined. After defining it to be 10^5 Pa, the following cause is found.

2. The second (the large) while loop in the program Prog() starts with a condition that uses the variable L.sp. That variable is undefined.

 

The Fail document fails for the same reasons.

 

Why oh why don't Mathcad 14 & 15 see this error?

 

 Anyway, make sure that L.sp is defined (a value) before it is used in the while loop.

 

Success1
Luc

Werner_E
25-Diamond I
(To:LucMeekes)


@LucMeekes wrote:

 

Why oh why don't Mathcad 14 & 15 see this error?

 


1) The unit "bar" IS defined - at least in Mathcad 15 and I guess in Mathcad 14 as well

 

2) The behavior wrt the undefined variable is indeed strange. I never noticed it but as the small test program below show its "standard" behavior in Mathcad 15 (and obviously also in Mathcad 14).

Werner_E_0-1588950771935.png

Mathcad sure should throw an error as of B being undefined when entering the loop, but it doesn't.
Replacing the while-loop with an if-statement gives he expected error, though:

Werner_E_1-1588950946865.png

 

 

LucMeekes
23-Emerald III
(To:Werner_E)

My remarks pertain to Mathcad 11 and the sheets under topic.

The unit bar is not defined (standard) in Mathcad 11. I don't know about 14 and 15, but I assumed it is defined in 14 (and then also in 15), because Jesus used it in more than one occasion.

Fortunately Mathcad 11 does not show the (14 and 15) weird behaviour with the test in a While statement.

Good that you can show this bug in a small example.

 

Luc

 

Werner_E
25-Diamond I
(To:jesusmor)

I, too, can confirm that both sheets run without any errors.
Mathcad 15, M050

Can you post a pic with the error message you get in the "fail" sheet and where the error traces back to (right click on the error and chose trace)

Thanks everybody!!

 

You guys found the trouble, good job!!.


But, the next question is: Why does MC14 fail?, doesn't it has solution?, should I change for the MC15?.

 

I don´t know if this change is possible, it is a work for an university.

 

Let´s going!!

LucMeekes
23-Emerald III
(To:jesusmor)

I don't see an immediate need to upgrade to Mathcad 15, as 14 and 15 are equally buggy with respect to this from my point of view.

 

But you should make sure that L.sp is given a value before the start of that large while loop.

If that solves your problem, then you can mark this thread as answered.

 

Success!
luc

Werner_E
25-Diamond I
(To:jesusmor)


@jesusmor wrote:

Thanks everybody!!

 

You guys found the trouble, good job!!.


No, obviousy we didn't as we don't experience any error using the versions of MC we use

 



But, the next question is: Why does MC14 fail?, doesn't it has solution?, should I change for the MC15?.

If your university can provide you a license for the current version of Mathcad for free it sure would be a good idea to upgrade to MC15. But if you have a personal license for MC14 and would have to pay to get MC15, I would rather stay with MC14 and wouldn't throw money at PTC.

@VladimirN  has posted here in the past ever so often the release notes for various Mathcad versions. These notes contain a list of bugs which were fixed in the new releases and which new feature were implemented. Maybe you are able to find these threads and can spot something there which may relate to the error message you experience.

 

Again: Can you post a screenshot showing the error message you experience in the "fail" sheet and can you try to trace back the error by right-clicking the red error, choosing "Trace Error" and then clicking on "First". Where does this procedure sends you to?

 

Its hard to say without seeing the error ourselves what the cause for MC14 failing may be.
It sure is bad programming to use a variable in the condition of the while-loop which is not defined when entering the loop the first time (as Luc had pointed out). But I can't say if thats the cause for your trouble as you had done this in the sheet which does not fail for you, too.

 

Hi everybody,

 

One guy give me a operative 15th version, I have installed and both sheets run whitout problem.

 

Which is the reason? Who knows, MC mania.

 

Thanks a lot for your time, I´m glad to meet you and it won´t be the last time I´ll come here.

 

Kind regards!!

 

jesusmor_0-1588958389108.png

 

Announcements

Top Tags