Error in the following problem input(s): nonlcon: Not enough input arguments.

Hello I am trying to use the optimization toolbox in MATLAB and in particular fmincon.
my objective function is:
function f = circle(x)
for i = 1:2:5;
xi = x(i);
end
for j = 2:2:6;
xj = x(j);
end
f = (xi.^2 + xj.^2)^0.5;
my nonlinear constraints function is:
function [c, ceq] = disk(x)
x1 = x(1);
x2 = x(2);
x3 = x(3);
x4 = x(4);
x5 = x(5);
x6 = x(6);
c(1) = -(x6 - x4)^2 - (x5 - x3)^2 - 4; % non-convex constraints
c(2) = x3.^2 + x4.^2 - ((x1.^2 + x2.^2)^0.5 - 1)^2; %convex non-linear constraints
c(3) = x5.^2 + x6.^2 - ((x1.^2 + x2.^2)^0.5 - 1)^2;
ceq = []; % the empty equality constraints.
but i get the error message above
can anyone help me please
many thanks

7 Comments

Please note that your code
for i = 1:2:5;
xi = x(i);
end
is equivalent to
xi = x(5);
If that is what you want then please write it in the straight-forward way.
Perhaps what you want is
xi = x(1:2:5);
xj = x(2:2:6);
@ Walter Roberson. Please forgive my amateurness as I am totally new to MATLAB. what do you mean by 'show your call to fmincon'?
For now this is how I am proceeding.
once i open the optimization toolbox and select fmincon and the active set algorithm, in the box for objective function I am typing in @circle, the m-file I have created for the objective function (I made the adjustment you have suggested above) and in the box for nonlinear constraints I am typing in @disk, the m-file created for the nonlinear constraints.
Having made the adjustments xi = x(1:2:5); xj = x(2:2:6); and having pressed the start button, I am now getting the error:
Undefined function 'circle' for input arguments of type 'double'.
please can you assist me further
thank you in advance
I am also supplying the start point as either [0 0] or [1 1]
Please confirm that you stored your function circle in a file named circle.m and that you stored your function disk in a file named disk.m, and that both files are on your MATLAB path? At the command line, command
cd
which circle
which disk
to see where it thinks you are and where it thinks circle and disk are. You might need to cd to the directory that you stored circle.m and disk.m in.
Yes I have stored both functions correctly as circle.m and disk.m. I think the problem was the path of the circle m-file.
In the editor window i right clicked on the file and added the path and then when I ran the above that you suggested in the command window i get:
C:\Users\PK
C:\Users\PK\Desktop\MATLAB\circle.m C:\Users\PK\Desktop\MATLAB\disk.m
which I think looks ok
So when I run the optimization now I get the error message:
'Index exceeds matrix dimensions.'
:(
in fact i am sure that i am approaching this the wrong way. my problem is:
find the minimum radius of a larger circle (container) within which n unit circles (radius 1) fit, given that 1) no two unit circles overlap (tangent to ok) 2) all unit circles lie within the container (tangent to ok)
the code i am trying to write is for n=2
I need to go back to the drawing board. I am a mathematician with 0 programming skills!!

Sign in to comment.

Answers (1)

Your functions expect x to be a vector of length 6, but you are using a starting point which has only 2 elements. Your starting point needs to be the same length as the number of elements in the vector.

5 Comments

I've done that and now I am getting the error:
Inputs must be a scalar and a square matrix. To compute elementwise POWER, use POWER (.^) instead.
I think my problem is more fundamental. I am not defining the problem correctly
sure.
objective function m-file:
function f = circle(x)
xi = x(1:2:5);
xj = x(2:2:6);
f = (xi.^2 + xj.^2)^0.5;
nonlinear constraints m-file:
function [c,ceq] = disk(x)
x1 = x(1);
x2 = x(2);
x3 = x(3);
x4 = x(4);
x5 = x(5);
x6 = x(6);
c(1) = -(x6 - x4)^2 - (x5 - x3)^2 - 4; % non-convex constraints
c(2) = x3.^2 + x4.^2 - ((x1.^2 + x2.^2)^0.5 - 1)^2; %convex non-linear constraints
c(3) = x5.^2 + x6.^2 - ((x1.^2 + x2.^2)^0.5 - 1)^2;
ceq = []; % the empty equality constraints.
However having been doing more research I think I am way off from defining the problem correctly :(
in fact i am sure that i am approaching this the wrong way. my problem is:
find the minimum radius of a larger circle (container) within which n unit circles (radius 1) fit, given that 1) no two unit circles overlap (tangent to ok) 2) all unit circles lie within the container (tangent to ok)
the code i am trying to write is for n=2
I need to go back to the drawing board. I am a mathematician with 0 programming skills!!
If x is the list of centers, then you need to calculate the pairwise distances between the centers and ensure that they are all at least 2, and you need to calculate the smallest circle that can contain those centers, and the radius of that circle is what the objective function needs to return.
The pairwise distance can be done with pdist() from the Stats toolbox, or you can do the calculation yourself to save on overhead.
The smallest containing circle, I am not sure of. Perhaps if you calculate the maximum distance between any of the centers and the centroid of the centers, and then add 1 (the unit radius) ?

Sign in to comment.

Asked:

on 26 Mar 2016

Commented:

on 1 Apr 2016

Community Treasure Hunt

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

Start Hunting!