フィルターのクリア

Passing a parameters in MATLAB.

2 ビュー (過去 30 日間)
Ba Ba Black Sheep!
Ba Ba Black Sheep! 2017 年 1 月 3 日
回答済み: Torsten 2017 年 1 月 3 日
Suppose, I have the following functions:
function [f,g] = rosenbrockwithgrad(x, a, b)
% Calculate objective f
f = rosenbrock(x, a, b);
% gradient required
if nargout > 1
g = gradient(x);
end
end
function out = rosenbrock(x, a, b)
xx = x(1);
yy = x(2);
out = (1 - xx + a)^2 + 100*(yy - b - (xx-a)^2)^2;
end
I need to use them in the following routine,
function [x, fval, eflag, iter, fcount] =
Optimization_With_Analytic_Gradient(a, b, 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(@rosenbrockwithgrad, x0, options);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
iter = output.iterations;
fcount = output.funcCount;
% plot window title
title 'Rosenbrock with Analytic Gradient...'
disp('Optimization_With_Analytic_Gradient...');
end
Note the use of fminunc().
So, How can I do that?
My code generates the following Error
>> main
Error using rosenbrockwithgrad (line 3)
Not enough input arguments.
Error in fminunc (line 271)
[f,GRAD] = feval(funfcn{3},x,varargin{:});
Error in Optimization_With_Analytic_Gradient (line 24)
[x, fval, eflag, output] = fminunc(@rosenbrockwithgrad, x0, options);
Error in main (line 53)
[x, fval, eflag, iter, fcount] = Optimization_With_Analytic_Gradient(a, b, starting_point);
Caused by:
Failure in initial user-supplied objective function evaluation. FMINUNC cannot continue.
>>

採用された回答

Torsten
Torsten 2017 年 1 月 3 日
[x, fval, eflag, output] = fminunc(@(x)rosenbrockwithgrad(x,a,b), x0, options);
Best wishes
Torsten.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with Optimization Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by