Matlab – Octave fminsearch: Problems with minimization and options

MATLABoctave

I am trying to use Octave's fminsearch function, which I have used in MATLAB before. The function seems not sufficiently documented (for me at least), and I have no idea how to set to options such that it would actually minimize.

I tried fitting a very simple exponential function using the code at the end of this message. I want the following:
I want the function to take as input the x- and y-values, just like MATLAB would do. Furthermore, I want some control over the options, to make sure that it actually minimizes (i.e. to a minimum!).
Of course, in the end I want to fit functions that are more complicated than exponential, but I want to be able to fit exponentials at least.

I have several problems with fminsearch:

  1. I tried handing over the x- and y-value to the function, but a matlab-style thing like this:

    [xx,fval]=fminsearch(@exponential,[1000 1],x,y);
    

    or

    [xx,fval]=fminsearch(@exponential,[33000 1],options,x,y)
    

    produces errors:

    error: options(6) does not correspond to known algorithm
    error: called from:
    error: /opt/local/share/octave/packages/optim-1.0.6/fmins.m at line 72, column 16
    error: /opt/local/share/octave/packages/optim-1.0.6/fminsearch.m at line 29, column 4

    Or, respectively (for the second case above):

    error: `x' undefined near line 4 column 3
    error: called from:
    error: /Users/paul/exponential.m at line 4, column 2
    error: /opt/local/share/octave/packages/optim-1.0.6/nmsmax.m at line 63, column 6
    error: /opt/local/share/octave/packages/optim-1.0.6/fmins.m at line 77, column 9
    error: /opt/local/share/octave/packages/optim-1.0.6/fminsearch.m at line 29, column 4

    Apparently, the order of arguments that fminsearch takes is different from the one in MATLAB. So, how is this order??
    How can I make fminsearch take values and options?

  2. I found a workaround to the problem that the function would not take values: I defined the x- and y values as global. Not elegant, but at least then the values are available in the function.
    Nonetheless, fminsearch does not minimize properly.
    This is shown below:

    Here is the function:

    function f=exponential(coeff)
    global x
    global y
    X=x;
    Y=y;
    a= coeff(1);
    b= coeff(2);
    Y_fun = a .* exp(-X.*b);
    DIFF = Y_fun - Y; 
    SQ_DIFF = DIFF.^2;
    
    f=sum(SQ_DIFF);
    end
    

    Here is the code:

    global x
    global y
    x=[0:1:200];
    y=4930*exp(-0.0454*x);
    options(10)=10000000;
    [cc,fval]=fminsearch(@exponential,[5000 0.01])
    

    This is the output:

    cc =

    4930.0 5184.6

    fval = 2.5571e+08

    Why does fminsearch not find the solution?

Best Answer

There is an fminsearch implementation in the octave-forge package "optim". You can see in its implementation file that the third parameter is always an options vector, the fourth is always a grad vector, so your ,x,y invocations will not work.

You can also see in the implementation that it calls an fmins implementation.

The documentation of that fmins implementation states:

         if options(6)==0 && options(5)==0 - regular simplex
         if options(6)==0 && options(5)==1 - right-angled simplex
            Comment: the default is set to "right-angled simplex".
              this works better for me on a broad range of problems,
              although the default in nmsmax is "regular simplex"

A recent problem of mine would solve fine with matlab's fminsearch, but not with this octave-forge implementation. I had to specify an options vector [0 1e-3 0 0 0 0] to have it use a regular simplex instead of a 'right-angled simplex'. The octave default makes no sense if your coefficients differ vastly in scale.

Related Topic