LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how to use linear fit

Until seing this post I was almost sure Linear Fit.vi has bugs.

I attached a VI which I think it shows how Linear Fit.vi fails.

Maybe I'm wrong but I can't find any other explanation apart from thinking that the Linear Fit.vi is buggy.

Please run it a tell me your opinions. Thanks!

0 Kudos
Message 11 of 23
(2,503 Views)

I later found out the formula for Line of Best Fit(Least Square Method) http://hotmath.com/hotmath_help/topics/line-of-best-fit.html and It seems to produce the same results as LabView.

So it is clearly a math problem, not a Lab View one, but I still don't understand how to fit a line to a curved line 😞

0 Kudos
Message 12 of 23
(2,488 Views)

These two fits (FAIL, PASS) look perfectly fine and are exactly waht I would expect from looking at the data. What else do you expect as solution???


MariusRusu wrote:

So it is clearly a math problem, not a Lab View one, but I still don't understand how to fit a line to a curved line 😞


Well, a linear fit to a curved line seems a bit silly. You always get a linear line, not a curved one. Please explain.

 

 

Message 13 of 23
(2,479 Views)

I expect the angle of the FAIL situation to be close pi/2 or 3pi/2 (http://www.coolmath.com/precalculus-review-calculus-intro/precalculus-trigonometry/images/28-trigono...) or with other words a rough vertical line.

I can say that the line in the FAIL situation is basically the line in the PASS situation but rotated anti-clockwise 90 degrees.

If the above sentence is right, doesn't that mean that the angle/slope of the linear fit in the FAIL situation should be equal with the one in the PASS situation + 90 degrees?

 

Here (So it is clearly a math problem, not a Lab View one, but I still don't understand how to fit a line to a curved line :smileysad:) I was probably not explaining clear. I'm just trying to find a straight line which best fits a curved line.

 

Thanks,

Marius

0 Kudos
Message 14 of 23
(2,474 Views)

@MariusRusu wrote:

I expect the angle of the FAIL situation to be close pi/2 or 3pi/2 (http://www.coolmath.com/precalculus-review-calculus-intro/precalculus-trigonometry/images/28-trigono...) or with other words a rough vertical line.


If the data is close to vertical and you want a vertical line, you can swap x and y, do the fit, and swap back at the end.

 

The fit assumes that X has no error and all error is in Y. In your vertical case, you get about the same result as if you would average the points for each x values and weight them according to the number of points averaged. Thus the current result is as expected.

 

IF (big IF!!!!) the points are sorted as in your example, you can do a parametric fit (fit x and y seperately against array index) and you would get what you expect in both cases. See attached modification.

Message 15 of 23
(2,468 Views)

This explains everything. I looked over the internet but didn't found anywhere the fact that the error must be in Y and nothing in X.

Thanks for the workarounds! both will work for me (my points come sorted).

 

Marius

0 Kudos
Message 16 of 23
(2,459 Views)

When you think you understand it all something else comes up...

When I'm using the "Least Absolute Residual" method within Linear Fit.vi, the VI doesn't return the line fit for certain inputs.

For now I'm using "Least Square" method, but I would be gratefull if somebody clears this mystery.

I attached a VI that shows the problem.

Marius

0 Kudos
Message 17 of 23
(2,445 Views)

Well, the fitting tool has an error output, giving error -20075 (Analysis:The filter buffer overflows.), which does not make a lot of sense here. Still the first thing is to hook up the error output when troubleshooting, else you are flying completely blind.

 

Since the inner code is in a dll, we cannot tell what's going on.

 

Somebody from NI needs to analyze this. Maybe there is some hidden bug.

 

(And please don't use "continuous run" for things like this, since the VI restarts as fast as the computer allows, You are recalculating the same data millions of times per second, slowing everything else down. If you feel you need to use continuous run anyway, place a small wait on the diagram (as in my example above).

Message 18 of 23
(2,432 Views)

As a workaround, you can use the General Polynomial Fit VI and use order=1 (linear). It does not seem to choke with a 22 input.

 

(there are older reports about bugs in the linear fit, probably related).

0 Kudos
Message 19 of 23
(2,427 Views)

Got it! I normally use timed structure and event structure but I made the VIs simple (just to show the issue).

Thanks for your reply!

Marius

0 Kudos
Message 20 of 23
(2,423 Views)