0
$\begingroup$

I have a requirement that, I use the following modified Rosenbrock function,

enter image description here

where the coefficients a and b are to be read from a text file.

I have tried to do it like the following,

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

And then,

% calling fminunc
[x, fval, eflag, output] = fminunc(@rosenbrockwithgrad, x0, options);

But, that is generating 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.

>>

Relevent Source Code,

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 [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 
  • 1
    Your `rosenbrock` function executes `coeff = load('coeff.txt');` each time you need to return the value of the function at a given point. This is certainly not optimal. You'd rather load the coefficients $a$ and $b$ outside the function and pass them as arguments to `rosenbrock`. It is strange that your analytic gradient does not depend on $a$ and $b$. How is that possible ?2017-01-03
  • 0
    @jibounet, plz, see the later part of the source code where I tried to solve it in a different way.2017-01-03
  • 1
    Again, it is strange that the function `gradient` does not depend on $a$ and $b$. I think it should. Regarding your error, Matlab is quite cool because it tells you exactly where the problem is : `Error using rosenbrockwithgrad (line 3) Not enough input arguments.` Your `rosenbrockwithgrad` function takes **3** arguments. You're calling it with only one ($x_0$, the starting point).2017-01-03

0 Answers 0