not enough input arguments error using fminunc

조회 수: 12 (최근 30일)
Han Kyul Yoo
Han Kyul Yoo 2016년 8월 20일
댓글: Han Kyul Yoo 2016년 8월 25일
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