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

1 view (last 30 days)
function [ f, df ] = o_obj( x )
f = 5*(x(1)^2)-23*x(1)*x(2)-9*(x(2)^2)+9*(x(3)^2)+15;
df(1) = 10*x(1)-23*x(2);
df(2) = -23*x(1)-18*x(2);
df(3) = 18*x(3);
end
function [ g, h, dg, dh ] = c_const( x )
g(1) = x(1)*x(3)-100;
g(2) = -x(1)-x(2)-x(3)-10;
g(3) = x(1)-500;
g(4) = -x(1)-500;
g(5) = x(2)-500;
g(6) = -x(2)-500;
g(7) = x(3)-500;
g(8) = -x(3)-500;
dg(1,1) = x(3);
dg(1,2) = 0;
dg(1,3) = x(1);
dg(2,1) = -1;
dg(2,2) = -1;
dg(2,3) = -1;
dg(3,1) = 1;
dg(3,2) = 0;
dg(3,3) = 0;
dg(4,1) = -1;
dg(4,2) = 0;
dg(4,3) = 0;
dg(5,1) = 0;
dg(5,2) = 1;
dg(5,3) = 0;
dg(6,1) = 0;
dg(6,2) = -1;
dg(6,3) = 0;
dg(7,1) = 0;
dg(7,2) = 0;
dg(7,3) = 1;
dg(8,1) = 0;
dg(8,2) = 0;
dg(8,3) = -1;
h(1) = x(1)^2+x(2)^2-10;
h(2) = 4*x(1)-19*x(2)-50;
h(3) = 3*x(1)+4*x(3)-100;
dh(1,1) = 2*x(1);
dh(1,2) = 2*x(2);
dh(1,3) = 0;
dh(2,1) = 4;
dh(2,2) = -19;
dh(2,3) = 0;
dh(3,1) = 3;
dh(3,2) = 0;
dh(3,3) = 4;
end
But I get the error message above!
Can anyone help me please?
Many thanks...
  6 Comments
William Rose
William Rose on 3 Apr 2021
I have never used the optimization app. I have only written scripts that do optimization. Usually I use the function fmincon() which is part of Matlab's Optimizaiton Toolbox. I assume that the optimization app provides an interface to the tools in the Optimization Toolbox, and it probably calls fmincon(). Did you try my suggestion about transposing dg in your constraints function, so that dg is 3x8 instead of 8x3?
You have not shown us how you use the function c_const(). Is g() a vector of inequality constraints, each of which must be <0? If h a vetor of equality constraints, each of which mut be =0? I suspect that is true, because that is what fmincon() expects. I think there are errors in the equations for some of the g constraint equations in function c_const(). For examle, you posted the following:
g(3) = x(1)-500;
g(4) = -x(1)-500;
g(3) is equivalent to x1>+500, and g(4) is equivalent to x1<-500. Both cannot be true. Likewise for g(5) through g(8). I htink you may have assumed that the inquality equations are "greater than", whereas fmincon() will try to keep them "less than". If I am right, then you fix it by chaning the signs on the right hand side (and don;t forget to fix the dg as well).
Furthermore, constaints g(3) through g(8) are bound constraints. If you are using using fmincon() or similar function, bound constraints can be more easily accomodated by passing vectors ub=upper bound and lb=lower bound to the function. See the help for fmincon().
If I am correct that h() are equality constraints, then h(1) says x1,x2 must lie on a circle of radius r=sqrt(10), and h(2) says x1,x2 lies on straight line. Therefore there are only two possible solutions (at most) to (x1,x2), since a straight line intersects a cricle at two points or fewer. h(3) is a linear constraint relating x2 and x3, so x3 is uniquely known once x2 is known. Therfore this seemingly complicated problem amounts to comparing the objective funciton at just two locations in (x1,x2,x3) space. Pick the location that give the lower value for o_obj(). And check that the inequality constraints (most of which are bound constraints) on (x1,x2,x3) are satisfied.

Sign in to comment.

Answers (2)

William Rose
William Rose on 3 Apr 2021
Edited: William Rose on 3 Apr 2021
In function c_const(), the array dg is too small. It shouldl be 8x8, but right now it is returned as 8x3. I realize the columns after 3 are zero. You just need to initialize the full array by adding
dg=zeros(8); %create 8x8 array of zeros
before you calculate the non-zero values of dg.

William Rose
William Rose on 3 Apr 2021
@Furkan pehlivanoglu, I assume @Walter Roberson is right that you are using the gradient in an optimization routine such as fmincon(). I now think that my previous answer, which was that dg() should be 8x8, is incorrect.
I suspect your dg should be 3x8. It is 8x3.
The inequality constraint gradient matrix should be MxN, where M=number of rows=length of x (x=the vector being fitted), and N=number of columns=number of inequality constraints. I believe x has 3 elements and there are 8 inequality constraints, i.e. M=3 and N=8. Therefore dg should be 3 rows x 8 columns. It looks ike your dg is 8x3.
There are 3 equality constraints. Therefore the equality constraint gradient matrix, dh, which is 3x3, seems to have the right size. You might want to confirm that the equaitons within it are correct and not transposed. It looks good to me.
If that is not the right answer, then please show us the part of your code where you initialize x0 and call fmincon() and the full error message.
  1 Comment
Furkan pehlivanoglu
Furkan pehlivanoglu on 3 Apr 2021
Yes, I have 3 x elements and 8 inequality constraints. I think there is an error on the dg part. I did something like this to try;
dg(1,1) = x(3);
dg(1,2) = 0;
dg(1,3) = x(1);
dg(1,4) = 0;
dg(1,5) = 0;
dg(1,6) = 0;
dg(1,7) = 0;
dg(1,8) = 0;
I applied this to all of the g values. But i am getting the same error.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!