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

Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X

Minimize & Maximize in Mathcad 15 and Prime 3

ValeryOchkov
24-Ruby IV

Minimize & Maximize in Mathcad 15 and Prime 3

I have an error by using Minimize & Maximize in Mathcad 15.

MinMaxError.png

But all is OK in Prime 3

MinMax.png

Why?

1 ACCEPTED SOLUTION

Accepted Solutions

HARVEY HENSLEY wrote:

I know that this has been answered,

Yes, Valery again has found the Correct Answer himself 😉

Harvey, as already written I guess that the unit is responsible for the numeric algorithm taking larger steps and this may lead to inaccurate results. In our case it also seems to be responsible for the algorithm to try negative values for t2. But the unit is not responsible for Maximize to fail.

The real reason for Minimize to fail in this example is because Valery's function is returning a string for negative temperatures, Slightly change the function rho2 in your example above: just add "+0" at the end. This will not change any valid result but now for negative temperatures this function fails because it can't add a string and a number. Using this function makes Maximize() work - regardless of the unit °C.

I already showed this here http://communities.ptc.com/message/261839#261839 andI then delved a bit deeper into the subject here http://communities.ptc.com/message/261840#261840 and a new question grew up (why does "IsString()" not do the job?).

But the thread got quite confusing already and navigating in this forum isn't as easy and clearly as we were sed from the old collab.

Anyway, as a conclusion I would say that even if we can get it to work by omitting the string error message its the best idea to get rid of the unit nonetheless as you had shown in your first reply.

View solution in original post

27 REPLIES 27

Two changes are needed in the MC15 file. First, the Maximize function needs to be part of a Solve block. Second, it can't use units with the independent variable.Maximize.png

HARVEY HENSLEY wrote:

Two changes are needed in the MC15 file. First, the Maximize function needs to be part of a Solve block.

Thanks and sorry

The Maximize function needs to be part of a Solve block only in Prime 1 & 2.

See the top of my Mathcad 15 file.

And in Prime - no problem - see please second picture above

I think it is one units bug in Mathcad 15. In Prime - all is OK here with units!

Valery Ochkov wrote:

I think it is one units bug in Mathcad 15. In Prime - all is OK here with units!

But here is all OK:

kgm.png

It also works with pseudounit temperature. The result depends on how the function is deifined an how it deals with the temperature argument.

I think the last but one approach using the inverse /°C is the correct one but your function seems to use the last appraoch. Not sure about this but maybe the problems occurs because you function yiels error strings instead of numb ers in some cases.

1.png

Valery,

I was wrong about the use of units as Werner has shown. Your problem is probably due to the lack of bounds on the temperature. The density function probably can't obtain values for all values of temperature.

HARVEY HENSLEY wrote:

Valery,

I was wrong about the use of units as Werner has shown. Your problem is probably due to the lack of bounds on the temperature. The density function probably can't obtain values for all values of temperature.

As it look now the problem seems to be both, the units plus the range of temps - see my other posts.

I just tried a solve block with t.0>1°C but to no avail. The used algorithms will evaluate for negative temps nonetheless and fails because of the string result there.

Valery Ochkov wrote:

HARVEY HENSLEY wrote:

Two changes are needed in the MC15 file. First, the Maximize function needs to be part of a Solve block.

Thanks and sorry

The Maximize function needs to be part of a Solve block only in Prime 1 & 2.

See the top of my Mathcad 15 file.

And in Prime - no problem - see please second picture above

Your test with y(t) was incorrect. You used a constant for the independent variable.

Maximize_test.png

Sorry, I was wrong. The maximize function does work as you showed with the y(x) function. In my example, I didn't provide a guess for x.

So the help files are incorrect. They say that the Minimize and Maximize functions must be part of a Solve block, but the function being optimized can be specified outside the solve block. Instead, it should read that the Minimize and Maximize functions need to be part of a solve block if constraints on the independent variables are needed. Regardless, an initial guess is needed.

Our posts seem to have crossed.

The help for maximize and minimize is quite extensive. While at first it reads like you need a solve block, they later state:

  • If there are no constraints, the keyword Given is not necessary, and Maximize and Minimize may be used without a Solve Block, like a multivariable version of root. They still require guess variables.

Harvey, maybe I am missing your point.

Minimize is a function which can be either used in a solve block or standalone. In either case you need to provide a guess value for the independent variable and this would of course be a constant (including the correct unit, if needed).

The problem in Valery's sheet occurs obviously because of the way the "dimension" temperature is handled by Mathcad and used by Valery in his function. I am not sure if its a bug, a missing feature or just a wrong usage of temperature variables. Prime uses a different unit handling and checking and so acts differently - in this case more intuitively.

BTW, the workaround you privided in your first reply work without the solve block as well.

1.png

Huuuh!! Whats that?? A late Halloween trick? Sure something for your Mathcad Madhouse collection, Valery.

Just adding +0 to your function rho makes the Maximize() function work correctly.

Not sure but it may have something to do with the fact, that for invalid (neagtive) temperatures we get now an invalid result rather than the string which your function would return.

1.png

Werner Exinger wrote:

Huuuh!! Whats that?? A late Halloween trick? Sure something for your Mathcad Madhouse collection, Valery.

Yes. I think the problem is in my function wspD1PT (Region1 of the IAPWS-IF97 - water). Sometimes it returns string.

No problem with the wspDPT function (all regions):

Ro-Max.png

Valery Ochkov wrote:

Werner Exinger wrote:

Huuuh!! Whats that?? A late Halloween trick? Sure something for your Mathcad Madhouse collection, Valery.

Yes. I think the problem is in my function wspD1PT (Region1 of the IAPWS-IF97 - water). Sometimes it returns string.

You may consider throwing an error using the error() function instead of returning an error string as calculation result.

See the last regions in the attached file here: http://communities.ptc.com/message/261842#261842

No problem with the wspDPT function (all regions):

Yes, but you still may run into precision problems as of the units. See my other post on this subject in this thread.

OK, now it seems to be clear that the real problem is the string which the function returns in case of a negative temperature. When using units in numerical algorithm Mathcad usually uses different step sizes, different precision (we had this a few times in the past) and that may be the reason we are not running into this problem with Harvey's workaround whith a unitless independent variable.

When I replace Valery's error string by anything else - here a NaN but it could be 0 or even 1/0!! - Maximize works OK.

1.png

But ...... can anybody explain why maximize() still fails when I use IsString() instead of testing for the exact error string???

2.png

Werner Exinger wrote:

in case of a negative temperature.

Sorry.

There is no negative temperature - in World and in Mathcad.

There are Kelvin (Rankine) and Celsius (Farenheit) scales.

Valery Ochkov wrote:

Werner Exinger wrote:

in case of a negative temperature.

Sorry.

There is no negative temperature - in World and in Mathcad.

There are Kelvin (Rankine) and Celsius (Farenheit) scales.

We were not talking about absolute temperatures!

http://en.wikipedia.org/wiki/Negative_temperature

But ...

http://www.youtube.com/watch?v=yTeBUpR17Rw (Its nice to see someone explain things that enthusiastic!)

http://www.quantum-munich.de/research/negative-absolute-temperature/

Sorry or not, I still think you should use the error() function instead of returning a string.

Werner Exinger wrote:

We were not talking about absolute temperatures!

http://en.wikipedia.org/wiki/Negative_temperature

Sorry!

1. We are talking about negative temperature in this water problem.

2. http://en.wikipedia.org/wiki/Negative_temperature - it is one hypothesis. For me.

3. Thanks one more - I will change return "Error" to return error("Error) in my wsp cloud files.

Here is some additional baublery with Maximize and units.

Even if we may not call it a bug it sure can be called a very bad implementation. At least the reference (OK, there is no reference since M12) and the help should point out that the function should be used unitless.

1.png

I know that this has been answered, but I think this might be useful. I think the problem in MC15 is that the deg C unit is not truly a unit...it is a function. Note that my earlier suggestions changed the function so that the variable was unitless, and the deg C function was applied in the density argument. Also note that the only example involving deg C that Werner gave that got the right answer when the independent variable had deg C as a " unit" was the one that had deg C eliminated by cancelation of numerator and denominator.

Further, look at the following comparison:

maximize_deg_c_issue.png

Note that the lack of conctraints is not the issue as I had earlier suggested.. It appears that the difference is the independent arguement. Werner has shown that the independent variable can have units, but in this case deg C is not a unit. Apparently this got changed for Prime.

HARVEY HENSLEY wrote:

I know that this has been answered,

Yes, Valery again has found the Correct Answer himself 😉

Harvey, as already written I guess that the unit is responsible for the numeric algorithm taking larger steps and this may lead to inaccurate results. In our case it also seems to be responsible for the algorithm to try negative values for t2. But the unit is not responsible for Maximize to fail.

The real reason for Minimize to fail in this example is because Valery's function is returning a string for negative temperatures, Slightly change the function rho2 in your example above: just add "+0" at the end. This will not change any valid result but now for negative temperatures this function fails because it can't add a string and a number. Using this function makes Maximize() work - regardless of the unit °C.

I already showed this here http://communities.ptc.com/message/261839#261839 andI then delved a bit deeper into the subject here http://communities.ptc.com/message/261840#261840 and a new question grew up (why does "IsString()" not do the job?).

But the thread got quite confusing already and navigating in this forum isn't as easy and clearly as we were sed from the old collab.

Anyway, as a conclusion I would say that even if we can get it to work by omitting the string error message its the best idea to get rid of the unit nonetheless as you had shown in your first reply.

Werner Exinger wrote:

Harvey, as already written I guess that the unit is responsible for the numeric algorithm taking larger steps and this may lead to inaccurate results. In our case it also seems to be responsible for the algorithm to try negative values for t2.

I finally got caught up with all of your investigations and results. It looks like you have the answer regarding the temperatures below the triple point of water. I guess you meant in the quote above that the unit affects the step size because it affects the magnitude of the variable. In my function definition, the independent variable has a magnitude of one. In Valery's definition, the magnitude is 100 (~275 K). If the first step by Maximize is a constant percent of the initial guess, then the step sizes will be quite different for the two functions. Thus, the first step for Valery's function may go below the triple point. The comment you supplied elsewhere explored these differences with examples in lengths.

Now the question is "why does Prime get the answer"? I don't think it has anything to do with units. It must be a change in the optimization algorithm. Maximize and minimize algorithms have been changed to KNITRO methods. Probably a smaller initial step size is being used. Or, maybe the new algorithms handle the non number results in a manner like your demos.

I don't have an idea why it works in Prime.

Maybe it has to do with the KNITRO methods, maybe the different unit handling in Prime (dynamic unit checking) has something to do with.

I am not absolutely sure about that but I think we discussed here accuracy/precision problems with numeric algorithms when using units already a while ago and as far as I remember Prime was affected as well and Richard suggested some kind of relative tolerance being used which I guess would bot be easy to implement.

So it could also be possible that the successful operation of Valery's sheet in Prime has nothing to do with Primes way of handling units and Prime algorithm (so it may be KNITRO, as you guessed) simply treats the strings (which throw the error) like an invalid calculation, avoids going in this direction and keeps on solving like MC 15 does when it encounters soemthing like a division by zero or a NaN. This would require some experiments in Prime with functions which return strings eventually which I couldn't be bothered to do.

Sorry,

I have used in Prime 3 others wspDPT function without strings as an error mesage.

Valery Ochkov wrote:

Sorry,

I have used in Prime 3 others wspDPT function without strings as an error mesage.

Are you saying that the wspDPT function for Prime was different from the one for MC15?

HARVEY HENSLEY wrote:

Valery Ochkov wrote:

Sorry,

I have used in Prime 3 others wspDPT function without strings as an error mesage.

Are you saying that the wspDPT function for Prime was different from the one for MC15?

Yes. Sorry one more.

It was a cloud function wspD1PT with return strings and one function with same name from www.wsp.ru

We cannot use cloud function wsp in Prime 3.

It was my error not a Halloween trick.

See please the attach.

It was not a Halloween trick. but a Halloween day

Ro-Max.png

Why do I use a simple string not a function error?

In the first case, I immediately see the nature of the error.

In the second case I have to move the mouse cursor and click on the red box.

This is a glitch Mathcad. I think.

Ro-Max1.png

And I have forgot that in WaterSteamPro we have:

Regions.png

Valery Ochkov wrote:

I have an error by using Minimize & Maximize in Mathcad 15.

MinMaxError.png

It is more better not download any Mathcad-function on your computer but do a link on clowd Mathcad-function.

Wen I see an error in my cloud Mathcad-function (in wspD1PT for ezample) I correct it and now problem over the World

Compare pls old (above) and new (below) Mathcad worksheet with a reference on the cloud function:

wspDPT-new.png

Top Tags