LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Nonlinear curve fit help needed

Solved!
Go to solution

I am in need of some help trying to fit a set of data that requires the use of nonlinear curve fitting.  I have attached a txt file containing the data that needs to be fitted.  The first row contains the x-axis values while the second row contains the y-axis values.  The model for this set of data can be described by a single parameter, A, for the first five data points.  The remainder of the data points can be described by the model A+A1*exp((0.4-x)/A2), where A, A1, and A2 are all parameter.  As you can see the two models share the parameter A.

 

I have determined some initial values for each of the parameters: A=176.32, A1=2133.4133, and A2=1.4936.

 

I have spent the last couple of days trying to figure out how to use the Lev-Mar nonlinear curve fit vi but have had little success.  I have been doing LabVIEW programing for many years now and I have never felt as lost as I do now trying to understand how the Lev-Mar nonlinear curve fit vi works.  Any help you can provide with my data fitting would be greatly appreciated.

 

Thanks.

 

-Alonzo

0 Kudos
Message 1 of 11
(5,354 Views)

Very little time for style points or error checking, but here is an example using the LM method.  I skip the first 5 points, but use their average to guess A. Make sure to put the two files into the same directory.

 

You could also check this out.

http://forums.ni.com/ni/board/message?board.id=170&message.id=446159#M446159

 

 

Download All
0 Kudos
Message 2 of 11
(5,339 Views)

Dear Darin,

 

First of all thank you very much for the assistance.  I really do appreciate it.

 

I certainly hope you don't mind if I ask a few questions for clarification.  It seems to me like the whole purpose of the ExponentialFunction.vi is to calculate the numerical values for the model and its derivative, is this all it does?

 

I also took my initial guess for to A be nothing more than the average of the first 5 numbers in my data.  This number turned out to be 176.32.  The exponential part of the data is supposed to decay down to a level that is pretty close to the baseline of the first five data points.  As a result I would think that the parameter A in the exponential model would be closer to the initial guess for A.  After running the vi's you so generously wrote up I am getting a best fit coefficient for A of -132.4.  Am I mistaken in what I should be expecting for this coefficient?  I feel like I am missing something here.

 

Thanks.

 

-Alonzo 

0 Kudos
Message 3 of 11
(5,323 Views)

I have not really used the exponential fit VI, but it appears that it uses a combination of the Lev-Mar method as well as linear least squares method.  It is a bit more complex, but is helpful if your initial guesses aren't very good or if you have some outliers in your data.  If your fit function isn't of the required form, you may have to massage the data a little, or just go straight to the nonlinear fit and try to provide reasonable guesses, and weight outliers by hand if necessary.

 

I was also a bit surprised at first when I saw the offset from the fit.  I would plot the best fit function a little further out to convince yourself that it is or isn't correct.  I would also double-check the VI which generates the function since I wrote it rather quickly.

0 Kudos
Message 4 of 11
(5,308 Views)
Solution
Accepted by topic author tyrosinase

I think the problem is that the first 5 points are not part of the data sent to Lev-Mar.  Because of this the fitting process fits the remaining data very well, but the constant term is able to freely move.  I modified the model function to output A for the first 5 terms, and the complete function for the rest.  The offset found is now more reasonable.

 

Also, your quote your model to be A+A1*exp((0.4-x)/A2), but what is implemented in the model VI seems to be A+A1*exp((x-0.4)/A2).

 

-Jim

Download All
Message 5 of 11
(5,293 Views)

Thank you very much for all the assistance.  I ran the modified vi's and they do seem to give more reasonable answers for the A parameter.  I am really grateful for the assistance that you guys provided.  I got a much better understanding of how this Lev-Mar nonlinear curve fitting vi works.  I must admit, I am really digging it.

 

Once again, thanks a lot! 

0 Kudos
Message 6 of 11
(5,276 Views)

Hello.

 

I am using the Nonlinear Curve Fit VI and I need some of the parameters to remain constant during the fitting. Can you please advise me on this matter?

Best regards,

Cristina

0 Kudos
Message 7 of 11
(4,981 Views)
Try using the Constrained Nonlinear Curve Fit VI, and set the upper and lower bounds to be the same value. Use reasonable bounds for the remaining parameters. -Jim
Message 8 of 11
(4,965 Views)

It worked! Thank you very much!

Cristina

0 Kudos
Message 9 of 11
(4,934 Views)

I am using the Levenburg-Marque vi to fit a spectrum in LV 6 on an old 75mhz win98 machine.  It is talking approximately 5 seconds per iteration...thus the fit can take some time.  So I decided to move it to a more modern machine.  My 2GHz machine is STILL taking 5 seconds per iteration.  This just doesn't make any since and for the life of me I can't figure it out.  Has anyone run into an issue like this?  The fit is working just fine but it surely should run much faster on a modern machine...

0 Kudos
Message 10 of 11
(4,544 Views)