フィルターのクリア

Error: Not enough input arguments.

1 回表示 (過去 30 日間)
Ba Ba Black Sheep!
Ba Ba Black Sheep! 2017 年 1 月 3 日
コメント済み: Walter Roberson 2017 年 1 月 3 日
The following code works fine.
See the inline function fungrad.
function [x, fval, eflag, iter, fcount] =
Optimization_With_Analytic_Gradient(start_point)
x0 = start_point;
% inline function defitions
fungrad = @(x)deal(fun(x),grad(x));
% options setup
options = optimoptions( 'fminunc', ...
'Display','off',...
'OutputFcn',@bananaout,...
'Algorithm','trust-region', ...
'GradObj','on');
% calling fminunc
[x,fval,eflag,output] = fminunc(fungrad,x0,options);
iter = output.iterations;
fcount = output.funcCount;
% plot window title
title 'Rosenbrock solution via fminunc with gradient'
disp('Optimization_With_Analytic_Gradient...');
end
But, when I call it using the following code,
function out = fungrad(x)
out = deal(fun(x),grad(x));
end
It displays the following error:
Error using fungrad (line 2)
Not enough input arguments.
Error in Optimization_With_Analytic_Gradient (line 24)
[x,fval,eflag,output] = fminunc(fungrad,x0,options);
Error in main (line 48)
[x, fval, eflag, iter, fcount] =
Optimization_With_Analytic_Gradient(starting_point);
Rest of the code:
function out = fun(x)
out = 100*(x(2) - x(1)^2)^2 + (1 - x(1))^2;
end
function out = grad( x )
out = [-400*(x(2) - x(1)^2)*x(1) - 2*(1 - x(1));
200*(x(2) - x(1)^2)];
end

採用された回答

Walter Roberson
Walter Roberson 2017 年 1 月 3 日
[x,fval,eflag,output] = fminunc(@fungrad,x0,options);
However, your code will not work. Your objective function must return the gradient information only when nargout > 1. Your use of deal() always returns two outputs, and that is going to fail.
  2 件のコメント
Ba Ba Black Sheep!
Ba Ba Black Sheep! 2017 年 1 月 3 日
編集済み: Ba Ba Black Sheep! 2017 年 1 月 3 日
Your objective function must return the gradient information
only when nargout > 1.
which one? banana? or, fungrad?
_
N.B. Sorry! I am absolutely new.
Walter Roberson
Walter Roberson 2017 年 1 月 3 日
You have
fungrad = @(x)deal(fun(x),grad(x));
so your fungrad is always returning two outputs. fungrad is being used as your objective function for fminunc . The objective function must only assign the gradient when it is asked to do so. See the example https://www.mathworks.com/help/optim/ug/fminunc.html#butzm_r-1 and notice that it tests nargout

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNonlinear Least Squares (Curve Fitting) についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by