Get Help

Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Community
- :
- PTC Mathcad
- :
- PTC Mathcad
- :
- Re: Using Minimize in Program Loop

Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

11-29-2005
03:00 AM

11-29-2005
03:00 AM

Using Minimize in Program Loop

I am analysing a system defined by a set of parameters, and I need to analyse it for an array of these parameters.

I want to use Minimize to find an array of minimum values of a function defined by the array of parameters. I also want to use the result found on one set of parameters as an initial guess for the next use of Minimize on the next set.

My original worksheet is too long to post (and to calculate), but I have attached an 11.2a sheet that I think shows the essence of what I want to do. If it's unclear, please say so.

Please don't worry about the details of the problem given here: it's a madeup example that might not work all the way through. It's the principle of how to do it that I'm after.

I'd be grateful for any assistance.

Jon

I want to use Minimize to find an array of minimum values of a function defined by the array of parameters. I also want to use the result found on one set of parameters as an initial guess for the next use of Minimize on the next set.

My original worksheet is too long to post (and to calculate), but I have attached an 11.2a sheet that I think shows the essence of what I want to do. If it's unclear, please say so.

Please don't worry about the details of the problem given here: it's a madeup example that might not work all the way through. It's the principle of how to do it that I'm after.

I'd be grateful for any assistance.

Jon

Labels:

11 REPLIES 11

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

11-29-2005
03:00 AM

11-29-2005
03:00 AM

Using Minimize in Program Loop

On 11/29/2005 5:38:55 AM, jonareynolds wrote:

>I want to use Minimize to find an array of minimum values of a function defined by the array of parameters. I also want to use the result found on one set of parameters as an initial guess for the next use of Minimize on the next set.

Do you mean something like the attached, Jon?

If you want to vary a parameter in a function, you've got to be able to pass it to the function.

Stuart

>I want to use Minimize to find an array of minimum values of a function defined by the array of parameters. I also want to use the result found on one set of parameters as an initial guess for the next use of Minimize on the next set.

Do you mean something like the attached, Jon?

If you want to vary a parameter in a function, you've got to be able to pass it to the function.

Stuart

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

11-29-2005
03:00 AM

11-29-2005
03:00 AM

Using Minimize in Program Loop

Stuart,

Thank you. That works fine. I modified it to use a short program, and that was fine too.

I then went back to my real problem, and couldn't make it work. I stripped the worksheet down to just the bare bones, and attempted to implement the solution.

The attached shows part of the problem. It seems to work if I declare the parameters A & B explicitly (Case 1), but not if I pass them at the function call (as C & D, Case 2). I can't see why.

Jon

Thank you. That works fine. I modified it to use a short program, and that was fine too.

I then went back to my real problem, and couldn't make it work. I stripped the worksheet down to just the bare bones, and attempted to implement the solution.

The attached shows part of the problem. It seems to work if I declare the parameters A & B explicitly (Case 1), but not if I pass them at the function call (as C & D, Case 2). I can't see why.

Jon

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

11-29-2005
03:00 AM

11-29-2005
03:00 AM

Using Minimize in Program Loop

On 11/29/2005 8:59:35 AM, jonareynolds wrote:

>The attached shows part of the

>problem. It seems to work if

>I declare the parameters A & B

>explicitly (Case 1), but not

>if I pass them at the function

>call (as C & D, Case 2). I

>can't see why.

I found a solution, but it surprised me: you can pass extra parameters into the solve block if you define them for the function you're going to minimize.

So, if you're interested in minimizing h(x,y,z) but want to include some other variables (A,B) in your solve block, you can define a new function:

h2(x,y,z,A,B) = h(x,y,z)

Now you can pass A & B into the solve block while only minimizing on (x,y,z):

Given [...]

Fn(x,y,z,A,B) = Minimize(h2,x,y,z)

- Guy

>The attached shows part of the

>problem. It seems to work if

>I declare the parameters A & B

>explicitly (Case 1), but not

>if I pass them at the function

>call (as C & D, Case 2). I

>can't see why.

I found a solution, but it surprised me: you can pass extra parameters into the solve block if you define them for the function you're going to minimize.

So, if you're interested in minimizing h(x,y,z) but want to include some other variables (A,B) in your solve block, you can define a new function:

h2(x,y,z,A,B) = h(x,y,z)

Now you can pass A & B into the solve block while only minimizing on (x,y,z):

Given [...]

Fn(x,y,z,A,B) = Minimize(h2,x,y,z)

- Guy

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

11-29-2005
03:00 AM

11-29-2005
03:00 AM

Using Minimize in Program Loop

On 11/29/2005 11:18:55 AM, GuyBeadie wrote:

>I found a solution, but it surprised me:

>you can pass extra parameters into the solve block if you define them for the function you're going to minimize.

That certainly seems to be the case, although it doesn't affect some of the other solve block functions, such as minerr.

... I'm sure I've seen something about this somewhere or other ...

Stuart

>I found a solution, but it surprised me:

>you can pass extra parameters into the solve block if you define them for the function you're going to minimize.

That certainly seems to be the case, although it doesn't affect some of the other solve block functions, such as minerr.

... I'm sure I've seen something about this somewhere or other ...

Stuart

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

11-29-2005
03:00 AM

11-29-2005
03:00 AM

Using Minimize in Program Loop

On 11/29/2005 12:04:54 PM, stuartafbruff wrote:

>... I'm sure I've seen something about this somewhere or other ...

Why is it one can always find something 2 seconds after it's no longer necessary?

http://www.mathcad.com/support/item/04apr_tip3.asp

"In order to iterate on a parameter which is included in the constraint equations,**the objective function must be made a function of that parameter**. Here, we will define the objective function, Vol, as a function of the applied load, P, even though Vol does not directly depend on P."

(my emphasis)

Stuart

>... I'm sure I've seen something about this somewhere or other ...

Why is it one can always find something 2 seconds after it's no longer necessary?

http://www.mathcad.com/support/item/04apr_tip3.asp

"In order to iterate on a parameter which is included in the constraint equations,

(my emphasis)

Stuart

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

11-30-2005
03:00 AM

11-30-2005
03:00 AM

Using Minimize in Program Loop

Guy, Stuart,

Thanks for the advice. I've tried it and it works.

It's interesting to note that the solutions obtained from the two techniques are different, sometimes significantly so. Passing the parameters in the function call gives a larger value of the quantity to be minimised, and a less reasonable set of x, y, z answers (based on problem domain knowledge). (See attached file.)

I've tried adjusting the options on Minimize, and the intial guess values, with no success.

I know that the problem is fairly ill-conditioned, and maybe the extra rounding errors caused by the introduction of the extra parameters explains the differences.

Thanks again for the help.

Jon

Thanks for the advice. I've tried it and it works.

It's interesting to note that the solutions obtained from the two techniques are different, sometimes significantly so. Passing the parameters in the function call gives a larger value of the quantity to be minimised, and a less reasonable set of x, y, z answers (based on problem domain knowledge). (See attached file.)

I've tried adjusting the options on Minimize, and the intial guess values, with no success.

I know that the problem is fairly ill-conditioned, and maybe the extra rounding errors caused by the introduction of the extra parameters explains the differences.

Thanks again for the help.

Jon

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

11-30-2005
03:00 AM

11-30-2005
03:00 AM

Using Minimize in Program Loop

Your problem is not particularly ill-conditioned (you should be able to solve it analytically and compare the results). There is no additional rounding from the extra parameters. They have absolutely no effect on the evaluation of the function. And the results with the parameters set as worksheet variables and as function parameters are identical.

The problem is that you put the additional parameters first. But you have to remember that the names of the formal parameters in a function definition have absolutely no significance. When you minimize a function the variables to be minimized are matched to the function purely by position, not by name. The variables on which you are minimizing are passed as the first set of parameters. Any additional parameters follow. The difference you got in the result was because you were minimizing on a different set of parameters -- two of which had no effect on the value at all.

BTW, this has come up several times in this collaboratory, and there are several examples of how to do it posted.

� � � � Tom Gutman

The problem is that you put the additional parameters first. But you have to remember that the names of the formal parameters in a function definition have absolutely no significance. When you minimize a function the variables to be minimized are matched to the function purely by position, not by name. The variables on which you are minimizing are passed as the first set of parameters. Any additional parameters follow. The difference you got in the result was because you were minimizing on a different set of parameters -- two of which had no effect on the value at all.

BTW, this has come up several times in this collaboratory, and there are several examples of how to do it posted.

� � � � Tom Gutman

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

11-30-2005
03:00 AM

11-30-2005
03:00 AM

Using Minimize in Program Loop

Thanks, Tom. I had completely missed the significance of the positions of the parameters in the function call. Now it makes sense.

Jon

Jon

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

07-07-2014
11:31 AM

07-07-2014
11:31 AM

Re: Using Minimize in Program Loop

Dear All,

I need to use Minimize in a For loop and I can't find the solution from the previous posts.

Here is my case:

I have a reference matrix Mx and 2 other matricies My1 and My2. I want to find alfa's such that the error between Mx and alfa*My is minimized. QuadError() computes the quadratic error between Mx and alfa*My. Error() isolates the parameter alfa. I then want to run a loop alfa_and_error that finds alfa for the two matrices My.