fmincon function ( constrained nonlinear multi-variable optimization)

1 回表示 (過去 30 日間)
hadi
hadi 2012 年 5 月 21 日
the question is how could I calculate the nonlinear constraints in the mycon function (constraints function) in the fmincon syntax. for instance: the objective function is fun:
function f=fun(x)
main program(calculate the fun value);
f=fun;
end
and the nonlinear constraints function is:
function [c,ceq]=mycon(x)
main program (calculating the c1; c2;c3;c4;c5);
c=[max(c1);min(c2);max(c3);min(c4);c5];
ceq=[];
end
the problem is I keep getting the"undefined function or variable "c1" error.(!)

回答 (8 件)

Sean de Wolski
Sean de Wolski 2012 年 5 月 21 日
c1 to c5 aren't being passed into the mycon() function. Write a wrapper anonymous function that does this for you:
fmycon = @(x)mycon(x,c)
fmincon(....,@fmycon)
%edit you mycon function too look like this:
function mycon(x,c) %c(1) = c1... c(5)=c5;
More info:
  1 件のコメント
hadi
hadi 2012 年 5 月 21 日
yes but all C variables are the function of x.
so c1=fourier(x) and so on..c5=fun(x)
and they all have been defined within the mycon function.(in the main program)
so how could still known as undefined function or variable?!!!

サインインしてコメントする。


Titus Edelhofer
Titus Edelhofer 2012 年 5 月 21 日
Hi Hadi,
you will need to compute them again in the mycon function, i.e.,
function [c,ceq]=mycon(x)
c1 = fourier(x);
c2 = ...;
c=[max(c1);min(c2);max(c3);min(c4);c5];
ceq=[];
end
Titus

hadi
hadi 2012 年 5 月 21 日
Hi,
I have done the actual mycon code is as follows:
function [c,ceq]=mycon(x)
persistent x3 Tc xdata
global fourier
for t=1:100*Tc
f1(t,i)=fourier(x(1:11),t);
f2(t,i)=fourier(x(12:22),t);
f3(t,i)= fourier(x3,t);
f4(t,i)=fourier(x(23:33),t);
f5(t,i)=fourier(x(34:44),t);
end
c=[max(f1);1.8+min(f1);max(f2)-1.7;1.1-min(f2);max(f4);-1.4-min(f4);max(f5);-1.2-min(f5);
max(-Fgrrz);max(-Fgrlz)];
ceq=[];
end
the input variable is x which is a 1x44 vector.
but i still get undefined f1 error?!!!!

Walter Roberson
Walter Roberson 2012 年 5 月 21 日
In your code, x3 Tc xdata are "persistent". That means that they are initialized to [] at the time that the function is parsed, and that they retain whatever values you assign them between calls. Your code does not assign any value to any of them, so they will continue to retain their initial value of [] .
100 times the empty array (100*Tc) is the empty array, and "for t = 1 : []" does no iterations at all, so you never assign a value to "f1".

hadi
hadi 2012 年 5 月 21 日
thanks, so How could I access to the variable which define in the worksapce and I want to use them in the mycon function?!( I thought by persistent syntax I will be able to make them accessible within the function?!) the question now is by which syntax I will be able to do this?
  1 件のコメント
Sargondjani
Sargondjani 2012 年 5 月 21 日
normally one would pass them as function arguments/parameters. can't you do that?
function [c,ceq]=mycon(x,x3,Tc,xdata)
and then to pass into fmincon:
my_con=@(x)mycon(x,x3,Tc,xdata);
also i dont see where mycon will get the values Fgrrz and Fgrlz from (should be function inputs maybe?)
if you have some intensive calculations that are shared between the objective and the constraint, then there is a solution to share the intermediate result between them:
http://www.mathworks.nl/matlabcentral/newsreader/view_thread/269936#872268

サインインしてコメントする。


hadi
hadi 2012 年 5 月 23 日
Yes, I should run the main program for each X however I am not sure whether the objective function or constraints function is the first one to be called in fmincon syntax. and I want to share the specific parameters between them. and the second question is: there are x1 to x26 input variables for the main program how could I input them from the workspace. can I do it by global syntax?!

Sargondjani
Sargondjani 2012 年 5 月 23 日
about the input variables: dont use them as global variables (as a rule,only in special cases use global variables). i dont know what you mean with 'input from workspace'. do you mean you are working from the command line??? (i wouldnt do this, ever, unless for some simple tests). either way, you have two basic options (i think):
- save workspace and load in main program
- make your main program a function and add the x1-x26 as input arguments in main program
The advantage of making your main program a function as well, is that you can more easily use subfunctions: you can define them after your main function, however you will have to save the output to get the results (i think). so you could have:
function mainfunction(X);
save ...
end;
%%sub function1: objective
function y=fun_objective(x,para1,para2,para3);
y=....
end
I almost always program like this, but i dont know if that is common practice.
then about the first part of your question. im not sure what you mean, but let's say you have parameters: para1, para2,para3, and they are scalars, stored in vectors (with all of them having the same size). you should define functions somewhere (as subfunction or mfiles or anonymous):
function y=fun_objective(x,para1,para2,para3);
function [c,ceq]=fun_constraint(x,para1,para2,para3);
now you can loop over the parameters like this:
for ip=1:length(para1);
f_obj=@(x)fun_objective(x,para1(ip),para2(ip),para3(ip));
f_con=@(x)fun_constraint(x,para1(ip),para2(ip),para3(ip));
[x(ip,:,y(ip,1)]=fmincon(f_obj,x0,[],[],[],[],[],[],f_con,options);
end
if this is not similar to what you want, then please be more precise...

hadi
hadi 2012 年 5 月 23 日
what I need to do is optimizing (by fmincon syntax) the energy (objective function) so I have to calculate the energy by main program. so I have define the objective function as follows: function E=Energy(x) Emt=0; load(x3,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15n,x16n,x17n,x18n,x19n,x20n,x21n,x22,x23n,x24n,x25,x26); main program; E=Emt; end and each of the inputs (x3..x26) are 11 column vectors(11x1) and I want to let the optimizer to predict the x1,x2,x4 and x5 so how could I make the x variable for the objective function from them. by the way the mainprogram is a m file whether will it be matter to make it function or not ?! Thanks a lot

カテゴリ

Help Center および File ExchangeSolver Outputs and Iterative Display についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by