How to fit a line on the plane?

6 Ansichten (letzte 30 Tage)
Mr M.
Mr M. am 24 Jan. 2019
Bearbeitet: David Goodmanson am 2 Feb. 2019
How to fit a line onto dots on the plane? polyfit is not the solution, because it is good for functions and not for points on the plane. ax+b function fit fails for vertical lines for example, and noise model also differs

Akzeptierte Antwort

David Goodmanson
David Goodmanson am 28 Jan. 2019
Bearbeitet: David Goodmanson am 2 Feb. 2019
Hi M,
Here is a way to fit a line in 2d with the equation
a1*x1 + a2*x2 = 1
where (x1,x2) are (x,y). This representation gets rid of infinite slope problems.
The same code works in general in m dimensions to fit an m-1 dimensional plane
a1*x1 + a2*x2 + ... am*xm = 1.
For n points, let X be an nxm matrix where each row in X represents a point in m dimensions.
X = rand(20,2) % for example
Xcm = mean(X); % coordinates of center of mass
[~,s,v] = svd(X-Xcm,'econ');
n = v(:,end); % unit vector for the smallest singular value (they are sorted)
L = Xcm*n; % displacement of the best fit plane from the origin
if L < 0 % turn displacement into distance
L = -L;
n = -n;
end
a = n/L; % fitted plane, a1*x1 + a2*x2 + ... am*xm = 1.
drms = rms(X*n -L);
% L = 1/norm(a), so drms also equals (1/norm(a))*rms(X*a-1)
drms is the rms distance of the set of points to the plane.
For older versions of Matlab, in the svd line you would have to use
X-repmat(Xcm,size(X,1),1) in place of X-Xcm

Weitere Antworten (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by