Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Optimization and file-read issue.

AinStinAinStin Kolkata, IndiaMember Posts: 2

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)
    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;

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()|?

I have tried the following,

 rosenbrockwithgrad_p = @(x)[rosenbrock(x, a, b); 
                        (nargout > 1) * gradient(x)];
 % calling fminunc
 [x, fval, eflag, output] = fminunc(@rosenbrockwithgrad_p, x0, options);

It doesn't work.

Relevant Source Code

 function [x, fval, eflag, iter, fcount] = 

    x0 = start_point;

    %   inline function defitions
    %fungrad = @(x)deal(fun(x),grad(x));

    % options setup
    options = optimoptions( 'fminunc', ...
                            'Algorithm','trust-region', ...

    % calling fminunc
    [x, fval, eflag, output] = fminunc(@rosenbrockwithgrad, x0, options);
    iter = output.iterations;
    fcount = output.funcCount;

    %   plot window title
    title 'Rosenbrock with Analytic Gradient...'    


 function out = gradient( x )
    out = [-400*(x(2) - x(1)^2)*x(1) - 2*(1 - x(1)); 
            200*(x(2) - x(1)^2)];

 function [f,g] = rosenbrockwithgrad(x)
    % Calculate objective f
    f = rosenbrock(x);    
    if nargout > 1 % gradient required
        g = gradient(x);
Sign In or Register to comment.