fmincon function ( constrained nonlinear multi-variable optimization)
2 次查看(过去 30 天)
显示 更早的评论
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.(!)
0 个评论
回答(8 个)
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:
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
0 个评论
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".
0 个评论
hadi
2012-5-21
1 个评论
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
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...
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with Optimization Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!