Error in Rosenbrock Optimization with Analytic Hessian

1 次查看(过去 30 天)
The following program is not working.
It is generating the following error:
Error using gradient (line 6)
Not enough input arguments.
Error in rosenbrockwithgradhess (line 7)
g = gradient(x);
Error in Optimization_With_Analytic_Hessian>@(x)rosenbrockwithgradhess(x,
double(a),double(b)) (line 27)
[x, fval, eflag, output] = fminunc(@(x)rosenbrockwithgradhess(x,
double(a), double(b)), x0, options);
Error in fminunc (line 280)
[f,GRAD,HESS] = feval(funfcn{3},x,varargin{:});
Error in Optimization_With_Analytic_Hessian (line 27)
[x, fval, eflag, output] = fminunc(@(x)rosenbrockwithgradhess(x,
double(a), double(b)), x0, options);
Error in main (line 57)
[x, fval, eflag, iter, fcount] =
Optimization_With_Analytic_Hessian(starting_point);
Caused by:
Failure in initial user-supplied objective function evaluation.
FMINUNC cannot continue.
Optimization_With_Analytic_Hessian.m
function [x, fval, eflag, iter, fcount] =
Optimization_With_Analytic_Hessian(start_point)
% assign start point
x0 = start_point;
% read coefficients a, b
coeff = load('coeff.txt');
a = coeff(1);
b = coeff(2);
% options setup
options = optimoptions( 'fminunc', ...
'Display','off',...
'OutputFcn',@bananaout,...
'Algorithm','trust-region', ...
'GradObj','on',...
'Hessian', 'on');
% calling fminunc
[x, fval, eflag, output] = fminunc(@(x)rosenbrockwithgradhess(x,
double(a), double(b)), x0, options);
iter = output.iterations;
fcount = output.funcCount;
% plot window title
title 'Optimization with Analytic Hessian...'
disp('Optimization_With_Analytic_Hessian...');
end
Relevant Source Code:
function [f, g, h] = rosenbrockwithgradhess(x, a, b)
% Calculate objective f
f = rosenbrock(x, a, b);
% gradient required
if nargout > 1
g = gradient(x);
end
% hessian required
if nargout > 2
h = hessian(x);
end
end
rosenbrock.m
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
gradient.m
function out = gradient(coord, a, b )
x = coord(1);
y = coord(2);
out = [2 *(-200 *(a - x) *(a^2 - 2*a*x + b + x^2 - y) - a + x - 1);
200 *(-(a - x)^2 - b + y)];
end
hessian.m
function out = hessian(coord, a, b )
x = coord(1);
y = coord(2);
out = 400 * (200 * (a-x)^2 + 200*b - 200*y + 1);
end
main.m
function main()
% initial data
points_count = 4;
coeff_file_name = 'coeff.txt';
start_points_file_name = 'data.txt';
file_write_mode = 'w';
a = gen_const();% integer
b = gen_const();% integer
% write coefficients to a file
write_to_file(coeff_file_name, [a, b], file_write_mode)
% generate 4 random points
m = gen_points(a, b, points_count);
% write the points to a file
write_to_file(start_points_file_name, m, file_write_mode);
% load points from file
points = load(start_points_file_name);
plot_ban();
% iterate through the points and use them in optimization
for i = 1:points_count
starting_point = points(i,:);
if i==1
file_write_mode = 'w' ;
else
file_write_mode = 'a';
end
% Optimization with Analytic Hessian
[x, fval, eflag, iter, fcount] =
Optimization_With_Analytic_Hessian(starting_point);
write_to_file('Optimization_With_Analytic_Hessian.txt',
[a, b,starting_point, x, fval, eflag, iter, fcount], file_write_mode);
end
end

采纳的回答

Walter Roberson
Walter Roberson 2017-1-5
Your rosenbeckwitgradhess routine is receiving a and b as arguments but is not passing them to your gradient.m

更多回答(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