Vertical line fit using polyfit

curve-fitting, matlab, numerical-methods, polynomial-math

Solution

First of all, this happens due to the method of fitting that you are using. When doing `polyfit`, you are using the least-squares method on `Y` distance from the line.

(source: une.edu.au)

Obviously, it will not work for vertical lines. By the way, even when you have something close to vertical lines, you might get numerically unstable results.

There are 2 solutions:

- Swap x and y, as you said, if you know that the line is almost vertical. Afterwards, compute the inverse linear function.

- Use least-squares on perpendicular distance from the line, instead of vertical (See image below) (more explanation in here)

(from MathWorld - A Wolfram Web Resource: wolfram.com)

Problem

Its just a basic question. I am fitting lines to scatter points using `polyfit`. I have some cases where my scatter points have same X values and `polyfit` cant fit a line to it. There has to be something that can handle this situation. After all, its just a line fit. I can try swapping X and Y and then fir a line. Any easier method because I have lots of sets of scatter points and want a general method to check lines. Main goal is to find good-fit lines and drop non-linear features.

Original source