How can i fix this errors while performing Optimization using fmincon.

20 views (last 30 days)
Warning: Length of lower bounds is < length(x); filling in missing lower bounds with -Inf. 
> In checkbounds (line 33)
  In fmincon (line 306)
  In companies1 (line 27) 
Warning: Length of upper bounds is < length(x); filling in missing upper bounds with +Inf. 
> In checkbounds (line 47)
  In fmincon (line 306)
  In companies1 (line 27) 
Attempt to execute SCRIPT companies1 as a function:
C:\Users\shahn\Desktop\companies1.m
Error in optimfcnchk/checkfun (line 315)
            f = userfcn(x,varargin{:});
Error in fmincon (line 534)
      initVals.f = feval(funfcn{3},X,varargin{:});
Error in companies1 (line 27)
[x,fval,exitflag,output]=fmincon(@companies1,x0,A,B,Aeq,beq,lb,ub,[],options);
Caused by:
    Failure in initial user-supplied objective function evaluation. FMINCON cannot continue.

Accepted Answer

Walter Roberson
Walter Roberson on 19 Apr 2018

The first two parts are warnings that your lb and ub variables are not empty but are also not as long as the number of variables.

This could happen if you attempted to use something like

lb = 0; ub = 10;

with a vector x0, thinking that you were setting all of the lower bounds to 0, and all of the upper bounds to 10. However, instead MATLAB treats this example as

lb = [0, -inf(1,length(x0)-1)]; ub = [10, inf(1,length(x0)-1)];

that is, it uses -inf for any lower bound you did not set and uses +inf for any upper bound that you did not set.

"Attempt to execute SCRIPT companies1 as a function:"

You named @companies1 as your objective function, so companies1.m was looked for. However it turns out that the code in companies1.m does not start with function or classdef: it starts with some other code, which makes it a "script" that cannot be called as a function. For example perhaps the first lines of companies1.m are

clear all
close all

Those are lines that make the file a "script" rather than a function that accepts an input argument and returns the "cost" evaluated at that input argument.

  5 Comments
Walter Roberson
Walter Roberson on 19 Apr 2018
I rearranged slightly and do not get any warnings:
global M N H
H=24;
M=3;
N=5;
num_x=(M*H);
lb=zeros(1,num_x);
ub=ones(1,num_x);
A=[];
B=[];
Aeq=ones(H,num_x);
beq=ones(H,1);
x0=rand(num_x,1);
options = optimoptions(@fmincon,'Algorithm','interior-point','MaxFunEvals',10000,'Display','iter','FunValCheck','on');
[x,fval,exitflag,output]=fmincon(@c_main,x0,A,B,Aeq,beq,lb,ub,[],options);

Sign in to comment.

More Answers (5)

NIKET shah
NIKET shah on 19 Apr 2018
Thank you so much. I have updated the code according. The next error is:
Error using fmincon (line 607) User supplied objective function must return a scalar value.
Error in companies1 (line 25) [x,fval,exitflag,output]=fmincon(@c_main,x0,A,B,Aeq,beq,lb,ub,[],options);
and can you help me check my values for x0, ub, lb.
Thank You.

NIKET shah
NIKET shah on 19 Apr 2018
Error using * Inputs must be 2-D, or at least one input must be scalar. To compute elementwise TIMES, use TIMES (.*) instead.
Error in formConstraints (line 14)
Error in barrier (line 175)
Error in fmincon (line 796) [X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = barrier(funfcn,X,A,B,Aeq,Beq,l,u,confcn,options.HessFcn, ...
Error in companies1 (line 25) [x,fval,exitflag,output]=fmincon(@c_main,x0,A,B,Aeq,beq,lb,ub,[],options);
The 1st error is about input/output argument for function. I am not able to fix it though. Can you help? Informing me what to change.

NIKET shah
NIKET shah on 20 Apr 2018
if i have constraint like:
x(1)+x(2)+x(3)=1; Aeq*x=beq;
How can i modify it for the following file.
  1 Comment
Walter Roberson
Walter Roberson on 20 Apr 2018
Are you trying to constrain the first 3 out of 72 input variables? Or are you trying to work with what is really a 24 x 3 array and you want to constrain each of the rows of that to sum to 1?

Sign in to comment.


NIKET shah
NIKET shah on 20 Apr 2018
In terms of Aeq??
as it gives me error stating :"Row dimension of Aeq is inconsistent with length of beq."

NIKET shah
NIKET shah on 20 Apr 2018
Following are the attachments. The one I am solving. If you can help me out further. I have to minimize u for per hour cycle. Can you help me modify the code.
  3 Comments
NIKET shah
NIKET shah on 20 Apr 2018
Do i have to re-arrange the complete structure. for objective function or just constraints would be good.
Walter Roberson
Walter Roberson on 20 Apr 2018
Your objective appears to me at the moment to fit within what can be done with quadprog. If I I am correct about that then you should switch to that instead, as the algorithms involved know how to divide everything up into subproblems that can each be solved exactly, and then the best is taken, leading to a global minimization.
If I have instead misread your objective (it is after 4am here), or if you choose to use fmincon even if quadprog would work, then the best you can get is a local minima.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!