Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
simple optimzation function syntax question

Subject: simple optimzation function syntax question

From: Marco Wu

Date: 30 Sep, 2011 20:22:10

Message: 1 of 5

Dear all,

==Background==

 I have a function [c, ceq] = pulley4constraint(z) {ceq always equal to zero}. Basically, there are a few inequalities in this function to create the output array 'c', and two of those elements are flags only.

 When I calculate the flags as the output 'c' in pulley4constraint(z), the computer needs to run for a long for loop to determine the flag. ** The cost value for the optimization is in another function called function cost = pulley4cost(z) which also need to run the same for loop and only add one extra steps to calculate the cost.

==Question===
My question is that normally I will use the following line to do the optimization
[x,fval] = fmincon(@pulley4cost,x0,[],[],[],[],[],[],@pulley4constraint,options);

If I make the [c, ceq] = pulley4constraint(z) has ONE more output and become [c, ceq, new_output] = pulley4constraint(z) , how I should change this following code to start the optimization ??
[x,fval] = fmincon(@pulley4cost,x0,[],[],[],[],[],[],@pulley4constraint,options);

Thanks

Subject: simple optimzation function syntax question

From: Matt J

Date: 1 Oct, 2011 02:17:15

Message: 2 of 5

"Marco Wu" wrote in message <j658dh$5v1$1@newscl01ah.mathworks.com>...
>
 make the [c, ceq] = pulley4constraint(z) has ONE more output and become [c, ceq, new_output] = pulley4constraint(z) , how I should change this following code to start the optimization ??
> [x,fval] = fmincon(@pulley4cost,x0,[],[],[],[],[],[],@pulley4constraint,options);
===============

If you want FMINCON to call pulley4constraint with extra output arguments, the only way to make this happen is to activate the GradConstr option,

options = optimset('GradConstr','on', ...);

However, if you do this, FMINCON will interpret new_output as the analytically computed gradient of c. Is this what you intend? Read more about this here

http://www.mathworks.com/help/toolbox/optim/ug/brhkghv-11.html#brhkghv-16

Subject: simple optimzation function syntax question

From: Matt J

Date: 1 Oct, 2011 02:18:30

Message: 3 of 5

"Marco Wu" wrote in message <j658dh$5v1$1@newscl01ah.mathworks.com>...
>
 make the [c, ceq] = pulley4constraint(z) has ONE more output and become [c, ceq, new_output] = pulley4constraint(z) , how I should change this following code to start the optimization ??
> [x,fval] = fmincon(@pulley4cost,x0,[],[],[],[],[],[],@pulley4constraint,options);
===============

If you want FMINCON to call pulley4constraint with extra output arguments, the only way to make this happen is to activate the GradConstr option,

options = optimset('GradConstr','on', ...);

However, if you do this, FMINCON will interpret new_output as the analytically computed gradient of c. Is this what you intend? Read more about this here

http://www.mathworks.com/help/toolbox/optim/ug/brhkghv-11.html#brhkghv-16

Subject: simple optimzation function syntax question

From: Marco Wu

Date: 1 Oct, 2011 03:05:25

Message: 4 of 5

Actually, I need the information of a local variable TEMPVALUE from the " [c, ceq] = pulley4constraint(z)" because if the 'z' is feasible solution, the function pulley4cost could be calculated as TEMPVALUE*constant. DO you think that I can use a GLOBAL variable for TEMPVALUE to pass the information across two functions? If I can past the information across function, the function "pulley4cost" does not need to run through the exact same long for loop in the pulley4constraint(z).











"Matt J" wrote in message <j65t9m$h01$1@newscl01ah.mathworks.com>...
> "Marco Wu" wrote in message <j658dh$5v1$1@newscl01ah.mathworks.com>...
> >
> make the [c, ceq] = pulley4constraint(z) has ONE more output and become [c, ceq, new_output] = pulley4constraint(z) , how I should change this following code to start the optimization ??
> > [x,fval] = fmincon(@pulley4cost,x0,[],[],[],[],[],[],@pulley4constraint,options);
> ===============
>
> If you want FMINCON to call pulley4constraint with extra output arguments, the only way to make this happen is to activate the GradConstr option,
>
> options = optimset('GradConstr','on', ...);
>
> However, if you do this, FMINCON will interpret new_output as the analytically computed gradient of c. Is this what you intend? Read more about this here
>
> http://www.mathworks.com/help/toolbox/optim/ug/brhkghv-11.html#brhkghv-16

Subject: simple optimzation function syntax question

From: Matt J

Date: 1 Oct, 2011 03:17:29

Message: 5 of 5

"Marco Wu" wrote in message <j6601l$ojs$1@newscl01ah.mathworks.com>...
> Actually, I need the information of a local variable TEMPVALUE from the " [c, ceq] = pulley4constraint(z)" because if the 'z' is feasible solution, the function pulley4cost could be calculated as TEMPVALUE*constant. DO you think that I can use a GLOBAL variable for TEMPVALUE to pass the information across two functions? If I can past the information across function, the function "pulley4cost" does not need to run through the exact same long for loop in the pulley4constraint(z).
=====================

Here is a thread that talks about passing information between constraint and cost functions,

http://www.mathworks.com/matlabcentral/newsreader/view_thread/269936

You could do similar things with global variables, but that's messier. Bear in mind also that you cannot know the order in which the functions are executed by FMINCON. You talk as if you think pulley4constraint will be executed first, generating TEMPVALUE, and that pulley4cost will be executed afterward. You cannot count on this, however.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us