error in fmincon problem
6 views (last 30 days)
Show older comments
Hello!
I have to solve a system of non linear equalities and inequalities. First I used fsolve, but it only works with non linear equalities. So I found that to include also inequalities I should use fmincon.
My system depends on the unknown vector "a" (what I want to find out by solving the system) and a matrix "X" of random variables that I would like to generate in advance, and fix, so that the system is solved only with respect to the vector "a".
Now maybe I have a little bit of confusion about the objective function that I should indicate to fmincon. I mean I want to solve a system, so should the objective function be the system itself?
The main script is the following "Main Program"
X = binornd(5,0.6,5,2);
a0 = 100*randn(14,1);
opts = optimset('Algorithm','interior-point','Display','off');
x = fmincon(minconstr,a0,[],[],[],[],[],[],[],opts)
---------------------------------------------------------------------
where "minconstr" should be the objective function, therefore my system of non linear equalities and inequalities, is it correct? Here I use "global X" to try to fix the matrix X generated in the previous script. Am I right?
function [c, ceq]=minconstr(a)
global X;
b=[1;1;1;1;1;1;1;0;0;0;0;0;0;0;0];
ceq=confun(a);
c=[-a(5)-b(1);-a(6)-b(1);-a(4)+a(5)-b(2);-a(4)+a(6)-b(2);-a(3)+a(4)-b(3);
-a(2)+a(3)-b(4);-a(1)+a(2)-b(5);-a(7)-b(8);-a(8)-b(9);-a(9)-b(10);
-a(10)-b(11);-a(11)-b(12);-a(12)-b(13);-a(13)-b(14);-a(14)-b(15)
];% Linear inequalities
--------------------------------------------------------------
where "confun" is a function that creates the vector of all the non linear equalities of my system. Here again I use "global X" to try to fix the matrix X generated in the previous script. The code of this function is quite long, but it is something like
function F = confun(a)
global X;
...
....
...
% Nonlinear equality constraints
F = [dY(1);
dc(1); dc(2); dc(3); dc(4); dLambda;
dmi(1);dmi(2);
dmi2(1);dmi2(2);dmi2(3)];
------------------------------------------------------------------------
If I run my "Main Program" script, the error says
??? Input argument "a" is undefined.
Error in ==> minconstr at 7
ceq=confun(a);
Error in ==> MainProgram at 9
x = fmincon(minconstr,a0,[],[],[],[],[],[],[],opts)
"a" should be the unknown of my system of which I already provided an initial guess "a0". Does anyone know where could it be the problem?
Thank you all!
0 Comments
Accepted Answer
Matt J
on 14 Oct 2014
Edited: Matt J
on 14 Oct 2014
Your objective function shouldn't be myconstr. Your objective should really be norm(confun(a)).^2
Also, you don't really have any nonlinear inequalities, or at least all of your inequalities looks quite linear to me. So you should use matrix data to express them and your call to fmincon should really look like,
x = fmincon(@(a) norm(confun(a)).^2,a0, A,B,[],[],[],[],[],opts)
for an appropriate matrix A and vector B.
3 Comments
Matt J
on 14 Oct 2014
Edited: Matt J
on 14 Oct 2014
I know that for linear inequalities I could use the matrices A and B, but I simply wrote explicitly the conditions that would derive from this matrix product and collected them in the vector "c". Should it work anyway?
It should work, but less efficiently. FMINCON can handle inequalities in an improved way when it knows that they are linear.
Could you please explain me why should norm(confun(a)).^2 be the objective function? And why norm(confun(a)).^2 and not norm(mincontr(a)).^2?
FMINCON expects the objective function to return a scalar value that you are trying to minimize. Since you said you were trying to generalize fsolve, I proposed the objective f(a)=norm(confun(a)).^2 so that the solver will look for a a least squares solution to your equalities. This is the most similar to what fsolve does in the unconstrained case.
However, I should point out that my proposal puts more priority on satisfying the inequalities than the equalities. It will try to satsify the inequalities exactly, but will give you only a least squares solution to the equalities if that's the best that satisfying the inequalities will allow. I don't know if this is acceptable to you, but it is similar to what lsqnonlin does when solving equalities with bound constraints.
I mean "confun" includes only the non linear equalities, while "minconstr" should represents the whole system (non linear equalities and linear inequalities).
I did not tell you to ignore the inequalities. I advised you to treat them as constraints and to handle them through matrix data A,B. Because you did not do this and instead made all 7 input arguments for the constraints [],[],[],[],[],[],[] empty, you received the error message
FMINCON is for constrained problems. Use FMINUNC for unconstrained problems.
Since you did not specify any constraints, fmincon wonders why you wouldn't just use fminunc.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!