MATLAB: Asking for help with syntax for fmincon

fminconoptimization

Hi all,
I am trying out the standard given examples in MATLAB for fmincon usage. I am referring to this particular example:
openExample('optim/NonlinearConstraintsExample')
fun = @(x) 100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
lb = [0,0.2];
ub = [0.5,0.8];
function [c,ceq] = circlecon(x)
c = (x(1)-1/3)^2 + (x(2)-1/3)^2 – (1/3)^2;
ceq = [];
A = [];
b = [];
Aeq = [];
beq = [];
x0 = [1/4,1/4];
nonlcon = @circlecon;
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)
I want to modify it to have multiple variables separately in separate vectors but still be able to solve the final problem. For example I make the following modification:
fun = @(x,y) 100*(x(2)-x(1)^2)^2 + (1-x(1))^2 + 100*(y(2)-y(1)^2)^2 + (1-y(1))^2;
lb = [0,0.2,0,0.2];
ub = [0.5,0.8,0.5,0.8];
function [c,ceq] = circlecon(x,y)
c = (x(1)-1/3)^2 + (x(2)-1/3)^2 – (1/3)^2 + (y(1)-1/3)^2 + (y(2)-1/3)^2 – (1/3)^2;
ceq = [];
A = [];
b = [];
Aeq = [];
beq = [];
x0 = [1/4,1/4];
y0 = [1/4,1/4];
nonlcon = @circlecon;
Notice that I have introduced a new 'y' variable. I can't figure out how to correctly call the fmincon function to solve the problem.
Kindly advise please.
Regards

Best Answer

The solvers in Matlab accept only one vector as variable. So change y(1) --> x(3) and y(2) --> x(4). Then edit the code this way and it will work:
fun = @(x) 100*(x(2)-x(1).^2).^2 + (1-x(1)).^2 + 100*(x(4)-x(3).^2).^2 + (1-x(3)).^2;
lb = [0,0.2,0,0.2];
ub = [0.5,0.8,0.5,0.8];
A = [];
b = [];
Aeq = [];
beq = [];
x0 = [1/4,1/4,1/4,1/4];
nonlcon = @circlecon;
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)
function [c,ceq] = circlecon(x)
c = (x(1)-1/3).^2 + (x(2)-1/3).^2 - (1/3)^2 + (x(3)-1/3).^2 + (x(4)-1/3).^2 - (1/3)^2;
ceq = [];
end