LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Attempting Nonlinear Curve Fit

I'm fairly new to LABView.  (I have version 8.5.1)  I'm using a VI to collect data from a photon counter and I'd like to do basic curve fitting to the array of values I get.  The values, when plotted over time (which corresponds to angle of the sample emitting photons, as the sample is rotating in time), appear in aggregate as a wave.  I believe the best-fit function is: a0 + a1*Cos[x] + a2*Cos[2x] + a3*Cos[3x] + a4*Cos[4x], where a0 through a4 are coefficients to be determined, and x is time (proportional to angle of sample orientation).  I'm using the Nonlinear Curve Fit (Lev-Mar) VI from the Function menu, but I'm having a hard time understanding this VI and the inputs it needs, in particular the function f(x,a).  I created a separate VI that attempts to model the function shown above, but I don't understand how it works and it doesn't appear to be set up correctly because I get a broken wire between its reference and the Nonlinear Curve Fit VI.  For example, in the function VI (which I named FitFunction.vi) I don't understand how the unwired items, like "data" and "output control" and "f'(X,a)" can do anything, so I don't know if they're set up correctly.
 
Ultimately, I'd like to know the coefficients a0 through a4 for the function above.  Can anyone help me out?
 
I've attached my master file SHG_VI6.vi and the referenced FitFunction.vi.  If you can help me see through the fog, I'd be very appreciative!  Thanks so much for your time and help!
 
Andrew
 
 
Download All
0 Kudos
Message 1 of 10
(7,280 Views)
Andrew,

Your Fit Function VI does not have its terminals connected to the appropriate controls and indicators. It also uses a different connector pane than the template. When I changed those things the broken reference line is fixed.

I could not run anything because of missing SR400 VIs and because I do not have your hardware.

Lynn
0 Kudos
Message 2 of 10
(7,275 Views)

Thanks Lynn,

I'm not really sure how to fix those things you mentioned.  Could you maybe upload the updated vi or post a screen shot?  I'd really appreciate it.

Andrew

0 Kudos
Message 3 of 10
(7,254 Views)
Andrew,

The easiest way to make a fit function is to start with the template linked from the detailed help for the Nonlinear Curve Fit VI:
"(x,a) is a reference to the VI that implements the fitting model. Use the VI template located at labview\vi.lib\gmath\NumericalOptimization\LM model function and gradient.vit to create the VI from a template."

It already has the connections to the connector pane.

To make connections to the connector pane, context-click on the icon of the VI from the front panel and choose "Show Connector." Then using the Operate tool click on a control (the cursor changes to the wiring tool) and then click on the terminal on the connector pane which you want connected to that control.

The standard connector pane for new VIs is a 4-2-2-4 pattern but the model function VI uses a 4-3 pattern.

The f'(X,a) can be used to provide the derivatives to the Nonlinear Curve Fit VI, but it will attempt to calculate them by numeric approximation if nothing is provided.

Lynn
0 Kudos
Message 4 of 10
(7,249 Views)
Lynn is right of course.
 
Here's a quick rewrite of the fit function with less code starting from the correct template. try it. 🙂 (LabVIEW 8.5.1)
 
 
0 Kudos
Message 5 of 10
(7,246 Views)

Thanks to both of you for your help.  I didn't know what the connector pane was before, but now I do.  altenbach, given your (much nicer) code, for how many iterations does the For Loop run?  Is it going to loop 5 times, given me a function that is a sum of 5 cosine terms (actually, the first term is just a constant because Cos[0] = 1, of course)?  Nothing is wired to the count terminal of the For Loop.  Same with my code.  Does that mean it runs just once?  If so, will the iteration terminal count up from 0 to 4 to give a sum of 5 cosine terms?  Does the Nonlinear Curve Fit VI see that I have 5 initial parameters wired to it and pass those along to the "A" control in FitFunction and that tells it how many terms I want in my sum?  Trying to understand the theory behind it all!

Again, thanks so much.

Andrew

0 Kudos
Message 6 of 10
(7,233 Views)


Dedalus7 wrote:
altenbach, given your (much nicer) code, for how many iterations does the For Loop run?  Is it going to loop 5 times, given me a function that is a sum of 5 cosine terms (actually, the first term is just a constant because Cos[0] = 1, of course)?  Nothing is wired to the count terminal of the For Loop. 
If you don't wire N, the loop will run for as many elements you have in the smallest autoindexing array input tunnel.
In this case, it will produce the exact same result as your code, because it autoindexes over the array of paramters. Try it!
 
(you autoidexed over the array of x while explicitly indexing the paramter array, so your loop run as many times as your x array size. Note that in your original code you could delete the FOR loop (right-click...remove FOR loop) and the result will still be exactly the same. :D.)
 
My code is very flexible, because you don't need to change anything of you later want to add two more terms or use fewer terms, for example. You would just ichange the size of "a" in the toplevel code and everything will continue to work. Your VI would require extensive rewiring and a new model VI for each case.
0 Kudos
Message 7 of 10
(7,226 Views)
What is the purpose of the a in template?
0 Kudos
Message 8 of 10
(7,130 Views)
They [i.e. the a's] are the coefficients that go in front of the cosine terms in the best-fit function.  They're what we're solving for.
0 Kudos
Message 9 of 10
(7,097 Views)

Hi Altenbach,

 

I have some very basic query about using nonlinear curve fitting in labview. When I use labview curve fitting express vi, I do get decent fit. I want to repeat this using the nonlinear curve fitting vi. If I understand the nonlinear curve fitting vi correctly, you have two options either you use a model vi to supply f(x,a) or use model string option. I tried using model strig option but without success.

 

I have attached the simple form of my vi if you would like to take a look.

 

Best,

0 Kudos
Message 10 of 10
(6,026 Views)