title: Octave fminsearch: Problems with minimization and options
body: <p>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.</p>
  
  <p>I tried fitting a very simple exponential function using the code at the end of this message. I want the following:<br>
  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!).<br>
  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.</p>
  
  <p>I have several problems with fminsearch:</p>
  
  <ol>
  <li><p>I tried handing over the x- and y-value to the function, but a matlab-style thing like this:</p>
  
  <pre><code>[xx,fval]=fminsearch(@exponential,[1000 1],x,y);
  </code></pre>
  
  <p>or</p>
  
  <pre><code>[xx,fval]=fminsearch(@exponential,[33000 1],options,x,y)
  </code></pre>
  
  <p>produces errors:</p>
  
  <blockquote>
    <p>error: options(6) does not correspond to known algorithm<br>
    error: called from:<br>
    error:   /opt/local/share/octave/packages/optim-1.0.6/fmins.m at line 72, column 16<br>
    error:   /opt/local/share/octave/packages/optim-1.0.6/fminsearch.m at line 29, column 4  </p>
  </blockquote>
  
  <p>Or, respectively (for the second case above):</p>
  
  <blockquote>
    <p>error: `x' undefined near line 4 column 3<br>
    error: called from:<br>
    error:   /Users/paul/exponential.m at line 4, column 2<br>
    error:   /opt/local/share/octave/packages/optim-1.0.6/nmsmax.m at line 63, column 6<br>
    error:   /opt/local/share/octave/packages/optim-1.0.6/fmins.m at line 77, column 9<br>
    error:   /opt/local/share/octave/packages/optim-1.0.6/fminsearch.m at line 29, column 4</p>
  </blockquote>
  
  <p>Apparently, the order of arguments that <code>fminsearch</code> takes is different from the one in MATLAB. So, how is this order??<br>
  How can I make <code>fminsearch</code> take values and options?</p></li>
  <li><p>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.<br>
  Nonetheless, <code>fminsearch</code> does not minimize properly.<br>
  This is shown below:  </p>
  
  <p>Here is the function:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Here is the code:</p>
  
  <pre><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])
  </code></pre>
  
  <p>This is the output:</p>
  
  <blockquote>
    <p>cc =  </p>
    
    <p>4930.0   5184.6</p>
    
    <p>fval =  2.5571e+08</p>
  </blockquote>
  
  <p>Why does <code>fminsearch</code> not find the solution?</p></li>
  </ol>