フィルターのクリア

Argument passing issue.

3 ビュー (過去 30 日間)
Ba Ba Black Sheep!
Ba Ba Black Sheep! 2017 年 1 月 3 日
編集済み: Walter Roberson 2017 年 1 月 3 日
I have a requirement that, I use the following modified Rosenbrock function,
where the coefficients a and b are to be read from a text file.
.
I tried the following,
function out = rosenbrock(x)
disp('rosenbrock()..........called');
coeff = load('coeff.txt');
a = coeff(1);
b = coeff(2);
xx = x(1);
yy = x(2);
out = (1 - xx + a)^2 + 100*(yy - b - (xx-a)^2)^2;
end
But it is doing two things,
(1) Slowing down the performance of the optimization.
(2) The output is also not correct (the optimization isn't converging).
How can I solve this issue wile fulfilling my requirement?
Is it possible to pass the values of a and b as the arguments of rosenbrockwithgrad()?
Relevant Source Code
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(@rosenbrockwithgrad, x0, options);
iter = output.iterations;
fcount = output.funcCount;
% plot window title
title 'Rosenbrock with Analytic Gradient...'
disp('Optimization_With_Analytic_Gradient...');
end
function out = gradient( x )
out = [-400*(x(2) - x(1)^2)*x(1) - 2*(1 - x(1));
200*(x(2) - x(1)^2)];
end
function [f,g] = rosenbrockwithgrad(x)
% Calculate objective f
f = rosenbrock(x);
if nargout > 1 % gradient required
g = gradient(x);
end
end

採用された回答

Walter Roberson
Walter Roberson 2017 年 1 月 3 日
  2 件のコメント
Ba Ba Black Sheep!
Ba Ba Black Sheep! 2017 年 1 月 3 日
編集済み: Ba Ba Black Sheep! 2017 年 1 月 3 日
I am extremely sorry. I could't figure it out.
rosenbrockwithgrad_p = @(x)[rosenbrock(x, a, b);
(nargout > 1) * gradient(x)];
% calling fminunc
[x, fval, eflag, output] = fminunc(rosenbrockwithgrad_p, x0, options);
Could you please help?
Walter Roberson
Walter Roberson 2017 年 1 月 3 日
編集済み: Walter Roberson 2017 年 1 月 3 日
function [x, fval, eflag, iter, fcount] =
Optimization_With_Analytic_Gradient(start_point, a, b)
x0 = start_point;
% options setup
options = optimoptions( 'fminunc', ...
'Display','off',...
'OutputFcn',@bananaout,...
'Algorithm','trust-region', ...
'GradObj','on');
% calling fminunc
[x, fval, eflag, output] = fminunc(@(x) rosenbrockwithgrad(x, a, b), x0, options);
iter = output.iterations;
fcount = output.funcCount;
% plot window title
title 'Rosenbrock with Analytic Gradient...'
disp('Optimization_With_Analytic_Gradient...');
end
function out = gradient( x, a, b )
%this routine probably needs to be changed to take a and b into account
out = [-400*(x(2) - x(1)^2)*x(1) - 2*(1 - x(1));
200*(x(2) - x(1)^2)];
end
function [f,g] = rosenbrockwithgrad(x, a, b)
% Calculate objective f
f = rosenbrock(x, a, b);
if nargout > 1 % gradient required
g = gradient(x, a, b);
end
end

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSurrogate Optimization についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by