MatLab fmincon constrained optimization "Not enough input arguments."

1.7k views Asked by At

I did this

function f=objfun(w)
a=0.5
w0=[0.1;0.2;0.3];
f=(a^2)/2 + w(1)+ w(2)+ w(3);

[w,fval]=fmincon('objfun',w0,[],[],[],[],[],[],'constraint')

But I got this error message.

Error using objfun (line 3)
Not enough input arguments.

What problem is it talking about?

I learned fmincon from

http://www.math.colostate.edu/~gerhard/classes/331/lab/fmincon.html

and it tells me that codes like this

function f=objfun(x)

f=x(1)^4-x(1)^2+x(2)^2-2*x(1)+x(2);

will be the first lines to do constrained optimization.

What has gone wrong?

1

There are 1 answers

0
Karl On

I believe you need to pass a function handle to fmincon. From the docs http://www.mathworks.com/help/optim/ug/fmincon.html

x = fmincon(@myfun,x0,A,b)

where myfun is a MATLABĀ® function such as

function f = myfun(x)
f = ...            % Compute function value at x

Try passing a function handle to fmincon. I assume that constraints is your non linear constraint function, it should be a function handle as well. I also assume that you are not calling fmincon from inside your objective function. If so then I think you will have some thing like this:

objfun.m

 function f = objfun(w)
    a=0.5;
    f=(a^2)/2 + w(1)+ w(2)+ w(3);
    return
end

main.m

 w0=[0.1;0.2;0.3];
[w,fval]=fmincon(@objfun,w0,[],[],[],[],[],[],@constraint)