Showing results for 
Search instead for 
Did you mean: 

Levenberg Marquardt curve fitting pt.2

Could you attach a simplified version of your code that shows the problem?
Message 11 of 32
Take a look at the attached example.  This is a simple exponential model (with offset).  On the front panel of the "fit exponential with" the default bounds were chosen to demonstrate some features of the interface.  Inf/-Inf are allowed as bounds.  Notice that the upper and lower bound on C are both the same, 2.9.  This causes the value to be held constant at 2.9. 
1.  The upper bounds must be greater or equal to the lower bounds.  This is easy to miss if you bundle two arrays in the wrong order to create the cluster.  Try creating a constant and use "bundle by name" to keep things straight.
2. The size of the upper bounds array must equal the size of the lower bounds array as well as the size of the initial guess parameters array. In this example the model uses three parameters, so the "initial guess parameters" array must have three elements, and the upper and lower bounds arrays must also have three elements. You can also leave the bounds cluster unwired to get unbounded fitting.


0 Kudos
Message 12 of 32
The graph lost some formatting.  Here is the fixed version.


0 Kudos
Message 13 of 32
Thanks, I re-wrote my code, and somehow fixed whatever was causing the differences between the constrained vi and the 8.2vi (I'm still not sure what I did different).  Now the issue is I have a 'singularity in matrix 1'.  I'm still trying to figure out how to fix it, but I see that it means the first 2 parameters aren't changing as the code goes through it's iterations (the last 2 out of 4 are changing). 

Thanks for all your help; unfortunatly, I have a feeling this problem has more to do with the fundamental theory of my specific code, and not the canned curve fitting code.  Unless... someone has a debugging suggestion for singularity problems.....Smiley Sad
0 Kudos
Message 14 of 32
Can you share the code that is causing the singularity problems?  Many times the curve fit itself is successful, but the inversion of the final Hessian matrix to compute the covariance may fail with this kind of message.  Your model/data may just be hard.  If we have your code to look at we might be able to figure out where the problem really lies.

0 Kudos
Message 15 of 32
OK, here you go.  You should be able to hit run, and it will simulate data using 4 parameters (and plot on the left).  If you have the fitting tab open, it will try to fit that data.  The last 2 parameters tend to fit pretty well, but the first 2 never change.

Um, I don't see how to attach my file.  There's an Attachment option below, and I use the Browse button to find my file, but I when I hit 'Preview Post' the attachment isn't there.  Am I missing a 'Save Attachment' type button somewhere?  I'm using Firefox, but I tried Explorer and I didn't see anything different.  Sorry for all the trouble.  I've attached stuff several months ago with no problems.

Download All
0 Kudos
Message 16 of 32
Well, nevermind about the attachments!
0 Kudos
Message 17 of 32
Preview Post drops attachments. So do your preview, then attach, then submit.

0 Kudos
Message 18 of 32
Did a little probing, and found some interesting items.  The error that is being returned from the curve fitting VI is due to the inversion of the Jacobian.  The root problem though is due to your model function.  The finite-difference approximation of the partial derivatives of your model function always returns zeroes for the partials w.r.t. your first and second parameters (diameter mean and sigma?). 

So I placed your model function in a loop and varied the parameters.  The smallest change in the first parameter that registers is about 2.6.  The finite difference VI within the Lev-Mar code was choosing a step-size of ~1E-4, yielding a partial derivative of zero at all X values.  The second parameter does not seem to ever change the function value.  Not sure if this is expected behavior, but this definitely explains the behavior of the Lev-Mar algorithm.  Because of the "damping" in the parameter-step computation within Lev-Mar the linear system is well conditioned and there is no error due to the singular Jacobian.  However, when the Jacobian is inverted to obtain the covariance matrix the system is not damped, resulting in the error.

To make Lev-Mar work for your problem you will need to make your model function continuous w.r.t. the input parameters.  If that is not a characteristic of your model function, then you may have more success using an approach that does not rely on derivatives.

Attached is the VI I used to explore the partial derivatives of your model function.  I included the partial derivative code from the Lev-Mar algorithm so you can see the columns of zeroes.  I didn't do much UI work, so you will need to set three of the four "delta" sliders to be zero to properly compute a partial derivative w.r.t. only one of the parameters.

0 Kudos
Message 19 of 32
Thanks a lot DSPGuy!  I really appreciate the help.  I've learned a lot from your last message (I think I can simplify the model function so it will work). 

0 Kudos
Message 20 of 32