Matlab: need help with optimization

2k Views Asked by At

Minimize the following objective function over $(x_1,x_2)$

$$f (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 the constraints

$$x_1^2 + x_2 = 1 - b \qquad \qquad -x_1 x_2 \leq 10$$

Note that $b$ is also in one of the constraints.


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

Update:

I tried to modified my objective function by adding the b,

function f = objfun(x,b)
f = exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1)+b;

and I got

    Error using objfun (line 2)
    Not enough input arguments.Error in fmincon (line 640)


    initVals.f = feval(funfcn{3},X,varargin{:});
    Caused by:
    Failure in initial user-supplied objective function evaluation. FMINCON cannot     continue.
1

There are 1 best solutions below

5
On BEST ANSWER

I haven't tested this, but something like it should work.

First, modify objfun so that b is an input arg. Then try this:

cnt = 1;
xopt = [];
fval = [];
for b = 1 : 20

  cfun = @(x)objfun(x,b);
  [xopt(cnt),fval(cnt)] = fmincon(cfun,[-1 1],[],[],[],[],[],[],... 
    @confuneq,options);

  cnt = cnt + 1;

end