LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Non linear curve fit lev mar problem

Solved!
Go to solution
Hi, I have a set of 10000 readings recorded every second. My aim to plot these readings vs time (1-10000 sec) in log scale, and fit the resultant exponential curve with my model equation: a1*exp (-t*b1) + a2*exp (-t*b2) + a3*exp (-t*b3) + a4*exp (-t*b4) and get the values of the coefficients (a1-a4, b1-b4). I modified the Nonlinear fit lev-mar.vi according to my model. However I have run into a problem.  I get the following error:Error -20041 occurred at NI_Gmath.lvlib:Nonlinear Curve Fit LM.vi:5 Possible reason(s): The system of equations cannot be solved because the input matrix is singular. I can't work out why I am getting this error. I have attached 3 files: data file (Y values), X=1-10000; non liner fit vi and my model vi. 

I am using Labview 8

 I would really appreciate your help and suggestions! Thanks a lot in advance.Anu
Download All
0 Kudos
Message 1 of 12
(7,491 Views)

I'm not an expert in this field, but I've seen this error before and spend some time on it. The problem is of numerical nature, some of your coefficients might differ in magnitudes. So any add operation on them will ignore some of the coefficients and this results in the matrix (I think it is the one called A) containing 0's.

Inside the code there is a place where a linear equation is evaluated (I think it is a kind of Eigenwert problem). There are several possible methods to solve the eigenwert problem in a good time (LU composition and SVD composition). Both have these add operations. If you rewrite this section of code using the much slower approach (I think it was the inverse or pseudo inverse functions), those numerical issues have less effect.

 

I'm sorry that I can't point you directly at the section of the code where you need to 'patch'. But the implementation of lev mar has changed with different versions of LV, and I'm not really into the mathematics. At the time I was using the non-linear fit, there was a lecture from MIT available as screencast on linear algebra. If you are willing to understand the problem, you need to study the full semester...

But maybe others are of better help to you (and me).

 

Felix

0 Kudos
Message 2 of 12
(7,477 Views)

Um.. Your matrix is singular?!  A singular matrix has a determinant of 0 and is not invertible.

 

You have not connected the X Terminal of the LM VI.

 

Why are you using the model.vi on the block diagram?

 

LM.png

Message Edited by battler. on 05-02-2010 04:58 AM
Message Edited by battler. on 05-02-2010 04:59 AM
0 Kudos
Message 3 of 12
(7,476 Views)

Battler, that braught some memories back. You can't calculated the inverse, but you can calculate the pseudoinverse in this situation. Using this, you at least get some numbers back which helps getting deeper into the problem...

 

Felix

0 Kudos
Message 4 of 12
(7,463 Views)

The book "Numerical Recipes in C" (http://www.nrbook.com) describes the LM method.  Thankfully you dont need to know or do any more than satisfy the minimum conditions.

Message 5 of 12
(7,457 Views)

The nrbook is an important reading for understanding the lev mar algorithm. It was the other reference I used then, thanks for posting the link.

 

Felix

0 Kudos
Message 6 of 12
(7,443 Views)

As Battler points out the X input to Lev-Mar is unwired.  This is not a problem unless your model function requires these X values (which your example does).  If the X values are wired, then I get an error indicating that the algorithm did not converge, but there is a non-empty 'best fit parameters' output from lev-mar in this case.  A sum of exponentials model can be difficult numerically, so I recommend explicitly computing the partial derivatives in your model function.  For the exponential this is trivial to implement.  Another suggestion, do not use an array of 1's for the initial guess parameters.  Mix it up a little bit, particularly if the data has no noise.

 

Attached is a modified version of your solver and model function showing the explicit partial derivatives and random guess for initial parameters.

 

-Jim

Download All
Message 7 of 12
(7,399 Views)
Solution
Accepted by topic author Anukmr

Hi

 

@Jim- Thanks a lot...ur modified vi helped a lot..but one thing strange about the fit is that it depends a lot on the initial guess of the coefficients..my model should have values of 'a' coeffs in the order 10^-7 and 'b' should be higher around 10^-1. As the initial guess is varied the best fit coefficients vary accordingly.

 

I really dont know if this can b corrected..I have attached my vi's.

Vi:-non-linear fit,model , exponential branch.

data-pol.txt

 

Thank u so much all of u!!

Download All
0 Kudos
Message 8 of 12
(7,344 Views)

sorry forgot my data file..

 

Thanks..:)

0 Kudos
Message 9 of 12
(7,341 Views)

data file attached in the earlier post.

 

Sorry.

Download All
0 Kudos
Message 10 of 12
(7,335 Views)