Glam Prestige Journal

Bright entertainment trends with youth appeal.

$\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 (()) 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$ 1

1 Answer

$\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$ 5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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