LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

function fit with a variable number of cubic curves

Solved!
Go to solution

Hi,

I have a set of data: a set representing the independent variable and a set representing the dependent variable. I need to find the minimum number of cubic curves (find their coefficients) that reduce the mean square error less than a given tollerance. If the tollerance is higher than given one with 17 segments, then I need to throw an error.

Have you an idea which function blocks I need of?

I could use General Polynomial Fit VI with order 3 and its default method to check the residual. If the residual is higher than the given tollerance, then I could try to use two cubic curves and test them on different starting and ending points until the residual is less than the given tollerance. If the tollerance is not guaranteed, I add another curve and I test again the fitting as above. Iteratively until the residual is less than the given tollerance or until I would need to add the 18th cubic curve.

If there were something ready, I would appreciate.

Thanks.

0 Kudos
Message 1 of 10
(3,373 Views)

Can you clarify a bit more about continuity and smoothness at the boundary between adjacent cubics?  Are the individual cubics required to meet at the same point?  Are the first derivatives required to be the same at the boundary?

-Jim

0 Kudos
Message 2 of 10
(3,342 Views)

Maybe you could use cubic spline fit and look at the residual as a function of the e.g. the balance parameter.

0 Kudos
Message 3 of 10
(3,331 Views)

The cubics are required to meet at the same point, but the first derivative can be discontinued. So the spline is not required.

My final algorithm is that:

1) Find first cubic curve with General Polynomial Fit VI, inserting 5 points [X,Y].

2) Subtract calculated Y from real Y to find error. If the absolute value of the error is greater than tollerance or there aren't more point, then go to step 3), in the other case, return to step 1 inserting a new point after the points already used.

3) Save the coefficient of the curve calculated on step 2, then reject all the points used with exception of the last point that cause the error greater than the tollerance. If there are more points and the curves calculated are less than 17, then return to point 1, in the other case exit.

 

There is a better way? Where I can find the explanation of the fitting algorithms?

Thanks.

0 Kudos
Message 4 of 10
(3,278 Views)

There is some numerical error in Labview Fitting Libraries, so I decide to create my own algorithm.

There is the algorithm attached.

The result is wrong. What is wrong in my VI? With four fitting points the numerical error should be very very low

Here the theory behind:

https://en.wikipedia.org/wiki/Linear_least_squares_(mathematics)#Using_a_quadratic_model

0 Kudos
Message 5 of 10
(3,219 Views)

@Fabiobonelli wrote:

There is some numerical error in Labview Fitting Libraries,


Can you be more specific?

 

Here are two possible alternatives using LabVIEW primitives. Seems to give the correct result.

0 Kudos
Message 6 of 10
(3,205 Views)

Also, your x-range is far from zero and over a relatively narrow range. This can cause problems with conditioning. Your data is probably OK, but this is something to consider if the data is even more extreme. You probably would get more accuracy of you would re-center on an average x value and correct for the shift later. 

0 Kudos
Message 7 of 10
(3,195 Views)

Please, can you test your VI adding another point to the four presented?

X = -6453

Y = -266

Check the residuals.

Thanks.

0 Kudos
Message 8 of 10
(3,170 Views)
Solution
Accepted by FabioBone

@Fabiobonelli wrote:

Please, can you test your VI adding another point to the four presented?

X = -6453

Y = -266

Check the residuals.

Thanks.


Did you even read my reply? You have a serious conditioning problem because the data is over a narrow range far from zero. For example -6500^3 is smaller than -2E11, i.e. a value that is many (many!) orders of magnitude different than the constant term (1). The resulting linear algebra problem is very ill conditioned and just blindly stepping on the gas pedal is ill adviced here.

 

This is not a problem with the LabVIEW implementation, but a fundamental flaw that you would encouter equally (or even worse) if you would do your own math. No way around it. (See also)

 

My recommendation was to remove the offset from X and add it later again. Now things work correctly. (note that fitting to a second order polynomial still works without the tweak).

 

Try the attached and see for yourself.

 

 

 

Message 9 of 10
(3,145 Views)

I haven't got your warning. Using reconditioning offset, my algorithm works very well. I was searching how Labview fits the data and I didn't catch the error in input data.

Thank you.

0 Kudos
Message 10 of 10
(3,126 Views)