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
Best Answer