
A custom fitting equation that also calls a fit

SailBlue5
I have written a fitting function to do some complicated fitting for me. to calculate a y value for given parameters my fit function runs a simulation of a differential equation to find out how a variable changes with time, and then fits this response to an exponential to find out the decaying time scale. for this exp fit fit I use a custom coefficients wave so to not interfere with the coefficient wave of the main fit. I still see problems, mainly:
1) My main fit freezes after awhile (I have to ctrl-alt-delete to exit), and at this time I see that it begins to think there are three coefficients instead of two that I expect. I wonder if somehow the exp fitting (which has three fit parameters) that happens during the fit some how messes with the fit.
2) I see the fitting algorithm run the same parameters (or very close to the same) a couple times in a row before moving on to the next point, this seems strange and adds significant time to my fit, maybe this is normal?
I know igor uses a lot of other variables during a fit, maybe I need to somehow save the current values before running my exp fit and then reload the values back in? If this is the case are there particular values which are important?
Michael
sounds a bit like a problem I had recently:
http://www.igorexchange.com/node/2546
maybe johnweeks' reply is also useful in your case.
October 20, 2011 at 08:39 am - Permalink
I recently added a check for recursion in the fitting code so that instead of crashing it would issue an error. What version of Igor are you using?
John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
October 20, 2011 at 10:57 am - Permalink
Any work arounds I could do?
October 20, 2011 at 12:47 pm - Permalink
Hm. I just checked our source code repository- I added that error message three years ago. Seems like just yesterday... You didn't show the code of your fit function, so it's hard to know what might be going wrong there. Possibly you need to use the special variable V_FitError. To read about it, execute this command:
DisplayHelpTopic "Special Variables for Curve Fitting"
Well, come to think of it, you might be able to do it by spawning a separate thread and doing your inner curve fit in that thread. Your inner fit function will have to be thread-safe. It's not the usual reason for threading (that would be speed, and since your outer fit requires the result from your inner thread, they can't run concurrently and won't save time). But a thread has its own environment, including global variables so it should solve the re-entrancy problem.
John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
October 20, 2011 at 04:43 pm - Permalink
There is my code. The fit is being done in the function run simulation right before the return. I'll look in to threading and the special variable. Thanks.
Michael
October 21, 2011 at 04:55 am - Permalink
Here is some code that might serve as a start on trying my idea. To read about threading, and what the calls in this code do, execute this command:
DisplayHelpTopic "ThreadSafe Functions and Multitasking"
Note that my code is not complete, so I haven't tested it. But it should serve as a starting point. I will be very interested to know if this works!
John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
October 21, 2011 at 09:51 am - Permalink
Creating a new thread worked. I was able to perform the fitting function within a fitting function.
October 28, 2011 at 12:47 pm - Permalink
Excellent! Thanks for letting me know. There may be others that could benefit from this.
John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
November 1, 2011 at 08:59 am - Permalink