please help me with the error

I used the following functions to do an optimization problem and got the given error.
Could you please help me correct it.
Thanks
function v = funv(x)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
x1=x(1);
x2=x(2);
x3=x(3);
x4=x(4);
x5=x(5);
v = 46.149*x1+39.158*x2+19.834*x3+59.307*x4+35.526*x5;
end
function [c,ceq] = nlconst(x)
x1=x(1);
x2=x(2);
x3=x(3);
x4=x(4);
x5=x(5);
c=[];
ceq = x1+x2+x3+x4+x5-1;
end
clc;
clear;
A = [];
B = []; % the linear inequality constraints: A*X <= B
Aeq = [];
Beq = []; % the linear equality constraints: Aeq*X = B
LB = [];
UB = []; % LB <= X <= UB
x0 = [1, 1, 1, 1, 1]; % initial guess
[x,FVAL,EXITFLAG,OUTPUT,LAMBDA] = fmincon(@funv,x0,A,B,Aeq,Beq,LB,UB,@nlconst);
ERROR:
Warning: The default trust-region-reflective algorithm does
not solve problems with the constraints you have specified.
FMINCON will use the active-set algorithm instead. For
information on applicable algorithms, see Choosing the
Algorithm in the documentation.
> In fmincon at 486
In sc at 12
Error using nlconst
Too many input arguments.
Error in fmincon (line 794)
[X,FVAL,LAMBDA,EXITFLAG,OUTPUT,GRAD,HESSIAN]=...
Error in sc (line 12)
[x,FVAL,EXITFLAG,OUTPUT,LAMBDA] =
fmincon(@funv,x0,A,B,Aeq,Beq,LB,UB,@nlconst);

답변 (1개)

Walter Roberson
Walter Roberson 2013년 8월 14일

1 개 추천

댓글 수: 7

dav
dav 2013년 8월 14일
thanks but i have the same number of arguments in both functions! I cant figure out why im getting this error.
Walter Roberson
Walter Roberson 2013년 8월 14일
I do not know at the moment. I wonder, though, why you are not using Aeq = [1 1 1 1 1] and beq = 1 instead of using the nonlinear constraints ?
dav
dav 2013년 8월 14일
i know but i need to modify it to a non linear constraint later. thats why i used it. is it impossible to do it that way?
thanks
and when i did it your way i got :
Solver stopped prematurely.
fmincon stopped because it exceeded the function evaluation limit,
options.MaxFunEvals = 500 (the default value).
is there a way to correct this please?
Use optimset() to create an options structure that you pass to fmincon(). You want to set MaxFunEvals
Your nlconst looks okay to me. Is it in the same file as your other code? If it is not then check with "which" to be sure you are getting the right nlconst function.
You could debug to some extent using
function [c,ceq] = nlconst(varargin)
if nargin ~= 1
error(sprintf('unexpectedly received %d arguments for nlconst', nargin))
end
x = varargin[1};
c = [];
ceq = sum(x) - 1;
dav
dav 2013년 8월 14일
I have saved "funv" and "nlconst" as two different functions in the same directory i'm working on.
Is that what you are asking.
Thanks
Please use
which -all nlconst
to check whether the nlconst.m you are getting is the one you expect.

댓글을 달려면 로그인하십시오.

카테고리

태그

질문:

dav
2013년 8월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by