5
$\begingroup$

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.
$\endgroup$
  • $\begingroup$ You could notice that, from the equality constraint, $x_2$ can be expressed as a function of $x_1$ and $b$ and that the inequality constraint defines the range for $x_1$. I suppose that this could make your problem easier. $\endgroup$ – Claude Leibovici Aug 25 '14 at 6:38
1
$\begingroup$

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
$\endgroup$
  • $\begingroup$ how should I modify "objfun" m-file to include the b input? $\endgroup$ – kuku Oct 26 '14 at 2:42
  • $\begingroup$ please see my update. $\endgroup$ – kuku Oct 26 '14 at 2:47
  • $\begingroup$ @kuku Can you include the code used to define the function handle and the call to fmincon? $\endgroup$ – AnonSubmitter85 Oct 26 '14 at 20:30
  • $\begingroup$ Is it already in my description ? That would be step number 1 and step number 3 of my question description $\endgroup$ – kuku Oct 26 '14 at 21:02
  • $\begingroup$ Sorry. My answer should have passed in cfun to fmincon and not @objfun. I fixed it. Let me know if this works or not. $\endgroup$ – AnonSubmitter85 Oct 27 '14 at 1:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.