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:
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 4Or, 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 4Apparently, the order of arguments that
fminsearch
takes is different from the one in MATLAB. So, how is this order??
How can I makefminsearch
take values and options?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?