not enough input arguments error using fminunc

12 次查看(过去 30 天)
while using fminunc with a function file, I keep running into the error "not enough input arguments". I want to minimize my objective function with respect to three parameters, which I represented with the vector 'm'
my minimization code:
x0 = [0.2 1 0.01];
options = optimoptions(@fminunc,'Display','iter','Algorithm','quasi-newton');
[x,fval,exitflag,output,grad,hessian] = fminunc(@(m) myfun,x0,options)
my function file:
function logl = myfun(m);
rs = m(2)*100*(0*puse(16,1)) + m(3)*(0-euse(16,1))*eprc(16,1)/100;
(I have a bunch of code in between which includes m(1), but the above line is where the error occured)
end
To fix the error I tried fixing the minimization code to:
[x,fval,exitflag,output,grad,hessian] = fminunc(@(x) myfun,x0,options)
[x,fval,exitflag,output,grad,hessian] = fminunc(myfun,x0,options)
I also tried defining m in the minimization code:
m = [0 0 0];
x0 = [0.2 1 0.01];
options = optimoptions(@fminunc,'Display','iter','Algorithm','quasi-newton');
[x,fval,exitflag,output,grad,hessian] = fminunc(@(m) myfun,x0,options)
in the function file, I tried eliminating the input argument,
function logl = myfun;
and changing the order by which elements of 'm' appear
rs = m(1)*100*(0*puse(16,1)) + m(2)*(0-euse(16,1))*eprc(16,1)/100;
The function file itself works as it is. Before, I had defined 'm' inside the function file (like m = [1 1 1]), but I think this caused the optimization routine to stay at that point.

采纳的回答

Alan Weiss
Alan Weiss 2016-8-22
I think that instead of
fminunc(@(m) myfun,x0,options)
you should call
fminunc(@myfun,x0,options)
or
fminunc(@(m) myfun(m),x0,options)
Alan Weiss
MATLAB mathematical toolbox documentation

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by