[HELP!!!!] Run fmincon repeatedly

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);

Réponses (1)

Alan Weiss
Alan Weiss le 25 Août 2014

0 votes

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

Question posée :

JJ
le 24 Août 2014

Community Treasure Hunt

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

Start Hunting!

Translated by