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

Looping with Local Variables

DAO
1-Newbie
1-Newbie

Looping with Local Variables

Apparently I don't understand local variables very well. In the code below, I'm trying to iterate the minimization function using solutions from the previous iteration. After the initial iteration, the values are not updated. Can someone please tell me what I'm doing wrong?

MathCad.jpg

1 ACCEPTED SOLUTION

Accepted Solutions
Werner_E
24-Ruby V
(To:DAO)

DAVID OLSON wrote:

In fact, if you manually replace the initial guess with the solution values (i.e. copy the values from y into x from the image above) and repeat you will get a better answer.

Thats not a rule. Most of the time you will get the same result again (within the set tolerance values). If the guess values are far away from the solutions or for very "flat" functions you might get an improvement by iteration. You may try if you can achieve a similar effect by tweaking the global tolerence variables TOL and CTOL. Sometimes feeding the result of the first call of minimize again would yield a slightly different, but not necessarily a better result.

You don't show the improvement you may achieve manually in your picture. Afte you have derived y (no matter if with or without iteration) write z:=Minimize(S.r,y). Now how much differ S.r(y) and S.r(z) ??

According to the documentation for the minimize function, the value of the variable(s) to be optimized that are passed to the function are used as initial guesses

No, thats not written that way in the help (while I admit it could be interpreted that way). The help explains that those arguments are the variables to optimize and that you have to define guess values for them before. It does not tell you that these guess values are passed when you call Minimize.

You can easily find by experiment (as you won't find it in the docs) that the guess values for Minimize must be defined globally - as I had already written no local variables are allowed here. Therefore you don't see any effect of your iteration, as Minimize() would always take the global x as guess values.

min1.png

As I wrote in my first post you can turn mimize into a function which has the guess value as argument. This function can be used inside a program the way you intend.

min2.png

As Richard had shown in a recent thread its even possible to make the function f itself an argument of Minimize_f(). Its a bit trick, but possible.

(I've copied a picture below because there are a number of Excel spreadsheets that are actually the sources of raw data for the MathCad worksheet.)

In that case you may consider creating a simplified, smaller sample sheet to show your question and post it for us to have something to play around with.

View solution in original post

3 REPLIES 3
Werner_E
24-Ruby V
(To:DAO)

Why do you think minimize() will do any better when it is called more than once? After all minimize() itselft is sort of an iteration.

So for whatever it may be worth: minimize() will not use a local variable as guess value at all. If you really insist on what you are trying to, you would have to turn minimize() into a function with the guess value(s) as argument.

BTW, its always better to attach the worksheet rather than just showing a picture.

DAO
1-Newbie
1-Newbie
(To:Werner_E)

According to the documentation for the minimize function, the value of the variable(s) to be optimized that are passed to the function are used as initial guesses. In fact, if you manually replace the initial guess with the solution values (i.e. copy the values from y into x from the image above) and repeat you will get a better answer. (I've copied a picture below because there are a number of Excel spreadsheets that are actually the sources of raw data for the MathCad worksheet.)MathCad2.jpg

Werner_E
24-Ruby V
(To:DAO)

DAVID OLSON wrote:

In fact, if you manually replace the initial guess with the solution values (i.e. copy the values from y into x from the image above) and repeat you will get a better answer.

Thats not a rule. Most of the time you will get the same result again (within the set tolerance values). If the guess values are far away from the solutions or for very "flat" functions you might get an improvement by iteration. You may try if you can achieve a similar effect by tweaking the global tolerence variables TOL and CTOL. Sometimes feeding the result of the first call of minimize again would yield a slightly different, but not necessarily a better result.

You don't show the improvement you may achieve manually in your picture. Afte you have derived y (no matter if with or without iteration) write z:=Minimize(S.r,y). Now how much differ S.r(y) and S.r(z) ??

According to the documentation for the minimize function, the value of the variable(s) to be optimized that are passed to the function are used as initial guesses

No, thats not written that way in the help (while I admit it could be interpreted that way). The help explains that those arguments are the variables to optimize and that you have to define guess values for them before. It does not tell you that these guess values are passed when you call Minimize.

You can easily find by experiment (as you won't find it in the docs) that the guess values for Minimize must be defined globally - as I had already written no local variables are allowed here. Therefore you don't see any effect of your iteration, as Minimize() would always take the global x as guess values.

min1.png

As I wrote in my first post you can turn mimize into a function which has the guess value as argument. This function can be used inside a program the way you intend.

min2.png

As Richard had shown in a recent thread its even possible to make the function f itself an argument of Minimize_f(). Its a bit trick, but possible.

(I've copied a picture below because there are a number of Excel spreadsheets that are actually the sources of raw data for the MathCad worksheet.)

In that case you may consider creating a simplified, smaller sample sheet to show your question and post it for us to have something to play around with.

Top Tags