[HELP!!!!] Run fmincon repeatedly

1 次查看(过去 30 天)
JJ
JJ 2014-8-24
回答: Alan Weiss 2014-8-25
Hi all
I am trying to minimize the objective function over (x(1),x(2)):
exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1)+b
subject to constraint
(x(1))^2+x(2)-1+b=0, (notice that b is also in constraint)
-x(1)*x(2)-10<=0.
I want to run the optimization 20 times , for b=1, b=2, b=3.....b=20 ( because b is between 0 to 20). I also want to store the optimal (x(1),x(2),b) so that I can plot it later.
There are existing reference ((<http://www.mathworks.com/help/optim/ug/nonlinear-equality-and-inequality-constraints.html)>) which I followed. How do I modify the following code so that it will do the above?
Step 1: Write a file objfun.m.
function f = objfun(x)
f = exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1)+b;
Step 2: Write a file confuneq.m for the nonlinear constraints.
function [c, ceq] = confuneq(x)
% Nonlinear inequality constraints
c = -x(1)*x(2) - 10;
% Nonlinear equality constraints
ceq = x(1)^2 + x(2) - 1+b;
Step 3: Invoke constrained optimization routine.
x0 = [-1,1]; % Make a starting guess at the solution
options = optimoptions(@fmincon,'Algorithm','sqp');
[x,fval] = fmincon(@objfun,x0,[],[],[],[],[],[],...
@confuneq,options);

回答(1 个)

Alan Weiss
Alan Weiss 2014-8-25
You might want to consult the documentation on passing extra parameters. Either anonymous functions or nested functions will work for your case.
Alan Weiss
MATLAB mathematical toolbox documentation

类别

Help CenterFile Exchange 中查找有关 Solver 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