How to fit a line on the plane?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Mr M.
le 24 Jan 2019
Modifié(e) : David Goodmanson
le 2 Fév 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
0 commentaires
Réponse acceptée
David Goodmanson
le 28 Jan 2019
Modifié(e) : David Goodmanson
le 2 Fév 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
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Curve Fitting Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!