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
  1 件のコメント
Han Kyul Yoo
Han Kyul Yoo 2016 年 8 月 25 日
Thank you so much! This works!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSolver 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