LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Need help with non-linear curve-fit

Solved!
Go to solution

Hi,

I'm attaching a VI that calls the non-linear "Lev-Mar" curve-fitting function with two slightly different formulas.

On my machine (Win7, LV2011) the best-fit is a straight line when the Example's FP bool is set to FALSE.

 

Is this expected behavior?

Is there a way to obtain a good fit, with the 0.97 coefficient, and _without_ increasing the number of points?  

 

Thanks in advance!

0 Kudos
Message 1 of 6
(2,855 Views)

I wonder about your formula.  When I implement the formula with LV functions I get a curve which is different from your data plot.

 

Plot from formula.png

 

With two of the initial parameters set to zero the 0.97 or 0.98 coefficient does not even apply on the first iteration.  Changing the initial value of c from 0 to 0.1 will generate a good fit.

 

The best fit parameters are 1, -1, 3.2..., -1.  These are a long way from your initial guess. If you change the formula to 1-(original formula), then it works well with the original initial parameters and the best fit parameters are the same as the initial parameters.

 

A good formula and a good initial guess are important to getting a good fit.

 

Lynn

Message 2 of 6
(2,843 Views)

Hi Lynn,

Thanks for taking time to reply, though, it's not clear whether you implemented a COS^2 function shifted left 90deg - my plots were consistent with this.  Also, the initial guesses are correct due to the variability of the actual input waveform.

 

The question remains as to why such a slight difference between two formulas produces such a radical effect.  Also, the straight-line "best-fit" appears to be a fitting-error, though no error is reported.

 

In case you're interested as to why the initial guesses are appropriate:

 

We're using a power-meter to measure the intensity of a polarized laser beam after it passes through a half-wave plate followed by a Glan-Laser polarizer.  As the wave-plate is turned, it changes the polarity of the beam entering the polarizer - affecting the amount of light exiting the polarizer.  The intensity of light exiting changes as a COS^2 function of the angle of the wave-plate.

 

'a', amplitude offset can fluctuate depending on ambient light and when the meter was zeroed. It will fluctuate around zero.

'b', amplitude, 1 Watt(max) is possible.

'c', angular-offset, is related to the initial angle of the wave-plate which is unknow, so the initial guess of zero is as good a guess as any.

'd', angular multiplier is 1, here, because the example data had a period of 2pi.

 

Thanks/Cheers

0 Kudos
Message 3 of 6
(2,822 Views)
Solution
Accepted by topic author 550nm

I just implemented the formula you fed to the fitting VI.  I did not look at the calculation you did for the "raw" data.

 

Consider: The first value of x is zero. To get a phase shift of ~pi/2 a good first guess for c is 1.57.  With any value close to that it gives a good fit for either choice of c.

 

good initial params.png

 

Lynn

Message 4 of 6
(2,819 Views)

I appreciate your persistence.  AmplitudeOffset(a), amplitude(b), angular offset(c), and angular multiplier(d) can be more closely approximated.  Hopefully this is enough to obtain reliable results from the fitting function.

 

NOTE: if we know the phase shift, it's simplifies things.

In practice, the phase-shift will be arbitrary.  Still 'c' might be approximated in the general case...

Since y = a + b*cos((x*pi/180+c)*d)^2

c = acos((y-a)/b)^0.5)/d-x*Pi/180 (i think)

 

Cheers!

0 Kudos
Message 5 of 6
(2,809 Views)

The Lev-Mar algorithm sometimes does not do well at fitting the phase or frequency of sinusoidal models.  Internally it uses derivatives to get the next approximation to the parameters. The derivative of cos(c) with respect to c = -sin(c) but that does not do much to force a change in c.  What this means is that a good initial estimate for c may be more important than for some of the other parameters.

 

Possibly something as simple as a pi/2 phase shift if the residue is large might work.

 

Lynn

Message 6 of 6
(2,788 Views)