From: Matthias on
Hi,

I'm using FMINSEARCH and FITFUN to optimise a double exponent curve fit:
http://www.mathworks.com/products/matlab/demos.html?file=/products/demos/shipping/matlab/fitdemo.html

The prob is that I have been trying, so far unsuccessfully, to find a way for FMINSEARCH to output the constants (c1, c2) generated in FITFUN:
s = c1*exp(-x*y1) + c2*exp(-x*y1)

Presently it outputs only the lambdas into the workspace. I can of course turn on 'Display', 'Iter' in FMINSEARCH outputFcn options, but this only *displays* the c parameters during each iteration. I want to include these in the workspace.

I have added c in the FITFUN function call:
[err, c] = fitfun_v2(lambda,t,y)

But I think now I must somehow ask FMINSEARCH outputFcn to include the c parameters. I'm just not sure how to do this......

Thanks in advance for any help
Cheers
From: John D'Errico on
"Matthias " <matthew.goddard(a)bcf.uni-freiburg.de> wrote in message <i1mp15$pnb$1(a)fred.mathworks.com>...
> Hi,
>
> I'm using FMINSEARCH and FITFUN to optimise a double exponent curve fit:
> http://www.mathworks.com/products/matlab/demos.html?file=/products/demos/shipping/matlab/fitdemo.html
>
> The prob is that I have been trying, so far unsuccessfully, to find a way for FMINSEARCH to output the constants (c1, c2) generated in FITFUN:
> s = c1*exp(-x*y1) + c2*exp(-x*y1)
>
> Presently it outputs only the lambdas into the workspace. I can of course turn on 'Display', 'Iter' in FMINSEARCH outputFcn options, but this only *displays* the c parameters during each iteration. I want to include these in the workspace.
>
> I have added c in the FITFUN function call:
> [err, c] = fitfun_v2(lambda,t,y)
>
> But I think now I must somehow ask FMINSEARCH outputFcn to include the c parameters. I'm just not sure how to do this......
>
> Thanks in advance for any help
> Cheers

So, you have fminsearch calling a function called fitfun.
Internally, it generates a variable called c, and you wish
to return that?

Have fitfun return TWO outputs. The second being c.
Fminsearch only ever expects to see one output, so it
will just get dumped in the bit bucket. After fminsearch
terminates and returns the value of lambda that it
optimizes over, call fitfun ONE more time, but this time
with TWO outputs, the second will be the values of
c that you so desire.

Another way, more of a kludge, would be to use the
debugger. Then you could use my putvar, from the
file exchange, to pop that variable back up to the base
workspace.

HTH,
John
From: Matthias on
Ahh, it seems the solution was easier than I had anticipated.

All I had to add to 'fitfun' was the following line of code:

assignin('base','c', c)

This essentially just grabs the constant(s) for the equation as they are iterated by 'fminsearch'.

Now, not only do I have a perfect curve fit, but a complete equation for a 3 exponent decay function :)